Chartjs-How can I move chartJs legend left side and change the width and Hight of the chart?

1๐Ÿ‘

โœ…

2 things, the config options for chart take an object and not an array, also the legend config has been moved to the plugins section so you will get this:

config: {
  responsive: true,
  plugins: {
    legend: {
      position: 'right'
    }
  }
}

Plain js example:

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,
        backgroundColor: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
        }
        ]
  },
  options: {
    plugins: {
        legend: {
        position: 'right'
      }
    }
  }
}

var 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.3.0/chart.js"></script>
</body>

Leave a comment