echartLine.vue 4.11 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: ""
    },
    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() {
      console.log(88888888, this.yData)
      var option = {
        grid: {
          bottom: "12%",
          top: "10%",
          left: "15%",
          right: "20px"
        },
        tooltip: {
          trigger: "axis",
          axisPointer: {
            lineStyle: {
              type: "solid"
            }
          },
          padding: 12,
          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}
</div>
    <div style="margin-left:18px; line-height: 20px;">环比: <span style="color:${
      rate.indexOf("-") > -1 ? "#2CCA74" : "#EB4E4E"
    }">${rate || 0}${rate.indexOf("-") > -1 ? "↓" : "↑"}</span></div>
  `
          }
        },
        xAxis: {
          type: "category",
          boundaryGap: false,
          splitNumber: 8,
          axisLine: {
            show: false
          },
          axisTick: {
            show: false
          },
          splitLine: {
            show: false
          },
          axisLine: {
            show: false
          },
          axisLabel: {
            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,
            lineStyle: {
              color: "#E7EBEF"
            }
          },
          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
    }
  }
}
</script>

<style scoped lang="scss">
.canvas {
  width: 100%;
  height: 100%;
}
</style>