This is a version of this D3v3 timeseries bar chart using the D3 v4 API.
It also uses data with UTC timestamps.
forked from zigahertz's block: D3v4 Bar Chart with Timeseries Data
forked from vikkya's block: D3v4 Bar Chart with Timeseries Data
| license: gpl-3.0 |
This is a version of this D3v3 timeseries bar chart using the D3 v4 API.
It also uses data with UTC timestamps.
forked from zigahertz's block: D3v4 Bar Chart with Timeseries Data
forked from vikkya's block: D3v4 Bar Chart with Timeseries Data
| date | value | target | |
|---|---|---|---|
| 2016-01-02 | -53 | 20 | |
| 2016-02-03 | -165 | 100 | |
| 2016-03-04 | 269 | 16 | |
| 2016-04-05 | 344 | 300 | |
| 2016-05-06 | 376 | 400 | |
| 2016-06-07 | 410 | 300 | |
| 2016-07-08 | 421 | 200 | |
| 2016-08-09 | 405 | 250 | |
| 2016-09-10 | 376 | 400 | |
| 2016-10-11 | 359 | 259 | |
| 2016-11-12 | 392 | 500 | |
| 2016-12-13 | 433 | 120 | |
| 2017-01-14 | 455 | 120 | |
| 2017-02-15 | 478 | 345 |
| <!DOCTYPE html> | |
| <meta charset="utf-8"> | |
| <head> | |
| <style> | |
| .axis { | |
| font: 10px sans-serif; | |
| } | |
| .axis path, | |
| .axis line { | |
| fill: none; | |
| stroke: #000; | |
| shape-rendering: crispEdges; | |
| } | |
| .axis .domain{ | |
| display: none; | |
| } | |
| </style> | |
| </head> | |
| <body> | |
| <script src="https://d3js.org/d3.v4.min.js"></script> | |
| <script> | |
| var month = ["January","February","March","April","May","June","July","August","September","October","November","December"] | |
| var margin = {top: 20, right: 20, bottom: 70, left: 40}, | |
| width = 600 - margin.left - margin.right, | |
| height = 400 - margin.top - margin.bottom; | |
| // Parse the date / time | |
| var parseDate = d3.isoParse | |
| var x = d3.scaleBand().rangeRound([0, width]).padding(0.8); | |
| var y = d3.scaleLinear().range([height, 0]); | |
| var xAxis = d3.axisBottom() | |
| .scale(x) | |
| .tickFormat(d3.timeFormat("%b")); | |
| var yAxis = d3.axisLeft() | |
| .scale(y) | |
| .ticks(10); | |
| var svg = d3.select("body").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("bar-data.csv", function(error, data) { | |
| data.forEach(function(d) { | |
| d.date = parseDate(d.date); | |
| d.value = +d.value; | |
| }); | |
| x.domain(data.map(function(d) { return d.date; })); | |
| y.domain([-d3.max(data, function(d) { return d.value; }), d3.max(data, function(d) { return d.value; })]); | |
| svg.selectAll("bar") | |
| .data(data) | |
| .enter().append("rect") | |
| .style("fill", function(d){ return d.value < d.target ? '#EF5F67': '#3FC974'}) | |
| .attr("x", function(d) { return x(d.date); }) | |
| .attr("y", function(d) { return d.value < 0 ? height/2 : y(d.value); }) | |
| .attr("width", x.bandwidth()) | |
| .attr("height", function(d) { return height/2 - y(Math.abs(d.value)) }); | |
| svg.selectAll("circles") | |
| .data(data) | |
| .enter().append("circle") | |
| .style("fill", function(d){ return d.value < d.target ? '#EF5F67': '#3FC974'}) | |
| .style("fill-opacity", 0.5) | |
| .attr("cx", function(d) { return x(d.date) + x.bandwidth()/4; }) | |
| .attr("r", function(d) { return Math.abs(d.value/20) }) | |
| .attr("cy", function(d) { return y(d.value); }); | |
| svg.selectAll("texts") | |
| .data(data) | |
| .enter().append("text") | |
| .style("fill", 'black') | |
| .attr("x", function(d) { return x(d.date) + x.bandwidth()/4; }) | |
| .text(function(d){ return month[new Date(d.date).getMonth()].slice(0,3)}) | |
| .attr("y", function(d) { return d.value < 0 ? y(d.value) : y(d.value); }) | |
| .style("text-anchor", "middle") | |
| }); | |
| </script> | |
| </body> |