Chartjs-Chart.js separation lines

1👍

Now I see another solution: there are additional properties of gridLines for these purposes.

gridLines: {
  display: true,
  drawBorder: true,
  drawOnChartArea: false
}

1👍

Yes! It is possible !!

You can achieve that using the following chart plugin :

Chart.plugins.register({
   afterDatasetsDraw: function(chart) {
      var ctx = chart.ctx;
      var x_axis = chart.scales['x-axis-0'];
      //loop through ticks array
      x_axis.ticks.forEach(function(tick, index) {
         var x = x_axis.getPixelForTick(index);
         var y = chart.scales['y-axis-0'].bottom;
         var lineHeight = 8;
         var lineColor = 'black';
         //draw line
         ctx.save();
         ctx.beginPath();
         ctx.moveTo(x, y);
         ctx.lineTo(x, y + lineHeight);
         ctx.strokeStyle = lineColor;
         ctx.stroke();
         ctx.restore();
      });
   }
});

* add this at the beginning of your script

ᴅᴇᴍᴏ ⧩

Chart.plugins.register({
   afterDatasetsDraw: function(chart) {
      var ctx = chart.ctx;
      var x_axis = chart.scales['x-axis-0'];
      //loop through ticks array
      x_axis.ticks.forEach(function(tick, index) {
         var x = x_axis.getPixelForTick(index);
         var y = chart.scales['y-axis-0'].bottom;
         var lineHeight = 8;
         var lineColor = 'black';
         //draw line
         ctx.save();
         ctx.beginPath();
         ctx.moveTo(x, y);
         ctx.lineTo(x, y + lineHeight);
         ctx.strokeStyle = lineColor;
         ctx.stroke();
         ctx.restore();
      });
   }
});

var chart = new Chart(ctx, {
   type: 'line',
   data: {
      labels: ['Jan', 'Feb', 'Mar', 'Apr', 'May'],
      datasets: [{
         label: 'LINE',
         data: [3, 1, 4, 2, 5],
         backgroundColor: 'rgba(0, 119, 290, 0.2)',
         borderColor: 'rgba(0, 119, 290, 0.6)',
         fill: false,
         tension: 0
      }]
   },
   options: {
      scales: {
         xAxes: [{
            gridLines: {
               display: false
            }
         }],
         yAxes: [{
            ticks: {
               beginAtZero: true,
               stepSize: 1
            }
         }]
      }
   }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.6.0/Chart.min.js"></script>
<canvas id="ctx"></canvas>

Leave a comment