1
votes

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?

2
Could you post live example, like jsFiddle?Kacper Madej
there's nothing to post really. If you copy and paste my chart code and provide some example data you will see what it producesgeorge

2 Answers

6
votes

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/

1
votes

You can use extension which allows do it.