Javascript 在d3中的鼠标上方更新行顺序(z方向)
我不熟悉使用d3和JavaScript,希望能得到一些建设性的反馈。我正在模拟一个实践示例来学习d3,它涉及从两个来源(GISS和HAD)绘制1880-2010年的气候数据(温度异常)。到目前为止,我已经使用这些数据在d3中生成了一个多折线图。代码和数据在这里 在本例中,数据最初以灰色打印,但每条线在鼠标上方的颜色不同 我想添加两个附加功能Javascript 在d3中的鼠标上方更新行顺序(z方向),javascript,d3.js,Javascript,D3.js,我不熟悉使用d3和JavaScript,希望能得到一些建设性的反馈。我正在模拟一个实践示例来学习d3,它涉及从两个来源(GISS和HAD)绘制1880-2010年的气候数据(温度异常)。到目前为止,我已经使用这些数据在d3中生成了一个多折线图。代码和数据在这里 在本例中,数据最初以灰色打印,但每条线在鼠标上方的颜色不同 我想添加两个附加功能 我想,在mouseover上,对行重新排序,使鼠标悬停的行位于顶部,本质上是对行重新排序。我已经读到这本质上需要重新填充SVG,并且我已经尝试了与此类似的代
source.append("path")
.attr("class", "line")
.attr("d", function(d) { return line(d.values); })
.style("stroke", "lightgrey")
.on("mouseover", function() {
if (active) active.classed("highlight", false);
active = d3.select(this.parentNode.appendChild(this))
.classed("highlight", true);
})
.style("stroke",function(d) {return color(d.name);})
.on("mouseout", function(d) {
d3.select('#path-' + d.name)
.transition()
.duration(750)
.style("stroke", "lightgrey")
})
.attr("id", function(d, i) { return "path-" + d.name; });
source.append("path")
.attr("class", "line")
.attr("d", function(d) { return line(d.values); })
.style("stroke", "lightgrey")
.on("mouseover", function(d){
d3.select(this)
.style("stroke",function(d) {return color(d.name);});
})
.on("mouseout", function(d) {
d3.select('#path-' + d.name)
.transition()
.duration(750)
.style("stroke", "lightgrey")
})
.attr("id", function(d, i) { return "path-" + d.name; });
source.append("text")
.datum(function(d) { return {name: d.name, value: d.values[d.values.length - 15]}; })
.attr("transform", function(d) { return "translate(" + x(d.value.date) + "," + y(d.value.temperature) + ")"; })
.attr("x", 5)
.attr("dy", ".35em")
.style("stroke", "lightgrey")
.on("mouseover", function(d){
d3.select('#path-' + d.name)
.style("stroke",function(d) {return color(d.name);});
})
.on("mouseout", function(d) {
d3.select('#path-' + d.name)
.transition()
.duration(750)
.style("stroke", "lightgrey")
})
.text(function(d) { return d.name; })
.attr("font-family","sans-serif")
.attr("font-size","11px")
.attr("id", function(d, i) { return "text-" + d.name; });
NateChris Viau在d3 Google group上为这个问题提供了一个很好的答案。 诀窍是选择路径的g父级以与其他路径一起重新排序: this.parentNode.parentNode.appendChild(this.parentNode) 这会将当前选择的容器“g”附加到所有其他“g”之上 我发现这在很多其他情况下也很有用
谢谢Chris!根据您当前的要点,您似乎已经解决了这些问题。如果是,您可以发布您的答案吗?如果不是,还有什么问题?