D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
ssongalice
Full window
Github gist
DVD32015_Module 5 Scatter plot
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Scatterplot</title> <script type="text/javascript" src="https://d3js.org/d3.v3.js"></script> <style type="text/css"> h1 { font-size: 24px; margin: 0; } p { font-size: 14px; margin: 10px 0 0 0; } circle:hover { fill: #13B4DE; } .axis path, .axis line { fill: none; stroke: #aaaaaa; } .axis text { font-family: sans-serif; font-size: 11px; fill:#aaaaaa; } </style> </head> <body> <h1>The number of CCTV cameras by district in Seoul, Korea</h1> <p>X axis : The number of cctv cameras <br> Y axis : The population of seoul <br> The text on circle is name of seoul district</p> <script type="text/javascript"> var w = 900; var h = 600; var padding = [ 20, 10, 50, 80 ]; //Top, right, bottom, left var xScale = d3.scale.linear() .range([ padding[3], w - padding[1] - padding[3] ]); var yScale = d3.scale.linear() .range([ h - padding[2], padding[0]]); var xAxis = d3.svg.axis() .scale(xScale) .orient("bottom"); var yAxis = d3.svg.axis() .scale(yScale) .orient("left"); var svg = d3.select("body") .append("svg") .attr("width", w) .attr("height", h); d3.csv("data.csv", function(data) { data.forEach(function(d){ d.value=+d.value; d.population=+d.population; }); xScale.domain([0,d3.max(data, function(d) { return d.value; }) ]); yScale.domain([0, d3.max(data, function(d) { return d.population; }) ]); var circles = svg.selectAll("circle") .data(data) .enter() .append("circle"); circles.attr("cx", function(d) { return xScale(d.value); }) .attr("cy", function(d) { return yScale(d.population); }) .attr("r", 6) .attr("fill", "#02436E"); svg.selectAll(".labeltext") .data(data) .enter() .append("text") .text(function(d) { return d.area; }) .attr("x", function(d) { return xScale(d.value); }) .attr("y", function(d) { return yScale(d.population)-10; }) .attr("font-family", "sans-serif") // 폰트 종류, sans-serif : 고딕 .attr("font-size", "10px") // 폰트 크기 .attr("fill", "#888888") // 텍스트 색상 .attr("text-anchor","middle"); // 텍스트 정렬 svg.append("g") .attr("class", "x axis") .attr("transform", "translate(0," + (h - padding[2] + 10) + ")") .call(xAxis); svg.append("g") .attr("class", "y axis") .attr("transform", "translate(" + (padding[3] - 10) + ",0)") .call(yAxis); }); </script> </body> </html>
Modified
http://d3js.org/d3.v3.js
to a secure url
https://d3js.org/d3.v3.js