简体   繁体   English

在d3js中,如何通过单击堆栈栏来过滤数据

[英]in d3js how can I filter data by clicking on stack bar

I'm trying to filter data by clicking on certain rect bar in a stacked bar. 我试图通过单击堆叠栏中的某些矩形条来过滤数据。 This stack bar is a two dimention graph, the horizontal index is years, and the vertical index different product revenues, I want to filter by certain product in certain year. 这个堆栈条是一个二维图,水平索引是年,垂直索引是不同的产品收入,我想按特定年份的特定产品进行过滤。 means when I click on a rect bar of the whole graph, the data should get filtered by certain year and certain product. 意味着当我单击整个图形的矩形条时,数据应该按特定年份和特定产品进行过滤。

my code is : 我的代码是:

metrics.on("click", function(d,i,j){
         d3.select(this)
         .style("fill","lightcoral")
         .style("stroke","red");
         fdata =rawdata.filter(function(d){return (d[xvalue]==_seriesA[i])&&(d[yvalue]==_seriesB[j])});
    });

but the weird thing is the value of j is always undefined, although if I attach a title to it : 但是奇怪的是j的值始终是不确定的,尽管如果我给它加上标题:

metrics.append("title").text(
            function(d,i,j) {
                return i + ' - '+j ;
            });

the value of j will get printed correctly, is there anything special about the 'on' click function? j的值将正确打印,单击功能是否有特殊之处? why I cannot get the value of j? 为什么我无法获得j的值? any help will be appreciated....this is triving me crazy for the whole night... 任何帮助将不胜感激....这使我整夜疯狂...

According to the documentation , the second argument to .on() is a function that takes two arguments, not three. 根据文档.on()的第二个参数是一个接受两个参数而不是三个参数的函数。 Similarly, it shouldn't work in your second example either. 同样,它在您的第二个示例中也不起作用。

I think you're getting confused about the distinction between the data and how it is rendered. 我认为您对数据及其呈现方式之间的区别感到困惑。 The .on() function will get you the data. .on()函数将为您获取数据。 The scales you have created somewhere in your code take that data and map it to x and y coordinates in your graph (or you use the data directly without scales). 您在代码中某处创建的比例尺将获取该数据并将其映射到图形中的xy坐标(或者直接使用数据而没有比例尺)。 In the .on() function, you use the data in exactly the same way you used it when you created the graph to start with. .on()函数中,您使用的数据与创建图表时使用的数据完全相同。 That is, the data item ( d in your code) contains the data for the specific position that you're highlighting. 也就是说,数据项(代码中的d )包含要突出显示的特定位置的数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM