How to replace a numerical label with a tag using Chart.js

Let's assume that you want to represent the status of a system from the 27th until the 30th of November.

If the status of the system is equal to 1, it means that it was working properly during the whole day (ok). Otherwise, it was broken for some time (ko).

Here is the data sample:

javascript
// You want to represent data as ok's & ko's instead of ones and zeros
// You need to transform data into [ok, ko, ok, ok]
let labels = ['27/11/2020', '28/11/2020', '29/11/2020', '30/11/2020'];
let data   = [1, 0, 1, 1];

If you wanted to represent the sample graphically:

javascript
var myChart = new Chart(ctx, {
    type: 'line',
    data: {
        labels: ['27/11/2020', '28/11/2020', '29/11/2020', '30/11/2020'],
        datasets: [{
            label: 'Status',
            data: [1, 0, 1, 1]
        }]
    },
    options: {
        tooltips:{
            callbacks: {
                label: (tooltipItem, data) => tooltipItem.yLabel === 1 ? 'ok' : 'ko'
            }
        }
    }
});

You can use the tooltips property to replace the numerical value with a string.

Hi, I'm Erik, an engineer from Barcelona. If you like the post or have any comments, say hi.