[英]How can I trigger a mouseover event using a dropdown in D3?
I am building a line chart using D3.js and I want users to be able to use a drop down menu to highlight values for their Zip Code. 我正在使用D3.js构建折线图,希望用户能够使用下拉菜单突出显示其邮政编码的值。 I am already doing this with a mouseover event when a user hovers over a line. 当用户将鼠标悬停在一行上时,我已经通过mouseover事件来执行此操作。
I've tried setting mouseover and mouseout events to call an "onmouseover" function as seen here: 我尝试设置mouseover和mouseout事件来调用“ onmouseover”函数,如下所示:
series.selectAll(".hover")
.on("mouseover", function(d,i) {
d3.selectAll(".line")
.style("opacity", 0.82)
.filter(function(p) { return p.zipcode == d.zipcode; })
.style("opacity", 1)
.style("stroke-width", 2)
.style("stroke", function(d,i) { return color2(i); });
d3.selectAll(".series text")
.style("opacity", 0)
.filter(function(p) { return p.zipcode == d.zipcode; })
.style("opacity", 1)
.on("mouseover", onmouseover)
.on("mouseout", onmouseout);
I then also have my "onmouseover" function that is to be activated by the dropdown: 然后,我还有要通过下拉菜单激活的“ onmouseover”功能:
function onmouseover(d,i){
d3.selectAll(".line")
.style("opacity", 0.82)
.filter(function(p) { return p.name == d.name; })
.style("opacity", 1)
.style("stroke-width", 2)
.style("stroke", function(d,i) { return color2(i); })
d3.selectAll(".series text")
.style("opacity", 0)
.filter(function(p) { return p.name == d.name; })
.style("opacity", 1);}})
Which I try to activate when using a dropdown menu: 我在使用下拉菜单时尝试激活的方法:
$("#dropdownselect").change(ziphandler)
function ziphandler(){
var key = $(this)
.children(":selected")
.val();
onmouseover({id : key});
}
The ideal outcome is that a user can hover over a line to see the new style and also highlight a line by selecting their Zip Code in a dropdown menu. 理想的结果是,用户可以将鼠标悬停在一行上以查看新样式,还可以通过在下拉菜单中选择其邮政编码来突出显示一行。
EDIT: The code is here to see in action: http://bl.ocks.org/cminshew/31581ca3e55fbf67862a 编辑:该代码是在这里看到在操作: http : //bl.ocks.org/cminshew/31581ca3e55fbf67862a
我认为您想致电$(this).onmouseover({id : key});
There are a few problems with your code: 您的代码存在一些问题:
zipselected
and not dropdownselect
. 下拉选择器的ID是zipselected
而不是dropdownselect
。 ziphandler
and onmouseover
functions are declared in different scopes. ziphandler
和onmouseover
函数在不同的范围内声明。 In particular, you can't call onmouseover
from ziphandler
. 特别是,您不能从ziphandler
调用onmouseover
。 onmouseover
function, you are referencing a .name
attribute, which does not exists. 在onmouseover
函数中,您引用的是.name
属性,该属性不存在。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.