Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Click event on highchart axis

I want to be able to bind an event to the axes in high charts, so I can expose some options for them in the UI (axis interval, text formatting, gridlines, etc.)

Doesn't seem to be a way to do this in highcharts. So far, I can get it to do my bidding when I click on the labels, but not when I click on the spaces between the labels. See the fiddle here, for version 2.2.4: http://jsfiddle.net/gW4p6/174/

var chart = new Highcharts.Chart({
    chart: {
        renderTo: 'container',
        marginRight: 80 // like left
    },
    xAxis: {
        categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
    },
    yAxis: [{
          lineWidth: 1,
          title: {
              text: 'Secondary Axis'
          }
    }],

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

var clearSelection = function() { $('.highcharts-axis').css('stroke', ''); };

$('.highcharts-axis').click(function(event) {
    clearSelection();
    $(this).css('stroke', 'green');
    return false;
});
$(document).click(clearSelection);
$('svg').click(clearSelection);

Is there any way that I can reliable catch a click event on the g.highcharts-axis element?

For bonus marks, what's the best way to link the axis with its axis.id in the options? Best I can come up with is to rely on the fact that axis are rendered in the order they are supplied, so I can just loop.

like image 615
XwipeoutX Avatar asked Dec 21 '25 01:12

XwipeoutX


1 Answers

$('.highcharts-axis text').click(function(){ alert($(this).text());  }); 

This alerts the text of the axis. This way you could bind click event to each elements in axis.

like image 120
Dinesh Kumar DJ Avatar answered Dec 23 '25 20:12

Dinesh Kumar DJ