javascript - 在表格或图表中动态过滤最近 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
}
});
解决方案
您可以为此目的使用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
推荐阅读
- c# - CsvHelper 类型转换,带字符串和整数的列到整数
- javascript - 用 JavaScript 彻底解决汽车加油问题
- php - 如何在 Mac OS 11 上使用 Homebrew 从源代码构建后卸载 PHP 7.3
- python - 覆盖率报告显示为零
- sql - 具有特定工作日的 SQL 查询
- python-3.x - 我正在尝试使用neatext 函数提取哈希标签,但我不断收到此错误。TypeError:预期的字符串或类似字节的对象
- r - 如何将关键变量添加到`dplyr::group_map()`?
- javascript - 页面重新加载后显示 sweetAlert 弹出窗口
- r - 当参数可能为空时,R 中的语句
- mailman - 更改邮递员列表关键字以加入/离开邮件列表