Repository navigation
Expand file tree
/
Copy pathradar.js
More file actions
515 lines (454 loc) · 18.4 KB
/
Copy pathradar.js
File metadata and controls
515 lines (454 loc) · 18.4 KB
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
/**
* echarts图表类:雷达图
*
* @desc echarts基于Canvas,纯Javascript图表库,提供直观,生动,可交互,可个性化定制的数据统计图表。
* @author Neil (杨骥, yangji01@baidu.com)
*
*/
define(function(require) {
/**
* 构造函数
* @param {Object} messageCenter echart消息中心
* @param {ZRender} zr zrender实例
* @param {Object} series 数据
* @param {Object} component 组件
*/
function Radar(messageCenter, zr, option, component) {
// 基类装饰
var ComponentBase = require('../component/base');
ComponentBase.call(this, zr);
// 可计算特性装饰
var CalculableBase = require('./calculableBase');
CalculableBase.call(this, zr, option);
var ecConfig = require('../config');
var ecData = require('../util/ecData');
var zrColor = require('zrender/tool/color');
var self = this;
self.type = ecConfig.CHART_TYPE_RADAR;
var series; // 共享数据源,不要修改跟自己无关的项
var serie;
var _zlevelBase = self.getZlevelBase();
var _queryTarget;
var _dropBoxList;
var _symbol = [
'circle', 'rectangle', 'triangle', 'diamond',
'emptyCircle', 'emptyRectangle', 'emptyTriangle', 'emptyDiamond'
];
var _radarDataCounter;
/**
* 绘制图形
*/
function _buildShape() {
self.selectedMap = {};
_dropBoxList = [];
_radarDataCounter = 0;
for (var i = 0, l = series.length; i < l ; i ++) {
if (series[i].type == ecConfig.CHART_TYPE_RADAR) {
serie = self.reformOption(series[i]);
_queryTarget = [serie, option];
// 添加可拖拽提示框,多系列共用一个极坐标,第一个优先
if (self.deepQuery(_queryTarget, 'calculable')) {
_addDropBox(i);
}
_buildSingleRadar(i);
}
}
for (var i = 0, l = self.shapeList.length; i < l; i++) {
self.shapeList[i].id = zr.newShapeId(self.type);
zr.addShape(self.shapeList[i]);
}
}
/**
* 构建数据图形
* @param {number} 序列的index
*/
function _buildSingleRadar(index) {
var legend = component.legend;
var iconShape;
var data = serie.data;
var defaultColor;
var name;
var pointList;
var calculable = self.deepQuery(_queryTarget, 'calculable');
for (var i = 0; i < data.length; i ++) {
name = data[i].name || '';
// 图例开关
self.selectedMap[name] = legend
? legend.isSelected(name)
: true;
if (!self.selectedMap[name]) {
continue;
}
// 默认颜色策略
if (legend) {
// 有图例则从图例中获取颜色定义
defaultColor = legend.getColor(name);
iconShape = legend.getItemShape(name);
if (iconShape) {
// 回调legend,换一个更形象的icon
iconShape.style.brushType = self.deepQuery(
[data[i], serie], 'itemStyle.normal.areaStyle'
) ? 'both' : 'stroke';
legend.setItemShape(name, iconShape);
}
}
else {
// 全局颜色定义
defaultColor = zr.getColor(i);
}
pointList = _getPointList(serie.polarIndex, data[i]);
// 添加拐点形状
_addSymbol(pointList, defaultColor, data[i], index);
// 添加数据形状
_addDataShape(
pointList, defaultColor, data[i],
index, i, calculable
);
_radarDataCounter++;
}
}
/**
* 获取数据的点集
* @param {number} polarIndex
* @param {Array<Object>} 处理的数据
* @return {Array<Array<number>>} 点集
*/
function _getPointList(polarIndex, dataArr) {
var pointList = [];
var vector;
var polar = component.polar;
for (var i = 0, l = dataArr.value.length; i < l; i++) {
vector = polar.getVector(polarIndex, i, dataArr.value[i]);
if (vector) {
pointList.push(vector);
}
}
return pointList;
}
/**
* 生成折线图上的拐点图形
*/
function _getSymbol(
x, y, symbol, symbolSize, normalColor, emphasisColor, lineWidth
) {
var itemShape = {
shape : 'icon',
zlevel : _zlevelBase + 1,
style : {
iconType : symbol.replace('empty', '').toLowerCase(),
x : x - symbolSize,
y : y - symbolSize,
width : symbolSize * 2,
height : symbolSize * 2,
brushType : 'both',
color : symbol.match('empty') ? '#fff' : normalColor,
strokeColor : normalColor,
lineWidth: lineWidth * 2
},
hoverable: false
};
if (symbol.match('star')) {
itemShape.style.iconType = 'star';
itemShape.style.n =
(symbol.replace('empty', '').replace('star','') - 0) || 5;
}
itemShape._x = x;
itemShape._y = y;
return itemShape;
}
/**
* 添加拐点
* @param {Array<Array<number>>} pointList 点集
* @param {string} defaultColor 默认填充颜色
* @param {object} data 数据
* @param {number} serieIndex
*/
function _addSymbol(pointList, defaultColor, data) {
// 多级控制
var queryTarget = [data, serie];
var symbol = self.deepQuery(queryTarget,'symbol')
|| _symbol[_radarDataCounter % _symbol.length]
|| 'cricle';
if (symbol != 'none') {
var symbolSize = self.deepQuery(queryTarget,'symbolSize');
var nColor = self.deepQuery(
queryTarget, 'itemStyle.normal.color'
);
var eColor = self.deepQuery(
queryTarget, 'itemStyle.emphasis.color'
);
var lineWidth = self.deepQuery(
queryTarget, 'itemStyle.normal.lineStyle.width'
);
for (var i = 0, l = pointList.length; i < l; i++) {
self.shapeList.push(_getSymbol(
pointList[i][0], // x
pointList[i][1], // y
symbol,
symbolSize,
nColor || defaultColor,
eColor || nColor || defaultColor,
lineWidth
));
}
}
}
/**
* 添加数据图形
* @param {Array<Array<number>>} pointList 点集
* @param {string} defaultColor 默认填充颜色
* @param {object} data 数据
* @param {number} serieIndex
* @param {number} dataIndex
* @param {boolean} calcalable
*/
function _addDataShape(
pointList, defaultColor, data,
seriesIndex, dataIndex, calculable
) {
// 多级控制
var queryTarget = [data, serie];
var nColor = self.deepQuery(
queryTarget, 'itemStyle.normal.color'
);
var nLineWidth = self.deepQuery(
queryTarget, 'itemStyle.normal.lineStyle.width'
);
var nLineType = self.deepQuery(
queryTarget, 'itemStyle.normal.lineStyle.type'
);
var nAreaColor =
8000
self.deepQuery(
queryTarget, 'itemStyle.normal.areaStyle.color'
);
var nIsAreaFill = self.deepQuery(
queryTarget, 'itemStyle.normal.areaStyle'
);
var shape = {
shape : 'polygon',
zlevel : _zlevelBase,
style : {
pointList : pointList,
brushType : nIsAreaFill ? 'both' : 'stroke',
color : nAreaColor
|| nColor
|| zrColor.alpha(defaultColor,0.5),
strokeColor : nColor || defaultColor,
lineWidth : nLineWidth,
lineType : nLineType
},
highlightStyle : {
brushType : self.deepQuery(
queryTarget,
'itemStyle.emphasis.areaStyle'
) || nIsAreaFill
? 'both' : 'stroke',
color : self.deepQuery(
queryTarget,
'itemStyle.emphasis.areaStyle.color'
)
|| nAreaColor
|| nColor
|| zrColor.alpha(defaultColor,0.5),
strokeColor : self.deepQuery(
queryTarget, 'itemStyle.emphasis.color'
) || nColor || defaultColor,
lineWidth : self.deepQuery(
queryTarget,
'itemStyle.emphasis.lineStyle.width'
) || nLineWidth,
lineType : self.deepQuery(
queryTarget,
'itemStyle.emphasis.lineStyle.type'
) || nLineType
}
};
ecData.pack(
shape,
series[seriesIndex], // 系列
seriesIndex, // 系列索引
data, // 数据
dataIndex, // 数据索引
data.name, // 数据名称
// 附加指标信息
component.polar.getIndicator(series[seriesIndex].polarIndex)
);
if (calculable) {
shape.draggable = true;
self.setCalculable(shape);
}
self.shapeList.push(shape);
}
/**
* 增加外围接受框
* @param {number} serie的序列
*/
function _addDropBox(index) {
var polarIndex = self.deepQuery(
_queryTarget, 'polarIndex'
);
if (!_dropBoxList[polarIndex]) {
var shape = component.polar.getDropBox(polarIndex);
shape.zlevel = _zlevelBase;
self.setCalculable(shape);
ecData.pack(shape, series, index, undefined, -1);
self.shapeList.push(shape);
_dropBoxList[polarIndex] = true;
}
}
/**
* 数据项被拖拽出去,重载基类方法
*/
function ondragend(param, status) {
if (!self.isDragend || !param.target) {
// 没有在当前实例上发生拖拽行为则直接返回
return;
}
var target = param.target; // 被拖拽图形元素
var seriesIndex = ecData.get(target, 'seriesIndex');
var dataIndex = ecData.get(target, 'dataIndex');
// 被拖拽的图形是饼图sector,删除被拖拽走的数据
component.legend && component.legend.del(
series[seriesIndex].data[dataIndex].name
);
series[seriesIndex].data.splice(dataIndex, 1);
// 别status = {}赋值啊!!
status.dragOut = true;
status.needRefresh = true;
// 处理完拖拽事件后复位
self.isDragend = false;
return;
}
/**
* 数据项被拖拽进来, 重载基类方法
*/
function ondrop(param, status) {
if (!self.isDrop || !param.target) {
// 没有在当前实例上发生拖拽行为则直接返回
return;
}
var target = param.target; // 拖拽安放目标
var dragged = param.dragged; // 当前被拖拽的图形对象
var seriesIndex = ecData.get(target, 'seriesIndex');
var dataIndex = ecData.get(target, 'dataIndex');
var data;
var legend = component.legend;
var value;
if (dataIndex == -1) {
data = {
value : ecData.get(dragged, 'value'),
name : ecData.get(dragged, 'name')
};
series[seriesIndex].data.push(data);
legend && legend.add(
data.name,
dragged.style.color || dragged.style.strokeColor
);
}
else {
// 数据被拖拽到某个数据项上,数据修改
var accMath = require('../util/accMath');
data = series[seriesIndex].data[dataIndex];
legend && legend.del(data.name);
data.name += option.nameConnector
+ ecData.get(dragged, 'name');
value = ecData.get(dragged, 'value');
for (var i = 0 ; i < value.length; i ++) {
data.value[i] = accMath.accAdd(data.value[i], value[i]);
}
legend && legend.add(
data.name,
dragged.style.color || dragged.style.strokeColor
);
}
// 别status = {}赋值啊!!
status.dragIn = status.dragIn || true;
// 处理完拖拽事件后复位
self.isDrop = false;
return;
}
/**
* 构造函数默认执行的初始化方法,也用于创建实例后动态修改
* @param {Object} newZr
* @param {Object} newSeries
* @param {Object} newComponent
*/
function init(newOption, newComponent) {
component = newComponent;
refresh(newOption);
}
/**
* 刷新
*/
function refresh(newOption) {
if (newOption) {
option = newOption;
series = option.series;
}
self.clear();
_buildShape();
}
function animation() {
var duration = self.deepQuery([option], 'animationDuration');
var easing = self.deepQuery([option], 'animationEasing');
var dataIndex;
var seriesIndex;
var data;
var serie;
var polarIndex;
var polar = component.polar;
var center;
var item;
var x;
var y;
for (var i = 0, l = self.shapeList.length; i < l; i++) {
if (self.shapeList[i].shape == 'polygon') {
item = self.shapeList[i];
seriesIndex = ecData.get(item, 'seriesIndex');
dataIndex = ecData.get(item, 'dataIndex');
serie = series[seriesIndex];
data = serie.data[dataIndex];
polarIndex = self.deepQuery(
[data, serie, option], 'polarIndex');
center = polar.getCenter(polarIndex);
x = center[0];
y = center[1];
zr.modShape(self.shapeList[i].id, {
scale : [0.1, 0.1, x, y]
});
zr.animate(item.id, '')
.when(
(self.deepQuery([serie],'animationDuration')
|| duration)
+ dataIndex * 100,
{scale : [1, 1, x, y]}
)
.start(
self.deepQuery([serie], 'animationEasing') || easing
);
}
else {
x = self.shapeList[i]._x || 0;
y = self.shapeList[i]._y || 0;
zr.modShape(self.shapeList[i].id, {
scale : [0, 0, x, y]
});
zr.animate(self.shapeList[i].id, '')
.when(
duration,
{scale : [1, 1, x, y]}
)
.start('QuinticOut');
}
}
}
self.init = init;
self.refresh = refresh;
self.animation = animation;
self.>
self.>
init(option, component);
}
// 图表注册
require('../chart').define('radar', Radar);
return Radar;
});
You can’t perform that action at this time.