This line chart is constructed from a TSV file storing the daily average temperatures of New York, San Francisco and Austin over the last year. The chart employs conventional margins and a number of D3 features:
forked from mbostock's block: Multi-Series Line Chart
forked from ShuangjiaoHu's block: Multi-Series Line Chart-8/29-4
xxxxxxxxxx
<meta charset="utf-8">
<style>
.axis--x path {
display: none;
}
.line {
fill: none;
stroke: steelblue;
stroke-width: 1.5px;
}
</style>
<svg width="960" height="500"></svg>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script>
var svg = d3.select("svg"),
margin = {top: 20, right: 80, bottom: 30, left: 50},
width = svg.attr("width") - margin.left - margin.right,
height = svg.attr("height") - margin.top - margin.bottom,
g = svg.append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")");
var parseTime = d3.timeParse("%Y%m%d");
var clicked = -1;
var x = d3.scaleTime().range([0, width]),
y = d3.scaleLinear().range([height, 0]),
z = d3.scaleOrdinal(d3.schemeCategory10);
var line = d3.line()
.curve(d3.curveBasis)
.x(function(d) { return x(d.date); })
.y(function(d) { return y(d.temperature); });
var zoom = d3.zoom();
d3.tsv("data.tsv", type, function(error, data) {
if (error) throw error;
var cities = data.columns.slice(1).map(function(id) {
return {
active: 1,
id: id,
values: data.map(function(d) {
return {date: d.date, temperature: d[id]};
})
};
});
x.domain(d3.extent(data, function(d) { return d.date; }));
y.domain([
d3.min(cities, function(c) { return d3.min(c.values, function(d)
{ return d.temperature; }); }),
d3.max(cities, function(c) { return d3.max(c.values, function(d)
{ return d.temperature; }); })
]);
z.domain(cities.map(function(c) { return c.id; }));
g.append("g")
.attr("class", "axis axis--x")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x));
g.append("g")
.attr("class", "axis axis--y")
.call(d3.axisLeft(y))
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", "0.71em")
.attr("fill", "#000")
.text("Temperature, ºF");
var city = g.selectAll(".city")
.data(cities)
.enter().append("g")
.attr("class", "city");
city.append("path")
.attr("class", "line")
.attr("d", function(d) { return line(d.values)})
.attr("id", function(d, i) { return 'tag'+i; })
.style("stroke", function(d) { return z(d.id); });
city.append("text")
.datum(function(d) { return {id: d.id, active: d.active, value:
d.values[d.values.length - 1]}; })
.attr("transform", function(d) { return "translate(" + x(d.value.date) + ","
+ y(d.value.temperature) + ")"; })
.attr("x", 3)
.attr("dy", "0.35em")
.style("font", "10px sans-serif")
.on("click", function(d, i){
var active = d.active==1 ? 0 : 1;
console.log(d.id);
console.log("d.active:" + d.active);
console.log("active:" + active);
newOpacity = active==0 ? 0 : 1;
clicked = active==0 ? i : -1;
if(clicked == i){
d3.select("#arcSelection").remove();
}
d3.select("#tag"+i)
.style("opacity", newOpacity);
d.active = active;
})
.on("mouseover", function(d, i) {
if(clicked != i){
g.append("path")
.attr("class", "line")
.attr("d", function() { return line(cities[i].values)})
.attr("id", "arcSelection")
.style("stroke", function() { return z(d.id); })
.style("stroke-width", 2);
}
var j;
for(j = 0; j < 3; j++){
if(i != j && i != clicked){
console.log(j);
d3.select("#tag"+j)
.transition().duration(100)
.style("stroke", "#d3d3d3");
}
}
})
.on("mouseout", function(d, i) {
d3.select("#arcSelection").remove();
var j;
for(j = 0; j < 3; j++){
if(i != j){
console.log(j);
d3.select("#tag"+j)
.transition().duration(100)
.style("stroke", function(d) { return z(d.id); });
}
}
})
.text(function(d) { return d.id; });
});
function type(d, _, columns) {
d.date = parseTime(d.date);
for (var i = 1, n = columns.length, c; i < n; ++i) d[c = columns[i]] = +d[c];
return d;
}
</script>
https://d3js.org/d3.v4.min.js