简体   繁体   English

高位图表 - 打开和关闭数据

[英]High Charts - Toggle data on and off

I'm trying to find a way of toggling data on and off in charts using Highcharts.我正在尝试找到一种使用 Highcharts 在图表中打开和关闭数据的方法。 Is there a feature in highcharts library itself that allows this or is it something I would need to do separately? highcharts 库本身是否有允许这样做的功能,还是我需要单独做的事情?

For instance, in the following example id like to be able to toggle 'delivered' and 'bounced' on or off.例如,在下面的示例中,id 希望能够打开或关闭“delivered”和“bounced”。

jsfiddle - https://jsfiddle.net/Luo5nfb1/4/ jsfiddle - https://jsfiddle.net/Luo5nfb1/4/

Highcharts.chart('container', {
    chart: {
        type: 'spline'
    },

    legend: {
        symbolWidth: 40
    },

    title: {
        text: 'Trends Over Time'
    },

    yAxis: {
        title: {
            text: 'Number of Clients'
        }
    },

    xAxis: {
        title: {
            text: 'Time'
        },
        categories: ['2020/08/01', '2020/08/07', '2020/08/14', '2020/08/21', '2020/08/28']
    },

    tooltip: {
        valueSuffix: '%'
    },

    plotOptions: {
        series: {
            point: {
                events: {
                    click: function () {
                        window.location.href = this.series.options.website;
                    }
                }
            },
            cursor: 'pointer'
        }
    },

    series: [
        {
            name: 'Attempted',
            data: [400738, 380154, 0, 410846, 402738],
            color: '#3f8ec5'
        }, {
            name: 'Delivered',
            data: [400236, 370436, 0, 400236, 392623],
            color: '#69b051'
        }, {
            name: 'Opened',
            data: [66404, 56204, 0, 63764, 62091],
            color: '#69b051'
        }, {
            name: 'Bounced',
            data: [502, 498, 0, 512, 500],
            color: '#91201a'
        }
    ],

    responsive: {
        rules: [{
            condition: {
                maxWidth: 550
            },
            chartOptions: {
                legend: {
                    itemWidth: 150
                },
                xAxis: {
                    categories: ['2020/08/01', '2020/08/07', '2020/08/14', '2020/08/21', '2020/08/28', '2020/09/04']
                },
                yAxis: {
                    title: {
                        enabled: false
                    },
                    labels: {
                        format: '{value}%'
                    }
                }
            }
        }]
    }
});

Highcharts doesn't offer such kind of a feature, but you should be able to achieve the wanted output by using the linkedTo feature and create a dummy series to keep the legend item for those series and attach them into it or create a custom button to toggle it. Highcharts 不提供这种功能,但您应该能够通过使用linkedTo功能来实现所需的输出并创建一个虚拟系列来保留这些系列的图例项目并将它们附加到其中或创建一个自定义按钮切换它。

Solution with using the custom button toggling: https://jsfiddle.net/BlackLabel/8tkvad0m/使用自定义按钮切换的解决方案: https : //jsfiddle.net/BlackLabel/8tkvad0m/

let btn = document.getElementById("btn");

btn.addEventListener('click', function(){
    chart.series.forEach(s => {
        if(s.name === "Delivered" || s.name === "Bounced") {
            s.visible ? s.hide() : s.show()
        }
    })
})

Solution with using the linkedTo option: https://jsfiddle.net/BlackLabel/mr0q1twb/使用linkedTo选项的解决方案: https : linkedTo

API: https://api.highcharts.com/class-reference/Highcharts.Series#show API: https : //api.highcharts.com/class-reference/Highcharts.Series#show

API: https://api.highcharts.com/highcharts/series.line.linkedTo API: https : //api.highcharts.com/highcharts/series.line.linkedTo

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

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