[Chartjs]-Charts JS: How to set units?

19👍

You can add units to tooltips using tooltip callbacks configuration.

For example, here is how to add “GB” unit to the tooltip:

const options = {
  tooltips: {
    callbacks: {
      label: (item) => `${item.yLabel} GB`,
    },
  },
}

8👍

I found that the options API has changed in later versions of Chart.js (v3.7.0 as of writing).

An example of adding temperature units is as follows:

const options = {
    plugins: {
        tooltip: {
            callbacks: {
                label: (item) =>
                    `${item.dataset.label}: ${item.formattedValue} °C`,
            },
        },
    },
}

3👍

for Angular 7, This works for me, might help you:

options: {
    tooltips: {
      callbacks: {
        label: (tooltipItems, data) => {
            return data.datasets[tooltipItems.datasetIndex].data[tooltipItems.index] + ' GB';
        }
    },
    }

Leave a comment