管道基础大数据平台系统开发-【前端】-新系統界面
TreeWish
2023-02-28 234409bb4a612c5b69b78b1e844492f0a7122521
src/components/chart/BaseBarChart.vue
@@ -4,20 +4,24 @@
<script>
import * as echarts from "echarts"
import { countCountryDimension } from "@/api/screen.js"
import {
  countCountryDimension,
  countProvinceDimension,
  countZhPipeStations,
} from "@/api/screen.js"
export default {
  props: {
    width: {
      type: String,
      default: '100%'
      default: "100%",
    },
    height: {
      type: String,
      default: '100%'
      default: "100%",
    },
    autoResize: {
      type: Boolean,
      default: true
      default: true,
    },
    // option: {
    //   type: Object,
@@ -25,13 +29,21 @@
    // },
    type: {
      type: String,
      default: 'canvas'
    }
      default: "canvas",
    },
    project: {
      type: String,
      default: "全国项目",
    },
    title: {
      type: String,
      default: "",
    },
  },
  data() {
    return {
      chart: null,
      dataList: []
      dataList: [],
    }
  },
  computed: {
@@ -88,10 +100,36 @@
      if (this.dataList) {
        data = this.dataList
      }
      let count = 0
      let countVal = 0
      const title = this.title
      data.forEach(item => {
        xAxisData.push(item.country)
        yAxisData.push(item.count)
        let name = item.province || item.country || item.输送介质
        let value = item.count || item.站场数量
        if (title == "站场座数" && item.管道数量) {
          count += item.管道数量
        } else {
          count += item.count || 0
        }
        // switch (title) {
        //   case "站场座数":
        //     countVal = item.管道数量
        //     break
        //   case "项目个数":
        //     countVal = item.count
        //     break
        // }
        xAxisData.push(name)
        yAxisData.push(value)
      })
      console.log("管道数量", countVal)
      if (typeof count === "number") {
        this.$bus.$emit("changeCount", count)
      }
      let option = {
        backgroundColor: "transparent",
        tooltip: {
@@ -129,6 +167,9 @@
                var maxLength = 2 //每项显示文字个数
                var valLength = value.length //X轴类目项的文字个数
                var rowN = Math.ceil(valLength / maxLength) //类目项需要换行的行数
                if (xAxisData.length < 7) {
                  return value
                }
                if (rowN > 1) {
                  //如果类目项的文字大于3,
                  for (var i = 0; i < rowN; i++) {
@@ -165,6 +206,7 @@
        ],
        series: [
          {
            name: this.title,
            type: "bar",
            data: yAxisData,
            barWidth: "12px",
@@ -234,28 +276,48 @@
          },
        ],
      }
      return option
    }
    },
  },
  watch: {
    // option: {
    //   deep: true,
    //   handler(newVal) {
    //     this.setOptions(newVal)
    //   }
    // },
    currentProject: {
    option: {
      deep: true,
      handler(newVal) {
        this.initData()
        this.setOptions(newVal)
      },
    },
    project: {
      deep: true,
      immediate: true,
      handler(newVal) {
        let requsetFn = null
        switch (newVal) {
          case "全球项目":
            requsetFn = countCountryDimension
            break
          case "全国项目":
            requsetFn = countProvinceDimension
            break
          case "全球管网图":
            requsetFn = countZhPipeStations
            break
          case "全国管网图":
            requsetFn = countZhPipeStations
            break
          default:
            break
        }
        requsetFn && this.initData(requsetFn)
        this.setOptions(this.option)
      }
    }
      },
    },
  },
  mounted() {
    // this.initData()
    this.initChart()
    if (this.autoResize) {
      window.addEventListener('resize', this.resizeHandler)
      window.addEventListener("resize", this.resizeHandler)
    }
  },
  beforeDestroy() {
@@ -263,24 +325,24 @@
      return
    }
    if (this.autoResize) {
      window.removeEventListener('resize', this.resizeHandler)
      window.removeEventListener("resize", this.resizeHandler)
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    resizeHandler() {
      this.chart.resize()
      this.chart && this.chart.resize()
    },
    initChart() {
      this.chart = echarts.init(this.$refs.chart, '', {
        renderer: this.type
      this.chart = echarts.init(this.$refs.chart, "", {
        renderer: this.type,
      })
      this.chart.setOption(this.option)
      this.chart.on('click', this.handleClick)
      this.chart.on("click", this.handleClick)
    },
    handleClick(params) {
      this.$emit('click', params)
      this.$emit("click", params)
    },
    setOptions(option) {
      this.clearChart()
@@ -294,12 +356,13 @@
    },
    clearChart() {
      this.chart && this.chart.clear()
    }
  },
  methods: {
    async initData() {
    },
    async initData(requsetFn = countCountryDimension) {
      const res = await requsetFn()
      if (res.code == 200) {
        this.dataList = res.result
        console.log("requsetFn", res)
      }
    },
  },
}