/
layer.ts
241 lines (218 loc) · 7.21 KB
/
layer.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import { CoordinateType } from '@antv/g2/lib/plot/interface';
import * as _ from '@antv/util';
import { registerPlotType } from '../../base/global';
import { LayerConfig } from '../../base/layer';
import PieLayer, { PieViewConfig } from '../pie/layer';
import responsiveMethods from './apply-responsive';
import './apply-responsive/theme';
import * as statisticTemplate from './component/statistic-template';
import * as EventParser from './event';
export interface RingViewConfig extends PieViewConfig {
innerRadius?: number;
statistic?: any; //FIXME: 指标卡
}
export interface RingLayerConfig extends RingViewConfig, LayerConfig {}
interface IAttrs {
[key: string]: any;
}
const G2_GEOM_MAP = {
ring: 'interval',
};
const PLOT_GEOM_MAP = {
interval: 'ring',
};
export default class RingLayer<T extends RingLayerConfig = RingLayerConfig> extends PieLayer<T> {
public static centralId = 0;
public type: string = 'ring';
private statistic: any; // 保存指标卡实例用于响应交互
private statisticClass: string; // 指标卡的class,用于重点文本容器的唯一标识,一个页面多个环图时,共用 class 交互会有问题。
public static getDefaultOptions(): any {
return _.deepMix({}, super.getDefaultOptions(), {
radius: 0.8,
innerRadius: 0.64,
statistic: {
visible: true,
triggerOn: 'mouseenter',
triggerOff: 'mouseleave',
},
});
}
public getOptions(props: T) {
const options = super.getOptions(props);
// @ts-ignore
const defaultOptions = this.constructor.getDefaultOptions();
return _.deepMix({}, options, defaultOptions, props);
}
public beforeInit() {
super.beforeInit();
RingLayer.centralId++;
this.statisticClass = `statisticClassId${RingLayer.centralId}`;
const props = this.options;
/** 响应式图形 */
if (props.responsive && props.padding !== 'auto') {
this.applyResponsive('preRender');
}
}
public afterInit() {
super.afterInit();
/** 处理环图中心文本响应交互的问题 */
if (this.statistic && this.statistic.visible && this.statistic.triggerOn) {
const triggerOnEvent = this.statistic.triggerOn;
this.view.on(
`interval:${triggerOnEvent}`,
_.debounce((e) => {
const displayData = this.parseStatisticData(e.data._origin);
const htmlString = this.getStatisticHtmlString(displayData);
document.getElementsByClassName(this.statisticClass)[0].innerHTML = htmlString;
}, 150)
);
const triggerOffEvent = this.statistic.triggerOff ? this.statistic.triggerOff : 'mouseleave';
this.view.on(
`interval:${triggerOffEvent}`,
_.debounce((e) => {
const totalValue = this.getTotalValue();
const displayData = this.parseStatisticData(totalValue);
const htmlString = this.getStatisticHtmlString(displayData);
document.getElementsByClassName(this.statisticClass)[0].innerHTML = htmlString;
}, 150)
);
}
}
protected geometryParser(dim, type) {
if (dim === 'g2') {
return G2_GEOM_MAP[type];
}
return PLOT_GEOM_MAP[type];
}
protected coord() {
const props = this.options;
const coordConfig = {
type: 'theta' as CoordinateType,
cfg: {
radius: props.radius,
innerRadius: props.innerRadius,
},
};
this.setConfig('coord', coordConfig);
}
protected annotation() {
const annotationConfigs = [];
const props = this.options;
if (props.statistic && props.statistic.visible) {
const statistic = this.drawStatistic(props.statistic);
annotationConfigs.push(statistic);
this.statistic = statistic;
}
this.setConfig('annotations', annotationConfigs);
}
protected parseEvents(eventParser) {
super.parseEvents(EventParser);
}
private drawStatistic(config) {
const statistic: IAttrs = _.deepMix(
{},
{
type: 'html',
top: true,
position: ['50%', '50%'],
triggerOn: 'mouseenter',
},
config
);
/** 中心文本内容 */
let displayData;
if (config.content) {
displayData = config.content;
} else {
/** 用户没有指定文本内容时,默认显示总计 */
const data = this.getTotalValue();
displayData = this.parseStatisticData(data);
}
/** 中心文本显示 */
let htmlString;
if (config.htmlContent) {
htmlString = config.htmlContent(displayData);
} else {
htmlString = this.getStatisticTemplate(displayData);
}
statistic.html = htmlString;
/** 是否响应状态量 */
if (statistic.triggerOn) {
this.setConfig('tooltip', false);
}
return statistic;
}
/** 获取总计数据 */
private getTotalValue(): object {
const props = this.options;
let total = 0;
_.each(props.data, (item) => {
if (typeof item[props.angleField] === 'number') {
total += item[props.angleField];
}
});
const data = {
[props.angleField]: total,
[props.colorField]: '总计',
};
return data;
}
private parseStatisticData(data) {
const props = this.options;
const angleField = props.angleField;
return props.colorField ? { name: data[props.colorField], value: data[angleField] } : data[angleField];
}
private getStatisticTemplate(displayData) {
const size = this.getStatisticSize();
let htmlString;
/** 如果文本内容为string或单条数据 */
if (_.isString(displayData)) {
htmlString = statisticTemplate.getSingleDataTemplate(displayData, this.statisticClass, size);
} else if (_.isObject(displayData) && _.keys(displayData).length === 2) {
/** 如果文本内容为两条数据 */
const content = displayData as IAttrs;
htmlString = statisticTemplate.getTwoDataTemplate(content.name, content.value, this.statisticClass, size);
}
/** 更为复杂的文本要求用户自行制定html模板 */
return htmlString;
}
/** 获取中心文本的htmlString */
private getStatisticHtmlString(_displayData): string {
const triggerOnConfig = this.statistic.triggerOn;
let htmlString: string;
if (_.isString(triggerOnConfig)) {
htmlString = this.getStatisticTemplate(_displayData);
}
if (_.isFunction(triggerOnConfig)) {
htmlString = triggerOnConfig(_displayData);
htmlString = `<div class="ring-guide-html ${this.statisticClass}">${htmlString}</div>`;
}
return htmlString;
}
private applyResponsive(stage) {
const methods = responsiveMethods[stage];
_.each(methods, (r) => {
const responsive = r as IAttrs;
responsive.method(this);
});
}
private getStatisticSize() {
return this.width * this.options.radius;
}
protected getDefaultLabelInnerOffset() {
let size = 0;
const { width, height } = this;
const { padding } = this.options;
if (width < height) {
size = width - padding[1] - padding[3];
} else {
size = height - padding[0] - padding[2];
}
const offset = Math.round((size / 8) * (this.options.radius - this.options.radius * this.options.innerRadius) * -1);
if (isNaN(offset) || offset === Infinity) {
return 0;
}
return offset;
}
}
registerPlotType('ring', RingLayer);