D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
saraquigley
Full window
Github gist
Aid Payments over Time
<!DOCTYPE html> <meta charset="utf-8"> <script src="https://d3js.org/d3.v3.js"></script> <style> body { font: 10px sans-serif; } .axis path, .axis line { fill: none; stroke: #000; shape-rendering: crispEdges; } #header { position: absolute; top: 50px; left: 7%; font: 200 42px "Helvetica Neue"; } #chart { margin: 0 0% 0% 20%; } .legend { position: absolute; top: 350px; left: 7%; font: 400 16px "Helvetica Neue"; } .axis text { font: 400 14px "Helvetica Neue"; } footer { position: absolute; bottom: 10%; left: 7%; font: 200 16px "Helvetica Neue"; } </style> <body> <div id="header"> Gift Aid & Loan Payments <BR> Over the course of the Fall 2012 semester </div> <div id="chart"> <script> var format = d3.time.format("%m/%d/%y"); var margin = {top: 200, right: 30, bottom: 230, left: 60}, width = 960 - margin.left - margin.right, height = 900 - margin.top - margin.bottom; var x = d3.time.scale() .range([0, width]); var y = d3.scale.linear() .range([height, 0]); var z = ['#7570B3', '#E6AB02', '#66A61E', '#E7298A', '#D95F02'] ; var xAxis = d3.svg.axis() .scale(x) .orient("bottom") .ticks(d3.time.months); var yAxis = d3.svg.axis() .scale(y) .orient("left"); var stack = d3.layout.stack() .offset("zero") .values(function(d) { return d.values; }) .x(function(d) { return d.date; }) .y(function(d) { return d.value; }); var nest = d3.nest() .key(function(d) { return d.key; }); var area = d3.svg.area() .interpolate("basis") // there are other interpolation options that can be used here .x(function(d) { return x(d.date); }) .y0(function(d) { return y(d.y0); }) .y1(function(d) { return y(d.y0 + d.y); }); var svg = d3.select("#chart").append("svg") .attr("width", width + margin.left + margin.right) .attr("height", height + margin.top + margin.bottom) .append("g") .attr("transform", "translate(" + margin.left + "," + margin.top + ")"); d3.csv("data.csv", function(data) { data.forEach(function(d) { d.date = format.parse(d.date); d.value = +d.value; }); var layers = stack(nest.entries(data)); var legend = d3.select("body").append("svg") .attr("width", 850) .attr("height", 200) .attr("class", "legend") .attr("transform", "translate(20, -600)") .selectAll("g") .data(layers) .enter().append("g") .attr("transform", function(d,i) { return "translate(0, " + ((i * 200) / 5 ) + ")" ;}); legend.append("rect") .attr("width", 20) .attr("height", 20) .attr("fill", function(d, i) { return z[i]; }) .style("fill-opacity", .85); legend.append("text") .attr("x", 28) .attr("y", 16) .text(function(d) { return d.key; }) .attr("fill", function(d, i) { return z[i]; }); x.domain(d3.extent(data, function(d) { return d.date; })); y.domain([0, d3.max(data, function(d) { return d.y0 + d.y; })]); svg.selectAll(".layer") .data(layers) .enter().append("path") .attr("class", "layer") .attr("d", function(d) { return area(d.values); }) .style("fill", function(d, i) { return z[i]; }) .style("fill-opacity", .85) .style("stroke-width", 5) .on('mouseover', function(d,i) { d3.select(this) .style("fill-opacity", .65); }) .on('mouseout', function(d,i) { d3.select(this) .style("fill-opacity", .85); }); svg.append("g") .attr("class", "x axis") .attr("transform", "translate(0," + height + ")") .call(xAxis); svg.append("g") .attr("class", "y axis") .call(yAxis); }); </script> </div> <footer> <P> <BR> Sara Quigley <BR> January 18, 2013 <P> </footer> </body> </html>
Modified
http://d3js.org/d3.v3.js
to a secure url
https://d3js.org/d3.v3.js