如何根据Highcharts中的选择获取系列名称

时间:2015-08-10 22:18:11

标签: javascript highcharts

我正在尝试根据Highcharts图表中选择的内容获取系列名称,但标签不显示系列名称。

以下是我一直在处理的代码的链接:http://jsfiddle.net/ktjno528/

$(function () {
    // create the chart
    $('#container').highcharts({
        chart: {
            events: {
                redraw: function () {
                    var label = this.renderer.label(this.series.name, 100, 120)
                        .attr({
                            fill: Highcharts.getOptions().colors[0],
                            padding: 10,
                            r: 5,
                            zIndex: 8
                        })
                        .css({
                            color: '#FFFFFF'
                        })
                        .add();

                    setTimeout(function () {
                        label.fadeOut();
                    }, 1000);

                }
            }
        },
        xAxis: {
            categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
        },

        series: [{name:'S1',
            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]
        }]
    });


    // activate the button
    $('#button').click(function () {
        var chart = $('#container').highcharts();
        chart.addSeries({name:'S2',
            data: [216.4, 194.1, 95.6, 54.4, 29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5]
        });

        $('#button').unbind('click');
    });
});

1 个答案:

答案 0 :(得分:1)

您正在尝试访问this.series.name,因为this.series是一个系列数组,因此不返回任何内容。因此,您可能需要使用this.series[0].namethis.series[1].name,具体取决于您要显示的系列名称。

updated JSFiddle here