[英]d3 interaction on click event
I have a map with d3 circles showing the site locations, as well as a linechart showing the time trend for each of the site. 我有一个带有d3圆圈的地图,显示站点位置,还有一个线形图,显示每个站点的时间趋势。 I am trying to make a particular line highlight when a corresponding circle is clicked. 单击相应的圆圈时,我试图突出显示特定的线条。 Here is the code. 这是代码。 I can't seem to connect the siteIDs with the following function: 我似乎无法使用以下功能连接siteID:
function highlightLine(id) {
lineGroup.classed("g-highlight", function(d) {
return d.siteID == id.siteID;
});
};
Insert a console.log
as shown below, and it should become clearer: 插入console.log
,如下所示,它应该变得更加清晰:
function highlightLine(id) {
lineGroup.classed("g-highlight", function(d) {
console.log(d);
return d.siteID == id.siteID;
});
};
Because you're binding to data that you've run through d3.nest
, the id of d
that you're interested in is actually d.key
not d.siteID
, which does not exist on that level. 因为您要绑定到通过d3.nest
运行的数据,所以您感兴趣的d
的ID实际上是d.key
而不是d.siteID
,它在该级别上不存在。 So the boolean inside classed
should be 因此,内部classed
的布尔值应为
return d.key == id.siteID
That will cause the appropriate trendline's <g>
to have a "g-highlight" class, however it still will not visibly color the line. 这将导致适当的趋势线的<g>
具有“ g-highlight”类,但是仍不会明显地给线着色。 I believe that's because your css rule .g-highlight { stroke:... }
applies the stroke to the containing <g>
instead of the <path>
inside it. 我相信这是因为您的css规则.g-highlight { stroke:... }
将描边应用于包含的<g>
而不是其中的<path>
。 You can change that css rule to be .g-highlight path { ... }
and that will color the path as you'd like. 您可以将css规则更改为.g-highlight path { ... }
并根据需要为路径着色。
To bind the click event in d3 you should select the object with that class and bind the click: 要在d3中绑定click事件,您应该选择具有该类的对象并绑定click:
d3.selectAll(".g-highlight").on("click", function(d) {
return d.siteID == id.siteID;
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.