score:3

Accepted answer

this is because you are using v2 syntax in v3, v3 has some major breaking changes over v2. please read the migration guide for all of them.

for your callback to work you need to define it in options.plugins.tooltip.callbacks.label

edit:

like the error says you are getting you can't reassign a constant variable since its a constant. if you change it to a let it works fine:

const options = {
  type: 'line',
  data: {
    labels: ["red", "blue", "yellow", "green", "purple", "orange"],
    datasets: [{
      label: '# of votes',
      data: [12, 19, 3, 5, 2, 3],
      bordercolor: 'pink'
    }]
  },
  options: {
    plugins: {
      tooltip: {
        callbacks: {
          label: function(context) {
            let label = context.dataset.label || '';

            if (label) {
              label += ': ';
            }
            if (context.parsed.y !== null) {
              label += new intl.numberformat('en-us', {
                style: 'currency',
                currency: 'usd'
              }).format(context.parsed.y);
            }
            return label;
          }
        }
      }
    },
  }
}

const ctx = document.getelementbyid('chartjscontainer').getcontext('2d');
new chart(ctx, options);
<body>
  <canvas id="chartjscontainer" width="600" height="400"></canvas>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/chart.js/3.6.0/chart.js"></script>
</body>


Related Query

More Query from same tag