简体   繁体   English

Chart.js v2 - 隐藏网格线

[英]Chart.js v2 - hiding grid lines

I am using Chart.js v2 to draw a simple line chart.我正在使用 Chart.js v2 来绘制一个简单的折线图。 Everything looks fine, except there are grid lines that I don't want:一切看起来都很好,除了有我不想要的网格线:

我不想要的网格线

The documentation for Line Chart is here: https://nnnick.github.io/Chart.js/docs-v2/#line-chart , but I can't find anything about hiding those "Grid Lines".折线图的文档在这里: https://nnnick.github.io/Chart.js/docs-v2/#line-chart ,但我找不到任何关于隐藏这些“网格线”的信息。

How can I remove the grid lines?如何删除网格线?

I found a solution that works for hiding the grid lines in a Line chart.我找到了一种可以在折线图中隐藏网格线的解决方案。

Set the gridLines color to be the same as the div's background color.gridLines颜色设置为与 div 的背景颜色相同。

var options = {
    scales: {
        xAxes: [{
            gridLines: {
                color: "rgba(0, 0, 0, 0)",
            }
        }],
        yAxes: [{
            gridLines: {
                color: "rgba(0, 0, 0, 0)",
            }   
        }]
    }
}

or use或使用

var options = {
    scales: {
        xAxes: [{
            gridLines: {
                display:false
            }
        }],
        yAxes: [{
            gridLines: {
                display:false
            }   
        }]
    }
}

From version 3.x, onwards use this syntax.从版本 3.x 开始,使用此语法。 Refer to chart.js migration guide: https://www.chartjs.org/docs/latest/getting-started/v3-migration.html参考 chart.js 迁移指南: https ://www.chartjs.org/docs/latest/getting-started/v3-migration.html

scales: {
  x: {
    grid: {
      display: false
    }
  },
  y: {
    grid: {
      display: false
    }
  }
}
options: {
    scales: {
        xAxes: [{
            gridLines: {
                drawOnChartArea: false
            }
        }],
        yAxes: [{
            gridLines: {
                drawOnChartArea: false
            }
        }]
    }
}

如果您希望它们默认消失,您可以设置:

Chart.defaults.scale.gridLines.display = false;

If you want to hide gridlines but want to show yAxes, you can set:如果要隐藏网格线但要显示 yAxes,可以设置:

yAxes: [{...
         gridLines: {
                        drawBorder: true,
                        display: false
                    }
       }]

OK, nevermind.. I found the trick:好吧,没关系..我找到了诀窍:

    scales: {
      yAxes: [
        {
          gridLines: {
                lineWidth: 0
            }
        }
      ]
    }

下面的代码仅从图表区域中删除网格线,而不是 x&y 轴标签中的网格线

Chart.defaults.scale.gridLines.drawOnChartArea = false;

In chartjs 3 there is a little difference in accessing this configuration.在 chartjs 3 中访问此配置有一点不同。 The name of the property is not gridLines , but grid , as it is shown in the official documentation:该属性的名称不是gridLines ,而是grid ,如官方文档中所示:

options.gridLines was renamed to options.grid options.gridLines重命名为options.grid

Source: https://www.chartjs.org/docs/latest/getting-started/v3-migration.html#ticks资料来源: https ://www.chartjs.org/docs/latest/getting-started/v3-migration.html#ticks

Here is how it looks:这是它的外观:

const options = {
  scales: {
    x: {
      grid: {
        display: false,
      },
    },
  },
};

Please refer to the official documentation:请参考官方文档:

https://www.chartjs.org/docs/latest/axes/styling.html#grid-line-configuration https://www.chartjs.org/docs/latest/axes/styling.html#grid-line-configuration

Below code changes would hide the gridLines:下面的代码更改将隐藏网格线:

scales: {
    xAxes: [{
        gridLines: {
            display:false
        }
    }],
    yAxes: [{
        gridLines: {
            display:false
        }   
    }]
}

在此处输入图像描述

An update for ChartJS 3: ChartJS 3 的更新:

  const options = {
    scales: {
      x: {
        grid: {
          display: false,
        },
      },

      y: {
        grid: {
          // display: false,
          color: 'rgba(217,143,7,0.1)',
        },
      },
    },
}

this did it for me on my react project这在我的反应项目中为我做到了

 scales: { xAxis:{ grid: { display: false } } }

i hope this helps我希望这有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM