Created
July 15, 2014 04:37
/dev/chartpipe
This file contains 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
date | close | |
---|---|---|
1 | 582.13 | |
2 | 583.98 | |
3 | 603.00 | |
4 | 607.70 | |
5 | 610.00 | |
6 | 560.28 | |
7 | 571.70 | |
8 | 572.98 | |
9 | 587.44 | |
10 | 608.34 | |
11 | 609.70 | |
12 | 580.13 | |
13 | 605.23 | |
14 | 622.77 | |
15 | 626.20 | |
16 | 628.44 |
This file contains 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 = 500 - margin.top - margin.bottom; | |
var x = d3.scale.linear() | |
.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 line = d3.svg.line() | |
.interpolate("basis") | |
.x(function(d) { return x(d.series); }) | |
.y(function(d) { return y(d.metric); }); | |
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("data.csv", function(error, data) { | |
var series = d3.keys(data[0]).slice(0); | |
color.domain(d3.keys(data[0]).filter(function(key) { return key !== series[0]; })); | |
var metrics = color.domain().map(function(name) { | |
return { | |
name: name, | |
values: data.map(function(d) { | |
return {series: +d[series[0]], metric: +d[name]}; | |
}) | |
}; | |
}); | |
x.domain(d3.extent(data, function(d) { return d[series[0]]; })); | |
y.domain([ | |
d3.min(metrics, function(c) { return d3.min(c.values, function(v) { return v.metric; }); }), | |
d3.max(metrics, function(c) { return d3.max(c.values, function(v) { return v.metric; }); }) | |
]); | |
svg.append("g") | |
.attr("class", "x axis") | |
.attr("transform", "translate(0," + height + ")") | |
.call(xAxis); | |
svg.append("g") | |
.attr("class", "y axis") | |
.call(yAxis) | |
var group = svg.selectAll(".metric") | |
.data(metrics) | |
.enter().append("g") | |
.attr("class", "metric"); | |
group.append("path") | |
.attr("class", "line") | |
.attr("d", function(d) { return line(d.values); }) | |
.style("stroke", function(d) { return color(d.name); }); | |
group.append("text") | |
.datum(function(d) { return {name: d.name, value: d.values[d.values.length - 1]}; }) | |
.attr("transform", function(d) { return "translate(" + x(d.value.series) + "," + y(d.value.metric) + ")"; }) | |
.attr("x", 3) | |
.attr("dy", ".35em") | |
.text(function(d) { return d.name; }); | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment