我正在尝试使用charts.js实现类似的功能:
我想要的是每个刻度线都处于一个特定的位置,因为它是在轴上均匀分布的。我已经设法通过根据其索引有条件地将刻度颜色设置为红色或透明来实现上述目的。与此类似:
const steps = [1,24, 54, 93]
const options = {
scales: {
yAxes: [{
gridLines: {
color: [...new Array(100)].map((x,i) => steps.includes(i) ? "rgba(255, 0, 0, 1)" : "rgba(255, 0, 0, 0)"),
}
}]
}
}
但是,这个解决方案感觉非常hacky,我想知道chartjs是否提供了一个更简单的解决方案
我正在寻找一个类似问题的答案,我最终得到了以下黑客,这不一定是比你更好的黑客:-)
链接到小提琴:https://jsfiddle.net/hdahle/obc4amfh/
var colors = ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange']
var votes = [5, 6, 35, 76, 20, 10];
var ctx = document.getElementById('myChart');
var myChart = new Chart(ctx, {
options: {
responsive: true,
legend: {
display: false
}
},
type: 'line',
data: {
labels: colors,
datasets: [{
label: 'Votes',
data: votes,
borderColor: 'rgba(30,150,60,0.8)',
backgroundColor: 'rgba(30,150,60,0.3)',
borderWidth: 1,
fill: true,
}]
}
});
function drawHorizontalLine(value, color) {
let d = [];
for (let i = 0; i < myChart.data.labels.length; i++) {
d.push(value)
}
myChart.data.datasets.push({
data: d,
pointRadius: 0,
type: 'line',
borderColor: color,
borderWidth: 1,
fill: false,
});
myChart.update();
}
drawHorizontalLine(76, 'rgba(175,40,50,0.6)');
drawHorizontalLine(44, 'rgba(175,40,50,0.6)');
drawHorizontalLine(21, 'rgba(175,40,50,0.6)');
drawHorizontalLine(12, 'rgba(175,40,50,0.6)');
drawHorizontalLine(3, 'rgba(175,40,50,0.6)');