封装echart

采用封装的写法

lineChart

<template>
  <div :id="id" :style="{height:height,width:width}" />
</template>

<script>
export default {
  props: {
    id: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '400px'
    },
    height: {
      type: String,
      default: '200px'
    },
    color: {
      type: Array,
      default: () => {
        return ['#00B17D', 'red']
      }
    },
    showArea: {
      type: Boolean,
      default: false
    },
    series: {
      type: Array,
      default: () => {
        return [{
          name: 'chart',
          data: []
        }]
      }
    },
    xAxis: {
      type: Object,
      default: () => {
        return {
          showSplitLine: true,
          data: ['6:00', '8:00', '10:00', '12:00', '14:00', '16:00', '18:00', '20:00', '22:00', '24:00']
        }
      }
    },
    yAxis: {
      type: Object,
      default: () => {
        return {
          showSplitLine: true,
          name: '(%)'
        }
      }
    },
    interval: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      chart: null,
      option: {
        color: this.color,
        title: {
          top: 20,
          text: this.id,
          textStyle: {
            fontWeight: 'normal',
            fontSize: 15,
            color: '#000'
          },
          left: 0
        },
        grid: {
          top: 80,
          width: '60%',
          bottom: '2%',
          containLabel: true
        },
        legend: {
          tooltip: {
            show: true
          },
          icon: 'rect',
          itemWidth: 14,
          itemHeight: 5,
          itemGap: 13,
          data: [],
          top: '50',
          right: '4%',
          textStyle: {
            fontSize: 12,
            color: this.color
          },
          formatter: (name) => {
            let clientValue = 0
            if (name === 'GPU占用') return name
            this.series.forEach(element => {
              if (element.name === name) {
                clientValue = element.data[element.data.length - 1] ? element.data[element.data.length - 1] : 0
              }
            })
            return name + ':' + clientValue + this.yAxis.name
          }
        },
        backgroundColor: '#fff',
        tooltip: {
          show: true,
          trigger: 'axis'
        },
        xAxis: {
          interval: this.interval,
          type: 'category',
          //   name:'(时间)',
          name: this.xAxis.name,
          // nameLocation: 'start',
          nameGap: '',
          boundaryGap: true,
          axisLine: {
            lineStyle: {
              color: '#57617B'
            }
          },
          axisLabel: {
            interval: this.interval
          },
          splitLine: {
            show: this.xAxis.showSplitLine, // true || false 网格
            interval: this.interval
          },
          axisTick: { length: 5 },
          data: this.xAxis.data
        },
        yAxis: {
          type: 'value',
          name: this.yAxis.name,
          nameGap: 15,
          //   min: 0,
          //   max: 1000,
          splitNumber: 4,
          // interval: 25,
          boundaryGap: true,
          axisTick: {
            show: false
          },
          axisLine: {
            lineStyle: {
              color: '#57617B'
            }
          },
          axisLabel: {
            margin: 10,
            textStyle: {
              color: '#000',
              fontSize: 14,
              fontFamily: 'Microsoft YaHe'
            }
          },
          splitLine: {
            show: this.yAxis.showSplitLine, // true || false 网格
            lineStyle: {
              color: '#000'
            }
          }
        },
        series: []
      }
    }
  },
  watch: {
    xAxis: {
      deep: true,
      handler: function(newVal) {
        const length = newVal.data.length
        let interval = Math.floor(length / 6)
        interval = length % 6 === 0 ? interval - 1 : interval
        this.chart.setOption({
          xAxis: {
            ...newVal,
            axisLabel: {
              interval
            },
            splitLine: {
              interval
            }
          }
        })
      }
    },
    series: {
      deep: true,
      handler(newVal) {
        this.chart.setOption({
          series: newVal
        })
      }
    }
  },
  mounted() {
    this.series.forEach(elemet => {
      this.option.legend.data.push(elemet.name)
    })
    this.$nextTick(() => {
      this.series.forEach(element => {
        this.option.series.push({
          name: element.name,
          type: 'line',
          smooth: true,
          symbol: 'none',
          data: element.data,
          areaStyle: this.showArea ? {
            color: this.color
          } : null,
          lineStyle: {
            normal: {
              width: 1
            }
          },
          itemStyle: {
            normal: {
            }
          }
        })
      })
      this.initChart()
    })
    window.addEventListener('resize', () => {
      if (this.chart) {
        this.chart.resize()
      }
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = this.$echarts.init(this.$el)
      this.chart.setOption(this.option)
    }
  }
}
</script>

