iSS*_*iSS 116 javascript chart.js2
我正在使用Chart.js v2绘制一个简单的折线图.一切都很好,除了有我不想要的网格线:
折线图的文档在这里:https://nnnick.github.io/Chart.js/docs-v2/#line-chart,但我找不到任何关于隐藏这些"网格线"的信息.
如何删除网格线?
Irv*_*ine 269
我找到了一个解决方案,用于隐藏折线图中的网格线.
将gridLines颜色设置为与div的背景颜色相同.
var options = {
scales: {
xAxes: [{
gridLines: {
color: "rgba(0, 0, 0, 0)",
}
}],
yAxes: [{
gridLines: {
color: "rgba(0, 0, 0, 0)",
}
}]
}
}
Run Code Online (Sandbox Code Playgroud)
或使用
var options = {
scales: {
xAxes: [{
gridLines: {
display:false
}
}],
yAxes: [{
gridLines: {
display:false
}
}]
}
}
Run Code Online (Sandbox Code Playgroud)
use*_*568 30
options: {
scales: {
xAxes: [{
gridLines: {
drawOnChartArea: false
}
}],
yAxes: [{
gridLines: {
drawOnChartArea: false
}
}]
}
}
Run Code Online (Sandbox Code Playgroud)
小智 22
从 3.x 版开始,使用此语法。参考chart.js迁移指南:https ://www.chartjs.org/docs/latest/getting-started/v3-migration.html
scales: {
x: {
grid:{
display:false
}
},
y:
{
grid:{
display:false
}
}
}
Run Code Online (Sandbox Code Playgroud)
dav*_*vid 18
如果你想让它们在默认情况下消失,你可以简单地设置:
Chart.defaults.scale.gridLines.display = false;
Run Code Online (Sandbox Code Playgroud)
小智 10
好吧,没关系..我找到了诀窍:
scales: {
yAxes: [
{
gridLines: {
lineWidth: 0
}
}
]
}
Run Code Online (Sandbox Code Playgroud)
小智 9
如果要隐藏网格线但要显示yAxes,则可以设置:
yAxes: [{...
gridLines: {
drawBorder: true,
display: false
}
}]
Run Code Online (Sandbox Code Playgroud)
下面的代码仅从图表区域中删除网格线,而不是 x&y 轴标签中的网格线
Chart.defaults.scale.gridLines.drawOnChartArea = false;
Run Code Online (Sandbox Code Playgroud)
在 ChartJS 3 中,访问此配置有一点不同。该属性的名称不是gridLines,而是grid,如官方文档所示:
options.gridLines被重命名为options.grid
来源: https ://www.chartjs.org/docs/latest/getting-started/v3-migration.html#ticks
它看起来是这样的:
const options = {
scales: {
x: {
grid: {
display: false,
},
},
},
};
Run Code Online (Sandbox Code Playgroud)
请参考官方文档:
https://www.chartjs.org/docs/latest/axes/styling.html#grid-line-configuration
下面的代码更改将隐藏网格线:
scales: {
xAxes: [{
gridLines: {
display:false
}
}],
yAxes: [{
gridLines: {
display:false
}
}]
}
Run Code Online (Sandbox Code Playgroud)
小智 5
更新了 ChartJs 的代码,版本 = 4.3.0
<div>
<canvas id="myChart"></canvas>
</div>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<script>
const ctx = document.getElementById('myChart');
new Chart(ctx, {
type: 'line',
data: {
labels: ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange'],
datasets: [{
label: '# of Votes',
data: [12, 19, 3, 5, 2, 3],
borderWidth: 1
}]
},
options: {
scales: {
y: {
beginAtZero: true,
grid:{
display:false
}
}
}
}
});
</script>Run Code Online (Sandbox Code Playgroud)