score:0

Accepted answer

here is how you define the custom tooltip function:

chart.defaults.global.customtooltips = function (tooltip) {}

this fires anytime a tooltip would fire. (note that tooltips do not identify hovering over an individual datapoint. instead, they return the entire vertical series.)

the tooltips object, contains these properties for each hovered (vertical) series:

  • title: x-axis label (vertical) e.g. "march"
  • labels: all data points (vertical) e.g. [29,86]

further, it is possible to access the dataset for the hovered series like this:

  • datasets[0]: first series (horizontal) e.g. [65, 59, 80, 81, 56, 55, 40]
  • data.datasets[1].label: label for 2nd horizontal data series

below is an example that extracts this data as you move the mouse:

jsfiddle demo

html:

<div class="panel panel-success">
    <div id="rpt"></div>
    <div class="panel-heading">
        <div id="i">
            change purple series: 
            <input id="ii" type="text" value="99"/> 
            <button id="ib">update</button>
        </div>
        <h3 class="panel-title">panel title</h3>
    </div>
    <div class="panel-body">
        <canvas id="mychart" width="600" height="400"></canvas>
    </div>
</div>

javascript/jquery:

//update 2nd datapoint, purple series
$('#ib').click(function(){
    tmp = $('#ii').val();
    mylinechart.datasets[0].points[2].value = tmp;
    mylinechart.update();
    $('#ii').val( ~~(math.random() * 80) + 20 ); //gen new number
});

//custom tooltip function - try uncommenting various alerts
chart.defaults.global.customtooltips = function (tooltip) {
    if (tooltip.y){
        tt = tooltip.title;
        ll = tooltip.labels;
        no = tooltip.labels.length;
        rpt = 'title: ' +tt+ ' - data: ';
        for (i=0;i<no;i++){
//            alert( data.datasets[i].strokecolor );
//            alert( data.datasets.length );
//            alert( data.datasets[i].label );
//            alert( tooltip.labels[i] );
            rpt += data.datasets[i].label +': '+ ll[i] +'; ';
        }
        alert(rpt);
        //alert('title: ' +tt+ ' - data: ' +ll );
        //alert( json.stringify(tooltip) );
        //$('#rpt').html( json.stringify(tooltip) );
    }
}

//chart data
var data = {
    labels: ["january", "february", "march", "april", "may", "june", "july"],
    datasets: [{
        label: "purple cows",
        fillcolor: "rgba(220,20,20,0.2)",
        strokecolor: "purple",
        pointcolor: "rgba(220,220,220,1)",
        pointstrokecolor: "#fff",
        pointhighlightfill: "#fff",
        pointhighlightstroke: "rgba(220,220,220,1)",
        data: [65, 59, 80, 81, 56, 55, 40]
    }, {
        label: "red heifers",
        fillcolor: "rgba(151,187,205,0.9)",
        strokecolor: "red",
        pointcolor: "rgba(151,187,205,1)",
        pointstrokecolor: "#fff",
        pointhighlightfill: "#fff",
        pointhighlightstroke: "rgba(151,187,205,1)",
        data: [28, 48, 40, 19, 86, 27, 90]
    }]
};

//chart options
var options = {
    animation: false,
    scaleoverride: true,
    scalesteps: 3,
    scalestepwidth: 30,
    scalestartvalue: 10,

    responsive: true,
    maintainaspectratio: false,
    scaleshowgridlines : true,
    scalegridlinecolor : "rgba(0,0,0,.05)",
    scalegridlinewidth : 1,
    scaleshowhorizontallines: true,
    scaleshowverticallines: true,
    beziercurve : false,
    beziercurvetension : 0.4,
    pointdot : true,
    pointdotradius : 3,
    pointdotstrokewidth : 1,
    pointhitdetectionradius : 20,
    datasetstroke : true,
    datasetstrokewidth : 2,
    datasetfill : false
}

//create the chart, based on above data
var ctx = $("#mychart").get(0).getcontext("2d");
var mylinechart = new chart(ctx).line(data, options);

sources:

detecting hover events over parts of a chart using chart.js


Related Query

More Query from same tag