@gm-pc/vision 1.27.0 → 1.27.1-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,135 +1,139 @@
1
- import { Chart } from '@antv/g2'
2
- import { bind } from 'size-sensor'
3
- import { ChartOptions, Instance } from '../../types/common'
4
- import { getContainerSize } from '../../common/utils/dom'
5
- import _ from 'lodash'
6
-
7
- export abstract class Base {
8
- chart: Chart
9
-
10
- options: ChartOptions
11
-
12
- container: HTMLElement
13
-
14
- unbind: (() => void) | undefined
15
-
16
- abstract name: string
17
-
18
- /**
19
- * 处理配置,geometry annotation axis...
20
- * */
21
- abstract getProcessor(): (params: Instance) => Instance
22
-
23
- constructor(container: string | HTMLElement, options: ChartOptions) {
24
- this.container =
25
- typeof container === 'string'
26
- ? (document.querySelector(container)! as HTMLElement)
27
- : container
28
- this.options = options
29
- this.chart = null!
30
- this.unbind = undefined
31
-
32
- this.createG2()
33
-
34
- this.bindSizeSensor()
35
- }
36
-
37
- createG2() {
38
- const { width, height, padding } = this.options
39
- // 通用配置
40
- this.chart = new Chart({
41
- container: this.container,
42
- width,
43
- height,
44
- autoFit: false,
45
- appendPadding: padding || [24, 24, 24, 24],
46
- })
47
- }
48
-
49
- initConfig() {
50
- let { data, theme = '', position } = this.options
51
- data = this.processData(data, position)
52
-
53
- this.chart.data(data).theme(theme)
54
-
55
- const processor = this.getProcessor()
56
- processor({
57
- chart: this.chart,
58
- options: this.options,
59
- })
60
- }
61
-
62
- processData(data: any, position: string) {
63
- if (position) {
64
- const [x, y] = position.split('*')
65
- // 数字要转成number类型,antv要求
66
- return _.map(data, (d) => {
67
- d[x] = _.isNaN(+d[x]) ? d[x] : +d[x]
68
- d[y] = _.isNaN(+d[y]) ? d[y] : +d[y]
69
- return d
70
- })
71
- }
72
- return data
73
- }
74
-
75
- render() {
76
- this.chart.clear()
77
-
78
- this.initConfig()
79
-
80
- this.chart.render()
81
- }
82
-
83
- updateOptions(options: ChartOptions) {
84
- this.options = _.merge(options, this.options)
85
- this.render()
86
- }
87
-
88
- updateData(data: any) {
89
- this.options.data = data
90
- this.render()
91
- }
92
-
93
- unbindSizeSensor() {
94
- if (this.unbind) {
95
- this.unbind()
96
- this.unbind = undefined
97
- }
98
- }
99
-
100
- bindSizeSensor() {
101
- if (this.unbind) {
102
- return
103
- }
104
-
105
- const { autoFit = true } = this.options
106
- if (autoFit) {
107
- this.unbind = bind(this.container, () => {
108
- // 获取最新的宽高信息
109
- const { width, height } = getContainerSize(this.container)
110
-
111
- // 主要是防止绑定的时候触发 resize 回调
112
- if (width !== this.chart.width || height !== this.chart.height) {
113
- this.triggerResize()
114
- }
115
- })
116
- }
117
- }
118
-
119
- triggerResize() {
120
- // 解决pie宽高变化后导致annotation没有重写render
121
- if (this.name === 'pie') {
122
- this.render()
123
- }
124
- this.chart.forceFit()
125
- }
126
-
127
- destroy() {
128
- this.unbindSizeSensor()
129
- this.chart.destroy()
130
- }
131
-
132
- resize() {
133
- this.chart.forceFit()
134
- }
135
- }
1
+ import { Chart } from '@antv/g2'
2
+ import { bind } from 'size-sensor'
3
+ import { ChartOptions, Instance } from '../../types/common'
4
+ import { getContainerSize } from '../../common/utils/dom'
5
+ import _ from 'lodash'
6
+
7
+ export abstract class Base {
8
+ chart: Chart
9
+
10
+ options: ChartOptions
11
+
12
+ container: HTMLElement
13
+
14
+ unbind: (() => void) | undefined
15
+
16
+ abstract name: string
17
+
18
+ /**
19
+ * 处理配置,geometry annotation axis...
20
+ * */
21
+ abstract getProcessor(): (params: Instance) => Instance
22
+
23
+ constructor(container: string | HTMLElement, options: ChartOptions) {
24
+ this.container =
25
+ typeof container === 'string'
26
+ ? (document.querySelector(container)! as HTMLElement)
27
+ : container
28
+ this.options = options
29
+ this.chart = null!
30
+ this.unbind = undefined
31
+
32
+ this.createG2()
33
+
34
+ this.bindSizeSensor()
35
+ }
36
+
37
+ createG2() {
38
+ const { width, height, padding } = this.options
39
+ // 通用配置
40
+ this.chart = new Chart({
41
+ container: this.container,
42
+ width,
43
+ height,
44
+ autoFit: false,
45
+ appendPadding: padding || [24, 24, 24, 24],
46
+ })
47
+ }
48
+
49
+ initConfig() {
50
+ let { data, theme = '', position } = this.options
51
+ data = this.processData(data, position)
52
+
53
+ this.chart.data(data).theme(theme)
54
+
55
+ const processor = this.getProcessor()
56
+ processor({
57
+ chart: this.chart,
58
+ options: this.options,
59
+ })
60
+ }
61
+
62
+ processData(data: any, position: string) {
63
+ if (position) {
64
+ const [x, y] = position.split('*')
65
+ // 数字要转成number类型,antv要求
66
+ return _.map(data, (d) => {
67
+ d[x] = _.isNaN(+d[x]) ? d[x] : +d[x]
68
+ d[y] = _.isNaN(+d[y]) ? d[y] : +d[y]
69
+ return d
70
+ })
71
+ }
72
+ return data
73
+ }
74
+
75
+ render() {
76
+ this.chart.clear()
77
+
78
+ this.initConfig()
79
+
80
+ this.chart.render()
81
+ }
82
+
83
+ updateOptions(options: ChartOptions) {
84
+ // this.options = _.merge(options, this.options)
85
+ this.options = {
86
+ ...this.options,
87
+ ...options,
88
+ }
89
+ this.render()
90
+ }
91
+
92
+ updateData(data: any) {
93
+ this.options.data = data
94
+ this.render()
95
+ }
96
+
97
+ unbindSizeSensor() {
98
+ if (this.unbind) {
99
+ this.unbind()
100
+ this.unbind = undefined
101
+ }
102
+ }
103
+
104
+ bindSizeSensor() {
105
+ if (this.unbind) {
106
+ return
107
+ }
108
+
109
+ const { autoFit = true } = this.options
110
+ if (autoFit) {
111
+ this.unbind = bind(this.container, () => {
112
+ // 获取最新的宽高信息
113
+ const { width, height } = getContainerSize(this.container)
114
+
115
+ // 主要是防止绑定的时候触发 resize 回调
116
+ if (width !== this.chart.width || height !== this.chart.height) {
117
+ this.triggerResize()
118
+ }
119
+ })
120
+ }
121
+ }
122
+
123
+ triggerResize() {
124
+ // 解决pie宽高变化后导致annotation没有重写render
125
+ if (this.name === 'pie') {
126
+ this.render()
127
+ }
128
+ this.chart.forceFit()
129
+ }
130
+
131
+ destroy() {
132
+ this.unbindSizeSensor()
133
+ this.chart.destroy()
134
+ }
135
+
136
+ resize() {
137
+ this.chart.forceFit()
138
+ }
139
+ }