Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chart.js remove gridline

Stuck on a chart.js item as the docs don't seem to be particularly clear.

Simply trying to remove a gridline from the chart. I tried both the old syntax that was referenced here in Stack overflow:

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

And the new syntax:

options: {
    scales: {
      y: {
        grid: {
          drawBorder: false
        }
      }
    }
  }

Neither work...

Here's my Snippet:

var ctx = document.getElementById("myChart");
var myChart = new Chart(ctx, {
  type: 'bar',
  data: {
    labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
    datasets: [{
      label: '# of Votes',
      data: [12, 19, 10, 5, 8, 11]
    }]
  },
  options: {
    scales: {
      xAxes: [{
        gridLines: {
          display: false,
        }
      }],
      y: {
        grid: {
          drawBorder: false
        }
      },
    }
  }
});
.myChartDiv {
  max-width: 600px;
  max-height: 400px;
}
<html>

  <body>
    <div class="myChartDiv">
      <canvas id="myChart" width="600" height="400"></canvas>
    </div>
  </body>
  <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>

</html>
like image 294
Parker Avatar asked Jun 06 '26 00:06

Parker


2 Answers

The property you are looking for is called display instead of drawBorder, also your xAxes are defined in the wrong way, you were using v2 syntax instead of v3

Example:

var ctx = document.getElementById("myChart");
var myChart = new Chart(ctx, {
  type: 'bar',
  data: {
    labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
    datasets: [{
      label: '# of Votes',
      data: [12, 19, 10, 5, 8, 11]
    }]
  },
  options: {
    scales: {
      x: {
        grid: {
          display: false,
        }
      },
      y: {
        grid: {
          display: false
        }
      },
    }
  }
});
.myChartDiv {
  max-width: 600px;
  max-height: 400px;
}
<html>

  <body>
    <div class="myChartDiv">
      <canvas id="myChart" width="600" height="400"></canvas>
    </div>
  </body>
  <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>

</html>
like image 75
LeeLenalee Avatar answered Jun 08 '26 14:06

LeeLenalee


Here is the solution.

Looks like lot of configuration has changed in the new version.

Reference from documentation here.

var ctx = document.getElementById("myChart");
var myChart = new Chart(ctx, {
  type: 'bar',
  data: {
    labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
    datasets: [{
      label: '# of Votes',
      data: [12, 19, 10, 5, 8, 11]
    }]
  },
   options: {
     scales: {
      x: {
        grid: {
           drawOnChartArea:false
         }
      },
       y: {
        grid: {
           drawOnChartArea:false
         }
      }
    }
   }
});
.myChartDiv {
  max-width: 600px;
  max-height: 400px;
}
<html>

  <body>
    <div class="myChartDiv">
      <canvas id="myChart" width="600" height="400"></canvas>
    </div>
  </body>
  <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>

</html>
like image 27
Shakti Avatar answered Jun 08 '26 13:06

Shakti



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!