D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
areologist
Full window
Github gist
area chart
Built with
blockbuilder.org
<!DOCTYPE html> <head> <meta charset="utf-8"> <script src="https://d3js.org/d3.v4.min.js"></script> <style> body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; } </style> </head> <body> <div id ="chartID"></div> <script> var margin = {top:10, right:10, bottom:90, left:10}; var width = 960 - margin.left - margin.right; var height = 360 - margin.top - margin.bottom; var xScale, yScale; var svg = d3.select("#chartID").append("svg") .attr("width", width+margin.left + margin.right) .attr("height",height+margin.top + margin.bottom) var margin = {top: 20, right: 20, bottom: 30, left: 50}, width = +svg.attr("width") - margin.left - margin.right, height = +svg.attr("height") - margin.top - margin.bottom, g = svg.append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")"); var parseTime = d3.timeParse("%d-%b-%y"); xScale = d3.scaleTime() .rangeRound([0, width]); yScale = d3.scaleLinear() .rangeRound([height, 0]); var area = d3.area() .x(function(d) { return xScale(d.date); }) .y1(function(d) { return yScale(d.close); }); var area2 = d3.area() .x(function(d) { return xScale(d.date); }) .y1(function(d) { return yScale(d.forecast); }); d3.tsv("data.tsv", function(d) { d.date = parseTime(d.date); d.close = +d.close; return d; }, function(error, data) { if (error) throw error; xScale.domain(d3.extent(data, function(d) { return d.date; })); yScale.domain([0, d3.max(data, function(d) { return d.close; })]); area.y0(yScale(0)); area2.y0(yScale(0)); g.append("path") .datum(data) .attr("fill", "#d189bf") .attr("d", area2); g.append("path") .datum(data) .attr("fill", "#6eb647") .attr("d", area); g.append("g") .attr("transform", "translate(0," + height + ")") .call(d3.axisBottom(xScale)); g.append("g") .call(d3.axisLeft(yScale)) .append("text") .attr("fill", "#000") .attr("transform", "rotate(-90)") .attr("y", 6) .attr("dy", "0.71em") .attr("text-anchor", "end") .text("Price ($)"); }); document.addEventListener("DOMContentLoaded", resize); d3.select(window).on('resize', resize); function resize() { width = parseInt(d3.select('#chartID').style('width'), 10); width = width - margin.left - margin.right; height = parseInt(d3.select("#chartID").style("height")); height = height - margin.top - margin.bottom; console.log('width, height: ', [width, height]); xScale = d3.scaleTime() .rangeRound([0, width]); yScale = d3.scaleLinear() .rangeRound([height, 0]); svg .attr("width", width + margin.left + margin.right) .attr("height", height + margin.top + margin.bottom); //xScale.domain(d3.extent(data, function(d) { return d.date; })); //yScale.domain([0, d3.max(data, function(d) { return d.close; })]); area.y0(yScale(0)); } </script> </body>
https://d3js.org/d3.v4.min.js