echartLine.vue 3.37 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: ""
    }
  },
  watch: {
    xData: {
      handler() {
        this.chart.setOption(this.getChartSetOption())
      }
    },
    yData: {
      handler() {
        this.chart.setOption(this.getChartSetOption())
      }
    }
  },
  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: {
          bottom: "12%",
          top: "10%",
          left: "15%",
          right: "20px"
        },
        tooltip: {
          show: true,
          trigger: "axis",
          triggerOn: "click",
          backgroundColor: "rgba(41, 45, 51, 1)",
          borderRadius: 15,
          borderColor: "rgba(41, 45, 51, 1)",
          textStyle: {
            // 悬浮框文字样式
            color: "#fff",
            fontSize: 12
          },
          formatter: (params) => {
            return params[0].value
          }
        },
        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: "inside",
              fontSize: 10,
              show: true,
              color: "#999999"
            },
            emphasis: {
              label: {
                show: false
              }
            },
            showSymbol: false,
            itemStyle: {
              color: "rgba(52, 123, 255, 1)"
            },
            data: this.yData
          }
        ]
      }
      return option
    }
  }
}
</script>

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