Kevin
Kevin

Reputation: 113

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

here is the image that i created PIE chart using chartJs

.html

 <p-chart type="bar"></p-chart>

.TS I have added the config part

config: [{                                                              
      legend: {display: true, position: 'right'},
      responsive: false,    
    },

I want to remove space that i show in red arrow. I want to get little bit left side chart legends and want to change the width and hight of the chart

Upvotes: 1

Views: 842

Answers (1)

LeeLenalee
LeeLenalee

Reputation: 31429

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>

Upvotes: 0

Related Questions