我正在使用Chart.js
创建一个折线图,该折线图显示两个具有不同y轴的数据集。我想更改其中一个y轴的颜色,x轴包含从1开始的测试数量,所以x轴上的第一个刻度是1,最后一个刻度随着autoSkip
和maxTicksLimit
的使用而变化,所以有时最后一个刻度不在第二个y轴上
const rootStyles = getComputedStyle(document.documentElement);
const mainColor = rootStyles.getPropertyValue('--main-color');
const subColor = rootStyles.getPropertyValue('--sub-color');
const data = {
labels: [] as number[],
datasets: [
{
label: 'WPM',
data: [] as number[],
fill: true,
backgroundColor: mainColor,
borderColor: mainColor,
lineTension: 0.4,
yAxisID: 'y',
},
{
label: 'Accuracy',
data: [] as number[],
fill: true,
backgroundColor: subColor,
borderColor: subColor,
lineTension: 0.4,
yAxisID: 'y1',
},
],
};
const options: any = {
tooltips: {
enabled: true,
mode: 'label',
},
bezierCurve: true,
responsive: true,
maintainAspectRatio: false,
plugins: {
legend: {
position: 'top',
display: false,
labels: {
usePointStyle: true,
font: {
size: 14,
family: 'lexend, sans-serif',
},
},
},
title: {
display: false,
},
},
interaction: {
intersect: false,
},
scales: {
x: {
title: {
display: true,
text: 'Test',
color: subColor,
font: {
size: 16,
family: 'lexend, sans-serif',
},
},
ticks: {
autoSkip: true,
maxTicksLimit: 10,
font: {
family: 'lexend, sans-serif',
},
color: subColor
},
grid: {
color: (context: any) => {
if (context.tick.value === data.labels[data.labels.length - 2] // here is the problem
|| context.tick.value === 0)
{
return subColor
}
else {
return subColor + "15"
}
},
},
},
y: {
min: 0,
max: Math.max(...wpmData) > 150 ? 290 : 200,
position: 'left',
title: {
display: true,
text: 'Words per minute',
color: subColor,
font: {
size: 16,
family: 'lexend, sans-serif',
},
},
ticks: {
font: {
family: 'lexend, sans-serif',
},
color: subColor,
},
grid: {
color: (context: any) => {
if (context.tick.value === 0) {
return subColor
}
else {
return subColor + "15"
}
},
},
},
y1: {
position: 'right',
max: 120,
min: 0,
title: {
display: true,
text: 'Accuracy',
color: subColor,
font: {
size: 16,
family: 'lexend, sans-serif',
},
},
ticks: {
font: {
family: 'lexend, sans-serif',
},
color: subColor,
},
grid: {
color: (context: any) => {
if (context.tick.value === 0) {
return subColor
}
else {
return "transparent"
}
},
},
},
},
elements: {
point: {
radius: 2,
},
},
请注意,我不想为gridLines
着色
当前
预期
1条答案
按热度按时间ht4b089n1#
假设你使用的是Chart.js 4,你可以在scales配置中使用
border
节点。编辑:如果你想让所有图表示例都使用相同的颜色,你也可以考虑在图表默认值中设置borderColor: