[Chartjs]-Showing 'undefined%' in the graph if data does not exist

1๐Ÿ‘

โœ…

You can check if val exists using if statements

datalabels: {
    formatter: (val, context) => {
        if(val) return `${val}%`
        return;
    },
  }, // 

1๐Ÿ‘

If you just want to show 0 in case of undefined (or any falsy statement) you can use the || operator.

datalabels: {
                formatter: (val, context) => `${val || 0}%`,
              }

1๐Ÿ‘

The problem seems to be this line: return ((value / this.max) * 100).toFixed(0) + "%";
Something in there, probably the value is undefined or so. You can display nothing conditionally with e.g. return value ? ((value / this.max) * 100).toFixed(0) + "%" : null;

Leave a comment