Vue.js+ECharts:图表数据异步加载 - Go语言中文社区

Vue.js+ECharts:图表数据异步加载


流程

1.设置其它的样式,显示一个空的直角坐标轴
2.获取数据
3.填入数据

loading 动画

1.调用 showLoading 方法显示加载动画
2.数据加载完成
3.调用 hideLoading 方法隐藏加载动画

<template>
  <div style="width: 800px;height:400px;"/>
</template>

<script>
import echarts from 'echarts'
import {getData} from '@/api/barchart'
require('echarts/theme/macarons')

export default {
  data() {
    return {
      loading: true
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() { // 异步加载数据
      this.loading = true
      getData().then(response => {
        this.chart.hideLoading()
        // 填入数据
        this.chart.setOption({
          xAxis: {
            data: response.history.time
          },
          series: [{
            // 根据名字对应到相应的系列
            name: '成功',
            data: response.history.success
          },
          {
            // 根据名字对应到相应的系列
            name: '失败',
            data: response.history.fail
          }]
        })
        this.loading = false
      }).catch(() => {
        this.loading = false
      })
    },
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.chart.setOption({ // 显示标题,图例和空的坐标轴
        title: {
          show: true,
          text: '异步数据加载示例',
          textStyle: {
            color: '#000',
            fontWeight: 'bolder'
          },
          left: 'center',
          top: 'top',
          borderWidth: 1,
          shadowColor: 'rgba(0, 0, 0, 0.5)',
          shadowBlur: 9,
          shadowOffsetX: 4,
          shadowOffsetY: 4
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow'
          }
        },
        xAxis: [{
          type: 'category',
          data: [],
          axisTick: {
            alignWithLabel: true
          }
        }],
        yAxis: [{
          type: 'value',
          axisTick: {
            show: false
          }
        }],
        series: [{
          name: '成功',
          type: 'bar',
          stack: 'vistors',
          barWidth: '60%',
          data: []
        }, {
          name: '失败',
          type: 'bar',
          stack: 'vistors',
          barWidth: '60%',
          data: []
        }],
        color: [
          '#5bc49f',
          '#FF9F7F'
        ]
      })
      this.chart.showLoading()
    }
  }
}
</script>

在这里插入图片描述
在这里插入图片描述
参考:
异步数据加载和更新

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/qq_44419617/article/details/100097252
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2020-03-01 17:46:17
  • 阅读 ( 1225 )
  • 分类:前端

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