0
votes

I have a chartjs chart, after I changed x axis's date format, the chart is spilling out of its container. It seems to be affected by that but I need for the format to stay dd-mm-yyyy. Can anyone think of why its doing that? I've tried changing the maintain aspect ratio/ responsive booleans around, i've tried changing the height and width of the canvas and that does nothing as well.

my coding:

   <div>
    <canvas id="Chart" width="400" height="200"></canvas>
</div>


<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.bundle.js"></script>

<script>

var ctx = document.getElementById("Chart").getContext('2d');
var recentActivityChart = new Chart(ctx, {
    type: 'bar',
    data: {
        labels: [],
        datasets: [{
            label: 'hours',
            data: [],
            barThickness: 12,
            fill: true,
            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',
                time: {
                    unit: 'day',
                    displayFormats: {
                        day: 'DD-MM-YYYY'
                    },
                },
                gridLines: {
                    scaleShowVerticalLines: false,
                    display: false,
                },
                ticks: {
                    beginAtZero: false,
                }
            }]
        },
    }
});
</script>

heres what it is appearing like :

enter image description here

1

1 Answers

1
votes

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>