Javascript 获取Chartjs数组中最近6个月的名称
我试图在图表js中动态获取最近六个月的名称作为标签Javascript 获取Chartjs数组中最近6个月的名称,javascript,arrays,chatjs,Javascript,Arrays,Chatjs,我试图在图表js中动态获取最近六个月的名称作为标签 var profitChart = { labels: ***** last six months name, datasets: [ { label: "Profit", backgroundColor: "rgba(58,181,74,0.3)", borderColor: "rgba(58,181,74,0.80)", pointRadius:
var profitChart = {
labels: ***** last six months name,
datasets: [
{
label: "Profit",
backgroundColor: "rgba(58,181,74,0.3)",
borderColor: "rgba(58,181,74,0.80)",
pointRadius: false,
pointColor: "rgba(210, 214, 222, 1)",
pointStrokeColor: "#c1c7d1",
pointHighlightFill: "#fff",
pointHighlightStroke: "rgba(220,220,220,1)",
data: profitdata,
},
创建一个包含月份的数组,使用getMonth of date对象获取当前月份,使用array.slice提取最后6个月 设月份=[一月、二月、三月、四月、六月、七月、八月、九月、十月、十一月、十二月]; 让currentMonth=new Date.getMonth var profitChart={ 标签:months.slicecurrentMonth-6.concatmonths.slice0,currentMonth, 数据集:[ { 标签:利润, 背景颜色:RGBA58181,74,0.3, 边框颜色:RGBA58181,74,0.80, pointRadius:false, 点颜色:rgba210、214、222、1、, pointStrokeColor:c1c7d1, pointHighlightFill:fff, pointHighlightStroke:RGBA220220,1, 数据:profitdata, }] }
console.logprofitchart将所有名称存储在一个数组中,从new Date.getMonth获取当前月份,它已经是零基的,然后只从arrayworks中获取最后六个,但是如果从1月份开始倒数,它将显示undefinedJanuary=0->0-1==-1,但在索引-1中没有->undefined。在那种情况下,你必须调整索引