1. 程式人生 > 其它 >echarts 堆疊柱狀圖 專案中的使用

echarts 堆疊柱狀圖 專案中的使用

效果如圖

堆疊柱狀圖的使用官方有,注意點兩個

1橫軸排列

2如何堆積起來

A1因為有的時候週末不用打卡,橫座標就不需要1-31都有,挑有人打卡的天數匯聚成陣列checkDate,當成橫座標

另,如果需要排序,還需要在獲得三組資料[checkDate/checkSuc/checkFail]之後再進行一次氣泡排序,三個依序改變

A2有相同stack的可以堆在一起

bubbleSort() {
      for (let i = 0; i < this.checkDate.length - 1; i++) {
        for (let j = 0; j < this.checkDate.length - 1; j++) {
          if (this.checkDate[j] > this.checkDate[j + 1]) {
            // Everytime, checkDate change the position,
            // checkSuc,checkFail company its change.
            let t = this.checkDate[j];
            this.checkDate[j] = this.checkDate[j + 1];
            this.checkDate[j + 1] = t;
            t = this.checkSuc[j];
            this.checkSuc[j] = this.checkSuc[j + 1];
            this.checkSuc[j + 1] = t;
            t = this.checkFail[j];
            this.checkFail[j] = this.checkFail[j + 1];
            this.checkFail[j + 1] = t;
          }
        }
      }
      // console.log('After bubbleSort',this.checkDate);
    },
    // Init [checkDate]\[checkSuc]\[checkFail]
    getDateList() {
      // Every Time Reload three arr
      this.checkDate = [];
      this.checkSuc = [];
      this.checkFail = [];

      this.formatDate = this.$moment(this.nMonth).format("YYYY-MM");

      let t = this.tableData.map((key) => {
        let tempD = this.$moment(key.date).format("YYYY-MM");
        if (tempD == this.formatDate) {
          this.checkDate.push(key);
        }
      });

      let dayCount = [];
      let tSuc = this.checkDate.map((key) => {
        // console.log(key.date, "=", key.date.slice(8, 10));
        dayCount.push(key.date.slice(8, 10));
      });
      let dayCountU = new Set(dayCount);
      console.log("dayCountUnique", dayCountU);

      let tFail = Array.from(dayCountU).map((keyD) => {
        let countSuc = 0;
        let countFail = 0;
        this.checkDate.map((key) => {
          let afterTwo = key.date.slice(8, 10);
          if (this.searchName == "") {
            if (keyD == afterTwo) {
              if (key.statement == "正常") {
                countSuc++;
              } else {
                countFail++;
              }
            }
          } else {
            if (keyD == afterTwo && this.searchName == key.name) {
              if (key.statement == "正常") {
                countSuc++;
              } else {
                countFail++;
              }
            }
          }
        });
        this.checkSuc.push(countSuc);
        this.checkFail.push(countFail);
      });
      this.checkDate = Array.from(dayCountU);
      this.bubbleSort();
      console.log(
        "Date",
        this.checkDate,
        "\nSuc",
        this.checkSuc,
        "\nFail",
        this.checkFail
      );
    },
    drawColAll() {
      this.getDateList();
      this.myChart = echarts.init(document.getElementById("colAll"));

      var option = {
        tooltip: {
          trigger: "axis",
          axisPointer: {
            // 座標軸指示器,座標軸觸發有效
            type: "shadow", // 預設為直線,可選為:'line' | 'shadow'
          },
        },
        legend: {
          data: ["打卡成功", "打卡異常"],
        },
        grid: {
          left: "3%",
          right: "4%",
          bottom: "3%",
          containLabel: true,
        },
        xAxis: [
          {
            type: "category",
            data: this.checkDate,
          },
        ],
        yAxis: [
          {
            type: "value",
          },
        ],
        series: [
          {
            name: "打卡成功",
            type: "bar",
            stack: "總量",
            emphasis: {
              focus: "series",
            },
            data: this.checkSuc,
          },
          {
            name: "打卡異常",
            type: "bar",
            stack: "總量",
            emphasis: {
              focus: "series",
            },
            data: this.checkFail,
          },
        ],
      };

      this.myChart.setOption(option);
    },
    drawColOne() {
      this.getDateList();
      this.myChart = echarts.init(document.getElementById("colOne"));

      var option = {
        tooltip: {
          trigger: "axis",
          axisPointer: {
            // 座標軸指示器,座標軸觸發有效
            type: "shadow", // 預設為直線,可選為:'line' | 'shadow'
          },
        },
        legend: {
          data: ["打卡成功", "打卡異常"],
        },
        grid: {
          left: "3%",
          right: "4%",
          bottom: "3%",
          containLabel: true,
        },
        xAxis: [
          {
            type: "category",
            data: this.checkDate,
          },
        ],
        yAxis: [
          {
            type: "value",
          },
        ],
        series: [
          {
            name: "打卡成功",
            type: "bar",
            stack: "總量",
            emphasis: {
              focus: "series",
            },
            data: this.checkSuc,
          },
          {
            name: "打卡異常",
            type: "bar",
            stack: "總量",
            emphasis: {
              focus: "series",
            },
            data: this.checkFail,
          },
        ],
      };

      this.myChart.setOption(option);
    },