Highcharts系列 - 想要显示/隐藏所有EXCEPT所选系列(默认逻辑的反转)

Ian*_*Ian 26 highcharts

默认情况下,Highcharts允许您单击数据系列集以隐藏/取消隐藏它.

一种更有用的方法是执行反向逻辑 - 即仅显示所选系列并隐藏/取消隐藏未选择的系列.

看一下这里的例子(http://jsfiddle.net/t2MxW/14/),很明显可以"拦截"'legendItemClick'事件,我只是不确定如何实现require逻辑

可以替换以下脚本以获得3个数据集.

期望的场景:能够点击'苹果'并显示/隐藏'梨'和'橙子'.

================= PASTE START =============================== ========

var chart = new Highcharts.Chart({
    chart: {
        renderTo: 'container'
    },
    xAxis: {
        categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
    },

    plotOptions: {
        series: {
            events: {
                legendItemClick: function(event) {
                    var visibility = this.visible ? 'visible' : 'hidden';
                    if (!confirm('The series is currently '+ 
                                 visibility +'. Do you want to change that?')) {
                        return false;
                    }
                }
            }
        }
    },

    series:[{name: 'apples',
            data: [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4]},
    {name:'pears',
    data: [19.9, 81.5, 96.4, 119.2, 124.0, 166.0, 155.6, 138.5, 116.4, 144.1, 95.6, 54.4]},

           {name:'oranges',
    data: [119.9, 181.5, 46.4, 219.2, 24.0, 66.0, 255.6, 238.5, 16.4, 44.1, 95.6, 54.4]}
           ]   

});
Run Code Online (Sandbox Code Playgroud)

Igo*_*mov 46

HighCharts中的每个事件都包含this包含当前元素的值(在本例中为系列).您可以选择使用所有系列this.chart.series并以任何方式处理它们.试试这个功能.

legendItemClick: function(event) {
    if (!this.visible)
        return false;

    var seriesIndex = this.index;
    var series = this.chart.series;

    for (var i = 0; i < series.length; i++)
    {
        if (series[i].index != seriesIndex)
        {
            series[i].visible ?
            series[i].hide() :
            series[i].show();
        } 
    }
    return false;
}
Run Code Online (Sandbox Code Playgroud)

小提琴:https://jsfiddle.net/t2MxW/21971/

  • +1,希望我能代表@Ian接受答案! (2认同)
  • 它有效,但对于中等数量(n = 20)的系列来说速度超慢 (2认同)

phi*_*reo 9

我想做类似的事情......我想拥有它,这样如果你控制点击(或cmd-点击)一个图例项目,它将隐藏所有其他项目.(但将普通点击作为默认行为).

plotOptions: {
    series: {
        events: {
            legendItemClick: function(e) {
                // Upon cmd-click of a legend item, rather than toggling visibility, we want to hide all other items.
                var hideAllOthers = e.browserEvent.metaKey;
                if (hideAllOthers) {
                    var seriesIndex = this.index;
                    var series = this.chart.series;

                    for (var i = 0; i < series.length; i++) {
                        // rather than calling 'show()' and 'hide()' on the series', we use setVisible and then
                        // call chart.redraw --- this is significantly faster since it involves fewer chart redraws
                        if (series[i].index === seriesIndex) {
                            if (!series[i].visible) series[i].setVisible(true, false);
                        } else {
                            if (series[i].visible) series[i].setVisible(false, false);
                        }
                    }
                    this.chart.redraw();
                    return false;
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 非常感谢!我用过`e.browserEvent.metaKey || e.browserEvent.ctrlKey`包括Windows用户 (5认同)