【发布时间】:2019-10-03 14:02:24
【问题描述】:
我正在尝试通过删除 strikethrough 效果来改变图例的外观,但不使用 chart.js 中的 legendCallback 函数。我不想使用legendCallback 函数的原因是因为我在chart.options.legend.onClick 中有自己的自定义。因此,如果我使用legendCallback,我将无法使用chart.options.legend.onClick。
在仔细查看Chart.js 的来源后,我知道在Chart.Legend 的绘制函数中,他们正在应用strikethrough 效果。
Here is the link to plugin.legend.js
这是应用样式的一段代码
var fillText = function(x, y, legendItem, textWidth) {
var halfFontSize = fontSize / 2;
var xLeft = boxWidth + halfFontSize + x;
var yMiddle = y + halfFontSize;
ctx.fillText(legendItem.text, xLeft, yMiddle);
if (legendItem.hidden) {
// Strikethrough the text if hidden
ctx.beginPath();
ctx.lineWidth = 2;
ctx.moveTo(xLeft, yMiddle);
ctx.lineTo(xLeft + textWidth, yMiddle);
ctx.stroke();
}
};
我想知道我们应该如何改变strikethrough 的行为,只需在图例未激活或隐藏时应用淡入淡出效果。
在寻找解决方案时,我遇到了这个codepen,其中有些人试图覆盖该功能,但不幸的是它现在可以与chart.js version 2.7.3 正常工作
链接到my fiddle
【问题讨论】:
标签: javascript chart.js legend chart.js2 legend-properties