Lleims
Lleims

Reputation: 1353

Modify the information box of the scatter chart in Chart.JS

I'm working with a scatter chart in chart.js and I need to modify the info what I show when I'm above a point. I think the box that contains this information is called tooltip.

Below I show what I have now,

enter image description here

What I want to do is modify the first one, and put 14:52 like the x-axis.

In the x-axis was easy,

options: {
    scales: {
        xAxes: [{
            type: 'linear',
            position: 'bottom',
            ticks: {
                callback: function(value, index, values) {
                    var time = String(value);
                    if (time.length == 3){
                        time = '0' + time;
                    }
                    var hour = time[0] + time[1];
                    var min = time[2] + time[3];
                    var label = hour + ':' + min;
                    return label;
                }
            }
        }]
    }
}

And I suppose here I have to use the same function, but I don't know where. I'm not able to access to the box with the information, isn't its name tooltips? For example, I'm doing the code below but nothing change.

tooltips:{
    title: "New title"
}

How can I modify that box?

Thank you very much

Upvotes: 0

Views: 1501

Answers (1)

uminder
uminder

Reputation: 26170

The title and other items on the tooltip can be customized using callback functions. The correct syntax looks as follows.

tooltips:{
    callbacks: {
        title: (tooltipItem, data) => "New title"
    }
}

Please also have a look on the following more complex sample derived from https://www.chartjs.org/samples/latest/tooltips/callbacks.html. It should make no difference whether you work with a scatter or a line chart.

new Chart(document.getElementById('myChart'), {
    type: 'line',
    data: {
        labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'],
        datasets: [{
            label: 'My First dataset',
            borderColor: 'red',
            backgroundColor: 'red',
            data: [15, 22, 18, 28, 8, 13, 24],
            fill: false,
        }, {
            label: 'My Second dataset',
            borderColor: 'blue',
            backgroundColor: 'blue',
            data: [5, 31, 15, 22, 19, 29, 12],
            fill: false,
        }]
    },
    options: {
        responsive: true,
        title: {
            display: true,
            text: 'Chart.js Line Chart - Custom Information in Tooltip'
        },
        tooltips: {
            mode: 'index',
            callbacks: {
                title: (tooltipItem, data) => data.labels[tooltipItem[0].index],
                footer: (tooltipItems, data) => {
                    var sum = 0;
                    tooltipItems.forEach(function(tooltipItem) {
                        sum += data.datasets[tooltipItem.datasetIndex].data[tooltipItem.index];
                    });
                    return 'Sum: ' + sum;
                },
            },
            footerFontStyle: 'normal'
        },
        hover: {
            mode: 'index',
            intersect: true
        },
        scales: {
            xAxes: [{
                display: true,
                scaleLabel: {
                    show: true,
                    labelString: 'Month'
                }
            }],
            yAxes: [{
                display: true,
                scaleLabel: {
                    show: true,
                    labelString: 'Value'
                }
            }]
        }
    }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.min.js"></script>
<canvas id="myChart" height="100"></canvas>

Upvotes: 4

Related Questions