首页 > 解决方案 > 在表格或图表中动态过滤最近 3 个月

问题描述

现在,我按标签手动过滤过去 3 个月:[“JAN”、“FEB”、“MAR”]、

我需要动态地每个月过滤最后 3 个月。如何才能做到这一点?

这是我的打字稿代码

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
      }

    });

标签: javascripthtmlcssangulartypescript

解决方案


您可以为此目的使用moment js库

就像你可以在最后一个月使用矩减法

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

推荐阅读