add event to x-axis labels of bar highchart

。_饼干妹妹 提交于 2019-12-13 18:51:43

问题


I am trying to retrieve the text of a label on the char x-axis by clicking on it. I am using bar chart and the code is the following:

var chart = new Highcharts.Chart({
    chart: {
        type: 'column',
        backgroundColor: '#eaedf1',
        zoomType: 'x',
        renderTo: 'container'
    },
    plotOptions: {
        series: {
            cursor: 'pointer',
            pointWidth: 10,
            point: {
                events: {
                    click: function (event) {
                        console.log(event.point.name + " " + this.y);
                    }
                }
            }
        }
    },
    title: {
        text: 'Total Flow Types'
    },
    xAxis: {
        type: 'category',
        labels: {
            rotation: -90
        }

    },
    yAxis: {
        min: 0,
        title: {
            text: 'millions'
        }
    },
    legend: {
        enabled: false
    },
    series: [{
        name: 'Flow Types'
    }]
}); 

Then by clicking on a button the chart gets populated using ajax which works fine. By checking the dom of the chart I saw that each of the labels is text /text. They are part of g/ element with a class highcharts-axis-labels highcharts-xaxis-labels. So I've tried retrieving the values using jquery like:

$('body').on('click', 'g.highcharts-axis-labels.highcharts-xaxis-labels text',   function () {
    console.log($(this).text());
}); 

or just

$('body').on('click', 'text',   function () {
    console.log($(this).text());
}); 

This all is part of the document.ready function. Unfortunately none of these retrieves the text of a x-axis label?


回答1:


Instead of:

$('body').on('click', 'g.highcharts-axis-labels.highcharts-xaxis-labels text', function () {
    console.log($(this).text());
});

Use this:

$('.highcharts-xaxis-labels text').on('click', function () {
    console.log($(this).text());
});

You should not connect classes highcharts-axis-labels and highcharts-xaxis-labels with a dot. And also the class highcharts-xaxis-labels is enough to add the event on. Here's the fiddle: http://jsfiddle.net/8sxLr5j8/




回答2:


You can use extension which allows do it.



来源:https://stackoverflow.com/questions/27446970/add-event-to-x-axis-labels-of-bar-highchart

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!