最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to Reverse Chart.js Label Order? - Stack Overflow

programmeradmin2浏览0评论

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.

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)',
        ],
      },
    ]
  }
});

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.

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)',
        ],
      },
    ]
  }
});
Share Improve this question asked Jul 6, 2017 at 14:54 sidyllsidyll 59.3k14 gold badges114 silver badges154 bronze badges 2
  • Maybe this helps? stackoverflow./questions/27992044/… – Matt Commented Jul 6, 2017 at 14:58
  • Thanks @mkaatman it does help. But apparently the exact option I was looking for exists, check the answer below. – sidyll Commented Jul 6, 2017 at 15:23
Add a ment  | 

2 Answers 2

Reset to default 10

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

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

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/zdh7591r/

发布评论

评论列表(0)

  1. 暂无评论