JSFiddle - React, Tailwind, and code Playground
CSS
body { font: 12px Arial;}
path {
stroke: steelblue;
stroke-width: 2;
fill: none;
}
.axis path,
.axis line {
fill: none;
stroke: grey;
stroke-width: 1;
shape-rendering: crispEdges;
}
JavaScript
var data = [
{"date":"2013-01","value":53},
{"date":"2013-02","value":165},
{"date":"2013-03","value":269},
{"date":"2013-04","value":344},
{"date":"2013-05","value":376},
{"date":"2013-06","value":410},
{"date":"2013-07","value":421},
{"date":"2013-09","value":376},
{"date":"2013-10","value":359},
{"date":"2013-11","value":392},
{"date":"2013-12","value":433},
{"date":"2014-01","value":455},
{"date":"2014-02","value":478}
];
// Set the dimensions of the canvas / graph
var margin = {top: 30, right: 20, bottom: 30, left: 50},
width = 600 - margin.left - margin.right,
height = 270 - margin.top - margin.bottom;
// Parse the date / time
var parseDate = d3.time.format("%Y-%m").parse;
// Set the ranges
var x = d3.time.scale().range([0, width]);
var y = d3.scale.linear().range([height, 0]);
x.range([0, 500]);
// Get the data
data.forEach(function(d) {
d.date = parseDate(d.date);
d.value = +d.value;
});
// Scale the range of the data
x.domain(d3.extent(data, function(d) { return d.date; }));
y.domain([0, d3.max(data, function(d) { return d.value; })]);
var domain = x.domain();
var maxDay = new Date(domain[1]);
maxDay.setDate(maxDay.getDate() + 1);
var buckets = d3.time.months(domain[0], maxDay);
var newData = [];
formatDate = d3.time.format("%Y-%m")
for (var i=0;i<buckets.length;i++)
{
newData[i] = {};
newData[i]['date'] = buckets[i];
newData[i]['value'] = 0;
for (var z=0;z<data.length;z++)
{
var date1 = newData[i]['date'];
var date2 = data[z]['date'];
if (formatDate(date1) == formatDate(date2)) {
newData[i]['value'] = data[z]['value'];
}
}
}
// Define the axes
var xAxis = d3.svg.axis().scale(x)
.orient("bottom").ticks(8);
var yAxis = d3.svg.axis().scale(y)
.orient("left").ticks(5);
// Define the line
var valueline = d3.svg.line()
.x(function(d) { return x(d.date); })
.y(function(d) { return y(d.value); });
var svg = d3.select("body")
.append("svg")
.attr("width", width + margin.left +...