我在React中用D3构建了一个折线图,并且我试图添加多条线,并将每条线悬停在每条线上显示工具提示。我被工具提示部分卡住了。目前,仅显示了一个工具提示,这似乎是最后一个显示该提示的行。
这是我的组件的样子:
class LineChart extends Component {
wrapper = React.createRef();
componentDidMount() {
const { dataSets } = this.props
let svg = d3.select("body svg.mySvg"),
margin = {top: 20, right: 20, bottom: 30, left: 40},
width = +svg.attr("width") - margin.left - margin.right,
height = +svg.attr("height") - margin.top - margin.bottom;
const bisectDate = d3.bisector(function(d) { return d.year; }).left;
const x = d3.scaleTime().range([0, width]);
const y = d3.scaleLinear().range([height, 0]);
const line = d3.line()
.x(function(d) { return x(d.year); })
.y(function(d) { return y(d.value); });
let g = svg.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
x.domain(d3.extent(dataSets.jitter, function(d) { return d.year; }));
y.domain([0, d3.max(dataSets.jitter, function(d) { return d.value; })]);
g.append("g")
.attr("class", "axis axis--x")
.attr("transform", "translate(0," + height + ")")
.style("color", "#fff")
.call(d3.axisBottom(x).ticks(3))
.selectAll("text")
.style("text-anchor", "end")
.attr("dx", "-.8em")
.attr("dy", ".3em")
.attr("transform", "rotate(-45)");
g.append("g")
.attr("class", "axis axis--y")
.style("color", "#fff")
.call(d3.axisLeft(y).ticks(7).tickFormat(function(d) { return d; }));
[dataSets.jitter, dataSets.latency].forEach((data, index) => {
g.append("path")
.datum(data)
.attr("class", "line")
.attr("d", line);
let focus = g.append("g")
.attr("class", `focus focus-${index}`)
.style("display", "none");
focus.append("line")
.attr("class", "y-hover-line hover-line")
.attr("x1", width)
.attr("x2", width);
focus.append("circle")
.attr("r", 7.5);
focus.append("text")
.attr("class", `text text-${index}`)
.style("color", "#fff")
.attr("x", 15)
.attr("dy", ".31em");
svg.append('rect')
.attr("transform", "translate(" + margin.left + "," + margin.top + ")")
.attr("class", `overlay overlay-${index}`)
.attr("width", width)
.attr("height", height)
.on("mouseover", function() { focus.style("display", null); })
.on("mouseout", function() { focus.style("display", "none"); })
.on("mousemove", function() {
var x0 = x.invert(d3.mouse(this)[0]),
i = bisectDate(data, x0, 1),
d0 = data[i - 1],
d1 = data[i],
d = x0 - d0.year > d1.year - x0 ? d1 : d0;
focus.attr("transform", "translate(" + x(d.year) + "," + y(d.value) + ")");
focus.select(`text.text-${index}`)
.style("color", "#fff")
.style("stroke", "#fff")
.text(function() { return d.value; });
});
})
}
render() {
const { width, height } = this.props
return (
<svg
ref={ this.wrapper }
height={ height }
width={ width }
className="mySvg"
></svg>
)
}
}
道具数据集如下:
{
latency: [
{ year: 2011, value: 3 },
{ year: 2012, value: 20 },
{ year: 2013, value: 2 },
{ year: 2014, value: 12 },
{ year: 2015, value: 8 },
{ year: 2016, value: 14 },
{ year: 2017, value: 8 }
],
jitter: [
{ year: 2011, value: 1 },
{ year: 2012, value: 28 },
{ year: 2013, value: 5 },
{ year: 2014, value: 12 },
{ year: 2015, value: 6 },
{ year: 2016, value: 4 },
{ year: 2017, value: 7 }
]
}
所以我的想法是在[dataSets.jitter, dataSets.latency].forEach
(line 44 here)的每次迭代中渲染每条线和工具提示,这似乎仅适用于渲染线,但不适用于两个工具提示,仅适用于最后一个。如果我更改数组中的顺序,以使dataSets.latency在dataSets.jitter之前,则将在该行显示工具提示。
是否只将鼠标悬停事件注册到最后一个事件?如果是,怎么办?