Added as an example for the question asked here on Stack Overflow.
Last active
November 30, 2019 18:37
-
-
Save d3noob/d5ecdbefb7cc43a48f59 to your computer and use it in GitHub Desktop.
Multi-series Associative
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
license: mit |
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 | New York | San Francisco | Austin | |
---|---|---|---|---|
1-May-12 | 43.98 | 48.13 | 43 | |
30-Apr-12 | 53.98 | 88.13 | 53 | |
27-Apr-12 | 63 | 68 | 63 | |
26-Apr-12 | 73 | 58 | 73 | |
25-Apr-12 | 83 | 48 | 83 |
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; | |
} | |
.line { | |
fill: none; | |
stroke: steelblue; | |
stroke-width: 1.5px; | |
} | |
.grid .tick { | |
stroke: lightgrey; | |
stroke-opacity: 0.7; | |
shape-rendering: crispEdges; | |
} | |
.grid path { | |
stroke-width: 0; | |
} | |
</style> | |
<body> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.17/d3.min.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 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.ordinal().range(["#FF0000", "#FFFF00", "#000000", "#FF00FF"]); | |
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.date); }) | |
.y(function(d) { return y(d.temperature); }); | |
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 + ")"); | |
function make_y_axis() { | |
return d3.svg.axis() | |
.scale(y) | |
.orient("left") | |
} | |
/* | |
var data = [ | |
{date:"1-May-12","New York":"58.13", "San Francisco":"58.13", "Austin": "43"}, | |
{date:"30-Apr-12","New York":"53.98" , "San Francisco":"48.13", "Austin": "53"}, | |
{date:"27-Apr-12","New York":"67.00", "San Francisco":"38.13", "Austin": "63"}, | |
{date:"26-Apr-12","New York":"89.70", "San Francisco":"28.13", "Austin": "73"}, | |
{date:"25-Apr-12","New York":"99.00", "San Francisco":"18.13", "Austin": "83"} | |
]; | |
*/ | |
d3.csv("data.csv", function(error, data) { | |
data.forEach(function(d) { | |
d.date = new Date (d.date); | |
}); | |
color.domain(d3.keys(data[0]).filter(function(key) { return key !== "date"; })); | |
//console.log(data); | |
var cities = color.domain().map(function(name) { | |
return { | |
name: name, | |
values: data.map(function(d) { | |
return {date: d.date, temperature: +d[name]}; | |
}) | |
}; | |
}); | |
console.log(cities); | |
x.domain(d3.extent(data, function(d) { return d.date; })); | |
y.domain([ | |
d3.min(cities, function(c) { return d3.min(c.values, function(v) { return v.temperature; }); }), | |
d3.max(cities, function(c) { return d3.max(c.values, function(v) { return v.temperature; }); }) | |
]); | |
svg.append("g") | |
.attr("class", "x axis") | |
.attr("transform", "translate(0," + height + ")") | |
.call(xAxis); | |
svg.append("g") | |
.attr("class", "y axis") | |
.call(yAxis) | |
svg.append("g") | |
.attr("class", "grid") | |
.call(make_y_axis() | |
.tickSize(-width, 0, 0) | |
.tickFormat("") | |
) | |
var city = svg.selectAll(".city") | |
.data(cities) | |
.enter().append("g") | |
.attr("class", "city"); | |
city.append("path") | |
.attr("class", "line") | |
.attr("d", function(d) { return line(d.values); }) | |
.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