简体   繁体   中英

Add a text as tooltip for each point on a line chart

i am wondering if there is an option in ChartJS for how i can see extra information if i hover over a single point on a line chart. Currently my data looks like this:

function drawChart(dataSets) {
    Chart.defaults.global.maintainAspectRatio = false;
    Chart.defaults.global.responsive = false;
    var data = {
        labels: ["ID"]
        , datasets: dataSets
    };
    var options = {
        title: {
            display: true
            , text: 'Custom Chart Title'
        }
        , scales: {
            yAxes: [{
                ticks: {
                    beginAtZero: true
                }
            }]
            , xAxes: [{
                type: "linear"
                , position: "bottom"
            }]
        }
        , tooltips: {
            callbacks: {
                label: function (tooltipItem, data) {
                    alert("Entered tooltip callback.");
                    return i18n.t('chart.count') + ': ' + getCount(tooltipItem, data.datasets);
                }
            }
        }
    };
    var myChart = Chart.Line(ctx, {
        data: data
        , options: options
    });
}

function getCount(tooltipItem, datasets) {
    return datasets[tooltipItem.datasetIndex].data.find(datum => {
        return datum.x === tooltipItem.xLabel && datum.y === tooltipItem.yLabel;
    }).count;
}

And this is how one data point looks like:

x:5
, y:5
, count:2

But the alert "Entered tooltip" never gets called, what am i doing wrong? Instead i get this error on my console (Google Chrome):

在此处输入图片说明

This looks like it:

label: sensorID,
data: [0,1,2,3],
backgroundColor: [
        'rgba(' + rndColor() + ',' + rndColor() + ',' + rndColor() + ',0.2)'
],
borderColor: [
        'rgba(' + rndColor() + ',' + rndColor() + ',' + rndColor() + ',0.2)'
],
options: {
    tooltips: {
        enabled: true
        custom: function(tooltip) {
            // code here to customize a tooltip
        }
    }
}

http://www.chartjs.org/docs/#chart-configuration-tooltip-configuration

http://www.chartjs.org/docs/#advanced-usage-external-tooltips

See sample/line-customTooltips.html for examples on how to get started.

The way i added my custom tooltips. Firstly i created a dataset in which data array i added additional info like "count"

x: 10,
y: 12,
count: 2

This count does not get shown in graph unless I manually get it in options hook under tooltips.

tooltips: {
      callbacks: {
                label: function(tooltipItem, data) {
                    return i18n.t('chart.count') +': ' + getCount(tooltipItem, data.datasets);
                }
      }
 }

Get count for given dataset that was not represented in chart

function getCount(tooltipItem, datasets) {
    return datasets[tooltipItem.datasetIndex].data.find(datum => {
        return datum.x === tooltipItem.xLabel && datum.y === tooltipItem.yLabel;
    }).count;
}

And the result: 在此处输入图片说明 Edit: adding an example of my dataset

return [{
    label: 'Example dataset',
    backgroundColor: '#98cc99',
    borderColor: '#98cc99',
    pointHoverRadius: 3,
    data: [ {x:1,y:2,count:3}, {x:2,y3,count:4}, ...]
}];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM