/
adaptor.ts
188 lines (165 loc) · 4.46 KB
/
adaptor.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import { deepMix, isNumber } from '@antv/util';
import { Params } from '../../core/adaptor';
import { interaction, animation, theme, tooltip } from '../../adaptor/common';
import { flow, pick } from '../../utils';
import { AXIS_META_CONFIG_KEYS } from '../../constant';
import { BulletOptions } from './types';
import { transformData } from './utils';
/**
* field 字段
* @param params
*/
function field(params: Params<BulletOptions>): Params<BulletOptions> {
const { chart, options } = params;
const { bulletStyle, targetField, rangeField, measureField, xField, layout } = options;
const { range, measure, target } = bulletStyle;
// 处理数据
const { min, max, ds } = transformData(options);
// 需要统一比列尺
chart.scale({
[measureField]: {
min,
max,
},
[rangeField]: {
sync: `${measureField}`,
},
[targetField]: {
sync: `${measureField}`,
},
});
chart.data(ds);
chart.axis(`${rangeField}`, false);
chart.axis(`${targetField}`, false);
const rangeGeometry = chart
.interval()
.position(`${xField}*${rangeField}`)
.adjust('stack')
.color('index')
.tooltip(false);
if (isNumber(range.size)) {
rangeGeometry.size(range.size);
}
if (range.color) {
rangeGeometry.color('index', range.color);
}
const measureGeometry = chart
.interval()
.position(`${xField}*${measureField}`)
.label('measure')
.adjust('stack')
.color('index');
if (isNumber(measure.size)) {
measureGeometry.size(measure.size);
}
if (measure.color) {
measureGeometry.color('index', measure.color);
}
const targetGeometry = chart.point().position(`${xField}*${targetField}`).shape('hyphen');
if (isNumber(target.size)) {
targetGeometry.size(target.size / 2); // 是半径
}
if (target.color) {
targetGeometry.color('index', target.color);
}
// 水平的时候,要转换坐标轴和换图形为 line
if (layout === 'horizontal') {
chart.coordinate().transpose();
targetGeometry.shape('line');
}
return params;
}
/**
* meta 配置
* @param params
*/
function meta(params: Params<BulletOptions>): Params<BulletOptions> {
const { chart, options } = params;
const { xAxis, yAxis, meta, targetField, rangeField, measureField, xField } = options;
if (meta) {
const scales = deepMix({}, meta, {
[xField]: pick(xAxis, AXIS_META_CONFIG_KEYS),
[measureField]: pick(yAxis, AXIS_META_CONFIG_KEYS),
[targetField]: {
sync: `${measureField}`,
},
[rangeField]: {
sync: `${measureField}`,
},
});
chart.scale(scales);
}
return params;
}
/**
* axis 配置
* @param params
*/
function axis(params: Params<BulletOptions>): Params<BulletOptions> {
const { chart, options } = params;
const { xAxis, yAxis, xField, measureField } = options;
// 为 false 则是不显示轴
if (xAxis === false) {
chart.axis(`${xField}`, false);
} else {
chart.axis(`${xField}`, xAxis);
}
if (yAxis === false) {
chart.axis(`${measureField}`, false);
} else {
chart.axis(`${measureField}`, yAxis);
}
return params;
}
/**
* legend 配置
* @param params
*/
function legend(params: Params<BulletOptions>): Params<BulletOptions> {
const { chart, options } = params;
const { legend } = options;
chart.removeInteraction('legend-filter');
chart.legend(legend);
return params;
}
/**
* label 配置
* @param params
*/
function label(params: Params<BulletOptions>): Params<BulletOptions> {
const { chart, options } = params;
const { label, measureField } = options;
const measureGeometry = chart.geometries[1];
measureGeometry.label(`${measureField}`, label);
return params;
}
/**
* style 配置
* @param params
*/
function style(params: Params<BulletOptions>): Params<BulletOptions> {
const { chart, options } = params;
const { bulletStyle } = options;
const { range, measure, target } = bulletStyle;
// 按绘图顺序
const [rangeGeometry, measureGeometry, targetGeometry] = chart.geometries;
if (range.style) {
rangeGeometry.style(range.style);
}
if (measure.style) {
measureGeometry.style(measure.style);
}
if (target.style) {
targetGeometry.style(target.style);
}
return params;
}
/**
* 子弹图适配器
* @param chart
* @param options
*/
export function adaptor(params: Params<BulletOptions>) {
// flow 的方式处理所有的配置到 G2 API
flow(field, meta, axis, legend, theme, label, style, tooltip, interaction, animation)(params);
}