【发布时间】:2023-01-23 08:27:21
【问题描述】:
我有多线图,我想在其中单击一条线并永久突出显示它。我在单击事件中使用了以下代码,但是在我单击并移动鼠标后,该行返回。
希望有人能帮助我!
编辑:我还有一个包含在代码 sn-p 中的 mouseover 和 mouseout 事件。
diagam.on('click', countryClick)
.on('mouseover, countryOver)
.on('mouseout, countryOut);
//Mouse event functions
function countryOver(event, d) {
d3.select(this)
.style('stroke', 'black')
.style('stroke-width', 5)
.attr('id', 'countryOver')
.selectAll('g')
.append('text')
.attr('class', 'tile-text')
.attr('x', x(dataByCountry.year) / 2)
.attr('y', y(dataByCountry) / 2)
.attr('text-anchor', 'middle')
.attr('font-size', '50')
.attr('fill', 'black')
.text(dataByCountry.name);
}
function countryOut(event, ) {
d3.select(this)
.style('stroke', 'green')
.style('stroke-width', 1.5)
.selectAll('g')
.append('text')
.attr('class', 'tile-text')
.attr('x', x(dataByCountry.year) / 2)
.attr('y', y(dataByCountry) / 2)
.attr('text-anchor', 'middle')
.attr('font-size', '50')
.attr('fill', 'black')
.text(dataByCountry.name)
}
function countryClick(event, d){
d3.select(this)
.style('stroke', 'red')
.style('stroke-width', 7)
.selectAll('g')
.append('text')
.attr('class', 'tile-text')
.attr('x', x(dataByCountry.year) / 2)
.attr('y', y(dataByCountry) / 2)
.attr('text-anchor', 'middle')
.attr('font-size', '50')
.attr('fill', 'black')
.text(dataByCountry.name)
}
【问题讨论】:
-
你也有一个在 mouseOut 上调用的函数吗?
-
是的,我愿意。这就是点击不是永久性的原因吗?
-
这取决于您在此功能中执行的操作。你能分享一下吗?检查您是否没有更改代码中其他地方的线条颜色。
-
我确实更改了上面的代码 sn-p。我确实有一个 mouseout 和 mouseover 事件,它们突出显示了一行。
标签: javascript d3.js mouseevent mouseclick-event