D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
GerardoFurtado
Full window
Github gist
Local Variables
forked from
mbostock
's block:
Local Variables
<!DOCTYPE html> <meta charset="utf-8"> <style> body { font: 10px sans-serif; margin: 0; } .line { fill: none; stroke: #666; stroke-width: 1.5px; } .area { fill: #e7e7e7; } .text { text-anchor: end; } </style> <body> <script src="https://d3js.org/d3.v4.min.js"></script> <script> var margin = {top: 10, right: 60, bottom: 10, left: 10}, width = 960 - margin.left - margin.right, height = 69 - margin.top - margin.bottom; var parseDate = d3.timeParse("%b %Y"); var x = d3.scaleTime() .range([0, width]); var y = d3.local(); var area = d3.local(); var line = d3.local(); var axis = d3.local(); d3.tsv("stocks.tsv", type, function(error, data) { if (error) throw error; var symbols = d3.nest() .key(function(d) { return d.symbol; }) .entries(data); x.domain([ d3.min(symbols, function(symbol) { return symbol.values[0].date; }), d3.max(symbols, function(symbol) { return symbol.values[symbol.values.length - 1].date; }) ]); var svg = d3.select("body").selectAll("svg") .data(symbols) .enter().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 + ")") .each(function(d) { var ty = y.set(this, d3.scaleLinear() .domain([0, d3.max(d.values, function(d) { return d.price; })]) .range([height, 0])); area.set(this, d3.area() .x(function(d) { return x(d.date); }) .y0(height) .y1(function(d) { return ty(d.price); })); line.set(this, d3.line() .x(function(d) { return x(d.date); }) .y(function(d) { return ty(d.price); })); axis.set(this, d3.axisRight(ty).ticks(3)); }); svg.append("path") .attr("class", "area") .attr("d", function(d) { return area.get(this)(d.values); }); svg.append("path") .attr("class", "line") .attr("d", function(d) { return line.get(this)(d.values); }); svg.append("text") .attr("class", "text") .attr("x", width - 6) .attr("y", height - 6) .text(function(d) { return d.key; }); svg.each(function(){ axis.get(this)(d3.select(this) .append("g") .attr("transform", "translate(" + width + ",0)")); }); }); function type(d) { d.price = +d.price; d.date = parseDate(d.date); return d; } </script>
https://d3js.org/d3.v4.min.js