jmchart 1.1.79 → 1.1.90
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.
- package/README.md +30 -28
- package/build.js +35 -35
- package/dist/jmchart.amd.js +700 -444
- package/dist/jmchart.cjs.js +699 -443
- package/dist/jmchart.esm.js +699 -444
- package/dist/jmchart.iife.js +701 -443
- package/dist/jmchart.system.js +699 -443
- package/example/bar.html +180 -180
- package/example/candlestick.html +288 -0
- package/example/index.html +294 -0
- package/example/js/vconsole.js +10 -0
- package/example/line.html +300 -297
- package/example/pie.html +353 -353
- package/example/stackBar.html +142 -142
- package/example/stackLine.html +147 -147
- package/example/test.html +40 -40
- package/example/vue.html +322 -322
- package/example/wk/line.html +314 -312
- package/index.js +8 -8
- package/package.json +42 -42
- package/rollup.config.js +26 -26
- package/server.js +45 -45
- package/src/common/style.js +259 -242
- package/src/common/tooltip.js +165 -165
- package/src/common/utils.js +21 -21
- package/src/component/vchart.js +130 -130
- package/src/core/axis/axis.js +622 -622
- package/src/core/legend/legend.js +163 -163
- package/src/core/markLine/manager.js +152 -0
- package/src/core/{axis → markLine}/markLine.js +190 -190
- package/src/jmChart.js +416 -464
- package/src/series/barSeries.js +135 -135
- package/src/series/candlestickSeries.js +114 -0
- package/src/series/lineSeries.js +243 -243
- package/src/series/pieSeries.js +350 -350
- package/src/series/series.js +397 -396
- package/src/series/stackBarSeries.js +144 -144
- package/src/series/stackLineSeries.js +145 -145
package/src/series/series.js
CHANGED
|
@@ -1,396 +1,397 @@
|
|
|
1
|
-
|
|
2
|
-
import { jmPath, jmList, jmControl } from 'jmgraph';
|
|
3
|
-
import utils from '../common/utils.js';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* 图形基类
|
|
7
|
-
*
|
|
8
|
-
* @class jmSeries
|
|
9
|
-
* @module jmChart
|
|
10
|
-
* @param {jmChart} chart 当前图表
|
|
11
|
-
* @param {array} mappings 图形字段映射
|
|
12
|
-
* @param {style} style 样式
|
|
13
|
-
*/
|
|
14
|
-
|
|
15
|
-
//构造线图
|
|
16
|
-
export default class jmSeries extends jmPath {
|
|
17
|
-
constructor(options) {
|
|
18
|
-
super(options);
|
|
19
|
-
|
|
20
|
-
this.option = options;
|
|
21
|
-
|
|
22
|
-
this.field = options.field || options.fields || '';
|
|
23
|
-
this.index = options.index || 1;
|
|
24
|
-
this.legendLabel = options.legendLabel || '';
|
|
25
|
-
this.___animateCounter = 0; // 动画计数
|
|
26
|
-
|
|
27
|
-
this.xAxis = this.graph.createXAxis(); // 生成X轴
|
|
28
|
-
|
|
29
|
-
// 生成当前Y轴
|
|
30
|
-
this.yAxis = this.yAxis || this.graph.createYAxis({
|
|
31
|
-
index: this.index,
|
|
32
|
-
format: options.yLabelFormat || this.graph.option.yLabelFormat
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
// 初始化一些参数, 因为这里有多个Y轴的可能,所以每次都需要重调一次init
|
|
36
|
-
this.yAxis.init({
|
|
37
|
-
field: this.field,
|
|
38
|
-
minYValue: options.minYValue,
|
|
39
|
-
maxYValue: options.maxYValue
|
|
40
|
-
});
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* 关联访问的是chart的数据源
|
|
45
|
-
*/
|
|
46
|
-
get data() {
|
|
47
|
-
return this.graph.data;
|
|
48
|
-
}
|
|
49
|
-
set data(d) {
|
|
50
|
-
this.graph.data = d;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
//是否启用动画效果
|
|
54
|
-
get enableAnimate() {
|
|
55
|
-
if(typeof this.option.enableAnimate !== 'undefined') return !!this.option.enableAnimate;
|
|
56
|
-
else {
|
|
57
|
-
return this.graph.enableAnimate;
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
set enableAnimate(v) {
|
|
61
|
-
this.option.enableAnimate = v;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* 图例名称
|
|
66
|
-
*
|
|
67
|
-
* @property legendLabel
|
|
68
|
-
* @type string
|
|
69
|
-
*/
|
|
70
|
-
legendLabel = '';
|
|
71
|
-
|
|
72
|
-
/**
|
|
73
|
-
* 当前图形下的所有子图
|
|
74
|
-
*/
|
|
75
|
-
shapes = new jmList();
|
|
76
|
-
|
|
77
|
-
/**
|
|
78
|
-
* 关健点集合
|
|
79
|
-
*/
|
|
80
|
-
keyPoints = [];
|
|
81
|
-
|
|
82
|
-
/**
|
|
83
|
-
* 标注集合
|
|
84
|
-
*/
|
|
85
|
-
labels = [];
|
|
86
|
-
|
|
87
|
-
// 图绑定的属性名
|
|
88
|
-
field = '';
|
|
89
|
-
/**
|
|
90
|
-
* Y轴的基线跟最底层的高度
|
|
91
|
-
*/
|
|
92
|
-
baseYHeight = 0;
|
|
93
|
-
/**
|
|
94
|
-
* Y轴基线的Y坐标
|
|
95
|
-
*/
|
|
96
|
-
baseY = 0;
|
|
97
|
-
/**
|
|
98
|
-
* 当前基线Y的值,不给basey就会默认采用当前Y轴最小值
|
|
99
|
-
*/
|
|
100
|
-
baseYValue = 0;
|
|
101
|
-
|
|
102
|
-
// 做一些基础初始化工作
|
|
103
|
-
initDataPoint(...args) {
|
|
104
|
-
//生成描点位
|
|
105
|
-
// 如果有动画,则需要判断是否改变,不然不需要重新动画
|
|
106
|
-
let dataChanged = false;
|
|
107
|
-
if(this.enableAnimate) {
|
|
108
|
-
// 拷贝一份上次的点集合,用于判断数据是否改变
|
|
109
|
-
this.lastPoints = this.graph.utils.clone(this.dataPoints, null, true, (obj) => {
|
|
110
|
-
if(obj instanceof jmControl) return obj;
|
|
111
|
-
});
|
|
112
|
-
|
|
113
|
-
// 重新生成描点
|
|
114
|
-
this.dataPoints = this.createPoints(...args);
|
|
115
|
-
dataChanged = utils.arrayIsChange(this.lastPoints, this.dataPoints, (s, t) => {
|
|
116
|
-
return s.x === t.x && s.y === t.y;
|
|
117
|
-
});
|
|
118
|
-
|
|
119
|
-
if(dataChanged) this.___animateCounter = 0;// 数据改变。动画重新开始
|
|
120
|
-
}
|
|
121
|
-
else {
|
|
122
|
-
this.dataPoints = this.createPoints(...args);
|
|
123
|
-
}
|
|
124
|
-
return {
|
|
125
|
-
dataChanged,
|
|
126
|
-
points: this.dataPoints
|
|
127
|
-
};
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
/**
|
|
131
|
-
* 根据X轴坐标,获取它最近的数据描点
|
|
132
|
-
* 离点最近的一个描点
|
|
133
|
-
* @param {number} x X轴坐标
|
|
134
|
-
*/
|
|
135
|
-
getDataPointByX(x) {
|
|
136
|
-
if(!this.dataPoints) return null;
|
|
137
|
-
// 获取最近的那个
|
|
138
|
-
let prePoint = undefined, nextPoint = undefined; // 跟上一个点和下一个点的距离,哪个近用哪个
|
|
139
|
-
for(let i=0; i< this.dataPoints.length; i++) {
|
|
140
|
-
const p = this.dataPoints[i];
|
|
141
|
-
if(p.x == x) return p;
|
|
142
|
-
|
|
143
|
-
// 上一个点
|
|
144
|
-
if(p.x < x) {
|
|
145
|
-
if(i === this.dataPoints.length - 1) return p;
|
|
146
|
-
prePoint = p;
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
// 下一个点
|
|
150
|
-
if(typeof nextPoint === 'undefined' && p.x > x) {
|
|
151
|
-
// 没有上一个,只能返回这个了
|
|
152
|
-
if(prePoint && x - prePoint.x < p.x - x) return prePoint;
|
|
153
|
-
else return p
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
return null;
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
/**
|
|
160
|
-
* 根据X轴值获取数据点
|
|
161
|
-
* @param {number} xValue X轴值
|
|
162
|
-
*/
|
|
163
|
-
getDataPointByXValue(xValue) {
|
|
164
|
-
if(!this.dataPoints) return null;
|
|
165
|
-
|
|
166
|
-
for(let i=0; i< this.dataPoints.length; i++) {
|
|
167
|
-
const p = this.dataPoints[i];
|
|
168
|
-
if(p.xValue == xValue) return p;
|
|
169
|
-
}
|
|
170
|
-
return null;
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
/**
|
|
175
|
-
* 重置属性
|
|
176
|
-
* 根据数据源计算轴的属性
|
|
177
|
-
*
|
|
178
|
-
* @method reset
|
|
179
|
-
*/
|
|
180
|
-
reset() {
|
|
181
|
-
// 重置所有图形
|
|
182
|
-
var shape;
|
|
183
|
-
while(shape = this.shapes.shift()) {
|
|
184
|
-
shape && shape.remove();
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
//生成图例 这里要放到shape清理后面
|
|
188
|
-
this.createLegend();
|
|
189
|
-
|
|
190
|
-
this.initAxisValue();// 处理最大值最小值
|
|
191
|
-
|
|
192
|
-
return this.chartInfo = {
|
|
193
|
-
xAxis: this.xAxis,
|
|
194
|
-
yAxis: this.yAxis
|
|
195
|
-
};
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
// 计算最大值和最小值,一般图形直接采用最大最小值即可,有些需要多值叠加
|
|
199
|
-
initAxisValue() {
|
|
200
|
-
// 计算最大最小值
|
|
201
|
-
// 当前需要先更新axis的边界值,轴好画图
|
|
202
|
-
for(var i=0; i< this.data.length;i++) {
|
|
203
|
-
if(Array.isArray(this.field)) {
|
|
204
|
-
this.field.forEach((f)=> {
|
|
205
|
-
const v = this.data[i][f];
|
|
206
|
-
this.yAxis.max(v);
|
|
207
|
-
this.yAxis.min(v);
|
|
208
|
-
});
|
|
209
|
-
}
|
|
210
|
-
else {
|
|
211
|
-
const v = this.data[i][this.field];
|
|
212
|
-
this.yAxis.max(v);
|
|
213
|
-
this.yAxis.min(v);
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
const xv = this.data[i][this.xAxis.field];
|
|
217
|
-
this.xAxis.max(xv);
|
|
218
|
-
this.xAxis.min(xv);
|
|
219
|
-
}
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
/**
|
|
223
|
-
* 生成序列图描点
|
|
224
|
-
*
|
|
225
|
-
* @method createPoints
|
|
226
|
-
*/
|
|
227
|
-
createPoints(data) {
|
|
228
|
-
data = data || this.data;
|
|
229
|
-
if(!data) return;
|
|
230
|
-
|
|
231
|
-
const xstep = this.xAxis.step();
|
|
232
|
-
const minY = this.yAxis.min();
|
|
233
|
-
const ystep = this.yAxis.step();
|
|
234
|
-
|
|
235
|
-
this.baseYValue = typeof this.graph.baseY === 'undefined'? minY: (this.graph.baseY||0);
|
|
236
|
-
this.baseYHeight = (this.baseYValue - minY) * ystep;// 基线的高度
|
|
237
|
-
this.baseY = this.graph.chartArea.height - this.baseYHeight;// Y轴基线的Y坐标
|
|
238
|
-
// 有些图形是有多属性值的
|
|
239
|
-
const fields = Array.isArray(this.field)? this.field: [this.field];
|
|
240
|
-
|
|
241
|
-
this.dataPoints = [];
|
|
242
|
-
for(let i=0;i < data.length; i++) {
|
|
243
|
-
const s = data[i];
|
|
244
|
-
|
|
245
|
-
const xv = s[this.xAxis.field];
|
|
246
|
-
|
|
247
|
-
const p = {
|
|
248
|
-
data: s,
|
|
249
|
-
index: i,
|
|
250
|
-
xValue: xv,
|
|
251
|
-
xLabel: xv,
|
|
252
|
-
points: [],
|
|
253
|
-
style: this.graph.utils.clone(this.style),
|
|
254
|
-
};
|
|
255
|
-
|
|
256
|
-
// 这里的点应相对于chartArea
|
|
257
|
-
p.x = xstep *
|
|
258
|
-
|
|
259
|
-
for(const f of fields) {
|
|
260
|
-
const yv = s[f];
|
|
261
|
-
p.yLabel = p.yValue = yv;
|
|
262
|
-
// 高度
|
|
263
|
-
p.height = (yv - this.baseYValue) * ystep;
|
|
264
|
-
|
|
265
|
-
const point = {
|
|
266
|
-
x: p.x,
|
|
267
|
-
// 高度
|
|
268
|
-
height: p.height,
|
|
269
|
-
yValue: yv
|
|
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
|
-
style
|
|
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
|
-
const
|
|
344
|
-
const
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
const
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
*
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
this.
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
*
|
|
374
|
-
*
|
|
375
|
-
*
|
|
376
|
-
* @
|
|
377
|
-
* @
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
events
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
}
|
|
1
|
+
|
|
2
|
+
import { jmPath, jmList, jmControl } from 'jmgraph';
|
|
3
|
+
import utils from '../common/utils.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* 图形基类
|
|
7
|
+
*
|
|
8
|
+
* @class jmSeries
|
|
9
|
+
* @module jmChart
|
|
10
|
+
* @param {jmChart} chart 当前图表
|
|
11
|
+
* @param {array} mappings 图形字段映射
|
|
12
|
+
* @param {style} style 样式
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
//构造线图
|
|
16
|
+
export default class jmSeries extends jmPath {
|
|
17
|
+
constructor(options) {
|
|
18
|
+
super(options);
|
|
19
|
+
|
|
20
|
+
this.option = options;
|
|
21
|
+
|
|
22
|
+
this.field = options.field || options.fields || '';
|
|
23
|
+
this.index = options.index || 1;
|
|
24
|
+
this.legendLabel = options.legendLabel || '';
|
|
25
|
+
this.___animateCounter = 0; // 动画计数
|
|
26
|
+
|
|
27
|
+
this.xAxis = this.graph.createXAxis(); // 生成X轴
|
|
28
|
+
|
|
29
|
+
// 生成当前Y轴
|
|
30
|
+
this.yAxis = this.yAxis || this.graph.createYAxis({
|
|
31
|
+
index: this.index,
|
|
32
|
+
format: options.yLabelFormat || this.graph.option.yLabelFormat
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
// 初始化一些参数, 因为这里有多个Y轴的可能,所以每次都需要重调一次init
|
|
36
|
+
this.yAxis.init({
|
|
37
|
+
field: this.field,
|
|
38
|
+
minYValue: options.minYValue,
|
|
39
|
+
maxYValue: options.maxYValue
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* 关联访问的是chart的数据源
|
|
45
|
+
*/
|
|
46
|
+
get data() {
|
|
47
|
+
return this.graph.data;
|
|
48
|
+
}
|
|
49
|
+
set data(d) {
|
|
50
|
+
this.graph.data = d;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
//是否启用动画效果
|
|
54
|
+
get enableAnimate() {
|
|
55
|
+
if(typeof this.option.enableAnimate !== 'undefined') return !!this.option.enableAnimate;
|
|
56
|
+
else {
|
|
57
|
+
return this.graph.enableAnimate;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
set enableAnimate(v) {
|
|
61
|
+
this.option.enableAnimate = v;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* 图例名称
|
|
66
|
+
*
|
|
67
|
+
* @property legendLabel
|
|
68
|
+
* @type string
|
|
69
|
+
*/
|
|
70
|
+
legendLabel = '';
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* 当前图形下的所有子图
|
|
74
|
+
*/
|
|
75
|
+
shapes = new jmList();
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* 关健点集合
|
|
79
|
+
*/
|
|
80
|
+
keyPoints = [];
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* 标注集合
|
|
84
|
+
*/
|
|
85
|
+
labels = [];
|
|
86
|
+
|
|
87
|
+
// 图绑定的属性名
|
|
88
|
+
field = '';
|
|
89
|
+
/**
|
|
90
|
+
* Y轴的基线跟最底层的高度
|
|
91
|
+
*/
|
|
92
|
+
baseYHeight = 0;
|
|
93
|
+
/**
|
|
94
|
+
* Y轴基线的Y坐标
|
|
95
|
+
*/
|
|
96
|
+
baseY = 0;
|
|
97
|
+
/**
|
|
98
|
+
* 当前基线Y的值,不给basey就会默认采用当前Y轴最小值
|
|
99
|
+
*/
|
|
100
|
+
baseYValue = 0;
|
|
101
|
+
|
|
102
|
+
// 做一些基础初始化工作
|
|
103
|
+
initDataPoint(...args) {
|
|
104
|
+
//生成描点位
|
|
105
|
+
// 如果有动画,则需要判断是否改变,不然不需要重新动画
|
|
106
|
+
let dataChanged = false;
|
|
107
|
+
if(this.enableAnimate) {
|
|
108
|
+
// 拷贝一份上次的点集合,用于判断数据是否改变
|
|
109
|
+
this.lastPoints = this.graph.utils.clone(this.dataPoints, null, true, (obj) => {
|
|
110
|
+
if(obj instanceof jmControl) return obj;
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
// 重新生成描点
|
|
114
|
+
this.dataPoints = this.createPoints(...args);
|
|
115
|
+
dataChanged = utils.arrayIsChange(this.lastPoints, this.dataPoints, (s, t) => {
|
|
116
|
+
return s.x === t.x && s.y === t.y;
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
if(dataChanged) this.___animateCounter = 0;// 数据改变。动画重新开始
|
|
120
|
+
}
|
|
121
|
+
else {
|
|
122
|
+
this.dataPoints = this.createPoints(...args);
|
|
123
|
+
}
|
|
124
|
+
return {
|
|
125
|
+
dataChanged,
|
|
126
|
+
points: this.dataPoints
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* 根据X轴坐标,获取它最近的数据描点
|
|
132
|
+
* 离点最近的一个描点
|
|
133
|
+
* @param {number} x X轴坐标
|
|
134
|
+
*/
|
|
135
|
+
getDataPointByX(x) {
|
|
136
|
+
if(!this.dataPoints) return null;
|
|
137
|
+
// 获取最近的那个
|
|
138
|
+
let prePoint = undefined, nextPoint = undefined; // 跟上一个点和下一个点的距离,哪个近用哪个
|
|
139
|
+
for(let i=0; i< this.dataPoints.length; i++) {
|
|
140
|
+
const p = this.dataPoints[i];
|
|
141
|
+
if(p.x == x) return p;
|
|
142
|
+
|
|
143
|
+
// 上一个点
|
|
144
|
+
if(p.x < x) {
|
|
145
|
+
if(i === this.dataPoints.length - 1) return p;
|
|
146
|
+
prePoint = p;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// 下一个点
|
|
150
|
+
if(typeof nextPoint === 'undefined' && p.x > x) {
|
|
151
|
+
// 没有上一个,只能返回这个了
|
|
152
|
+
if(prePoint && x - prePoint.x < p.x - x) return prePoint;
|
|
153
|
+
else return p
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
return null;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* 根据X轴值获取数据点
|
|
161
|
+
* @param {number} xValue X轴值
|
|
162
|
+
*/
|
|
163
|
+
getDataPointByXValue(xValue) {
|
|
164
|
+
if(!this.dataPoints) return null;
|
|
165
|
+
|
|
166
|
+
for(let i=0; i< this.dataPoints.length; i++) {
|
|
167
|
+
const p = this.dataPoints[i];
|
|
168
|
+
if(p.xValue == xValue) return p;
|
|
169
|
+
}
|
|
170
|
+
return null;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* 重置属性
|
|
176
|
+
* 根据数据源计算轴的属性
|
|
177
|
+
*
|
|
178
|
+
* @method reset
|
|
179
|
+
*/
|
|
180
|
+
reset() {
|
|
181
|
+
// 重置所有图形
|
|
182
|
+
var shape;
|
|
183
|
+
while(shape = this.shapes.shift()) {
|
|
184
|
+
shape && shape.remove();
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
//生成图例 这里要放到shape清理后面
|
|
188
|
+
this.createLegend();
|
|
189
|
+
|
|
190
|
+
this.initAxisValue();// 处理最大值最小值
|
|
191
|
+
|
|
192
|
+
return this.chartInfo = {
|
|
193
|
+
xAxis: this.xAxis,
|
|
194
|
+
yAxis: this.yAxis
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
// 计算最大值和最小值,一般图形直接采用最大最小值即可,有些需要多值叠加
|
|
199
|
+
initAxisValue() {
|
|
200
|
+
// 计算最大最小值
|
|
201
|
+
// 当前需要先更新axis的边界值,轴好画图
|
|
202
|
+
for(var i=0; i< this.data.length;i++) {
|
|
203
|
+
if(Array.isArray(this.field)) {
|
|
204
|
+
this.field.forEach((f)=> {
|
|
205
|
+
const v = this.data[i][f];
|
|
206
|
+
this.yAxis.max(v);
|
|
207
|
+
this.yAxis.min(v);
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
else {
|
|
211
|
+
const v = this.data[i][this.field];
|
|
212
|
+
this.yAxis.max(v);
|
|
213
|
+
this.yAxis.min(v);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
const xv = this.data[i][this.xAxis.field];
|
|
217
|
+
this.xAxis.max(xv);
|
|
218
|
+
this.xAxis.min(xv);
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* 生成序列图描点
|
|
224
|
+
*
|
|
225
|
+
* @method createPoints
|
|
226
|
+
*/
|
|
227
|
+
createPoints(data) {
|
|
228
|
+
data = data || this.data;
|
|
229
|
+
if(!data) return;
|
|
230
|
+
|
|
231
|
+
const xstep = this.xAxis.step();
|
|
232
|
+
const minY = this.yAxis.min();
|
|
233
|
+
const ystep = this.yAxis.step();
|
|
234
|
+
|
|
235
|
+
this.baseYValue = typeof this.graph.baseY === 'undefined'? minY: (this.graph.baseY||0);
|
|
236
|
+
this.baseYHeight = (this.baseYValue - minY) * ystep;// 基线的高度
|
|
237
|
+
this.baseY = this.graph.chartArea.height - this.baseYHeight;// Y轴基线的Y坐标
|
|
238
|
+
// 有些图形是有多属性值的
|
|
239
|
+
const fields = Array.isArray(this.field)? this.field: [this.field];
|
|
240
|
+
|
|
241
|
+
this.dataPoints = [];
|
|
242
|
+
for(let i=0;i < data.length; i++) {
|
|
243
|
+
const s = data[i];
|
|
244
|
+
|
|
245
|
+
const xv = s[this.xAxis.field];
|
|
246
|
+
|
|
247
|
+
const p = {
|
|
248
|
+
data: s,
|
|
249
|
+
index: i,
|
|
250
|
+
xValue: xv,
|
|
251
|
+
xLabel: xv,
|
|
252
|
+
points: [],
|
|
253
|
+
style: this.graph.utils.clone(this.style),
|
|
254
|
+
};
|
|
255
|
+
|
|
256
|
+
// 这里的点应相对于chartArea
|
|
257
|
+
p.x = xstep * i + this.xAxis.labelStart;
|
|
258
|
+
|
|
259
|
+
for(const f of fields) {
|
|
260
|
+
const yv = s[f];
|
|
261
|
+
p.yLabel = p.yValue = yv;
|
|
262
|
+
// 高度
|
|
263
|
+
p.height = (yv - this.baseYValue) * ystep;
|
|
264
|
+
|
|
265
|
+
const point = {
|
|
266
|
+
x: p.x,
|
|
267
|
+
// 高度
|
|
268
|
+
height: p.height,
|
|
269
|
+
yValue: yv,
|
|
270
|
+
field: f
|
|
271
|
+
}
|
|
272
|
+
//如果Y值不存在。则此点无效,不画图
|
|
273
|
+
if(yv == null || typeof yv == 'undefined') {
|
|
274
|
+
point.m = p.m = true;
|
|
275
|
+
}
|
|
276
|
+
else {
|
|
277
|
+
if(this.yAxis.dataType != 'number') {
|
|
278
|
+
yv = i;
|
|
279
|
+
}
|
|
280
|
+
point.y = p.y = this.baseY - point.height;
|
|
281
|
+
}
|
|
282
|
+
p.points.push(point);
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
// 初始化项
|
|
286
|
+
if(typeof this.option.initItemHandler === 'function') {
|
|
287
|
+
this.option.initItem.call(this, p);
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
this.dataPoints.push(p);
|
|
291
|
+
}
|
|
292
|
+
return this.dataPoints;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
// 生成颜色
|
|
296
|
+
getColor(p) {
|
|
297
|
+
if(typeof this.style.color === 'function') {
|
|
298
|
+
return this.style.color.call(this, p);
|
|
299
|
+
}
|
|
300
|
+
else {
|
|
301
|
+
return this.style.color;
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
/**
|
|
306
|
+
* 生成图例
|
|
307
|
+
*
|
|
308
|
+
* @method createLegend
|
|
309
|
+
*/
|
|
310
|
+
createLegend() {
|
|
311
|
+
//生成图例前的图标
|
|
312
|
+
const style = this.graph.utils.clone(this.style);
|
|
313
|
+
style.fill = this.getColor();
|
|
314
|
+
//delete style.stroke;
|
|
315
|
+
const shape = this.graph.createShape('rect',{
|
|
316
|
+
style
|
|
317
|
+
});
|
|
318
|
+
this.graph.legend.append(this, shape);
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
// 生成柱图的标注
|
|
322
|
+
createItemLabel(point, position) {
|
|
323
|
+
if(!this.style.label || this.style.label.show !== true) return;
|
|
324
|
+
|
|
325
|
+
const text = this.option.itemLabelFormat?this.option.itemLabelFormat.call(this, point): point.yValue;
|
|
326
|
+
if(!text) return;
|
|
327
|
+
|
|
328
|
+
// v如果指定了为控件,则直接加入
|
|
329
|
+
if(text instanceof jmControl) {
|
|
330
|
+
this.addShape(text);
|
|
331
|
+
return text;
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
const style = this.graph.utils.clone(this.graph.style.itemLabel, {
|
|
335
|
+
zIndex: 21,
|
|
336
|
+
...this.style.label
|
|
337
|
+
});
|
|
338
|
+
|
|
339
|
+
if(typeof style.fill === 'function') {
|
|
340
|
+
style.fill = style.fill.call(this, point);
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
const barWidth = (this.barTotalWidth||0) / 2 - (this.barWidth||0) * (this.barIndex||0) - (this.barWidth||0) / 2;
|
|
344
|
+
const baseOffset = point.y - this.baseY;
|
|
345
|
+
const label = this.graph.createShape('label', {
|
|
346
|
+
style,
|
|
347
|
+
text: text,
|
|
348
|
+
data: point,
|
|
349
|
+
position: function() {
|
|
350
|
+
const offh = style.offset || 5;
|
|
351
|
+
const size = this.testSize();
|
|
352
|
+
return {
|
|
353
|
+
x: point.x - size.width / 2 - barWidth,
|
|
354
|
+
y: baseOffset>0?(point.y + offh): (point.y - size.height - offh)
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
});
|
|
358
|
+
|
|
359
|
+
this.addShape(label);
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
/**
|
|
363
|
+
* 在图上加下定制图形
|
|
364
|
+
* @param {jmShape} shape 图形
|
|
365
|
+
*/
|
|
366
|
+
addShape(shape) {
|
|
367
|
+
this.graph.chartArea.children.add(shape);
|
|
368
|
+
this.shapes.add(shape);
|
|
369
|
+
return shape;
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
/**
|
|
373
|
+
* 获取指定事件的集合
|
|
374
|
+
* 比如mousedown,mouseup等
|
|
375
|
+
*
|
|
376
|
+
* @method getEvent
|
|
377
|
+
* @param {string} name 事件名称
|
|
378
|
+
* @return {list} 事件委托的集合
|
|
379
|
+
*/
|
|
380
|
+
getEvent(name) {
|
|
381
|
+
|
|
382
|
+
const event = this.option? this.option[name]: null;
|
|
383
|
+
if(!event) {
|
|
384
|
+
return super.getEvent(name);
|
|
385
|
+
}
|
|
386
|
+
else {
|
|
387
|
+
const events = new jmList();
|
|
388
|
+
events.add(event);
|
|
389
|
+
|
|
390
|
+
const oldevents = super.getEvent(name);
|
|
391
|
+
if(oldevents) {
|
|
392
|
+
events.concat(oldevents);
|
|
393
|
+
}
|
|
394
|
+
return events;
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
};
|