简体   繁体   English

zeroLineColor 和 zeroLineWidth 不适用于 Chartjs 中的 x 轴

[英]zeroLineColor and zeroLineWidth not working for the x-axis in Chartjs

How to change the width and the color of the x-axis (horizontal axis) in Chartjs? Chartjs中如何改变x轴(横轴)的宽度和颜色? It seems to work using zeroLineColor and zeroLineWidth for the y-axis (vertical one), but it does not work for the x-axis.它似乎对 y 轴(垂直轴)使用zeroLineColorzeroLineWidth有效,但它不适用于 x 轴。

If we add:如果我们添加:

ticks: {
  beginAtZero: true
}

It works as expected, but I don't want the vertical ticks to start from 0.它按预期工作,但我不希望垂直刻度从 0 开始。

Is there a way to make this happen?有没有办法做到这一点?

scales: {
        xAxes: [{
          gridLines: {
            zeroLineColor: 'black', // WORKS
            zeroLineWidth: 2 // WORKS
          },
          display: true
        }],
        yAxes: [{
          gridLines: {
            zeroLineColor: 'black', // DOES NOT WORK
            zeroLineWidth: 2, // DOES NOT WORK
          },
          id: 'y-axis-0',
          display: true,
          ticks: {
            // beginAtZero: true // This is what I dont't want to use
          }
        }]
      }

This is the result I get with the previous code: Graphic这是我使用之前的代码得到的结果: Graphic

As you can see, the x-axis is displayed without changes in color or width.如您所见,x 轴显示时颜色或宽度没有变化。

I'm using version 2.9.4 of the Chartjs library.我正在使用 Chartjs 库的 2.9.4 版本。

Go to the.js of the library (in my case: 'node_modules/chart.js/dist/Chart.bundle.js') and, in the line 12202, you will find this conditional: Go 到库的.js(在我的例子中:'node_modules/chart.js/dist/Chart.bundle.js'),在第 12202 行,你会发现这个条件:

if (i === me.zeroLineIndex && options.offset === offsetGridLines) {

Transform it into this:将其转换为:

if ((i === me.zeroLineIndex || (!isHorizontal && i === ticksLength - 1)) && options.offset === offsetGridLines) {

Then, do whatever compression or manipulation of the file you need to do to add it to your project.然后,对文件进行任何压缩或操作,以将其添加到项目中。

It's not the best solution, but it's the one I came up with.这不是最好的解决方案,但这是我想出的。

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

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