Mattia Verga
Mattia Verga

Reputation: 77

ChartJS: unwanted top/bottom padding when legend is aligned right

With the old ChartJS v2, when I setup the legend to be right placed to a doughnut chart, the whole canvas height is auto sized to the minimum needed space, without any extra padding on top and bottom:

doughnut chart in v2 with right placed legend

In ChartJS v3 the canvas height is always the same, either with the default legend on top or on the right:

doughnut chart in v3 with default legend doughnut chart in v3 with right placed legend

This causes a lot of unwanted white space inside the canvas above and below the graph. Is there a way to avoid this? I couldn't find a relevant parameter to set in the documentation.

Thanks.

Upvotes: 0

Views: 935

Answers (1)

LeeLenalee
LeeLenalee

Reputation: 31371

You can play around with the aspect ratio

var options = {
  type: 'pie',
  data: {
    labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
    datasets: [{
        label: '# of Votes',
        data: [12, 19, 3, 5, 2, 3],
        borderWidth: 1
      },
      {
        label: '# of Points',
        data: [7, 11, 5, 8, 3, 7],
        borderWidth: 1
      }
    ]
  },
  options: {
    aspectRatio: 1.2,
    plugins: {
      legend: {
        position: 'right'
      }
    }
  }
}

var ctx = document.getElementById('chartJSContainer').getContext('2d');
new Chart(ctx, options);
canvas {
  background-color: #eee;
}
<body>
  <canvas id="chartJSContainer"></canvas>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.8.0/chart.js"></script>
</body>

Upvotes: 1

Related Questions