首页 代码干货

  let date = new Date(); //当前时间
  let apiData = apiData; //接口取到的数据
  let lastMonth = moment(date).subtract(1, "months"); //取上一个的月份
  let xDataNew = new Array(lastMonth.daysInMonth()).fill(0); //生成上个月整月的数据
  let xDateNewDay = new Array(date.getDate()).fill(0); //生成本月1号至今的数据
  let newArrX = xDataNew.concat(xDateNewDay); //合并上个月和本月的数据 日期
  let newArrY = xDataNew.concat(xDateNewDay); //合并上个月和本月的数据 数据
  let xData = [],yData = [];
  newArrX.map((v, k) => {
    let key = k + 1 < 10 ? "0" + (k + 1) : k + 1; //日期小于10补零
    let dateDefult;
    if (Number(key) > lastMonth.daysInMonth()) {
      //如果超过上一个月最后一天
      dateDefult = moment(date).format("MM-") + (key - 30);
    } else {
      dateDefult = lastMonth.format("MM-") + key;
    }
    xData.push(dateDefult);
  });
  apiData.map((v) => {
    let key = xData.lastIndexOf(v.date.substr(5));
    if (key != -1) {
      newArrY[key] = v.payAmount;
    }
  });
  yData = newArrY;
  this.xData = xData;
  this.echartsData = yData;

文章评论