D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
ssongalice
Full window
Github gist
The number of CCTV cameras by region in Seoul, Korea
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Scale and Axis</title> <script src="https://d3js.org/d3.v3.min.js" charset="utf-8"></script> <style type="text/css"> body { background-color: white; font-family: Helvetica, Arial, sans-serif; } h1 { font-size: 24px; margin: 0; } p { font-size: 14px; margin: 10px 0 0 0; } svg { background-color: white; } rect:hover { fill: #18709C; } .axis path, .axis line { fill: none; stroke: #aaaaaa; shape-rendering: crispEdges; } .axis text { font-family: sans-serif; font-size: 11px; } .y.axis path, .y.axis line { opacity: 0; } </style> </head> <body> <h1>The number of CCTV cameras by region in Seoul, Korea</h1> <script type="text/javascript"> var w = 800; var h = 600; var padding = [ 30, 30, 30, 100 ]; var widthScale = d3.scale.linear() .range([ 0, w - padding[1] - padding[3] ]); var heightScale = d3.scale.ordinal() .rangeRoundBands([ padding[0], h - padding[2] ], 0.3); var xAxis = d3.svg.axis() .scale(widthScale) .orient("bottom"); var yAxis = d3.svg.axis() .scale(heightScale) .orient("left"); var svg = d3.select("body") .append("svg") .attr("width", w) .attr("height", h); d3.csv("data.csv", function(error, data) { data.sort(function(a, b) { return d3.descending(+a.value, +b.value); }); widthScale.domain([ 0, d3.max(data, function(d) { return +d.value; }) ]); heightScale.domain(data.map(function(d) { return d.area; } )); var rects = svg.selectAll("rect") .data(data) .enter() .append("rect"); rects.attr("x", padding[3]) .attr("y", function(d) { return heightScale(d.area); }) .attr("width", function(d) { return widthScale(d.value); }) .attr("height", heightScale.rangeBand()) .attr("fill","#4DAAAB"); svg.append("g") .attr("class", "x axis") .attr("transform", "translate(" + padding[3] + "," + (h - padding[2]) + ")") .call(xAxis); svg.append("g") .attr("class", "y axis") .attr("transform", "translate(" + padding[3] + ",0)") .call(yAxis); var texts = svg.selectAll(".datatext") .data(data) .enter() .append("text"); texts.attr("x", function(d) { return widthScale(d.value)+ padding[3]+5; }) .attr("y", function(d) { return heightScale(d.area)+10; }) .attr("fill","#777777") .text(function(d) { return d.value;}) .attr("font-size","11px"); }); </script> </body> </html>
Modified
http://d3js.org/d3.v3.min.js
to a secure url
https://d3js.org/d3.v3.min.js