D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
saraquigley
Full window
Github gist
Blocks by Reason
<!DOCTYPE html> <meta charset="utf-8"> <html> <head> <script src="https://d3js.org/d3.v3.js"></script> <link rel="stylesheet" type="text/css" href="style.css" /> <div id="header">Blocks by Reason <br>Through February 15, 2013</div> <div id="sara">Sara Quigley</div> </head> <body> <script type="text/javascript"> var m = [0, 85, 30, 20], w = 1400 - m[1] - m[3], h = 100 - m[0] - m[2], parse = d3.time.format("%m/%d/%y").parse; // Scales. Note the inverted domain for the y-scale: bigger is up! var x = d3.time.scale().range([0, w]), y = d3.scale.linear().range([h, 0]); // X-axis. var xAxis = d3.svg.axis() .scale(x) .tickSize(10) .tickPadding(5) .orient("bottom"); // An area generator, for the light fill. var area = d3.svg.area() .interpolate("monotone") .x(function(d) { return x(d.date); }) .y0(h) .y1(h); // A line generator, for the dark stroke. var line = d3.svg.line() .interpolate("monotone") .x(function(d) { return x(d.date); }) .y(h); d3.csv("data.csv", function(data) { // Nest values by symbol. var symbols = d3.nest() .key(function(d) { return d.key + " blocks"; }) .entries(data); // Parse dates and numbers. We assume values are sorted by date. // Also compute the maximum enrollment per course, needed for the y-domain. symbols.forEach(function(s) { s.values.forEach(function(d) { d.date = parse(d.date); d.value = +d.value; d.maxBlocks = d3.max(s.values, function(d) { return +d.value; })}); }); // Compute the minimum and maximum date across symbols. x.domain([ d3.min(symbols, function(d) { return d.values[0].date; }), d3.max(symbols, function(d) { return d.values[d.values.length - 1].date; }) ]); // Add an SVG element for each symbol, with the desired dimensions and margin. var svg = d3.select("body").selectAll("svg") .data(symbols) .enter().append("svg") .attr("width", w + m[1] + m[3]) .attr("height", h + m[0] + m[2]) .append("g") .attr("transform", "translate(" + m[3] + "," + m[0] + ")"); // Add the area path elements. Note: the y-domain is set per element. svg.append("path") .attr("class", "area") .attr("d", function(d) { y.domain([0, 0]); return area(d.values); }); // Add the x-axis. svg.append("g") .attr("class", "x axis") .attr("transform", "translate(0," + h + ")") .call(xAxis); // Y-axis. var yAxis = d3.svg.axis() .scale(y) .orient("right"); // Add the y-axis svg.append("g") .attr("class", "y axis") .attr("transform", "translate(" + w + ",0)"); // Add the line path elements. Note: the y-domain is set per element. svg.append("path") .attr("class", "line") .attr("d", function(d) { y.domain([0,0]); return line(d.values); }); // Add a small label for the block reason. svg.append("text") .attr("x", w - 6) .attr("y", 36) .attr("text-anchor", "end") .attr("class", "blockReason") .text(function(d) { return d.key; }); // Add a label for the max blocks. svg.append("text") .attr("x", w - 6) .attr("y", 20) .attr("text-anchor", "end") .attr("class", "maxBlocks") .text(function(d) { return d.values[0].maxBlocks ; }); //transition to actual graphs area.y1(function(d) { return y(d.value); }); line.y(function(d) { return y(d.value); }); d3.selectAll("path.line") .attr("d", function(d) { y.domain([0, d.values[0].maxBlocks]); return line(d.values); }); d3.selectAll("path.area") .attr("d", function(d) { y.domain([0, d.values[0].maxBlocks]); return area(d.values); }); }); </script> </body> </html>
Modified
http://d3js.org/d3.v3.js
to a secure url
https://d3js.org/d3.v3.js