Created
May 30, 2014 10:24
-
-
Save jpillora/7ce5b3f8567434b16ddb to your computer and use it in GitHub Desktop.
Line Graph with D3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<meta charset="utf-8"> | |
<style> | |
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; | |
} | |
</style> | |
<body> | |
<script src="http://d3js.org/d3.v3.js"></script> | |
<script> | |
var margin = {top: 20, right: 80, bottom: 30, left: 50}, | |
width = 960 - margin.left - margin.right, | |
height = 200 - margin.top - margin.bottom; | |
var parseDate = d3.time.format("%Y%m%d").parse; | |
var x = d3.time.scale() | |
.range([0, width]); | |
var y = d3.scale.linear() | |
.range([height, 0]); | |
var color = d3.scale.category10(); | |
var xAxis = d3.svg.axis() | |
.scale(x) | |
.orient("bottom"); | |
var yAxis = d3.svg.axis() | |
.scale(y) | |
.orient("left"); | |
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 data = [ | |
{"title":"1", "date":"20140401", "value": 1}, | |
{"title":"2", "date":"20140415", "value": 1}, | |
{"title":"3", "date":"20140416", "value": 2}, | |
{"title":"4", "date":"20140422", "value": 2}, | |
{"title":"5", "date":"20140423", "value": 3}, | |
{"title":"6", "date":"20140502", "value": 2}, | |
{"title":"7", "date":"20140509", "value": 1} | |
]; | |
data.forEach(function(d) { | |
d.date = parseDate(d.date); | |
}); | |
color.domain(["data"]); | |
x.domain(d3.extent(data, function(d) { return d.date; })); | |
y.domain([ | |
d3.min(data, function(v) { return v.value; }), | |
d3.max(data, function(v) { return v.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") | |
.style("text-anchor", "end") | |
.text("Entries"); | |
var set = svg.selectAll(".city") | |
.data([ {name: "Values", values: data} ]) | |
.enter().append("g") | |
.attr("class", "city"); | |
//line creation function | |
var line = d3.svg.line() | |
.interpolate("basis") | |
.x(function(d) { return x(d.date); }) | |
.y(function(d) { return y(d.value); }); | |
set.append("path") | |
.attr("class", "line") | |
.attr("d", function(d) { return line(data); }) | |
.style("stroke", function(d) { return color(d.name); }); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment