Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Reverse Chart.js Label Order?

I'm using a lot of pie and doughnut charts with two data values in a project. It really annoys me that since the chart slices starts from the top going clockwise, the labels end up looking reversed: on the opposite side of the data it represents.

Chart.js Doughtnut Chart

Is there an easy way to reverse the label order, or reverse the chart to run counter-clockwise?

new Chart(context, {
  type: 'doughnut',
  data: {
    labels: [ 'Blue', 'Red' ],
    datasets: [
      {
        data: [7, 3],
        backgroundColor: [
          'rgba(54, 162, 235, 0.7)',
          'rgba(208, 54, 100, 0.7)',
        ],
      },
    ]
  }
});
like image 877
sidyll Avatar asked Jan 30 '23 01:01

sidyll


2 Answers

I think what you are looking for is the reverse option under legend which will show datasets in reverse order

http://www.chartjs.org/docs/latest/configuration/legend.html?h=reverse

enter image description here

like image 79
ckatsara Avatar answered Feb 02 '23 16:02

ckatsara


Try giving it rotation via options in the Chart object.

new Chart(context, {
type: 'doughnut',
data: {
labels: [ 'Blue', 'Red' ],
datasets: [{
          data: [7, 3],
          backgroundColor: [
          'rgba(54, 162, 235, 0.7)',
          'rgba(208, 54, 100, 0.7)',
          ],
      }]
     },
  options: {rotation: (0.5 * Math.PI)}
});

Fiddle: https://jsfiddle.net/zdh7591r/

like image 37
AJD- Avatar answered Feb 02 '23 16:02

AJD-