javascript - Formatting x-axis labels as time values in Chart.js - Stack Overflow

Using chart.js I would like to reformat my bottom chart labels into MM DD format (ie. Feb 2)How do I g

Using chart.js I would like to reformat my bottom chart labels into MM DD format (ie. Feb 2) How do I get chart.js to recognize the data as dates and convert it? It es in like this from my csv file: 2020-02-06

Here is my chart.js coding but it doesn't seem to work.

    data: {
            labels: dollar.years,
            datasets: [
              {label: 'Cdn dollar in cents',
                data: dollar.vals,
                fill: false,
                borderColor: '#4BD0B0',
                pointRadius: 0,
                borderWidth: 8
              }
                      ]
                },

          options: {
          legend:{
          display:false,
          usePointStyle: true,

          labels: {
          boxwidth:10,
          rotation:90
                    }
                  },

          scales: {

          yAxes: [{
          gridLines:{
          display:true
                    },

          ticks: {
          min: .74,
          max: .76,
          stepSize: .025,
                }
                }],

          xAxes: [{
          gridLines:{
          display:true
                },
          ticks: {
          labelOffset: 1,
                },
                  }],
                  }
                  }
        });
      }

Using chart.js I would like to reformat my bottom chart labels into MM DD format (ie. Feb 2) How do I get chart.js to recognize the data as dates and convert it? It es in like this from my csv file: 2020-02-06

Here is my chart.js coding but it doesn't seem to work.

    data: {
            labels: dollar.years,
            datasets: [
              {label: 'Cdn dollar in cents',
                data: dollar.vals,
                fill: false,
                borderColor: '#4BD0B0',
                pointRadius: 0,
                borderWidth: 8
              }
                      ]
                },

          options: {
          legend:{
          display:false,
          usePointStyle: true,

          labels: {
          boxwidth:10,
          rotation:90
                    }
                  },

          scales: {

          yAxes: [{
          gridLines:{
          display:true
                    },

          ticks: {
          min: .74,
          max: .76,
          stepSize: .025,
                }
                }],

          xAxes: [{
          gridLines:{
          display:true
                },
          ticks: {
          labelOffset: 1,
                },
                  }],
                  }
                  }
        });
      }
Share Improve this question edited Feb 13, 2020 at 21:04 TamTaminToronto asked Feb 13, 2020 at 19:24 TamTaminTorontoTamTaminToronto 1215 silver badges13 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 3

All you need is to define your xAxis as a time cartesian axis with a 'day' unit.

The default display format of 'day' is 'MMM D' (for instance 'Feb 2').

options: {
    ...
    scales: {
        xAxes: [{
            type: 'time',
            time: {
                unit: 'day',
                tooltipFormat: 'MMM DD' 
            }
            ...
        }]
    }
    ... 
}

Please have a look at the following runnable code snippet:

new Chart(document.getElementById('myChart'), {
  type: 'line',  
  data: {
    labels: ['2020-02-06', '2020-02-07', '2020-02-08', '2020-02-09', '2020-02-10', '2020-02-11', '2020-02-12'],
    datasets: [{
      label: 'My Dataset',
      data: [0.758, 0.756, 0.755, 0.754, 0.753, 0.758, 0.76],
      fill: false,
      backgroundColor: 'green',
      borderColor: 'green'
    }]
  },
  options: {
    responsive: true,
    title: {
      display: false
    },
    legend: {
      display: false
    },
    scales: {
      yAxes: [{
        ticks: {
          min: .74,
          max: .76,
          stepSize: .005
        }
      }],
      xAxes: [{
        type: 'time',
        time: {
          unit: 'day',
          tooltipFormat: 'MMM DD'
        }
      }],
    }
  }
});
<script src="https://cdnjs.cloudflare./ajax/libs/Chart.js/2.9.3/Chart.bundle.min.js"></script>
<canvas id="myChart" height="80"></canvas>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745390386a4625635.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信