vue+echars动态渲染折线图

<template>
  <div class="tm-card">
    <div class="card-body">
      <div class="chart-box">
        <div id="traffic_line_chart" style="height:300px; width:100%" />
      </div>
    </div>
  </div>

</template>

<script>
import echarts from 'echarts'
import { EleResize } from '@/utils/esresize.js'
import { getRealtimeflowApi } from '@/api/dashboard'

export default {
  name: 'RealTimeTraffic',
  props: {
    index: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      chart: null
    }
  },
  mounted() {
    this.initChart()
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    // 图表初始化
    initChart() {
      const chartOption = {
        color: ['#3398DB'],
        title: {
          show: false,
          text: '实时流量'
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross',
            label: {
              backgroundColor: '#6a7985'
            }
          }
        },
        legend: {
          show: false,
          data: ['实时流量']
        },
        toolbox: {
          show: false
        },
        grid: {
          top: '3%',
          right: '3%',
          bottom: '3%',
          left: '3%',
          containLabel: true
        },
        xAxis: [
          {
            type: 'category',
            boundaryGap: false,
            data: []
          }
        ],
        yAxis: [
          {
            type: 'value'
          }
        ],
        series: [
          {
            name: '实时流量',
            type: 'line',
            symbol: 'none',
            stack: '总量',
            data: []
          }
        ]
      }
      this.chart = echarts.init(document.getElementById('traffic_line_chart'))
      this.chart.setOption(chartOption)
      const resizeDiv = document.getElementById('traffic_line_chart')
      var that = this
      const listener = function() {
        that.chart.resize()
      }
      EleResize.on(resizeDiv, listener)
      getRealtimeflowApi({ DataView: '每60秒刷新一次' }).then(res => {
        res.data.series.data = res.data.series.map((item, index) => {
          return {
            data: item
          }
        })
        this.chart.setOption({
          xAxis: {
            data: res.data.xAxis
          },
          series: res.data.series
        })
      })
    }
  }
}
</script>

在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值