JSFiddle - React, Tailwind, and code Playground
HTML
<script src="//cdn.jsdelivr.net/lodash/2.4.1/lodash.min.js"></script>
<script src="//cdn.jsdelivr.net/d3js/3.4.3/d3.min.js"></script>
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; })).ticks(d3.time.month);
y.domain([0, d3.max(data, function(d) { return d.value; })]);
var newData = x.ticks().map(function(monthBucket) {
return _.find(data, { date: monthBucket }) || {date: monthBucket, value: 0};
});
// 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 + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
// Add the valueline path.
svg.append("path")
.attr("class", "line")
.attr("d", valueline(newData));
// Add the X Axis
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
//...