Make Chart.js horizontal bar labels multi-line

Viewed 19081

Just wondering if there is any way to set the horizontal bar labels for y-axis using chart.js. Here is how I set up the chart:

<div class="box-body">
<canvas id="chart" style="position: relative; height: 300px;"></canvas>
</div>

Javascript:

    var ctx = document.getElementById('chart').getContext("2d");
    var options = {
        layout: {
            padding: {
              top: 5,
            }
        },
        responsive: true,
        animation: {
            animateScale: true,
            animateRotate: true
        },
    };

    var opt = {
      type: "horizontalBar",
      data: { 
        labels: label, 
        datasets: [{ 
        data: price, 
        }] 
      }, 
      options: options
    };

    if (chart) chart.destroy();
    chart= new Chart(ctx, opt);

    chart.update();

As you all can see, the first and third labels are too long and cut off. Is there a way to make the label multi-line?

2 Answers

If you want to have full control over how long labels are broken down across lines you can specify the breaking point by providing labels in a nested array. For example:

var chart = new Chart(ctx, {
...
  data: {
    labels: [["Label1 Line1:","Label1 Line2"],["Label2 Line1","Label2 Line2"]],
    datasets: [{
...
});
Related