【问题标题】:Removing axes lines on chart.js删除 chart.js 上的轴线
【发布时间】:2017-11-16 11:08:30
【问题描述】:

我一直在使用一些 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,

                }
            }]

        }

    }
});

任何建议将不胜感激。

【问题讨论】:

    标签: javascript chart.js chart.js2


    【解决方案1】:

    您需要将网格线的 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
          }
       }]
    }
    ...
    

    【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

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

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

      这是related GitHub issue

      【讨论】:

        猜你喜欢
        • 2014-06-18
        • 2022-11-02
        • 2019-10-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多