高图表 + 从 jquery 中的选项设置事件点击

Highcharts + set event click from options in jquery

本文关键字:设置 事件 选项 jquery 高图表      更新时间:2023-09-26

我有一个这样的数组选项:

var options = {
    chart: {
        type: 'bar',
        events: {
            click: function(event) {
                
            }
        }
    },
    xAxis: {
        categories: '',
        title: {
            text: null
        }
    },
    yAxis: {
        min: 0,
        title: {
            text: 'Aantal keer gekozen',
            align: 'high'
        },
        labels: {
            overflow: 'justify'
        }
    },
    tooltip: {
        //valueSuffix: ' aantal keer gekozen'
    },
    plotOptions: {
        bar: {
            dataLabels: {
                enabled: true,
                color: 'black',
                formatter: function() {
                    if (this.y === 0) {
                        return null;
                    } else {
                        return this.y;
                    }
                }
            },
            stacking: 'normal'
        }
    },
    legend: {
        layout: 'vertical',
        align: 'right',
        verticalAlign: 'top',
        x: -40,
        y: 100,
        floating: true,
        borderWidth: 1,
        backgroundColor: '#FFFFFF',
        shadow: true
    },
    credits: {
        enabled: false
    },
    exporting: {
        enabled: true
    },
    series: []
}

如您所见,我不在点击功能上做任何事情。

我在foreach的jQuery中创建高图表(所以我创建了多个图表)。

options.chart.renderTo = 'container' + index;
chart = new Highcharts.Chart(options);

现在如何像renderTo那样添加事件点击?

您可能需要阅读有关 javascript 中的闭包的信息 - 否则为每个图表添加相同的事件。

另一种解决方案是为图表创建 id 数组,然后使用:options.chart.events.click = function() { window.location.href = '/path/' + ids[index]; } ;