echartBar.vue 4.99 KB
<template>
  <div :ref="id" class="canvas"></div>
</template>
<script>
import * as echarts from "echarts"
export default {
  name: "barChart",
  props: {
    title: {
      type: String
    },
    colors: {
      type: Array,
      default: () => ["#FFC154", "#FF9671"]
    },
    xData: {
      type: Array,
      default: []
    },
    yData: {
      type: Array,
      default: []
    },
    rate: {
      type: String
    },
    dataRate: {
      type: Array,
      default: () => []
    },
    id: {
      type: String,
      default: ""
    }
  },
  data() {
    return {
      client: "",
      chart: ""
    }
  },
  //   created() {
  //     const { rotate, analysisList } = this
  //     const obj = {
  //       rotate,
  //       analysisList
  //     }
  //     this.lineOptions = this.revese ? getBarReverseOptions(obj) : getBarOptions(obj)
  //   },
  watch: {
    yData: {
      handler() {
        this.chart.setOption(this.getChartSetOption(), { notMerge: true })
      },
      deep: true
    }
  },
  mounted() {
    this.client = this.$refs[this.id]
    this.chart = echarts.init(this.client)
    this.chart.setOption(this.getChartSetOption(), { notMerge: true })
    window.addEventListener("resize", this.resizeHandler)
  },
  methods: {
    resizeHandler() {
      if (this.chart) {
        this.chart.clear()
        setTimeout(() => {
          this.chart.setOption(this.getChartSetOption(), { notMerge: true })
          this.chart.resize()
        }, 1000)
      }
    },
    getChartSetOption() {
      var options = {
        tooltip: {
          trigger: "axis",
          axisPointer: {
            lineStyle: {
              type: "solid"
            }
          },
          padding: 12,
          borderColor: "transparent",
          borderWidth: 1,
          backgroundColor: "rgba(255, 255, 255, 0.95)",
          borderRadius: 4,
          textStyle: {
            color: "rgba(0, 0, 0, 0.65)",
            fontSize: 14
          },
          formatter: (params) => {
            let rate = String(this.dataRate[params[0].dataIndex][this.rate])
            return `
    <div style="font-weight:bold;color:rgba(0, 0, 0, 0.65); line-height: 20px;">${
      params[0].name
    }</div>
    <div style="rgba(0, 0, 0, 0.65);display:flex;align-items:center; line-height: 20px;"> <div style="width: 6px;
height: 6px;background:  ${
              params[0].color
            };border-radius:50%;margin-right:8px;"></div>
${this.title}:${
              params[0].value === 0 ? params[0].value : params[0].value || "-"
            }
</div>
    <div style="margin-left:18px; line-height: 20px;">环比: <span style="color:${
      rate == "null" || rate == 0
        ? ""
        : rate.indexOf("-") > -1
        ? "#2CCA74"
        : "#EB4E4E"
    }">${rate == "null" ? "-" : rate ? rate + "%" : 0 + "%"}${
              rate == "null" || rate == 0
                ? ""
                : rate.indexOf("-") > -1
                ? "↓"
                : "↑"
            }</span></div>
  `
          }
        },
        xAxis: {
          type: "category",
          splitNumber: 4,
          axisLabel: {
            // rotate: 30, // 横轴名称旋转
            interval: 0,
            show: true,
            color: "#000000a6",
            fontSize: "11px"
          },
          splitLine: {
            show: false
          },
          axisTick: {
            show: false
          },
          axisLine: {
            show: false
          },
          data: this.xData
        },
        grid: {
          left: "6%",
          right: "6%",
          top: "8%",
          bottom: "4%",
          // bottom: obj.rotate ? '4%' : '12%',
          containLabel: true
        },
        yAxis: {
          type: "value",
          axisLabel: {
            show: false,
            color: "transparent"
          },
          axisLine: { show: false, color: "#E7EBEF" },
          axisTick: { show: false, color: "transparent" },
          splitLine: {
            show: true, // 根据截图隐藏网格线
            color: "#F5F7F8"
          }
          //   splitNumber: 4
        },
        series: [
          {
            type: "bar",
            symbol: "circle",
            backgroundStyle: {
              color: "#6578FF"
            },
            label: {
              position: "top",
              show: true,
              color: "#999999"
            },
            itemStyle: {
              color: "#2A58AD"
            },
            barWidth: "25%",
            data: this.yData
          }
        ]
      }
      return options
    },
    destroyChart() {
      // 移除窗口 resize 事件监听器
      window.removeEventListener("resize", this.resizeHandler)

      // 销毁 ECharts 实例
      if (this.chart) {
        this.chart.dispose()
        this.chart = null
      }
    }
  },
  beforeDestroy() {
    this.destroyChart()
  },

  // 如果使用的是 Vue 3,则使用 beforeUnmount
  beforeUnmount() {
    this.destroyChart()
  }
}
</script>
<style lang="scss" scoped>
.canvas {
  width: 100%;
  height: 100%;
}
</style>