我目前正在做我的项目在一个屏幕上,将显示收入/结果的业务。我正在尝试使用“react-native-chart kit”的依赖关系在图中显示数据。
我的目标是显示当月(10月)和前3个月(7月、8月、9月)和未来2个月(11月、12月)的收入,但现在我只能在图表中显示月份。我能够获得当前月份并显示它,但我不能计算出显示前几个月和前几个月的公式。
代码:
const d = new Date();
const v = d.getMonth() + 1;
console.log("V => ", v);
const data = {
labels: ["Jul","Aug","Sep",v,"Nov","Dec"],
datasets: [
{
data: [1300, 45, 28, 80, 99, 43],
color: (opacity = 1) => `rgba(134, 65, 244, ${opacity})`, // optional
strokeWidth: 2, // optional
},
],
legend: ["Incomes - Quarter displayed"], // optional
};
如何将月份提取到labels数组中?
发布于 2020-10-10 21:00:09
您可以执行以下操作:
var month = new Date().getMonth() + 1; //To get the Current Month
labels: [this.checkMonthName(month-3), this.checkMonthName(month-2), this.checkMonthName(month-1),
this.checkMonthName(month), this.checkMonthName(month+1), this.checkMonthName(month+2)]
然后标识此函数checkMonthName以从其编号中返回月份名称:
checkMonthName =(month)=>{
var monthName ;
if(month == 1) monthName = "January"
else if(month == 2) monthName = "February"
else if(month == 3) monthName = "March"
else if(month == 4) monthName = "April"
else if(month == 5) monthName = "May"
else if(month == 6) monthName = "June"
else if(month == 7) monthName = "July"
else if(month == 8) monthName = "August"
else if(month == 9) monthName = "September"
else if(month == 10) monthName = "October"
else if(month == 11) monthName = "November"
else if(month == 12) monthName = "December"
return monthName;
}
-如果当前月份较早,则应进行处理,因为-3将为负数,这将影响结果
或
-如果当前月份是像12月这样的月末,则month+1将等于13
也许有更简单的方法,但这就是我现在的想法。
https://stackoverflow.com/questions/64293010
复制相似问题