index.vue 1.5 KB
<template>
  <div>
    <Chart :cdata="cdata" />
  </div>
</template>

<script>
  import Chart from "./Chart";
  import work from "@/api/work";
  export default {
    data () {
      return {
        cdata: [],
        getdata: [],
      };
    },
    components: {
      Chart,
    },
    mounted () {
      this.addhousetotal();
      window.addEventListener("resize", () => {
        this.cdata = [];
        this.addhousetotal();
      });

    },
    methods: {
      async addhousetotal () {
        if (this.cdata == 0) {
          this.getdata = [];
          try {
            let { result: res } = await work.addhousetotal();
            res.map((item) => {
              return this.cdata.push({
                name: `${item.fwyt + '(' + item.fwxz})`,
                value: item.mj,
              });

            });
            let delarr = this.cdata.sort(this.up)
            this.setadat(delarr)

          } catch (error) {
            console.log("error", error);
          }
        }


      },
      // 处理数据方法
      setadat (delarr) {

        this.cdata = delarr.splice(0, 6);
        let sum = 0
        for (var i = 0; i < delarr.length; i++) {
          sum = sum + delarr[i].value
        }
        sum = sum.toFixed(2)
        this.cdata.push({
          name: "其他(其他种类)",
          values: "其他种类",
          value: sum,
        })
      },
      // 排序方法
      up (x, y) { return y.value - x.value }

    }
  };
</script>

<style lang="scss" scoped></style>