Chart.js 3.7.1 折线图 - 如何在 y 轴上单独设置每个标签的格式?
我有一个折线图。数据介于 -10 和 10 之间。 y 轴上的标签是正确的(-10 到 10,增量为 1)。
我需要根据颜色数组使每个标签的颜色不同。标签数和颜色数均为 21(-10 到 10,包括 0)。 我真的很想要一个渐变的“条带”,以便每个标签都位于颜色的垂直位置。
我在代码中尝试了这一点,但了解到 html 在图表中不可用:
options: {
responsive: true,
plugins: {
title: {
display: true,
text: 'Topic Sentiment'
}
},
scales: {
y: {
title: {
display: true,
text: 'Sentiment Scores'
},
min: -10,
max: 10,
ticks: {
stepSize: 1,
callback: function(value, index, ticks) {
return value + " <div style='height:100%; width:8px; background:" + arColors[index] + ";' ></div>"
}
}
}
},
onClick: (e, activeEls) => {
var oChart = e.chart, label = "";
}
}
I have a line chart. The data is anywhere between -10 and 10.
The labels on the y axis are correct (-10 to 10 incremented by 1).
I need the color of each label to be different, based on an array of colors. The number of labels and the number of colors are both 21 (-10 to 10 including 0).
I'd really like a 'strip' of a gradient so that each label is at the vertical position of the color.
I tried this in the code but learned that html is not available within the chart:
options: {
responsive: true,
plugins: {
title: {
display: true,
text: 'Topic Sentiment'
}
},
scales: {
y: {
title: {
display: true,
text: 'Sentiment Scores'
},
min: -10,
max: 10,
ticks: {
stepSize: 1,
callback: function(value, index, ticks) {
return value + " <div style='height:100%; width:8px; background:" + arColors[index] + ";' ></div>"
}
}
}
},
onClick: (e, activeEls) => {
var oChart = e.chart, label = "";
}
}
This is what I mean. The y gradient is added to an image of the actual chart in Photoshop.
Can I do anything like this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以将 y.ticks.color 定义为 rgb 颜色数组。这些颜色可以即时生成。
受到来自 的这个令人惊叹的答案的启发Pevara,我想出了以下解决方案:
You can define
y.ticks.color
as an array ofrgb
colors. These colors could be generated on the fly.Inspired by this amazing answer from Pevara, I came up with the following solution: