skip to Main Content

The dates in the x axis are showing times and time zones. I just want to show month and day.

function dateSeries(startDate, endDate) {
    let dates = [];
    let currentDate = new Date(startDate);
    while (currentDate <= endDate) {
        dates.push(new Date(currentDate));
        currentDate.setDate(currentDate.getDate() + 1);
    }
    return dates;
};

let ctx = document.getElementById('myChart');

let goal = [5,4,3,2,1,0];
let dates = dateSeries(new Date('2024-12-03'), new Date('2024-12-08'));

let myChart = new Chart(ctx, {
    type: 'line',
    data: {
        labels: dates,
        datasets: [
            {
                label: 'Goal',
                data: goal,
                borderColor: 'rgba(142, 180, 227, 1)',
            }
        ]
    },
    options: {
        scales: {
            x: {
                type: 'time',
                time: {
                    unit: 'day',
                    displayFormats: {
                        day: 'MMM DD'
                    }
                }
            }
        }
    }
});

It renders as:

Chartjs with verbose x axis

I feel like my "options" code looks like all the examples I found, but I can’t get the axis to format properly.

Update: Adapters

I didn’t have any adapter defined. I thought I read that Chart.js has a default adapter, which I took to mean I didn’t need a special one unless I needed something special. But I have no problem including one.

I changed to day: 'MMM dd' and my stuff looks just like @kikon’s fiddle, which clearly works. But mine still doesn’t.

Chart showing web elements

Script.js looks just like the above except for the change to the date format.

2

Answers


  1. you can use this and instead (‘fa-IR’) use your local time for format time

    day: (t) => new Date(t).toLocaleDateString('fa-IR').slice(5,10)
    
    Login or Signup to reply.
  2. From your screenshot, you are using the old version of Chart.js (v2.8.0).

    In summary

    Version Usage of Axis Reference
    v2 x-axis: xAxes (array), y-axis: yAxes (array) Creating Multiple Axes
    v3 or later x-axis: x (object), y-axis: y (object) 3.x Migration Guide (Scale section)

    Hence, your configuration should be as follows for Chart.js v2.8.0.

    let myChart = new Chart(ctx, {
      type: 'line',
      data: {
        labels: dates,
        datasets: [
          {
            label: 'Goal',
            data: goal,
            borderColor: 'rgba(142, 180, 227, 1)',
          },
        ],
      },
      options: {
        scales: {
          xAxes: [
            {
              type: 'time',
              time: {
                unit: 'day',
                displayFormats: {
                  day: 'MMM dd',
                },
              },
            },
          ],
        },
      },
    });
    

    Demo @ StackBlitz

    Otherwise, you need to follow the configuration in the JsFiddle link provided by @kikon in the comment for applying Chart.js v3 or later.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search