我只需要对齐图表图例,使它看起来不像默认显示那样杂乱,这里是我想要实现的一个例子:
new Chart(document.getElementById("field-0"), {
type: 'pie',
data: {
labels: ["Chat", "Prospeção", "Whatsapp", "Trial", "Site", "Telefone", "E-mail", "Evento"],
datasets: [{
data: [700, 400, 200, 150, 80, 50, 20, 10],
borderWidth: 2,
hoverBorderWidth: 10,
backgroundColor: pieColors,
hoverBackgroundColor: pieColors,
hoverBorderColor: pieColors,
borderColor: pieColors
}]
},
options: {
legend: {
labels: {
padding: 20
}
}
}
});
Run Code Online (Sandbox Code Playgroud)
有legend.labels.generateLabels钩你通常可以使用自定义您的图例标签.我发现,你可以把下面这样的东西调整成Chart.js计算.
generateLabels: function (chart) {
chart.legend.afterFit = function () {
var width = this.width; // guess you can play with this value to achieve needed layout
this.lineWidths = this.lineWidths.map(function(){return width;});
};
// here goes original or customized code of your generateLabels callback
}
Run Code Online (Sandbox Code Playgroud)
奇怪的是,没有实际的配置选项来实现这一点.