Chart.js PolarArea Chart. Changing the labels to a 45 degree angle

笑着哭i 提交于 2020-01-01 11:33:08

问题


I have searched through the net with no success on how to solve this. I have seen people use a background image but that seems lazy and unhelpful. I want to move the labels to a 45 degree angle so it looks more professional.

My chart currently looks like this -

But I am wanting it to look like this -

Anyone have any ideas on how I can do this?

Copy of my code is underneath.

    new Chart(DummyChart, {
      type: "polarArea",
      data: {
        labels: ["Dummy1", "Dummy 2", "Dummy 3", "Dummy 4"],
        datasets: [
          {
            data: [125, 375, 300, 240],
            backgroundColor: "#57C5C8",
            borderWidth: 4,
            hoverBorderColor: "white",
            label: "Dummy Dummy "
          }
        ]
      },
      options: {
        cutoutPercentage: 20,
        legend: {
          display: false
        },
        layout: {
          padding: 0
        },
        scale: {
          ticks: {
            max: 450,
            maxTicksLimit: 1,
            display: false
          },
          angleLines: {
            display: true
          },
          pointLabels: {
            display: true
          }
        },
        plugins: {
          datalabels: {
            display: false
          }
        }
      }
    });


回答1:


Here's my solution with the datalables plugin.

https://jsbin.com/bigedugasi/1/edit?html,js,output

I have updated my previous solutions to make the chart responsive.

    plugins: {
      datalabels: {
        formatter: function(value, context) {
          return context.chart.data.labels[context.dataIndex];
        },
        anchor: 'start',
        align: 'end',
        offset: 0 // Gets updated
      }
    },
    onResize: function() {
      let width = document.getElementById("pie-chart").width
      let padding = myChart.options.layout.padding
      myChart.options.plugins.datalabels.offset = width/2-padding
    }



回答2:


I have updated source code from HeadhunterKev, plugin label is removed.

The idea is using startAngle: 20 and CSS Transform to Rotate the Chart.

new Chart(document.getElementById("pie-chart"), {
      type: "polarArea",
      data: {
        labels: ["Dummy1", "Dummy 2", "Dummy 3", "Dummy 4"],
        datasets: [
          {
            data: [125, 375, 300, 240],
            backgroundColor: "#57C5C8",
            borderWidth: 4,
            hoverBorderColor: "white",
            label: "Dummy Dummy "
          }
        ]
      },
      options: {
        cutoutPercentage: 20,
        legend: {
          display: false
        },
        layout: {
          padding: 0
        },
        startAngle: 20,
        scale: {
          ticks: {
            max: 450,
            maxTicksLimit: 1,
            display: false
          },
          angleLines: {
            display: true
          },
          pointLabels: {
            display: true
          }
        }
      }
    });
#pie-chart{
  transform: rotate(-25deg);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.8.0/Chart.bundle.js"></script>
<div>
    <canvas id="pie-chart" width="200" height="200"></canvas>
  </div>

Please run Code Snipped to view the result, Please optimize rotate value to get the best result.



来源:https://stackoverflow.com/questions/59158030/chart-js-polararea-chart-changing-the-labels-to-a-45-degree-angle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!