Simple line chart
CSS
body {
font: 10px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.x.axis path {
display: none;
}
.line {
fill: none;
stroke: steelblue;
stroke-width: 1.5px;
}
JavaScript
var data = [{
"key": [2000, 1, 1, 12],
"value": 1000
}, {
"key": [2000, 2, 1, 12],
"value": 3896
}, {
"key": [2000, 7, 1, 24],
"value": 289
}, {
"key": [2000, 8, 1, 24],
"value": 389
}]
var margin = {
top: 20,
right: 20,
bottom: 30,
left: 50
},
width = 760 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var parseDate = d3.time.format("%Y.%m.%d.%H").parse;
var x = d3.time.scale()
.range([0, width]);
var y = d3.scale.linear()
.range([height, 0]);
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(y)
.orient("left");
var line = 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 + ")");
var mapped_data = data.map(function(d) {
return {
date: 4,
value: +d.value
}
});
console.log(mapped_data);
x.domain(d3.extent(mapped_data, function(d) {
return d.date;
}));
y.domain(d3.extent(mapped_data, function(d) {
return d.value;
}));
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em");
svg.append("path")
.datum(mapped_data)
.attr("class", "line")
.attr("d", line);