我使用的是chartjs 2,我试图在xAxis上禁用网格线,在yAxis上启用网格线,并将它们设为虚线。
我已经通过将这个添加到我的折线图配置中实现了这个功能;
scales: {
xAxes: [{
display: true,
gridLines: {
display: false,
}
}],
yAxes: [{
display: true,
gridLines: {
display: true,
borderDash: [2],
borderDashOffset: [2],
color: 'rgba(0, 0, 0, 0.04)',
drawBorder: false,
drawTicks: false,
}
}]
}然而,当我这样做时,它会随机地向我的图表添加另一个轴,这些轴的值完全是假的。我想删除这个轴并保留原来的轴,但也要保留gridLines配置。

发布于 2021-11-02 14:04:16
您可以为此设置默认值。您可以将其设置为scale默认值,这样它就可以对所有图表类型和比例执行此操作。如果您特别希望仅隐藏X轴gridLines,则需要将其设置为图表类型级别。
Chart.defaults.scale.gridLines.display = false // hides all the gridLines in all charts for all axes
Chart.defaults.line.scales.xAxes[0].gridLines = {
display: false
} // Hides only the X axes gridLines in all line charts
var options = {
type: 'line',
data: {
labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
datasets: [{
label: '# of Votes',
data: [12, 19, 3, 5, 2, 3],
borderWidth: 1
},
{
label: '# of Points',
data: [7, 11, 5, 8, 3, 7],
borderWidth: 1
}
]
},
options: {
scales: {
yAxes: [{
ticks: {
reverse: false
}
}]
}
}
}
var ctx = document.getElementById('chartJSContainer').getContext('2d');
new Chart(ctx, options);<body>
<canvas id="chartJSContainer" width="600" height="400"></canvas>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.4/Chart.js"></script>
</body>
https://stackoverflow.com/questions/69811530
复制相似问题