Dynamically last 3 month Filter in Table or Graph

Viewed 46

Right now, I manually filter the last 3 months by labels: ["JAN", "FEB", "MAR"],

I need dyanamically the last 3 months should be filtered every month. How can this be done?

This is my typescript code

HTML

<div>
<canvas id="myChart" width="400" height="300"></canvas>
</div>

TS

  this.canvas = document.getElementById('myChart');
    this.ctx = this.canvas.getContext('2d');
    let myChart = new Chart(this.ctx, {
      type: 'bar',
      //type: 'doughnut',
      data: {
        // labels: ["Sold", "Booked", "Unsold"],
        // labels: this.barchartLables,
        labels: ["JAN", "FEB", "MAR"],
        datasets: [{
          label: 'Total Expenditure.',
          //data: this.barchartdata,
          data: [2517453, 2617453, 3917453],
          //backgroundColor: ["red", , , , , , , , "blue"],
          backgroundColor: [
            '#ec6666',
            '#78d1dd',
            '#147ad6'
          ],
          borderWidth: 1
        }]
      },
      options: {
        legend: {
          display: false
        },
        responsive: false,
        display: true
      }

    });
1 Answers

you can use moment js library for this purpose

like you can use moment subtract method for last any month

moment().subtract(1, 'month')//for last month
moment().subtract(2, 'month') // for second last month
moment().subtract(3, 'month') // for the third one
Related