This multi-line chart uses an invisible Voronoi tessellation to handle mouseover; the closest point to the mouse on any line is highlighted. Click the checkbox in the top-right to toggle the visibility of the Voronoi overlay.
forked from mbostock's block: Multi-Line Voronoi
forked from juanprq's block: Multi-Line Voronoi with colors
xxxxxxxxxx
<meta charset="utf-8">
<style>
.axis--y path {
display: none;
}
.cities {
fill: none;
stroke-linejoin: round;
stroke-linecap: round;
stroke-width: 1.5px;
}
.context-cities {
fill: none;
stroke-linejoin: round;
stroke-linecap: round;
stroke-width: 1.5px;
}
.tooltip text {
text-anchor: middle;
text-shadow: 0 1px 0 #fff, 1px 0 0 #fff, 0 -1px 0 #fff, -1px 0 0 #fff;
}
.voronoi path {
fill: none;
pointer-events: all;
}
.voronoi--show path {
stroke: red;
stroke-opacity: 0.2;
}
#form {
position: absolute;
top: 20px;
right: 30px;
}
</style>
<svg width="960" height="500"></svg>
<label id="form" for="show-voronoi">
Show Voronoi
<input type="checkbox" id="show-voronoi" disabled>
</label>
<script src="//d3js.org/d3.v4.min.js"></script>
<script>
var months,
monthKeys,
monthParse = d3.timeParse("%Y-%m");
var svg = d3.select("svg"),
margin = {top: 20, right: 30, bottom: 110, left: 40},
margin2 = {top: 430, right: 20, bottom: 30, left: 40},
width = svg.attr("width") - margin.left - margin.right,
height = svg.attr("height") - margin.top - margin.bottom,
height2 = +svg.attr("height") - margin2.top - margin2.bottom,
g = svg.append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")");
var x = d3.scaleTime()
.range([0, width]),
x2 = d3.scaleTime().range([0, width]),
y2 = d3.scaleLinear().range([height2, 0]);
var y = d3.scaleLinear()
.range([height, 0]);
var z = d3.scaleOrdinal(d3.schemeCategory10);
var voronoi = d3.voronoi()
.x(function(d) { return x(d.date); })
.y(function(d) { return y(d.value); })
.extent([[-margin.left, -margin.top], [width + margin.right, height + margin.bottom]]);
var line = d3.line()
.x(function(d) { return x(d.date); })
.y(function(d) { return y(d.value); });
var line2 = d3.line()
.x(function(d) { return x2(d.date); })
.y(function(d) { return y2(d.value); });
var focus = g.append("g")
.attr("class", "focus")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
var context = g.append("g")
.attr("class", "context")
.attr("transform", "translate(" + margin2.left + "," + margin2.top + ")");
d3.tsv("unemployment.tsv", type, function(error, data) {
if (error) throw error;
x.domain(d3.extent(months));
y.domain([0, d3.max(data, function(c) { return d3.max(c.values, function(d) { return d.value; }); })]).nice();
z.domain(data.map(function(d) { return d.name; }));
x2.domain(x.domain());
y2.domain(y.domain());
focus.append("g")
.attr("class", "axis axis--x")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x));
focus.append("g")
.attr("class", "axis axis--y")
.call(d3.axisLeft(y).ticks(10, "%"))
.append("text")
.attr("x", 4)
.attr("y", 0.5)
.attr("dy", "0.32em")
.style("text-anchor", "start")
.style("fill", "#000")
.style("font-weight", "bold")
.text("Unemployment Rate");
focus.append("g")
.attr("class", "cities")
.selectAll("path")
.data(data)
.enter().append("path")
.attr("d", function(d) { d.line = this; return line(d.values); })
.style("stroke", function(d) { return z(d.name); });
var tooltip = focus.append("g")
.attr("transform", "translate(-100,-100)")
.attr("class", "tooltip");
tooltip.append("circle")
.attr("r", 3.5);
tooltip.append("text")
.attr("y", -10);
var voronoiGroup = focus.append("g")
.attr("class", "voronoi");
voronoiGroup.selectAll(".cities")
.data(voronoi.polygons(d3.merge(data.map(function(d) { return d.values; }))))
.enter().append("path")
.attr("d", function(d) { return d ? "M" + d.join("L") + "Z" : null; })
.on("mouseover", mouseover)
.on("mouseout", mouseout);
d3.select("#show-voronoi")
.property("disabled", false)
.on("change", function() { voronoiGroup.classed("voronoi--show", this.checked); });
context.append("g")
.attr("class", "context-cities")
.selectAll("path")
.data(data)
.enter().append("path")
.attr("d", function(d) { d.line = this; return line2(d.values); })
.style("stroke", function(d) { return z(d.name); });
context.append("g")
.attr("class", "axis axis--x")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x2));
var brush = d3.brushX()
.extent([[0, 0], [width, height2]])
.on("brush end", brushed);
context.append("g")
.attr("class", "x-brush")
.call(brush)
.call(brush.move, x.range());
function mouseover(d) {
d3.select(d.data.city.line)
.style('stroke-width', 3)
.style('stroke', d3.hsl(z(d.data.city.name)).brighter(1));
d.data.city.line.parentNode.appendChild(d.data.city.line);
tooltip.attr("transform", "translate(" + x(d.data.date) + "," + y(d.data.value) + ")");
tooltip.select("text").text(d.data.city.name + ': ' + d.data.value);
}
function mouseout(d) {
console.log(d)
d3.select(d.data.city.line)
.style("stroke-width", 1.5)
.style('stroke', z(d.data.city.name));
tooltip.attr("transform", "translate(-100,-100)");
}
function brushed() {
if (d3.event.sourceEvent && d3.event.sourceEvent.type === "zoom") return;
// x.domain(d3.event.selection === null ? x2.domain() : brush.extent());
var s = d3.event.selection || x2.range();
var cities = d3.select(".cities");
x.domain(s.map(x2.invert, x2));
cities.selectAll("path").attr("d", function(d) { return line(d.values);});
focus.select("axis axis--x").call(d3.axisBottom(x));
// g.select(".zoom").call(zoom.transform, d3.zoomIdentity
// .scale(width / (s[1] - s[0]))
// .translate(-s[0], 0));
}
var zoom = d3.zoom()
.scaleExtent([1, Infinity])
.translateExtent([[0, 0], [width, height]])
.extent([[0, 0], [width, height]]);
});
function type(d, i, columns) {
if (!months) monthKeys = columns.slice(1), months = monthKeys.map(monthParse);
var c = {name: d.name.replace(/ (msa|necta div|met necta|met div)$/i, ""), values: null};
c.values = monthKeys.map(function(k, i) { return {city: c, date: months[i], value: d[k] / 100}; });
return c;
}
</script>
https://d3js.org/d3.v4.min.js