Search code examples
chart.jschart.js2

How can I get access to the index of the highlighted dataset in Chart.js


I am trying to display a value (Prod_ary1[selected index of chart] in the tool tips using the beforeBody callback:

tooltips: {
    mode: 'index',
    intersect: true,
    callbacks: {
        beforeBody: function(tooltipItems, data){
            return 'Count:  ' + "<display Prod_ary[1][highlighted index]";
        },

        label: function(tooltipItems, data) { 
            return data.datasets[tooltipItems.datasetIndex].label +': '  + TS_ary[tooltipItems.datasetIndex][tooltipItems.index];

        }
}

I have attempted to access the index to tell me which element I need to display in the array using:

return 'Count:  ' + Prod_ary[1][tooltipItems.index];

But it returns undefined. I'm screwing something up but can't quite figure out what.
My fiddle


Solution

  • In the tooltips callbacks I changed this:

    return 'Count:  ' + Prod_ary[1][tooltipItems.index];
    

    to this:

    return 'Count:  ' + Prod_ary[1][tooltipItems[0]["index"]];