2018-02-17 11:36:17 +08:00
|
|
|
// 所有 G2 图表组件都引用此 mixin
|
|
|
|
|
|
|
|
|
|
import G2 from '@antv/g2'
|
|
|
|
|
// import * as DataSet from '@antv/data-set'
|
|
|
|
|
|
|
|
|
|
// 关闭 G2 的体验改进计划打点请求
|
|
|
|
|
G2.track(false)
|
|
|
|
|
|
|
|
|
|
export default {
|
|
|
|
|
props: {
|
2018-02-17 12:02:47 +08:00
|
|
|
// 宽度 http://antv.alipay.com/zh-cn/g2/3.x/api/chart.html#_width
|
2018-02-17 11:50:04 +08:00
|
|
|
width: {
|
|
|
|
|
type: Number,
|
|
|
|
|
required: false,
|
|
|
|
|
default: 400
|
|
|
|
|
},
|
2018-02-17 12:02:47 +08:00
|
|
|
// 高度 http://antv.alipay.com/zh-cn/g2/3.x/api/chart.html#_height
|
2018-02-17 11:36:17 +08:00
|
|
|
height: {
|
|
|
|
|
type: Number,
|
|
|
|
|
required: false,
|
|
|
|
|
default: 300
|
|
|
|
|
},
|
2018-02-17 12:02:47 +08:00
|
|
|
// 设置图表的内边距 http://antv.alipay.com/zh-cn/g2/3.x/api/chart.html#_padding
|
|
|
|
|
padding: {
|
2018-02-17 11:36:17 +08:00
|
|
|
required: false,
|
2018-02-17 12:02:47 +08:00
|
|
|
default: () => [40, 40, 40, 40]
|
2018-02-17 11:36:17 +08:00
|
|
|
},
|
2018-02-17 12:02:47 +08:00
|
|
|
// 设置图表整体的边框和背景样式 http://antv.alipay.com/zh-cn/g2/3.x/api/chart.html#_background
|
|
|
|
|
background: {
|
|
|
|
|
type: Object,
|
|
|
|
|
required: false,
|
|
|
|
|
default: () => ({})
|
|
|
|
|
},
|
|
|
|
|
// 图表绘图区域的边框和背景样式 http://antv.alipay.com/zh-cn/g2/3.x/api/chart.html#_plotBackground
|
|
|
|
|
plotBackground: {
|
|
|
|
|
type: Object,
|
|
|
|
|
required: false,
|
|
|
|
|
default: () => ({})
|
|
|
|
|
},
|
|
|
|
|
// 图表的宽度自适应开关 http://antv.alipay.com/zh-cn/g2/3.x/api/chart.html#_forceFit
|
2018-02-17 11:36:17 +08:00
|
|
|
forceFit: {
|
|
|
|
|
type: Boolean,
|
|
|
|
|
required: false,
|
|
|
|
|
default: true
|
2018-02-17 12:02:47 +08:00
|
|
|
},
|
|
|
|
|
// 图表动画开关 http://antv.alipay.com/zh-cn/g2/3.x/api/chart.html#_animate
|
|
|
|
|
animate: {
|
|
|
|
|
type: Boolean,
|
|
|
|
|
required: false,
|
|
|
|
|
default: true
|
|
|
|
|
},
|
|
|
|
|
// 设置设备像素比 http://antv.alipay.com/zh-cn/g2/3.x/api/chart.html#_pixelRatio
|
|
|
|
|
pixelRatio: {
|
|
|
|
|
type: Number,
|
|
|
|
|
required: false,
|
|
|
|
|
default: window.devicePixelRatio
|
|
|
|
|
},
|
|
|
|
|
// 图表数据 此 data 非 官方文档中的 data
|
|
|
|
|
data: {
|
|
|
|
|
type: Array,
|
|
|
|
|
required: false,
|
|
|
|
|
default: () => []
|
|
|
|
|
},
|
|
|
|
|
// 高度 开启自动填充父元素 (非G2自带)
|
|
|
|
|
autoHeight: {
|
|
|
|
|
type: Boolean,
|
|
|
|
|
required: false,
|
|
|
|
|
default: false
|
2018-02-17 11:36:17 +08:00
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
data () {
|
|
|
|
|
return {
|
|
|
|
|
// 在页面中不需要再引入 直接使用 this.G2
|
|
|
|
|
G2,
|
|
|
|
|
// 数据处理模块
|
|
|
|
|
// DataSet,
|
|
|
|
|
// 图表实例
|
|
|
|
|
chart: null,
|
|
|
|
|
// 在组件 mounted 后立即初始化图表
|
2018-02-17 12:02:47 +08:00
|
|
|
autoInit: true
|
2018-02-17 11:36:17 +08:00
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
mounted () {
|
|
|
|
|
// 如果设置了在 mounted 后自动初始化 就在这里初始化
|
|
|
|
|
if (this.autoInit) {
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
this.initHandler()
|
|
|
|
|
}, 0)
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
watch: {
|
|
|
|
|
// 数据改变
|
|
|
|
|
data () {
|
|
|
|
|
this.changeData()
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
methods: {
|
|
|
|
|
// 创建图表对象
|
|
|
|
|
creatChart () {
|
2018-02-17 11:50:04 +08:00
|
|
|
// API http://antv.alipay.com/zh-cn/g2/3.x/api/chart.html
|
2018-02-17 11:36:17 +08:00
|
|
|
this.chart = new this.G2.Chart({
|
2018-02-17 11:50:04 +08:00
|
|
|
// 对应图表的 DOM 容器
|
2018-02-17 11:36:17 +08:00
|
|
|
container: this.$refs.chart,
|
2018-02-17 12:02:47 +08:00
|
|
|
// 指定图表的宽度
|
2018-02-17 11:50:04 +08:00
|
|
|
width: this.width,
|
2018-02-17 11:36:17 +08:00
|
|
|
// 高度
|
|
|
|
|
// 设置 autoHeight = true 后取 $refs.chart 的高度
|
|
|
|
|
// 设置 autoHeight = false 后取 this.height
|
|
|
|
|
height: this.autoHeight ? this.G2.DomUtil.getHeight(this.$refs.chart) : this.height,
|
2018-02-17 12:02:47 +08:00
|
|
|
// 设置图表的内边距
|
|
|
|
|
padding: this.padding,
|
|
|
|
|
// 设置图表整体的边框和背景样式
|
|
|
|
|
background: this.background,
|
|
|
|
|
// 图表绘图区域的边框和背景样式
|
|
|
|
|
plotBackground: this.plotBackground,
|
|
|
|
|
// 自动宽度
|
|
|
|
|
forceFit: this.forceFit,
|
|
|
|
|
// 动画开关
|
|
|
|
|
animate: this.animate,
|
|
|
|
|
// 设置设备像素比
|
|
|
|
|
pixelRatio: this.pixelRatio
|
2018-02-17 11:36:17 +08:00
|
|
|
})
|
|
|
|
|
},
|
|
|
|
|
// 重绘大小
|
|
|
|
|
resize (width, height) {
|
|
|
|
|
if (this.chart) {
|
|
|
|
|
this.chart.changeSize(width || this.G2.DomUtil.getWidth(this.$refs.chart), height || this.G2.DomUtil.getHeight(this.$refs.chart))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|