首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >调整chart.js的图例元素对齐方式

调整chart.js的图例元素对齐方式
EN

Stack Overflow用户
提问于 2018-01-10 17:52:55
回答 1查看 2.3K关注 0票数 0

有没有办法使用标准图例并以某种方式编辑它,使其不居中对齐,并且每个元素都有固定的宽度,这样它看起来就不会那么混乱?

我知道生成html图例的方法,但这样就失去了打开或关闭数据集的功能,就像使用内置图例一样。

让图例自动排序的最好方法是什么?例如,向左浮动,每个元素的尺寸固定,并保持所有交互仍然可用?谢谢你的帮助。basic generated legend for chart.js (aligment centered)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-01-23 20:01:12

在收到关于这个问题的未反馈后,我猜这个例子可能会帮助其他人获得一个快速的解决方案,关于如何在保持所有交互可用的情况下为chart.js创建一个排序的、外观更好的图例。

此示例为每个项目生成固定的框。它会将图例项分成第二行-保持列的宽度-同时最小化图表的宽度。

代码语言:javascript
运行
AI代码解释
复制
function updateLegendAction(e, datasetIndex) {
	var index = datasetIndex;
    var ci = e.view.chart;
    var meta = ci.getDatasetMeta(index);
    // See controller.isDatasetVisible comment
    meta.hidden = meta.hidden === null? !ci.data.datasets[index].hidden : null;
	document.getElementById("label"+index).style.opacity = document.getElementById("label"+index).style.opacity === '0.2' ? '1' : '0.2';
	// We hid a dataset ... rerender the chart
     ci.update();                                                                                      
};    

Chart.defaults.global.defaultFontFamily = 'Helvetica', 'Helvetica Neue', 'Arial', 'sans-serif';
Chart.defaults.global.defaultFontColor = 'grey';

var chart = new Chart(myChart, {
  type: 'line',
  data: {
  labels: ["January", "February", "March", "April"],
  datasets: [{
    label: "First dataset",
    fillColor: "rgba(220,220,220,0.2)",
    borderColor: "rgba(220,20,20,1)",
    pointColor: "rgba(220,20,20,1)",
    pointStrokeColor: "#fff",
    borderWidth: 3,
    pointHighlightFill: "#fff",
    pointHighlightStroke: "rgba(220,220,220,1)",
    data: [65, 59, 80, 81]
  }, {
    label: "Second dataset",
    fillColor: "rgba(151,187,205,0.2)",
    borderColor: "rgba(15,187,25,1)",
    pointColor: "rgba(15,187,25,1)",
    pointStrokeColor: "#fff",
    borderWidth: 3,
    pointHighlightFill: "#fff",
    pointHighlightStroke: "rgba(151,187,205,1)",
    data: [38, 55, 50, 65]
  },{
    label: "Third dataset",
    fillColor: "rgba(220,220,220,0.2)",
    borderColor: "rgba(220,20,20,1)",
    pointColor: "rgba(220,20,20,1)",
    pointStrokeColor: "#fff",
    borderWidth: 3,
    pointHighlightFill: "#fff",
    pointHighlightStroke: "rgba(220,220,220,1)",
    data: [4, 56, 34, 21]
  },{
    label: "And so on...",
    fillColor: "rgba(220,220,220,0.2)",
    borderColor: "rgba(220,20,20,1)",
    pointColor: "rgba(220,20,20,1)",
    pointStrokeColor: "#fff",
    borderWidth: 3,
    pointHighlightFill: "#fff",
    pointHighlightStroke: "rgba(220,220,220,1)",
    data: [25, 13, 11, 5]
  }]
  },
  options: {
      responsive: false,
      //Define a new HTML Legend
      legendCallback: function(legendarray) {
            var legendHtml = [];
			legendHtml.push('<div class="boxxcontainer">');
            for (var i=0; i<legendarray.data.datasets.length; i++) {
                legendHtml.push('<div id="label' + i + '" class="containerItem" style="width:' + (100/(legendarray.data.datasets.length)-1) +'%;"> <div class="boxx" style="background-color:' + legendarray.data.datasets[i].fillColor + '; border-top:' + legendarray.data.datasets[i].borderWidth +'px solid ' + legendarray.data.datasets[i].borderColor +';"></div>');                    
                if (legendarray.data.datasets[i].label) {
                    legendHtml.push('<div class="boxxlabel" onclick="updateLegendAction(event, ' + '\'' + legendarray.legend.legendItems[i].datasetIndex + '\'' + ')">&nbsp;' + legendarray.data.datasets[i].label + '</div></div>');
                }                                                                              
            }      			
			legendHtml.push('</div>');                                                   
            return legendHtml.join("");                                                        
        }, 
      legend: {
		   display: false,
	   },
     scales: {
      yAxes: [{
        ticks:{
          min: 0,
          max: 100,
         }
      }]
     }
      },
}, newlegend);
var newlegend = document.getElementById("htmllegend").innerHTML = chart.generateLegend();
代码语言:javascript
运行
AI代码解释
复制
div {
  font-family: 'Helvetica', 'Helvetica Neue', 'Arial', 'sans-serif';
}

.boxxcontainer {
  margin-left:30px;
  width: 90%;
  float: left;
}

.boxx {
  background-color: green;
  width: 10%;
  height: 15px;
  margin-left: 0%;
  float: left;
  line-height: 100%;
  display: block;
  border-top: 1px solid white;
}

.boxxlabel {
  color: grey;
  font-size: 13px;
  width: 86%;
  margin-left: 2%;
  float: left;
  line-height: 125%;
  display: block;
  cursor: pointer;
  -webkit-touch-callout: none;
  /* iOS Safari */
  -webkit-user-select: none;
  /* Safari */
  -khtml-user-select: none;
  /* Konqueror HTML */
  -moz-user-select: none;
  /* Firefox */
  -ms-user-select: none;
  /* Internet Explorer/Edge */
  user-select: none;
  /* Non-prefixed version, currently
                                  supported by Chrome and Opera */
}

.containerItem {
  height: 30px;
  min-width:150px;
  float: left;
  opacity: 1;
}
代码语言:javascript
运行
AI代码解释
复制
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.6.0/Chart.min.js"></script>
<div id="htmllegend"></div>
<canvas id="myChart" width="600" height="400"></canvas>

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48193376

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文