javascript - How to hide grid lines and x-axis labels in chart.js? - Stack Overflow

I'm using chart.js v3.2.0 and I want to disable the grid lines and x-axis labels. I've tried

I'm using chart.js v3.2.0 and I want to disable the grid lines and x-axis labels. I've tried various examples from other stack overflow posts but none seem to work.

/

html

<div style = "width: 600px; height: 400px;">
    <canvas id="chartJSContainer" width="1" height="400"></canvas>
</div>

js

var options = {
  type: 'line',
  data: {
    labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
    datasets: [
        {
          label: '# of Votes',
          data: [12, 19, 3, 5, 2, 3],
        borderWidth: 1,
        backgroundColor: 'rgba(255, 99, 132, 0.2)',
        borderColor: 'rgba(255, 99, 132,1)'
        },  
            {
                label: '# of Points',
                data: [7, 11, 5, 8, 3, 7],
                borderWidth: 1,
        backgroundColor: 'rgba(54, 162, 235, 0.2)',
        borderColor: 'rgba(54, 162, 235, 1)'
            }
        ]
  },
  options: {
        scales: {
          xAxes: [{
            display: false,
            ticks: {
              display: false //this will remove only the label
            }
          }],
          yAxes: [{
            display:false
          }]      
        },
        responsive: true,
        maintainAspectRatio: false
    }
}

var ctx = document.getElementById('chartJSContainer').getContext('2d');
new Chart(ctx, options);

Does anyone know how to do it?

Thanks

I'm using chart.js v3.2.0 and I want to disable the grid lines and x-axis labels. I've tried various examples from other stack overflow posts but none seem to work.

https://jsfiddle/gcp95hjf/1/

html

<div style = "width: 600px; height: 400px;">
    <canvas id="chartJSContainer" width="1" height="400"></canvas>
</div>

js

var options = {
  type: 'line',
  data: {
    labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
    datasets: [
        {
          label: '# of Votes',
          data: [12, 19, 3, 5, 2, 3],
        borderWidth: 1,
        backgroundColor: 'rgba(255, 99, 132, 0.2)',
        borderColor: 'rgba(255, 99, 132,1)'
        },  
            {
                label: '# of Points',
                data: [7, 11, 5, 8, 3, 7],
                borderWidth: 1,
        backgroundColor: 'rgba(54, 162, 235, 0.2)',
        borderColor: 'rgba(54, 162, 235, 1)'
            }
        ]
  },
  options: {
        scales: {
          xAxes: [{
            display: false,
            ticks: {
              display: false //this will remove only the label
            }
          }],
          yAxes: [{
            display:false
          }]      
        },
        responsive: true,
        maintainAspectRatio: false
    }
}

var ctx = document.getElementById('chartJSContainer').getContext('2d');
new Chart(ctx, options);

Does anyone know how to do it?

Thanks

Share Improve this question edited May 2, 2021 at 18:55 omega asked May 2, 2021 at 18:41 omegaomega 44.1k90 gold badges286 silver badges523 bronze badges 2
  • Pretty strange, even on their official websites example parameters are also not getting applied. – Himanshu Saxena Commented May 2, 2021 at 18:53
  • Maybe the documentation is not updated for the new v3 version? – omega Commented May 2, 2021 at 18:56
Add a ment  | 

2 Answers 2

Reset to default 3

Chart.js v3 has multiple breaking changes, one of them is how you define the scales, it is stated in the migration guide and shown in a lot of the examples (https://www.chartjs/docs/3.2.1/getting-started/v3-migration.html)

You will have to change scales: { yAxes: [], xAxes: []} to scales: { y: {}, x: {}}

Working fiddle: https://jsfiddle/Leelenaleee/r26h71fm/2/

Hide GridLines on Both Axis -

ChartJS v2 (older version) -

scales: {
    x: [
      {
        gridLines: {
          display: false,
        },
      },
    ],

    y: [
      {
        gridLines: {
          display: false,
        },
      },
    ],
  },

Newer Version ChartJS v3 -

scales: {
    x: {
      grid: {
        display: false,
      },
    },

    y: {
      grid: {
        display: false,
      },
    },
  },

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745380669a4625209.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信