简体   繁体   English

如何在c3js图表中只显示点击图例数据?

[英]How to show only clicking legend data in c3js chart?

I have a line chart with multiple data.我有一个包含多个数据的折线图。 Here is the sample of my chart.这是我的图表示例。

传奇

Normally legend data are automatically show/hide if I click that legend on chart.如果我单击图表上的图例,通常图例数据会自动显示/隐藏。

But after I adding a function in legend like this,但是在我像这样在图例中添加了一个函数之后,

legend: {
        position: 'right',
        item: {
                onclick: function (d) { 
                            console.log("onclick", d); 
                            show(d); //when I click legend show some data
                }
            }
},

I don't get automatically show/hide if I click that legend on chart.如果我点击图表上的图例,我不会自动显示/隐藏。 But its not important for me.但这对我来说并不重要。

Instead of this, I want to show only clicking legend data in chart.而不是这个,我只想在图表中显示点击图例数据。 if I click data1 legend, I want to show only data1 line in chart and hide another data2 , data3 and data4 line in chart.如果我点击data1的传奇,我想只显示data1的图表线和隐藏另一个data2data3data4的图表线。

But, I don't have any idea how to do this.但是,我不知道如何做到这一点。 I'm very appreciate for any suggestion.我非常感谢任何建议。

Now, I can solve my problem.现在,我可以解决我的问题了。 Here is the code.这是代码。

legend: {
        position: 'right',
        item: {
                onclick: function (d) { 
                        console.log("onclick", d); //data1
                        show(d); //when I click legend show some data
                        chart.hide();
                        chart.show(d);
                }
            }
},

First, I use chart.hide() .首先,我使用chart.hide() This function will hide all data on chart.此功能将隐藏图表上的所有数据。

After that, I use chart.show(d) and it can show only data of user click because I add user clicking legend as an argument.之后,我使用chart.show(d)并且它只能显示用户点击的数据,因为我添加了用户点击图例作为参数。

Note: show(d) is not related with chart.show(d) .注意: show(d)chart.show(d) Just a function that I create for my need.只是我为我的需要创建的一个函数。

For those of you stumbling across this like I did while looking for a way to focus on a single series from a legend click (I'm used to a double-click doing this), it seems c3 added the ability to use alt-click to focus on a single legend item.对于那些像我一样在寻找一种从图例单击中专注于单个系列的方法时偶然发现的人(我习惯于双击执行此操作),似乎c3添加了使用alt-click专注于单个图例项目。 See the commit here .请参阅此处的提交。 It turns out this works in billboard.js as well, which is a fork of c3 .事实证明这也适用于billboard.js ,它是c3一个分支。

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

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