简体   繁体   中英

Chart x axis displays in timestamp instead of dates Chart.js

I am doing a graph of a json with chart js. Even if I set the formatted to date I still get timestamp. I hope someone can help me. BTW the data is being retrieved from firebase. I attach the code I am using.

Thanks in advance.

Output:

Desired output:

在此处输入图像描述

Code:

            var currentDate= val.date; //Extracting the date value in dd/mm/yyyy format from the mentioned text box

I get the data from firebase

                     //Printing the extracted date value before making the change
                    var newDate = currentDate.split('/'); //Splitting the extracted date value using the delimiter /, which is the seperator used in the date value         
                    currentDate = newDate[1] + "/" + newDate[0] + "/" + newDate[2];//Constructing a new date value (string) using the splitted values.

I format it to MMDDYYYY

var dateObject = new Date(currentDate);

Then it is changed to a date data type

var data_point={
  x:dateObject,
  y:parseFloat(val.sa_quantity)
};
datapoints.push(data_point);

I add all the data changed to an array that is used to plot the data

var chartData = [];
      var ctxR = document.getElementById("data_chart").getContext('2d');
      var myChart = new Chart(ctxR, {
        type: 'scatter',
        data: {
          labels:dates,
          datasets: [{
            label: 'Predicted Sales($)',
            data: datapoints
          }]
        },
        responsive: true,
            title:{
                display: true,
                text:"Chart.js Time Scale"
            },
            scales:{
                xAxes: [{
                  type: 'time',
                  time: {
                    displayFormats: {
                      'millisecond': 'MMM DD',
                      'second': 'MMM DD',
                      'minute': 'MMM DD',
                      'hour': 'MMM DD',
                      'day': 'MMM DD',
                      'week': 'MMM DD',
                      'month': 'MMM DD',
                      'quarter': 'MMM DD',
                      'year': 'MMM DD',
                    },
                    round: 'day',
                    unit: 'day',
                  }
                }]
            }
        });
        myChart.update();

You have to change the display format. chart.js likes unix, people like date strings. You should add something like this function to display your dates:

function getTimeString(date) {
  var day = date.getDate();
  var month = date.getMonth() + 1;
  var year = date.getFullYear();

  return day + '/' + month + '/' + year;
}

Whenever I work with a time-scale in chart.js I add moment.js because it's much more powerful. But it can be kind of an overkill for webapps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM