D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
nydame
Full window
Github gist
Exercise 3 for Data Visualization and Infographics with D3
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Loading CSV Data with D3</title> <style type="text/css"> body { background-color: whitesmoke; } svg { background-color: mistyrose; } </style> <script type="text/javascript" src="https://d3js.org/d3.v3.js"></script> </head> <body> <h1>California Counties Ranked By Diabetes Rate</h1> <p>Hover over a bar to see which county it represents!</p> <svg width="600" height="600"> <line x1="10" y1="5" x2="10" y2="590" stroke="gray" stroke-width="3" /> </svg> <script type="text/javascript"> //Load in contents of CSV file and create a bar graph d3.csv("CA-diabetes-data.csv", function(data) { var svg1 = d3.select("svg"); // Sort the data in order of ascending Diabetes Value (DV) data.sort(function(a, b) { return d3.ascending(a["Diabetes Value"], b["Diabetes Value"]); }); // Bind data to rectangles in the SVG element svg1.selectAll("rect") .data(data) .enter() .append("rect"); // Give rectangles width proportional to DV // and position along y axis determined by index svg1.selectAll("rect") .attr("x", 15) .attr("y", function(d, i) { return (i * 10) + 5; }) .attr("width", function(d) { return parseInt(d["Diabetes Value"] * 4000); }) .attr("height", 8) .attr("fill", "gray") .append("title") .text(function(d) {return d["County"];}); }); </script> </body> </html>
Modified
http://d3js.org/d3.v3.js
to a secure url
https://d3js.org/d3.v3.js