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 azywong's block: Adding interaction to Multi-Series Line Chart
xxxxxxxxxx
<meta charset="utf-8">
<style>
.tooltip {
background-color: white;
border: 1px solid lightgray;
}
.line {
fill: none;
stroke: steelblue;
stroke-width: 1.5px;
}
.hover-line {
stroke: gray;
stroke-width: 1px;
stroke-dasharray: 3,3;
}
.overlay {
fill: none;
pointer-events: all;
}
.axis text {
fill: gray;
}
.axis path {
stroke: gray;
}
.tick line {
stroke: gray
}
.tick text {
fill: gray
}
</style>
<svg width="960" height="500"></svg>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script>
var actual = [
{
date: "2006-04",
value: 27000
},
{
date: "2006-07",
value: 27000
},
{
date: "2006-10",
value: 42000
},
{
date: "2007-01",
value: 32000
},
{
date: "2007-04",
value: 33000
},
{
date: "2007-07",
value: 41000
},
{
date: "2007-10",
value: 42000
},
{
date: "2008-01",
value: 71000
},
{
date: "2008-04",
value: 74000
},
{
date: "2008-07",
value: 76000
}
];
var predicted = [
{
date: "2006-04",
value: 27000
},
{
date: "2006-07",
value: 28000
},
{
date: "2006-10",
value: 44000
},
{
date: "2007-01",
value: 33000
},
{
date: "2007-04",
value: 34000
},
{
date: "2007-07",
value: 41000
},
{
date: "2007-10",
value: 38000
},
{
date: "2008-01",
value: 65000
},
{
date: "2008-04",
value: 63000
},
{
date: "2008-07",
value: 63000
}
];
var bisectDate = d3.bisector(function(d) { return d.date; }).left;
var parseTime = d3.timeParse("%Y-%m");
actual.forEach(function (item) {
item.date = parseTime(item.date)
})
predicted.forEach(function (item) {
item.date = parseTime(item.date)
})
var svg = d3.select("svg"),
margin = {top: 20, right: 80, bottom: 100, 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 x = d3.scaleTime().range([0, width]),
y = d3.scaleLinear().range([height, 0]),
z = d3.scaleOrdinal(d3.schemeCategory10);
var xAxis = d3.axisBottom(x)
.tickFormat(d3.timeFormat("%Y-%m"));
var yAxis = d3.axisLeft(y);
var line = d3.line()
.y(function(d) { return y(d.value); })
.x(function(d) { return x(d.date); });
var xGroup = g.append("g")
.attr("class", "axis axis--x")
.attr("transform", "translate(0," + height + ")");
var yGroup = g.append("g")
.attr("class", "axis axis--y");
var data = [
{
id: "actual",
values: actual
},
{
id: "predicted",
values: predicted
}
]
x.domain([
d3.min(data, function(c) { return d3.min(c.values, function(d) { return d.date; }); }),
d3.max(data, function(c) { return d3.max(c.values, function(d) { return d.date; }); })
]);
y.domain([
d3.min(data, function(c) { return d3.min(c.values, function(d) { return d.value; }); }),
d3.max(data, function(c) { return d3.max(c.values, function(d) { return d.value; }); })
]);
z.domain(data.map(function(c) { return c.id; }));
xGroup.call(xAxis)
.selectAll("text")
.style("text-anchor", "end")
.attr("dx", "-.8em")
.attr("dy", ".15em")
.attr("transform", "rotate(-65)");
yGroup.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", "0.71em")
.attr("fill", "#000")
.text("$");;
var focus = g.append("g")
.attr("class", "focus")
.style("display", "none");
var city = g.selectAll(".city")
.data(data)
.enter().append("g")
.attr("class", "city")
var paths = city.append("path")
.attr("class", "line")
.attr("d", function(d) {return line(d.values); })
.style("stroke", function(d) { return z(d.id); })
.attr("id", function(d) {
return d.id.substring(0, 3).toUpperCase();
})
city.append("text")
.datum(function(d) {
return {id: d.id, value: d.values[d.values.length - 1]}; })
.attr("transform", function(d) {
return "translate(" + x(d.value.date) + "," + y(d.value.value) + ")"; })
.attr("x", 3)
.attr("dy", "0.35em")
.style("font", "13px sans-serif")
.style("fill", function(d) { return z(d.id) })
.text(function(d) { return d.id; })
var hover = city.append("g")
.attr("class", function(d) { return "hover hover-" + d.id; })
.style("display", "none")
hover.append("line")
.attr("class", "x-hover-line hover-line")
.attr("y1", 0)
.attr("y2", height);
hover.append("line")
.attr("class", "y-hover-line hover-line")
.attr("x1", width)
.attr("x2", width);
hover.append("circle")
.attr("r", 5)
.style("stroke", function(d) { return z(d.id) })
.style("fill", "white")
hover.append("text")
.style("font", "13px sans-serif")
.style("fill", function(d) { return z(d.id) })
.style("stroke", "white")
.style("paint-order", "stroke")
.style("stroke-width", "5")
svg.append("rect")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")")
.attr("class", "overlay")
.attr("width", width)
.attr("height", height)
.on("mouseover", function() { hover.style("display", null); })
.on("mouseout", function() { hover.style("display", "none"); })
.on("mousemove", mousemove);
function mousemove() {
var x0 = x.invert(d3.mouse(this)[0])
data.forEach(function (line) {
var i = bisectDate(line.values, x0, 1),
d0 = line.values[i - 1],
d1 = line.values[i],
d = x0 - d0.date > d1.date - x0 ? d1 : d0;
var tooltip = d3.selectAll(".hover-" + line.id)
tooltip.attr("transform", "translate(" + x(d.date) + "," + y(d.value) + ")");
tooltip.selectAll("text").text("$" + d.value)
tooltip.selectAll(".x-hover-line").attr("y2", height - y(d.value));
tooltip.selectAll(".y-hover-line").attr("x2", width + width);
})
}
</script>
https://d3js.org/d3.v4.min.js