删除 chart.js 上的轴线

Removing axes lines on chart.js

我一直在与一些 Chart.js 合作,并且我一直在尝试弄清楚如何删除图表上的轴线。

You can see the grid lines I am trying to remove in this image.

我已经设法删除了显示数据的图表中的网格线,但我在删除这两条线时遇到了问题。

您可以看到我为下面的图表创建的chart.js。

var ctx = document.getElementById("volCause").getContext('2d');
var volCause_Doughnut = new Chart(ctx, {
    type: 'horizontalBar',
    data: {
            labels: <?php echo $cause_label_json?>,
        datasets: [{
            backgroundColor: benefactoGraph_colours,
            data: <?php echo $cause_value_json?>

        }]
    },

    options: {
        maintainAspectRatio: false,
        legend: {
            display: false,
        },
        scales: {
            xAxes: [{
                scaleLabel: {
                    display: true,
                    labelString: 'Volunteer Hours',
                },
                gridLines: {
                    display: false,
                },
            }],

            yAxes: [{
                gridLines: {
                    display: false,

                }
            }]

        }

    }
});

如有任何建议,我们将不胜感激。

您需要将网格线的drawBorder属性设置为false为了删除那些轴线,像这样:

...
scales: {
   xAxes: [{
      scaleLabel: {
         display: true,
         labelString: 'Volunteer Hours',
      },
      gridLines: {
         display: false,
         drawBorder: false //<- set this
      },
   }],
   yAxes: [{
      gridLines: {
         display: false,
         drawBorder: false //<- set this
      }
   }]
}
...

您可以在轴对象中设置 display: false

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

其他答案对于 chart.js 的不同版本都是正确的,但截至 2020 年 1 月,对于版本 2.9.3,我能够通过在 [= 中嵌套 display: false 来解决问题12=]如下:

    ...
  , options:
      scales: {
        xAxes: [{
          gridLines: {
              display: false
          },
    ...

这是一个related GitHub issue