pieChart

<template>
  <div />
</template>
<script>
export default {
  props: {
    name: {
      type: String,
      default: ''
    },
    title: {
      type: [String, Number],
      default: ''
    },
    subTitle: {
      type: String,
      default: ''
    },
    color: {
      type: Array,
      default: () => {
        return ['#00B17D', '#D2D2D2']
      }
    },
    chartData: {
      type: Number,
      default: 0
    },
    unit: {
      type: String,
      default: '%'
    }
  },
  data() {
    return {
      chart: null,
      data: [
        {
          value: Number(this.chartData).toFixed(4) * 100,
          name: '',
          label: {
            show: true
          },
          itemStyle: {
            color: this.color[0]
          }
        },
        {
          value: 100 - Number(this.chartData) * 100,
          name: '',
          itemStyle: {
            color: this.color[1]
          }
        }
      ]
    }
  },
  watch: {
    chartData(newVal) {
      if (newVal !== '') {
        this.data[0].value = Number(newVal).toFixed(4) * 100
        this.data[1].value = 100 - Number(newVal) * 100
        this.chart.setOption({
          series: {
            data: this.data
          }
        })
      }
    },
    title(newVal) {
      if (newVal !== '') {
        this.chart.setOption({
          title: {
            text: newVal
          }
        })
      }
    }
  },
  mounted() {
    this.initChart()
    window.addEventListener('resize', () => {
      if (this.chart) {
        this.chart.resize()
      }
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      const _this = this
      this.chart = this.$echarts.init(this.$el)
      this.chart.setOption({
        title: {
          text: _this.title,
          left: 'center',
          bottom: -5,
          textStyle: {
            fontSize: 13,
            color: _this.color[0],
            fontWeight: 'bold'
          },
          subtext: _this.subTitle,
          subtextStyle: {
            fontWeight: 'bold',
            fontSize: 15,
            color: '#000'
          }
        },
        color: _this.color,
        tooltip: {
          show: false
        },
        label: {
        },
        series: [{
          // name: '',
          type: 'pie',
          radius: ['30%', '50%'],
          avoidLableOverLap: false,
          label: {
            normal: {
              show: false,
              position: 'center',
              formatter: `{d} ${this.unit}`,
              fontSize: 12,
              fontWeight: 'bold'
            }
          },
          labelLine: {
            normal: {
              show: false
            }
          },
          clockwise: false,
          legendHoverLinK: false,
          name: _this.name + '占比',
          data: _this.data
        }]
      })
    }
  }
}
</script>

不采用封装的写法

 <!-- 负载路数统计/负载速率统计 -->
    <div class="loadControl" style="margin-top:10px">
      <el-row>
        <el-col class="loadControl-way" :xs="{span: 24}" :sm="{span: 24}" :lg="{span: 12}">
          <div id="loadControl-way-chart" :style="{height:'200px'}" style="margin:10px 0 0 10px" />
        </el-col>
        <el-col class="loadControl-speed" :xs="{span: 24}" :sm="{span: 24}" :lg="{span: 11,offset:1}">
          <div id="loadControl-speed-chart" :style="{height:'200px'}" style="margin:10px 0 0 10px" />
        </el-col>
      </el-row>
    </div>
 mounted() {
    const init = this.$echarts.init
    // 负载数路统计
    this.loadControlWay = init(document.getElementById('loadControl-way-chart'))
    // 负载速率统计
    this.loadControlSpeed = init(document.getElementById('loadControl-speed-chart'))
    // 1.获取首页负载统计信息(负载路数&&负载速率)
    this.getLoadInfo()
    // 负载统计信息(负载路数&&负载速率图表)
    this.timer1 = setInterval(() => {
      this.getLoadInfo()
    }, 2000)
    window.addEventListener('resize', () => {
      this.loadControlWay.resize()
      this.loadControlSpeed.resize()
    })
  },
beforeDestroy() {
    this.clearTimer()
  },
methods: {
	clearTimer() {
      if (this.timer1) {
        clearInterval(this.timer1)
      }
      this.timer1 = null
    },
    // 3.获取首页负载统计信息(负载路数&&负载速率)
    getLoadInfo() {
      if (this.loadControlXAxis.length >= 30) {
        this.loadControlXAxis = [0, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30]
        this.loadControlWayUp.shift()
        this.loadControlWayDown.shift()
        this.loadControlSpeedUp.shift()
        this.loadControlSpeedDown.shift()
      }
      this.loadControlXAxis = [0, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30]

      this.loadControlWayUp.push(25 + Math.floor(Math.random() * 10))
      this.loadControlWayDown.push(15 + Math.floor(Math.random() * 10))

      this.loadControlSpeedUp.push(45 + Math.floor(Math.random() * 10))
      this.loadControlSpeedDown.push(35 + Math.floor(Math.random() * 10))

      this.charLoadControlWay()
      this.charLoadControlSpeed()
    },
     // 图表配置
     // 负载路数统计
    charLoadControlWay() {
      const loadControlWayOption = {
        // 主标题
        title: {
          text: '负载路数统计'
        },
        // grid 直角坐标系内绘图网格
        grid: {
          ...commonEchartStyle.grid
        },
        // 图列
        legend: {
          formatter: function() {
            var index = 0
            var clientlabels = ['上行 :', '下行 :']
            var clientcounts = [9, 1]
            clientlabels.forEach(function(value, i) {
              if (value === name) {
                index = i
              }
            })
            return name + ' : ' + clientcounts[index]
          },
          ...commonEchartStyle.legendCommon
        },
        // x轴
        xAxis: {
          interval: 5,
          type: 'category',
          //   name:'(时间)',
          name: '时间',
          // nameLocation: 'start',
          nameGap: '',
          boundaryGap: true,
          axisLine: {
            lineStyle: {
              color: '#57617B'
            }
          },
          axisLabel: {
            interval: 15
          },
          splitLine: {
            show: false, // true || false 网格
            interval: 5
          },
          axisTick: { length: 5 },
          data: this.loadControlXAxis,
          ...commonEchartStyle.xAxisOption
        },
        // xAxis: {

        //   // data: this.loadControlXAxis,
        //   // ...commonEchartStyle.xAxisOption
        // },
        yAxis: {
          name: '(路)',
          axisLine: {
            lineStyle: {
              color: '#57617B'
            }
          },
          ...commonEchartStyle.yAxisOption
        },

        tooltip: {
          // 移动鼠标显示实时数据
          show: true,
          trigger: 'axis'
        },
        series: [
          {
            ...commonEchartStyle.seriesUpOption,
            areaStyle: {
              color: '#1496D0'
            },
            data: this.loadControlWayUp,
            lineStyle: {
              width: 1
            }
          },
          // 下行
          {
            ...commonEchartStyle.seriesDownOption,
            areaStyle: {
              color: '#00B17D'
            },
            data: this.loadControlWayDown,
            lineStyle: {
              width: 1
            }
          }
        ],
        color: ['#1496D0', '#00B17D']
      }
      this.loadControlWay.setOption(loadControlWayOption)
    },
    // 负载速率统计
    charLoadControlSpeed() {
      const loadControlSpeedOption = {
        title: {
          text: '负载速率统计'
        },
        grid: {
          ...commonEchartStyle.grid
        },
        legend: {
          formatter: function(name) {
            var index = 0
            var clientlabels = ['上行 :', '下行 :']
            var clientcounts = []
            clientcounts.push(1, 2)
            clientlabels.forEach(function(value, i) {
              if (value === name) {
                index = i
              }
            })
            return name + ' : ' + clientcounts[index]
          },
          ...commonEchartStyle.legendCommon
        },
        xAxis: {
          interval: 5,
          type: 'category',
          //   name:'(时间)',
          name: '时间',
          // nameLocation: 'start',
          nameGap: '',
          boundaryGap: true,
          axisLine: {
            lineStyle: {
              color: '#57617B'
            }
          },
          axisLabel: {
            interval: 15
          },
          splitLine: {
            show: false, // true || false 网格
            interval: 5
          },
          axisTick: { length: 5 },
          data: this.loadControlXAxis,
          ...commonEchartStyle.xAxisOption
        },
        yAxis: {
          name: '(MB/S)',
          ...commonEchartStyle.yAxisOption
        },
        tooltip: {
          show: true,
          trigger: 'axis'
        },
        series: [
          // 上行
          {
            ...commonEchartStyle.seriesUpOption,
            areaStyle: {
              color: '#1496D0'
            },
            data: this.loadControlSpeedUp,
            lineStyle: {
              width: 1
            }
          },
          // 下行
          {
            ...commonEchartStyle.seriesDownOption,
            areaStyle: {
              color: '#00B17D'
            },
            data: this.loadControlSpeedDown,
            lineStyle: {
              width: 1
            }
          }
        ],
        color: ['#1496D0', '#00B17D']
      }
      this.loadControlSpeed.setOption(loadControlSpeedOption)
    },
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值