D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
mountainsophist
Full window
Github gist
Module 4 Exercise
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Loading CSV Data with D3</title> <script type="text/javascript" src="https://d3js.org/d3.v3.js"></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: burlywood; } rect:hover { fill: steelblue; } .axis path, .axis line { fill: none; stroke: black; shape-rendering: crispEdges; } .y.axis path, .y.axis line { opacity: 0 } .axis text { font-family: sans-serif; font-size: 11px; font-weight: bold; } </style> </head> <body> <h1>Happy Tree Usage</h1> <h2>How often did Bob Ross draw trees during each season</h2> <script type="text/javascript"> var h = 600; var w = 500; var padding = [20,10,20,80]; //Top, Right, Bottom, Left var svg = d3.select("body") .append("svg") .attr("width", w) .attr("height", h); 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"); //Load in contents of CSV file d3.csv("bob_ross_aggregate_data_by_season.csv", function(data) { data.sort(function(a,b) { return d3.descending(+a.trees, +b.trees); }); widthScale.domain([0, d3.max(data, function(d) { return +d.trees; })]) // heightScale.domain(d3.range(data.length)); heightScale.domain(data.map(function(d) { return "Season " + d.seaon_num; } )); var rects = svg.selectAll("rect") .data(data) .enter() .append("rect"); rects.attr("x", padding[3]) .attr("y", function(d,i) { return heightScale("Season " + d.seaon_num); }) .attr("width", function(d) { return widthScale(d.trees); }) .attr("height", heightScale.rangeBand()) .attr("fill", "green") .append("title") .text(function(d) { return d.trees; }); svg.append("g") .attr("class", "y axis") .attr("transform", "translate(" + (padding[3] - 5) + ",0)") .call(yAxis); svg.append("g") .attr("class", "x axis") .attr("transform", "translate(" + padding[3] + "," + (h - padding[2]) + ")") .call(xAxis); }); </script> </body> </html>
Modified
http://d3js.org/d3.v3.js
to a secure url
https://d3js.org/d3.v3.js