Files
mes-ui-d2/docs/zh/components/charts.md
liyang e5039f52d8 增加备注
Former-commit-id: a8e46867e9522000e42727a9d38d6401090b1ded [formerly a8e46867e9522000e42727a9d38d6401090b1ded [formerly a8e46867e9522000e42727a9d38d6401090b1ded [formerly a8e46867e9522000e42727a9d38d6401090b1ded [formerly fade37895c6fba1290720b0bc7c2e4233f325f04 [formerly fdaebd2e69eb598fcdef739cb74c0e8b875f0e0c]]]]]
Former-commit-id: fdb111a4739a4ae9ae9ddd06bbc95aa26399624a
Former-commit-id: a1faaae1260729c1f341f545df9106be0888d943
Former-commit-id: 0d8b37c2620cf143bf9d45857f288ce8dd352859 [formerly 1dbbbf47e3fcb00305effc9f01612301d9daefb5]
Former-commit-id: 6bd33287a1806bc4cdc2311f89debf75a237a7ab
Former-commit-id: 8d1aca3bfdc67d3928d55e4a54947751915c5bae
Former-commit-id: de2fea42a64c5cb5bc135dc390fc76e032ecde8b
Former-commit-id: 5cd349cefd282d9a448b6697079bb2e20d9c3b30
Former-commit-id: 41a3020904a2ed4d81406c1434ab350edb1d59d0
2018-07-22 19:50:20 +08:00

76 lines
2.6 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 图表 [ G2 ] 旧
::: tip
此文档仅对 `1.1.0` 及其以下版本有效,`1.1.1` 开始变更了图表库
:::
## 介绍
D2Admin 集成了由蚂蚁金服出品的 **G2** 图表库
## 实现方式
`src/components/charts/register.js` 为注册图表组件的文件
`src/components/charts/G2` 为图表组件存放位置
`src/components/charts/G2/mixins/G2.js` 是图表最主要的文件,这是一个所有的图表组件都会使用的 mixin这个 mixin 主要有以下用途
- 将 G2 和 DataSet 绑定到 data 上,方便组件使用,省去重复 `import G2 from '@antv/g2'`
- 将 [G2 Chart类](http://antv.alipay.com/zh-cn/g2/3.x/api/chart.html#_Chart) 的属性全部暴露为 Vue 组件参数,这些参数会在初始化图表时用到
- 提供了额外的设置参数,比如自动高度,自动初始化,初始化延时
- 关闭 G2 的体验改进计划打点请求
- data 上的 chart 对象
- 自动初始化(或者不初始化)图表
- `creatChart` 方法,根据参数设置生成 data 上的 chart 对象
- `resize` 方法
在图表组件中使用这个 mixin 示例
``` vue
<template>
<!-- 如果需要开启自动高度功能 需要在这里设置 style="height: 100%;" -->
<div ref="chart" style="height: 100%;"></div>
</template>
<script>
// 引入公用 mixin
import G2 from '@/components/charts/G2/mixins/G2.js'
export default {
mixins: [
G2
],
methods: {
// 初始化图表
init () {
// mixin 中提供 creatChart 方法,这部分每个图表都一样
this.creatChart()
// 本组件的特殊设置 这部分每个图表不一样 你只需要改这部分内容
this.chart.source(this.data)
this.chart.coord().transpose()
this.chart.interval().position('x*y')
// 最后一步 渲染图表 这部分每个图表都一样
this.chart.render()
},
// 数据源改变 重新渲染新的数据
changeData () {
this.chart.changeData(this.data)
}
}
}
</script>
```
上面的代码段展示了如何使用 mixin 快速制作一个图表组件,只需根据某个图表的个性化需要,在组件中重新定义 `init` 和 `changeData` 方法即可
你可以修改这个 mixin 去实现更多的功能,同时影响所有的图表组件
::: tip
这只仅仅是作者个人对于图表封装的一个实现思路
:::
## 为什么没有选择其他产品
G2 完全可以胜任一般的后台界面报表图表需求,而且官网文档清晰友好
如果你需要更酷炫的图表,也完全可以剔除集成的库,换用 百度的[echarts](http://echarts.baidu.com/) 或者超级强大的 [d3.js](https://d3js.org/)