简体   繁体   English

chartjs饼图图例的每个标签都在一个新行上

[英]Each label of the chartjs pie chart's legend on a new line

I have a pie chart using chart.js 2.0 .我有一个使用chart.js 2.0饼图 ( jsfiddle ) ( jsfiddle )

var ctx = document.getElementById("myChart").getContext('2d');
var myChart = new Chart(ctx, {
  type: 'pie',
  data: {
    labels: ["Green", "Blue", "Gray", "Purple", "Yellow", "Red", "Black"],
    datasets: [{
      backgroundColor: [
        "#2ecc71",
        "#3498db",
        "#95a5a6",
        "#9b59b6",
        "#f1c40f",
        "#e74c3c",
        "#34495e"
      ],
      data: [12, 19, 3, 17, 28, 24, 7],
    }]
  },
  options:  {
    legend: {
      display: true,
      position: 'top'
    }
  }
});

饼图

I wanna to determine the best way to place legend's labels on top-left position and each label will be on a new line:我想确定将图例标签放置在左上角位置的最佳方式,并且每个标签都将在一个新行上:

picart-目标

What is the simplest way to do this?什么是最简单的方法来做到这一点? Is it possible to do this out of the box?是否可以开箱即用?

After reading the documentation, I found generatelabels function which generates a legend that we can use and assign to an DOM element and stylize and etc... But it seems to me that this is a difficult way and I believe that there is easier.阅读文档后,我发现generatelabels函数会生成一个图例,我们可以使用该图例并将其分配给 DOM 元素并进行样式化等......但在我看来,这是一种困难的方法,我相信有更简单的方法。

This is a hack but a satisfactory and dirt-cheap one, until better support is provided officially.在官方提供更好的支持之前,这是一个黑客,但令人满意且非常便宜。 Just append a long string of spaces eg.只需附加一长串空格,例如。 " " to the end of each label string. " "到每个标签字符串的末尾。

This should successfully force the labels to be formatted one-per-line.这应该成功地强制将标签格式化为每行一个。 Ideally this should be used with align: 'start' (v2.9+)理想情况下,这应该与align: 'start' (v2.9+) 一起使用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM