Chartjs-Chartjs chart spilling out of container

1👍

You should add option offset: true to the x-axis as follows:

xAxes: [{
  type: 'time',
  offset: true,

Please take a look at your amended code and see how it works.

var ctx = document.getElementById("Chart").getContext('2d');
var recentActivityChart = new Chart(ctx, {
  type: 'bar',
  data: {
    labels: ['2020-01-01', '2020-01-02', '2020-01-03'],
    datasets: [{
      label: 'hours',
      data: [60, 44, 43],
      barThickness: 12,
      backgroundColor: "rgba(54, 162, 235, 1)",
      borderColor: "rgba(54, 162, 235, 1)",
      borderWidth: 1
    }]
  },
  options: {
    animation: {
      duration: 1000,
      easing: "linear",
    },
    responsive: true,
    maintainAspectRatio: true,
    legend: {
      display: false,
      position: 'bottom',
      usePointStyle: true,
      labels: {
        fontColor: "grey",
        usePointStyle: true,
      },
    },
    scales: {
      yAxes: [{
        gridLines: {
          display: true,
          borderDash: [8, 4],
        },
        scaleLabel: {
          display: true,
          labelString: 'hours',
        },
        ticks: {
          beginAtZero: false,
        }
      }],
      xAxes: [{
        type: 'time',
        offset: true,
        time: {
          unit: 'day',
          displayFormats: {
            day: 'DD-MM-YYYY'
          },
        },
        gridLines: {
          scaleShowVerticalLines: false,
          display: false,
        },
        ticks: {
          beginAtZero: false,
        }
      }]
    },
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.bundle.js"></script>
<canvas id="Chart" width="400" height="200"></canvas>

Leave a comment