【发布时间】:2020-04-26 22:49:47
【问题描述】:
我正在尝试在我的 chartJs 图表的插件下创建一个线性渐变。不幸的是,我收到一个名为:
的错误在“CanvasRenderingContext2D”上执行“createLinearGradient”失败:提供的双精度值是非有限的。
我正在尝试在插件中添加我的 linearGradient,因为我希望该渐变在每个比例上对齐。
这是我在下面尝试过的
barChart = new Chart(elem, {
plugins: [
{
id: "responsiveGradient",
afterLayout: function(chart, options) {
var scales = chart.scales;
var color = chart.ctx.createLinearGradient(
scales["x-axis-0"].left,
scales["y-axis-0"].bottom,
scales["x-axis-0"].right,
scales["y-axis-0"].top
);
// add gradients stops
color.addColorStop(0, "black");
color.addColorStop(0.25, "red");
color.addColorStop(0.5, "orange");
color.addColorStop(0.75, "yellow");
color.addColorStop(1, "green");
// changes the background color option
chart.data.datasets[0].backgroundColor = color;
}
}
],
type: 'horizontalBar',
data: datasets,
options: {
maintainAspectRatio: false,
tooltips: { enabled: false },
title: {
display: false,
},
responsive: true,
legend: {
display: false,
position: "top"
},
scales: {
xAxes: [{
ticks: {
beginAtZero: false,
min:0.5,
max:0.8,
maxTicksLimit: 6,
},
scaleLabel: {
display: false
},
barThickness: 5,
gridLines: {
display: false,
zeroLineColor: "transparent",
}
}],
yAxes: [{
barThickness: 5,
ticks: {
maxTicksLimit: 6,
padding: 15,
},
gridLines: {
drawTicks: false,
display: false
}
}]
}
},
});
【问题讨论】:
-
在
afterLayout方法中,在调用 createLinearGradient 之前,执行console.log(scales["x-axis-0"].left, scales["y-axis-0"].bottom, scales["x-axis-0"].right, scales["y-axis-0"].top)并检查输出的内容。
标签: javascript chart.js