228

I am using Chart.js v2 to draw a simple line chart. Everything looks fine, except there are grid lines that I don't want:

Grid Lines 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".

How can I remove the grid lines?

Tot Zam
  • 8,406
  • 10
  • 51
  • 76
iSS
  • 2,598
  • 2
  • 14
  • 17

12 Answers12

460

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.

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
            }   
        }]
    }
}
J Livengood
  • 2,729
  • 1
  • 12
  • 26
Irvine
  • 4,801
  • 1
  • 13
  • 5
  • 4
    This is actually setting the gridLines color to a `0 opacity` black (a transparent color). So this should work regardless of the div's background color. – XCS Apr 18 '16 at 11:24
  • 55
    Or use display:false, instead of "color" – Irvine Apr 18 '16 at 11:25
  • I wanted to keep the scale but lose the gridlines on the back of the chart so this answer did not work for me. – adg Sep 03 '18 at 07:39
  • 2
    While this first answer may get to the desired picture, it is a bit of a hack. The second solution, that actually sets the gridLines display property to false, seems to be more correct. – Tot Zam Nov 09 '18 at 08:06
  • 7
    This is also removing lines of x axis(single line) and y axis(single line). But i only wanted to remove lines on the graph. So drawOnChartArea: false is doing the right thing. – Rajeshwar Jun 07 '20 at 18:53
  • 1
    gridLines is now grid in ChartJS 3 – hanumanDev Mar 24 '22 at 13:30
187

From version 3.x, onwards use this syntax. Refer to chart.js migration guide: https://www.chartjs.org/docs/latest/getting-started/v3-migration.html

scales: {
  x: {
    grid: {
      display: false
    }
  },
  y: {
    grid: {
      display: false
    }
  }
}
svarog
  • 9,477
  • 4
  • 61
  • 77
Ishan Varshney
  • 2,933
  • 2
  • 9
  • 11
96
options: {
    scales: {
        xAxes: [{
            gridLines: {
                drawOnChartArea: false
            }
        }],
        yAxes: [{
            gridLines: {
                drawOnChartArea: false
            }
        }]
    }
}
Vahidrk
  • 1,178
  • 10
  • 5
28

If you want them gone by default, you can set:

Chart.defaults.scale.gridLines.display = false;
Kees de Kooter
  • 7,078
  • 5
  • 38
  • 45
david
  • 1,814
  • 1
  • 16
  • 19
17

If you want to hide gridlines but want to show yAxes, you can set:

yAxes: [{...
         gridLines: {
                        drawBorder: true,
                        display: false
                    }
       }]
Akhilesh krishnan
  • 799
  • 2
  • 8
  • 22
Kathy
  • 409
  • 4
  • 7
11

The code below removes remove grid lines from chart area only not the ones in x&y axis labels

Chart.defaults.scale.gridLines.drawOnChartArea = false;
Ahmed Adewale
  • 2,943
  • 23
  • 19
  • 2
    This is the correct answer since this is not change anything out the draw area, Thanks Ahmed – Ranny Sep 03 '20 at 09:44
10

OK, nevermind.. I found the trick:

    scales: {
      yAxes: [
        {
          gridLines: {
                lineWidth: 0
            }
        }
      ]
    }
hygorbudny
  • 109
  • 1
  • 4
7

In chartjs 3 there is a little difference in accessing this configuration. The name of the property is not gridLines, but grid, as it is shown in the official documentation:

options.gridLines was renamed to options.grid

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

Here is how it looks:

const options = {
  scales: {
    x: {
      grid: {
        display: false,
      },
    },
  },
};
  • Hristo, next time, could you please point to the documentation of the library to help guide the person asking the question. Thank you so much for taking the time. – user906573 Aug 18 '21 at 17:07
6

Please refer to the official documentation:

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
        }   
    }]
}

enter image description here

Devkinandan Chauhan
  • 1,785
  • 1
  • 17
  • 42
4

An update for ChartJS 3:

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

      y: {
        grid: {
          // display: false,
          color: 'rgba(217,143,7,0.1)',
        },
      },
    },
}
hanumanDev
  • 6,592
  • 11
  • 82
  • 146
2

this did it for me on my react project

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

i hope this helps

abdulfrfr
  • 21
  • 2
0

Updated code for ChartJs, version = 4.3.0

<div>
  <canvas id="myChart"></canvas>
</div>

<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>

<script>
  const ctx = document.getElementById('myChart');

  new Chart(ctx, {
    type: 'line',
    data: {
      labels: ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange'],
      datasets: [{
        label: '# of Votes',
        data: [12, 19, 3, 5, 2, 3],
        borderWidth: 1
      }]
    },
    options: {
      scales: {
        y: {
          beginAtZero: true,
          grid:{
              display:false
          }
        }
        
      }
    }
  });
</script>