D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
owendall
Full window
Github gist
NIFA Non-Formula Awards by Month
<!DOCTYPE html> <meta charset="utf-8"> <style> svg { font: 10px sans-serif; } path { fill: steelblue; } .axis path, .axis line { fill: none; stroke: #000; shape-rendering: crispEdges; } .brush .extent { stroke: #fff; fill-opacity: .125; shape-rendering: crispEdges; } </style> <body> <script src="https://d3js.org/d3.v3.min.js"></script> <script> var total_width = 960, total_height = 400, margin = {top: 10, right: 10, bottom: total_height*.2, left: 120}, bottompanelmargin = {top: total_height - margin.bottom + 30, right: margin.right, bottom: 20, left: margin.left}, leftpanelmargin = {top: margin.top, right: margin.left-40, bottom: margin.bottom, left: 40}, width = total_width - margin.left - margin.right, height = total_height - margin.top - margin.bottom, bottompanelheight = total_height - bottompanelmargin.top - bottompanelmargin.bottom, leftpanelwidth = leftpanelmargin.right - leftpanelmargin.left; var parseDate = d3.time.format("%b %Y").parse; var x = d3.time.scale().range([0, width]), bottompanelx = d3.time.scale().range([0, width]), leftpanelx = d3.scale.linear().range([0, leftpanelwidth]), y = d3.scale.linear().range([height, 0]), bottompanely = d3.scale.linear().range([bottompanelheight, 0]), leftpanely = d3.scale.linear().range([height, 0]); var xAxis = d3.svg.axis().scale(x).orient("bottom"), bottompanelxAxis = d3.svg.axis().scale(bottompanelx).orient("bottom"), yAxis = d3.svg.axis().scale(y).orient("left"), leftpanelyAxis = d3.svg.axis().scale(leftpanely).orient("left"); var brushx = d3.svg.brush() .x(bottompanelx) .on("brush", brushedx); var brushy = d3.svg.brush() .y(leftpanely) .on("brush", brushedy); var area = d3.svg.area() .interpolate("monotone") .x(function(d) { return x(d.date); }) .y0(height) .y1(function(d) { return y(d.price); }); var bottompanelarea = d3.svg.area() .interpolate("monotone") .x(function(d) { return bottompanelx(d.date); }) .y0(bottompanelheight) .y1(function(d) { return bottompanely(d.price); }); var leftpanelarea = d3.svg.area() .interpolate("monotone") .x(function(d) { return leftpanelx(d.date); }) .y0(height) .y1(function(d) { return leftpanely(d.price); }); var svg = d3.select("body").append("svg") .attr("width", width + margin.left + margin.right) .attr("height", height + margin.top + margin.bottom); svg.append("defs").append("clipPath") .attr("id", "clip") .append("rect") .attr("width", width) .attr("height", height); var focus = svg.append("g") .attr("transform", "translate(" + margin.left + "," + margin.top + ")"); var leftpanelcontext = svg.append("g") .attr("transform", "translate(" + leftpanelmargin.left + "," + leftpanelmargin.top + ")"); var bottompanelcontext = svg.append("g") .attr("transform", "translate(" + bottompanelmargin.left + "," + bottompanelmargin.top + ")"); d3.csv("nifa-awards-by-month.csv", function(error, data) { data.forEach(function(d) { d.date = parseDate(d.date); d.price = +d.price; }); x.domain(d3.extent(data.map(function(d) { return d.date; }))); y.domain([0, d3.max(data.map(function(d) { return d.price; }))]); bottompanelx.domain(x.domain()); bottompanely.domain(y.domain()); leftpanelx.domain(x.domain()); leftpanely.domain(y.domain()); focus.append("path") .datum(data) .attr("clip-path", "url(#clip)") .attr("d", area); focus.append("g") .attr("class", "x axis") .attr("transform", "translate(0," + height + ")") .call(xAxis); focus.append("g") .attr("class", "y axis") .call(yAxis); leftpanelcontext.append("path") .datum(data) .attr("d", leftpanelarea); bottompanelcontext.append("path") .datum(data) .attr("d", bottompanelarea); bottompanelcontext.append("g") .attr("class", "x axis") .attr("transform", "translate(0," + bottompanelheight + ")") .call(bottompanelxAxis); leftpanelcontext.append("g") .attr("class", "y axis") .attr("transform", "translate(" - leftpanelwidth + ", 0 )") .call(leftpanelyAxis); bottompanelcontext.append("g") .attr("class", "x brush") .call(brushx) .selectAll("rect") .attr("y", -6) // 6 goes along with 7 below for shifting. .attr("height", bottompanelheight + 7); leftpanelcontext.append("g") .attr("class", "y brush") .call(brushy) .selectAll("rect") .attr("width", leftpanelwidth); }); function brushedx() { x.domain(brushx.empty() ? bottompanelx.domain() : brushx.extent()); focus.select("path").attr("d", area); focus.select(".x.axis").call(xAxis); } function brushedy() { y.domain(brushy.empty() ? leftpanely.domain() : brushy.extent()); focus.select("path").attr("d", area); focus.select(".y.axis").call(yAxis); } </script>
Modified
http://d3js.org/d3.v3.min.js
to a secure url
https://d3js.org/d3.v3.min.js