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
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
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 margin = {top: 20, right: 20, bottom: 70, left: 40}, | |
width = 1200 - 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], .05).padding(0.1); | |
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([0, d3.max(data, function(d) { return d.value; })]); | |
svg.append("g") | |
.attr("class", "x axis") | |
.attr("transform", "translate(0," + height + ")") | |
.call(xAxis.ticks(null).tickSize(0)) | |
.selectAll("text") | |
.style("text-anchor", "middle") | |
// .attr("dx", "-.8em") | |
// .attr("dy", "-.55em") | |
// .attr("transform", "rotate(-90)" ); | |
svg.append("g") | |
.attr("class", "y axis") | |
.call(yAxis.ticks(null).tickSize(0)) | |
.append("text") | |
// .attr("transform", "rotate(-90)") | |
.attr("y", 6) | |
// .attr("dy", ".71em") | |
.style("text-anchor", "middle") | |
.text("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("width", x.bandwidth()) | |
.attr("y", function(d) { return y(d.value); }) | |
.attr("height", function(d) { return height - y(d.value); }); | |
svg.selectAll("lines") | |
.data(data) | |
.enter().append("line") | |
.style("fill", 'none') | |
.attr("x1", function(d) { return x(d.date) + x.bandwidth()+5; }) | |
.attr("x2", function(d) { return x(d.date)-5; }) | |
.attr("y1", function(d) { return y(d.target); }) | |
.attr("y2", function(d) { return y(d.target); }) | |
.style("stroke-dasharray", [2,2]) | |
.style("stroke", "#000000") | |
.style("stroke-width", 2) | |
}); | |
</script> | |
</body> |