我试图在ChartJS v2.0中创建自定义图例模板。在ChartJS的v1 *中,我只是向新的Chart构造函数添加了一个属性,例如......
(&obj == &obj.array[0])
我似乎无法在v2.0中找到此选项的任何文档。它甚至可用吗?任何人都可以展示如何实现这一目标的例子吗?
谢谢!
更新 - 下面的工作代码
legendTemplate : '<ul>'
+'<% for (var i=0; i<datasets.length; i++) { %>'
+'<li>'
+'<span style=\"background-color:<%=datasets[i].lineColor%>\"></span>'
+'<% if (datasets[i].label) { %><%= datasets[i].label %><% } %>'
+'</li>'
+'<% } %>'
+'</ul>'
答案 0 :(得分:22)
如果你们经历过这篇文章并尝试过发布的答案而没有成功, 试试这个:
legendCallback: function(chart) {
var text = [];
text.push('<ul>');
for (var i=0; i<chart.data.datasets.length; i++) {
console.log(chart.data.datasets[i]); // see what's inside the obj.
text.push('<li>');
text.push('<span style="background-color:' + chart.data.datasets[i].borderColor + '">' + chart.data.datasets[i].label + '</span>');
text.push('</li>');
}
text.push('</ul>');
return text.join("");
},
然后在下面添加:
document.getElementById('chart-legends').innerHTML = myChart.generateLegend();
创建传说。确保您有一个元素<div id="chart-legends"></div>
答案 1 :(得分:15)
有一个legendCallback
函数:
legendCallback 功能
function (chart) { }
用于生成图例的功能。接收图表对象以生成图例。默认 实现返回一个HTML字符串。
详细信息可以在这里找到: http://www.chartjs.org/docs/latest/configuration/legend.html#html-legends 例如(via - https://github.com/chartjs/Chart.js/issues/5070)-默认图例回调:
legendCallback: function(chart) {
var text = [];
text.push('<ul class="' + chart.id + '-legend">');
for (var i = 0; i < chart.data.datasets.length; i++) {
text.push('<li><span style="background-color:' +
chart.data.datasets[i].backgroundColor +
'"></span>');
if (chart.data.datasets[i].label) {
text.push(chart.data.datasets[i].label);
}
text.push('</li>');
}
text.push('</ul>');
return text.join('');
}
答案 2 :(得分:1)
我希望这会对您有所帮助
var configd = {
type: 'doughnut',
data: {
datasets: [{
data: [
50,
60,
20
],
backgroundColor: [
'#33b35a',
"#ffce56",
"#4bc0c0",
"#CDDC39",
"#9C27B0",
"#fb7145",
"#5971f9"
],
label: 'Energy usage'
}],
labels: [
'E1',
'E2',
'E3'
]
},
options: {
responsive: true,
legend: {
display: false
},
legendCallback: function (chart) {
// Return the HTML string here.
console.log(chart.data.datasets);
var text = [];
text.push('<ul class="' + chart.id + '-legend">');
for (var i = 0; i < chart.data.datasets[0].data.length; i++) {
text.push('<li><span id="legend-' + i + '-item" style="background-color:' + chart.data.datasets[0].backgroundColor[i] + '" onclick="updateDataset(event, ' + '\'' + i + '\'' + ')">');
if (chart.data.labels[i]) {
text.push(chart.data.labels[i]);
}
text.push('</span></li>');
}
text.push('</ul>');
return text.join("");
},
title: {
display: false,
text: 'Chart.js Doughnut Chart'
},
animation: {
animateScale: true,
animateRotate: true
},
tooltips: {
mode: 'index',
intersect: false,
callbacks: {
label: function (tooltipItem, data) {
let label = data.datasets[tooltipItem.datasetIndex].label + ' - ' + data.labels[tooltipItem.index];
let datasetLabel = data.datasets[tooltipItem.datasetIndex].data[tooltipItem.index];
return label + ': ' + datasetLabel.toLocaleString();
}
}
},
}
};
var ctxd = document.getElementById('canvas').getContext('2d');
window.myDoughnut = new Chart(ctxd, configd);
$("#do_legend").html(window.myDoughnut.generateLegend());
// Show/hide chart by click legend
updateDataset = function (e, datasetIndex) {
var index = datasetIndex;
var ci = e.view.myDoughnut;
var meta = ci.getDatasetMeta(0);
var result= (meta.data[datasetIndex].hidden == true) ? false : true;
if(result==true)
{
meta.data[datasetIndex].hidden = true;
$('#' + e.path[0].id).css("text-decoration", "line-through");
}else{
$('#' + e.path[0].id).css("text-decoration","");
meta.data[datasetIndex].hidden = false;
}
ci.update();
};
#do_legend{
height:62px;
}
#do_legend{
width:100%;
}
#do_legend> ul{
padding: 0;
text-align: center;
}
#do_legend {
width:100%;
bottom:10%;
}
#do_legend li {
cursor: pointer;
margin: 4px 3px;
display: inline-table;
}
#do_legend li span {
position: relative;
padding: 3px 10px;
border-radius: 13px;
color: white;
z-index: 2;
font-size: 11px;
}
#do_legend{
height: 62px;
overflow-y: auto;
}
.donut-area{
height:calc(100% - 62px)
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.5.0/Chart.min.js"></script>
<div style="width:40%">
<div id="do_legend"></div>
<canvas id="canvas"></canvas>
</div>
答案 3 :(得分:0)
此代码对我有用
transform()