D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
bricedev
Full window
Github gist
Area Chart
<!DOCTYPE html> <meta charset="utf-8"> <style> body { font: 10px sans-serif; } svg { background-color: #FF9618 } .axis path, .axis line { display: none; } .overlay { fill: none; pointer-events: all; } </style> <body> <script src="https://d3js.org/d3.v3.js"></script> <script> var width = 960, height = 500; var parseDate = d3.time.format("%d-%b-%y").parse, bisectDate = d3.bisector(function(d) { return d.date; }).left, formatValue = d3.format(",.2f"), formatCurrency = function(d) { return "$" + formatValue(d); }, formatDate = d3.time.format("%d %B %Y"); var x = d3.time.scale() .range([0, width]); var y = d3.scale.linear() .range([height, 50]); var xAxis = d3.svg.axis() .scale(x) .orient("top"); var yAxis = d3.svg.axis() .scale(y); var area = d3.svg.area() .x(function(d) { return x(d.date); }) .y0(height) .y1(function(d) { return y(0); }); var svg = d3.select("body").append("svg") .attr("width", width) .attr("height", height) .append("g"); d3.tsv("data.tsv", function(error, data) { data.forEach(function(d) { d.date = parseDate(d.date); d.close = +d.close; }); data.sort(function(a, b) { return a.date - b.date; }); x.domain([data[0].date, data[data.length - 1].date]); y.domain(d3.extent(data, function(d) { return d.close; })); var path = svg.append("path") .datum(data) .attr("class", "area") .attr("d", area) .style("fill", "#FEAE4F") area.y1(function(d) { return y(d.close); }); path.transition().duration(2000).attr("d",area); svg.append("g") .attr("class", "x axis") .attr("transform", "translate(0," + height + ")") .call(xAxis) .selectAll("text") .style("font-weight","bold") .style("font-size",12) .style("fill", "#fff"); var keys = svg.append("g") .attr("class","keys"); keys.append("text") .attr("class","price") .attr("x", width/2) .attr("y", 100) .style("fill","#fff") .style("text-anchor","middle") .style("font-size","35px") .transition() .duration(2000) .tween("text", function() { var i = d3.interpolate(0, data[data.length-1].close); return function(t) { this.textContent = formatCurrency(i(t)); }; }); keys.append("text") .attr("class","date") .attr("x", width/2) .attr("y", 50) .style("fill","#fff") .style("text-anchor","middle") .style("font-size","35px") .text(formatDate(data[data.length-1].date)); var focus = svg.append("g") .attr("class", "focus") .style("display", "none"); focus.append("circle") .attr("r", 4.5) .style("fill","#fff"); focus.append("line") .attr("x1", 0) .attr("y1", 0) .attr("x2", 0) .attr("y2", height) .style("stroke","#fff") .style("stroke-width",.5); svg.append("rect") .attr("class", "overlay") .attr("width", width) .attr("height", height) .on("mouseover", function() { focus.style("display", null); }) .on("mouseout", function() { focus.style("display", "none"); }) .on("mousemove", mousemove); function mousemove() { var x0 = x.invert(d3.mouse(this)[0]), i = bisectDate(data, x0, 1), d0 = data[i - 1], d1 = data[i], d = x0 - d0.date > d1.date - x0 ? d1 : d0; focus.select("circle").attr("transform", "translate(" + x(d.date) + "," + y(d.close) + ")"); focus.select("line").attr("transform", "translate(" + x(d.date) + ",0)"); keys.select(".price").transition().text(formatCurrency(d.close)); keys.select(".date").transition().text(formatDate(d.date)); }; }); </script>
https://d3js.org/d3.v3.js