echartLine.vue 5.33 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: ""
    }
  },
  watch: {
    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)
      }
    },
    getMax() {
      let tickIntervalNum = 0
      if (this.yData.length) {
        let s = this.yData[0]
        for (var i = 1; i < this.yData.length; i++) {
          if (s < this.yData[i]) {
            s = this.yData[i]
          }
        }
        tickIntervalNum = s
      }
      return tickIntervalNum || 1
    },
    getChartSetOption() {
      let maxVal = this.getMax()
      // 计算合适的最大值,向上取整到最接近的整数
      let yAxisMax = Math.ceil(maxVal)
      // 确保最小值为0
      let yAxisMin = 0
      // 计算间隔,确保正好显示5个点 (0, interval, 2*interval, 3*interval, 4*interval)
      let interval = Math.ceil(yAxisMax / 5)

      var option = {
        grid: {
          top: "37",
          bottom: "0",
          left: "13",
          right: "16",
          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) => {
            return `
    <div style="font-weight:normal;color:rgba(0, 0, 0, 0.65); line-height: 20px;margin-bottom:4px;">${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:7.8px;"></div>
${this.title}:${params[0].value}辆
</div>
  `
          }
        },
        xAxis: {
          type: "category",
          // boundaryGap: false,
          // splitNumber: 4,
          axisLine: {
            show: false
          },
          axisTick: {
            show: false,
            lineStyle: {
              color: "#fff"
            }
          },
          splitLine: {
            show: false, // 隐藏网格线
            color: "#E9E9E9"
          },
          axisLine: {
            show: false
          },
          axisLabel: {
            // rotate: 30, // 横轴名称旋转
            // interval: 0,// 强制显示所有标签
            show: true,
            color: "rgba(0, 0, 0, 0.65)",
            fontSize: "12px"
          },
          data: this.xData
        },
        yAxis: {
          type: "value",
          name: "辆",
          nameTextStyle: {
            color: "rgba(0, 0, 0, 0.65)",
            align: "right"
          },
          offset: 10,
          labelLine: {
            show: true
          },
          axisLine: {
            show: false,
            lineStyle: {
              color: "#E7EBEF"
            }
          },
          axisLabel: {
            show: true,
            color: "rgba(0, 0, 0, 0.65)",
            fontSize: "12px"
          },
          splitLine: {
            show: true, // 根据隐藏网格线
            color: "#E9E9E9"
          },
          axisTick: {
            show: false
          },
          // 设置y轴固定显示5个点
          min: yAxisMin,
          max: interval * 5, // 确保最大值是间隔的4倍,这样就有0,1,2,3,4共5个点
          interval: interval
        },
        series: [
          {
            type: "line",
            avoidLabelOverlap: false,
            label: {
              formatter: "{c}%",
              position: "top",
              fontSize: 11,
              show: false,
              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>