简体   繁体   中英

Truncating canvas label in ChartJS v3.5.1

I have a bar chart with very long labels that is shrinking the canvas. I am trying to truncate the label if it has more than 10 characters. I tried a solution I found on another post here:

options: {
        responsive: true,
        maintainAspectRatio: false,
        scales: {
            xAxes: [{
                ticks: {
                    callback: function (value: string) {
                        return value.substr(0, 10);//truncate
                    },
                }
            }],
            yAxes: [{}]
        },
        tooltips: {
            enabled: true,
            mode: 'label',
        },
    }

This doesn't seem to work and cannot figure out why.

bar char image here

here you just remove string in the function it's work for me

options: {
  responsive: true,
  maintainAspectRatio: false,
  scales: {
      xAxes: [{
          ticks: {
              callback: function (value) {
                  return value.substr(0, 10);//truncate
              },
          }
      }],
      yAxes: [{}]
  },
  tooltips: {
      enabled: true,
      mode: 'label',
  },
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM