vue antV G2-3.X组件化

发布时间:2019-05-17 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了vue antV G2-3.X组件化脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

从网上看到 阿里系的图表 antv 觉得非常不错,就想整合到vue中使用。参考了Vuejs2.X组件化-阿里的G2图表组件

// 2019.03.30更新
附上 G2的快速上手 供大家参考

// 2019.04.24
重亲测试了一遍 demo没有问题

安装

npm install @antv/g2 --save

创建vue组件 components/G2Line.vue

<template>
  <div :id="id"></div>
</template>

<script>
import G2 from '@antv/g2'
export default {
  data () {
    return {
      chart: null
    }
  },
  props: {
    charData: {
      type: Array,
      default: function () {
        return {
          data: []
        }
      }
    },
    id: String
  },
  // 如果使用serverData传过来的静态数据 请使用mounted()方法 并注释掉watch
  mounted () {
    this.drawChart()
  },
  // 监听API接口传过来的数据 使用watch  2018-08-21更新
  // watch: {
      // charData: function (val, oldVal) {    // 监听charData,当发生变化时,触发这个回调函数绘制图表
      // console.log('new: %s, old: %s', val, oldVal);
      // this.drawChart(val);
    // }
  },
  methods: {
    drawChart: function () {
      // 2019.03.30 更新 destory方法已被废弃
      // this.chart && this.chart.destory()
      this.chart = new G2.Chart({
        container: this.id,
        width: 600,
        height: 300
      })
      this.chart.source(this.charData)
      this.chart.scale('value', {
        min: 0
      })
      this.chart.scale('year', {
        range: [0, 1]
      })
      this.chart.tooltip({
        crosshairs: {
          type: 'line'
        }
      })
      this.chart.line().position('year*value')
      this.chart.point().position('year*value').size(4).shape('circle').style({
        stroke: '#fff',
        lineWidth: 1
      })
      this.chart.render()
    }
  }
}
</script>

修改HelloWorld.vue 引用组件

<template>
  <div>
    <g2-line :charData="serverData" :id="'c1'"></g2-line>
  </div>
</template>

<script>
import G2Line from './G2Line.vue'
export default {
  components: {
    G2Line
  },
  data () {
    return {
      serverData: [{
        year: '2010',
        value: 3
      }, {
        year: '2011',
        value: 4
      }, {
        year: '2012',
        value: 3.5
      }, {
        year: '2013',
        value: 5
      }, {
        year: '2014',
        value: 4.9
      }, {
        year: '2015',
        value: 6
      }, {
        year: '2016',
        value: 7
      }, {
        year: '2017',
        value: 9
      }, {
        year: '2018',
        value: 13
      }]
    }
  },
  methods: {
    // 此处省略从服务器获取数据并且赋值给this.serverData
    // 推荐使用axios请求接口
  }
}
</script>

效果

vue antV G2-3.X组件化

脚本宝典总结

以上是脚本宝典为你收集整理的vue antV G2-3.X组件化全部内容,希望文章能够帮你解决vue antV G2-3.X组件化所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:Vue