Get x-axis value in Highcharts


In this example we get the corresponding latest x-axis value of the max data in a y-axis series in Highcharts.

Here's the code below with comments:

$(function () {
    $('#container').highcharts({
        xAxis: {
            categories: ['a', 'b', 'c', 'd', 'e', 'f']
        },
        series: [{
            data: [5, 16, 10, 3, 12, 16]
        }, {
            data: [11, 18, 15, 16, 18, 11]
        }]
    });


    $("#click").click(function () {
        var chart = $("#container").highcharts(),
            series,
            i = 0,
            arr = [],
            myIndex = [],
            //change the index of the series that you like to get
            s = chart.series[1],

            max = s.dataMax,
            len = s.data.length;

        //getting the index with max value in y-axis
        for (var j = 0; j < len; j++) {
            arr[j] = s.data[j].y;
            if (arr[j] === max) {
                myIndex.push(j);
            }
        }
        //get the max of the indices
        var maxIndex = Math.max.apply(Math, myIndex);
        console.log(myIndex);
        $("#report").html("x: " + chart.xAxis[0].categories[maxIndex] + ", y: " + max);
    });
});

Popular posts from this blog

Auto-calculate your Splinterlands Assets/Income

Creating a Simple Button for Demo

Splinterlands: Water Deck for Beginners