@gm-pc/vision 1.25.0-beta.0 → 1.25.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,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.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.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,209 +1,209 @@
1
- import _ from 'lodash'
2
- import { Instance } from '../../../types/common'
3
- import { flatData, getPositionAndColor } from './utils'
4
- import themes from '../../../theme'
5
-
6
- /**
7
- * Geek Blue 极客蓝 #5B8FF9
8
- * Cyan 翡翠绿 #5AD8A6
9
- * Grey 商务灰 #5D7092
10
- * Sunrise Yellow 旭日黄 #F6BD16
11
- * Dust Red 薄暮红 #E86452
12
- * Daybreak Blue 破晓蓝 #6DC8EC
13
- * Golden Purple 罗兰紫 #945FB9
14
- * Sunset Orange 落日橘 #FF9845
15
- * Dark Green 天水青 #1E9493
16
- * Magenta 桃花粉 #FF99C3
17
- */
18
- const colors = [
19
- '#5B8FF9',
20
- '#5AD8A6',
21
- '#5D7092',
22
- '#F6BD16',
23
- '#E86452',
24
- '#6DC8EC',
25
- '#945FB9',
26
- '#FF9845',
27
- '#1E9493',
28
- '#FF99C3',
29
- ]
30
-
31
- /**
32
- * - 几何标记 Geometry
33
- * - 度量 Scale
34
- * - 坐标系 Coordinate
35
- * - 可视化组件 Component
36
- * - 包括坐标轴 Axis,
37
- * - 图例 Legend,
38
- * - 提示信息 Tooltip,
39
- * - 图形标记 Annotation,
40
- * - 滑动条 Slider 等。
41
- */
42
-
43
- function geometry(params: Instance) {
44
- const { chart, options } = params
45
-
46
- const { position, color } = getPositionAndColor(params)
47
-
48
- let geometry
49
- if (color) {
50
- geometry = chart.line().position(position).color(color)
51
- } else {
52
- geometry = chart.line().position(position)
53
- }
54
-
55
- return { ...params, geometry }
56
- }
57
-
58
- function scale(params: Instance) {
59
- const { chart, options } = params
60
-
61
- const { position, data, xFieldName, yFieldName, scale = {} } = options
62
- if (_.isArray(position) || !position) {
63
- if (data.length) {
64
- const x = position ? position[0].split('*')[0] : xFieldName
65
- const d = flatData(data, x, yFieldName)
66
- chart.scale({
67
- value: {
68
- nice: true,
69
- formatter: (value) => value,
70
- },
71
- ...scale,
72
- })
73
- chart.scale({
74
- [x]: {
75
- nice: true,
76
- formatter: (value) => value,
77
- },
78
- ...scale,
79
- })
80
- chart.data(d)
81
- }
82
- } else {
83
- const y = position.split('*')[1]
84
- // TODO 通用配置 y轴nice: true
85
- chart.scale({
86
- [y]: {
87
- nice: true,
88
- },
89
- ...scale,
90
- })
91
- }
92
-
93
- return params
94
- }
95
-
96
- function axis(params: Instance) {
97
- const { chart, options } = params
98
- const { position, color } = options
99
- if (_.isArray(position)) {
100
- chart.axis('value', {
101
- label: {
102
- formatter: (text) => text,
103
- },
104
- })
105
- }
106
- return params
107
- }
108
-
109
- function annotation(params: Instance) {
110
- const { chart, options } = params
111
- const { data, annotation } = options
112
- // 辅助线
113
- if (annotation) {
114
- const { text, value } = annotation
115
- const yPosition = typeof value === 'string' ? value : value(data)
116
- chart.annotation().line({
117
- start: ['min', yPosition],
118
- end: ['max', yPosition],
119
- style: {
120
- stroke: 'red',
121
- lineWidth: 1,
122
- lineDash: [3, 3],
123
- },
124
- text: {
125
- position: 'right',
126
- content: text,
127
- },
128
- })
129
- }
130
-
131
- return params
132
- }
133
-
134
- function tooltip(params: Instance) {
135
- const { chart } = params
136
- // tooltip样式
137
- chart.tooltip({
138
- showMarkers: true,
139
- showCrosshairs: true,
140
- shared: true,
141
- domStyles: {
142
- 'g2-tooltip-marker': {
143
- width: '10px',
144
- height: '2px',
145
- borderRadius: '0%',
146
- display: 'inline-block',
147
- marginBottom: '4px',
148
- marginRight: '6px',
149
- },
150
- },
151
- })
152
-
153
- return params
154
- }
155
-
156
- function legend(params: Instance) {
157
- const { chart, options } = params
158
- const { legend } = options
159
-
160
- if (legend !== undefined) {
161
- chart.legend(legend)
162
- }
163
-
164
- return params
165
- }
166
-
167
- function theme(params: Instance) {
168
- const { chart, options, geometry } = params
169
- const { theme } = options
170
- if (theme) {
171
- const themeConfig = themes[theme as 'ocean'].chart
172
-
173
- if (themeConfig) {
174
- // ---------- geometry配置 ----------------
175
- geometry!.shape(themeConfig.line.shape).style(themeConfig.line.style)
176
-
177
- // ---------- tiptop配置 ----------------
178
- chart.tooltip({
179
- showMarkers: true,
180
- showCrosshairs: true,
181
- shared: true,
182
- domStyles: {
183
- 'g2-tooltip-marker': {
184
- display: 'none',
185
- },
186
- },
187
- })
188
-
189
- // ---------- area图 ----------------
190
- if (themeConfig.line.area) {
191
- const { position, color } = getPositionAndColor(params)
192
- chart
193
- .area()
194
- .position(position)
195
- .color(color)
196
- .shape(themeConfig.line.area.shape)
197
- .style({
198
- fill: themeConfig.line.area.fill,
199
- fillOpacity: 1,
200
- })
201
- }
202
- }
203
- }
204
- return params
205
- }
206
-
207
- export function processor(params: Instance) {
208
- return _.flow(geometry, annotation, tooltip, scale, axis, legend, theme)(params)
209
- }
1
+ import _ from 'lodash'
2
+ import { Instance } from '../../../types/common'
3
+ import { flatData, getPositionAndColor } from './utils'
4
+ import themes from '../../../theme'
5
+
6
+ /**
7
+ * Geek Blue 极客蓝 #5B8FF9
8
+ * Cyan 翡翠绿 #5AD8A6
9
+ * Grey 商务灰 #5D7092
10
+ * Sunrise Yellow 旭日黄 #F6BD16
11
+ * Dust Red 薄暮红 #E86452
12
+ * Daybreak Blue 破晓蓝 #6DC8EC
13
+ * Golden Purple 罗兰紫 #945FB9
14
+ * Sunset Orange 落日橘 #FF9845
15
+ * Dark Green 天水青 #1E9493
16
+ * Magenta 桃花粉 #FF99C3
17
+ */
18
+ const colors = [
19
+ '#5B8FF9',
20
+ '#5AD8A6',
21
+ '#5D7092',
22
+ '#F6BD16',
23
+ '#E86452',
24
+ '#6DC8EC',
25
+ '#945FB9',
26
+ '#FF9845',
27
+ '#1E9493',
28
+ '#FF99C3',
29
+ ]
30
+
31
+ /**
32
+ * - 几何标记 Geometry
33
+ * - 度量 Scale
34
+ * - 坐标系 Coordinate
35
+ * - 可视化组件 Component
36
+ * - 包括坐标轴 Axis,
37
+ * - 图例 Legend,
38
+ * - 提示信息 Tooltip,
39
+ * - 图形标记 Annotation,
40
+ * - 滑动条 Slider 等。
41
+ */
42
+
43
+ function geometry(params: Instance) {
44
+ const { chart, options } = params
45
+
46
+ const { position, color } = getPositionAndColor(params)
47
+
48
+ let geometry
49
+ if (color) {
50
+ geometry = chart.line().position(position).color(color)
51
+ } else {
52
+ geometry = chart.line().position(position)
53
+ }
54
+
55
+ return { ...params, geometry }
56
+ }
57
+
58
+ function scale(params: Instance) {
59
+ const { chart, options } = params
60
+
61
+ const { position, data, xFieldName, yFieldName, scale = {} } = options
62
+ if (_.isArray(position) || !position) {
63
+ if (data.length) {
64
+ const x = position ? position[0].split('*')[0] : xFieldName
65
+ const d = flatData(data, x, yFieldName)
66
+ chart.scale({
67
+ value: {
68
+ nice: true,
69
+ formatter: (value) => value,
70
+ },
71
+ ...scale,
72
+ })
73
+ chart.scale({
74
+ [x]: {
75
+ nice: true,
76
+ formatter: (value) => value,
77
+ },
78
+ ...scale,
79
+ })
80
+ chart.data(d)
81
+ }
82
+ } else {
83
+ const y = position.split('*')[1]
84
+ // TODO 通用配置 y轴nice: true
85
+ chart.scale({
86
+ [y]: {
87
+ nice: true,
88
+ },
89
+ ...scale,
90
+ })
91
+ }
92
+
93
+ return params
94
+ }
95
+
96
+ function axis(params: Instance) {
97
+ const { chart, options } = params
98
+ const { position, color } = options
99
+ if (_.isArray(position)) {
100
+ chart.axis('value', {
101
+ label: {
102
+ formatter: (text) => text,
103
+ },
104
+ })
105
+ }
106
+ return params
107
+ }
108
+
109
+ function annotation(params: Instance) {
110
+ const { chart, options } = params
111
+ const { data, annotation } = options
112
+ // 辅助线
113
+ if (annotation) {
114
+ const { text, value } = annotation
115
+ const yPosition = typeof value === 'string' ? value : value(data)
116
+ chart.annotation().line({
117
+ start: ['min', yPosition],
118
+ end: ['max', yPosition],
119
+ style: {
120
+ stroke: 'red',
121
+ lineWidth: 1,
122
+ lineDash: [3, 3],
123
+ },
124
+ text: {
125
+ position: 'right',
126
+ content: text,
127
+ },
128
+ })
129
+ }
130
+
131
+ return params
132
+ }
133
+
134
+ function tooltip(params: Instance) {
135
+ const { chart } = params
136
+ // tooltip样式
137
+ chart.tooltip({
138
+ showMarkers: true,
139
+ showCrosshairs: true,
140
+ shared: true,
141
+ domStyles: {
142
+ 'g2-tooltip-marker': {
143
+ width: '10px',
144
+ height: '2px',
145
+ borderRadius: '0%',
146
+ display: 'inline-block',
147
+ marginBottom: '4px',
148
+ marginRight: '6px',
149
+ },
150
+ },
151
+ })
152
+
153
+ return params
154
+ }
155
+
156
+ function legend(params: Instance) {
157
+ const { chart, options } = params
158
+ const { legend } = options
159
+
160
+ if (legend !== undefined) {
161
+ chart.legend(legend)
162
+ }
163
+
164
+ return params
165
+ }
166
+
167
+ function theme(params: Instance) {
168
+ const { chart, options, geometry } = params
169
+ const { theme } = options
170
+ if (theme) {
171
+ const themeConfig = themes[theme as 'ocean'].chart
172
+
173
+ if (themeConfig) {
174
+ // ---------- geometry配置 ----------------
175
+ geometry!.shape(themeConfig.line.shape).style(themeConfig.line.style)
176
+
177
+ // ---------- tiptop配置 ----------------
178
+ chart.tooltip({
179
+ showMarkers: true,
180
+ showCrosshairs: true,
181
+ shared: true,
182
+ domStyles: {
183
+ 'g2-tooltip-marker': {
184
+ display: 'none',
185
+ },
186
+ },
187
+ })
188
+
189
+ // ---------- area图 ----------------
190
+ if (themeConfig.line.area) {
191
+ const { position, color } = getPositionAndColor(params)
192
+ chart
193
+ .area()
194
+ .position(position)
195
+ .color(color)
196
+ .shape(themeConfig.line.area.shape)
197
+ .style({
198
+ fill: themeConfig.line.area.fill,
199
+ fillOpacity: 1,
200
+ })
201
+ }
202
+ }
203
+ }
204
+ return params
205
+ }
206
+
207
+ export function processor(params: Instance) {
208
+ return _.flow(geometry, annotation, tooltip, scale, axis, legend, theme)(params)
209
+ }