javascript – 如何对齐Chart.js 2中的图例项?

前端之家收集整理的这篇文章主要介绍了javascript – 如何对齐Chart.js 2中的图例项?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我只需要对齐图表图例,使它看起来不像默认显示那样杂乱,这里是我想要实现的一个例子:


请提供一些代码建议:https://jsfiddle.net/holp/68wf75r8/

  1. new Chart(document.getElementById("field-0"),{
  2. type: 'pie',data: {
  3. labels: ["Chat","Prospeção","Whatsapp","Trial","Site","Telefone","E-mail","Evento"],datasets: [{
  4. data: [700,400,200,150,80,50,20,10],borderWidth: 2,hoverBorderWidth: 10,backgroundColor: pieColors,hoverBackgroundColor: pieColors,hoverBorderColor: pieColors,borderColor: pieColors
  5. }]
  6. },options: {
  7. legend: {
  8. labels: {
  9. padding: 20
  10. }
  11. }
  12. }
  13. });

解决方法

您可以使用legend.labels.generateLabels钩子来自定义图例标签.
我发现,你可以把下面这样的东西调整成Chart.js计算.
  1. generateLabels: function (chart) {
  2. chart.legend.afterFit = function () {
  3. var width = this.width; // guess you can play with this value to achieve needed layout
  4. this.lineWidths = this.lineWidths.map(function(){return width;});
  5.  
  6. };
  7. // here goes original or customized code of your generateLabels callback
  8. }

奇怪的是,没有实际的配置选项来实现这一点.

猜你在找的JavaScript相关文章