echartLine.vue 5.28 KB
<template>
  <div :ref="id" class="canvas"></div>
</template>
<script>
//  按需引入 echarts
import * as echarts from "echarts"
export default {
  data() {
    return {
      client: "",
      chart: ""
    }
  },
  props: {
    colors: {
      type: Array,
      default: () => ["#FFC154", "#FF9671"]
    },
    xData: {
      type: Array,
      default: []
    },
    yData: {
      type: Array,
      default: []
    },
    id: {
      type: String,
      default: ""
    },
    title: {
      type: String,
      default: ""
    },
    rate: {
      type: String
    },
    dataRate: {
      type: Array,
      default: () => []
    }
  },
  watch: {
    // xData: {
    //   handler() {
    //     this.chart.setOption(this.getChartSetOption())
    //   }
    // },
    yData: {
      handler() {
        this.chart.setOption(this.getChartSetOption())
      },
      deep: true
    }
  },
  mounted() {
    this.client = this.$refs[this.id]
    this.chart = echarts.init(this.client)
    this.chart.setOption(this.getChartSetOption())
    window.addEventListener("resize", this.resizeHandler)
  },
  methods: {
    resizeHandler() {
      if (this.chart) {
        this.chart.clear()
        setTimeout(() => {
          this.chart.setOption(this.getChartSetOption())
          this.chart.resize()
        }, 1000)
      }
    },
    getChartSetOption() {
      var option = {
        grid: {
          top: "8%",
          bottom: "4%",
          left: "6%",
          right: "6%",
          containLabel: true
        },
        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) => {
            console.log(params)
            console.log(this.dataRate)
            let rate = String(this.dataRate[params[0].dataIndex][this.rate])
            console.log(
              rate,
              rate == "null" || params[0].value === 0,
              rate.indexOf("-") > -1
            )
            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
                ? "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",
          // boundaryGap: false,
          splitNumber: 4,
          axisLine: {
            show: false
          },
          axisTick: {
            show: false
          },
          splitLine: {
            show: false, // 根据截图隐藏网格线
            color: "#F5F7F8"
          },
          axisLine: {
            show: false
          },
          axisLabel: {
            // rotate: 30, // 横轴名称旋转
            interval: 0,
            show: true,
            color: "#000000a6",
            fontSize: "11px"
          },
          data: this.xData
        },
        yAxis: {
          type: "value",
          labelLine: {
            show: false
          },
          axisLine: {
            show: false,
            lineStyle: {
              color: "#E7EBEF"
            }
          },
          axisLabel: {
            show: false
          },
          splitLine: {
            show: true, // 根据截图隐藏网格线
            color: "#F5F7F8"
          },
          axisTick: {
            show: false
          }
        },
        series: [
          {
            type: "line",
            avoidLabelOverlap: false,
            label: {
              formatter: "{c}%",
              position: "top",
              fontSize: 11,
              show: true,
              color: "#999999"
            },
            showSymbol: true,
            itemStyle: {
              color: "#2A58AD"
            },
            data: this.yData
          }
        ]
      }
      return option
    },
    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 scoped lang="scss">
.canvas {
  width: 100%;
  height: 100%;
}
</style>