jmchart 1.2.9 → 1.2.11

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,404 +1,387 @@
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: Array.isArray(this.field)? this.field[0]: 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
- // 执行初始化函数回调
125
- if(this.option && this.option.onInit) {
126
- this.option.onInit.apply(this, args);
127
- }
128
- return {
129
- dataChanged,
130
- points: this.dataPoints
131
- };
132
- }
133
-
134
- /**
135
- * 根据X轴坐标,获取它最近的数据描点
136
- * 离点最近的一个描点
137
- * @param {number} x X轴坐标
138
- */
139
- getDataPointByX(x) {
140
- if(!this.dataPoints) return null;
141
- // 获取最近的那个
142
- let prePoint = undefined, nextPoint = undefined; // 跟上一个点和下一个点的距离,哪个近用哪个
143
- for(let i=0; i< this.dataPoints.length; i++) {
144
- const p = this.dataPoints[i];
145
- if(p.x == x) return p;
146
-
147
- // 上一个点
148
- if(p.x < x) {
149
- if(i === this.dataPoints.length - 1) return p;
150
- prePoint = p;
151
- }
152
-
153
- // 下一个点
154
- if(typeof nextPoint === 'undefined' && p.x > x) {
155
- // 没有上一个,只能返回这个了
156
- if(prePoint && x - prePoint.x < p.x - x) return prePoint;
157
- else return p
158
- }
159
- }
160
- return null;
161
- }
162
-
163
- /**
164
- * 根据X轴值获取数据点
165
- * @param {number} xValue X轴值
166
- */
167
- getDataPointByXValue(xValue) {
168
- if(!this.dataPoints) return null;
169
-
170
- for(let i=0; i< this.dataPoints.length; i++) {
171
- const p = this.dataPoints[i];
172
- if(p.xValue == xValue) return p;
173
- }
174
- return null;
175
- }
176
-
177
-
178
- /**
179
- * 重置属性
180
- * 根据数据源计算轴的属性
181
- *
182
- * @method reset
183
- */
184
- reset() {
185
- // 重置所有图形
186
- var shape;
187
- while(shape = this.shapes.shift()) {
188
- shape && shape.remove();
189
- }
190
-
191
- this.initAxisValue();// 处理最大值最小值
192
-
193
- //生成图例 这里要放到shape清理后面
194
- this.createLegend();
195
-
196
- return this.chartInfo = {
197
- xAxis: this.xAxis,
198
- yAxis: this.yAxis
199
- };
200
- }
201
-
202
- // 计算最大值和最小值,一般图形直接采用最大最小值即可,有些需要多值叠加
203
- initAxisValue() {
204
- // 计算最大最小值
205
- // 当前需要先更新axis的边界值,轴好画图
206
- for(var i=0; i< this.data.length;i++) {
207
- if(Array.isArray(this.field)) {
208
- this.field.forEach((f)=> {
209
- const v = this.data[i][f];
210
- this.yAxis.max(v);
211
- this.yAxis.min(v);
212
- });
213
- }
214
- else {
215
- const v = this.data[i][this.field];
216
- this.yAxis.max(v);
217
- this.yAxis.min(v);
218
- }
219
-
220
- const xv = this.data[i][this.xAxis.field];
221
- this.xAxis.max(xv);
222
- this.xAxis.min(xv);
223
- }
224
- }
225
-
226
- /**
227
- * 生成序列图描点
228
- *
229
- * @method createPoints
230
- */
231
- createPoints(data) {
232
- data = data || this.data;
233
- if(!data) return;
234
-
235
- const xstep = this.xAxis.step();
236
- const minY = this.yAxis.min();
237
- const ystep = this.yAxis.step();
238
-
239
- this.baseYValue = typeof this.graph.baseY === 'undefined'? minY: (this.graph.baseY||0);
240
- this.baseYHeight = (this.baseYValue - minY) * ystep;// 基线的高度
241
- this.baseY = this.graph.chartArea.height - this.baseYHeight;// Y轴基线的Y坐标
242
- // 有些图形是有多属性值的
243
- const fields = Array.isArray(this.field)? this.field: [this.field];
244
-
245
- this.dataPoints = [];
246
- for(let i=0;i < data.length; i++) {
247
- const s = data[i];
248
-
249
- const xv = s[this.xAxis.field];
250
-
251
- const p = {
252
- data: s,
253
- index: i,
254
- xValue: xv,
255
- xLabel: xv,
256
- points: [],
257
- style: this.graph.utils.clone(this.style),
258
- };
259
-
260
- // 这里的点应相对于chartArea
261
- p.x = xstep * i + this.xAxis.labelStart;
262
-
263
- for(let j=0; j<fields.length; j++) {
264
- const f = fields[j];
265
-
266
- const yv = s[f];
267
- p.yLabel = p.yValue = yv;
268
- // 高度
269
- p.height = (yv - this.baseYValue) * ystep;
270
-
271
- const point = {
272
- x: p.x,
273
- // 高度
274
- height: p.height,
275
- yValue: yv,
276
- field: f
277
- }
278
- //如果Y值不存在。则此点无效,不画图
279
- if(yv == null || typeof yv == 'undefined') {
280
- point.m = p.m = true;
281
- }
282
- else {
283
- if(this.yAxis.dataType != 'number') {
284
- yv = i;
285
- }
286
- point.y = p.y = this.baseY - point.height;
287
- }
288
- p.points.push(point);
289
- }
290
-
291
- // 初始化项
292
- if(typeof this.option.initItemHandler === 'function') {
293
- this.option.initItem.call(this, p);
294
- }
295
-
296
- this.dataPoints.push(p);
297
- }
298
- return this.dataPoints;
299
- }
300
-
301
- // 生成颜色
302
- getColor(p) {
303
- if(typeof this.style.color === 'function') {
304
- return this.style.color.call(this, p);
305
- }
306
- else {
307
- return this.style.color;
308
- }
309
- }
310
-
311
- /**
312
- * 生成图例
313
- *
314
- * @method createLegend
315
- */
316
- createLegend() {
317
- //生成图例前的图标
318
- const style = this.graph.utils.clone(this.style);
319
- style.fill = this.getColor();
320
- //delete style.stroke;
321
- const shape = this.graph.createShape('rect',{
322
- style
323
- });
324
- this.graph.legend.append(this, shape);
325
- return shape;
326
- }
327
-
328
- // 生成柱图的标注
329
- createItemLabel(point, position) {
330
- if(!this.style.label || this.style.label.show !== true) return;
331
-
332
- const text = this.option.itemLabelFormat?this.option.itemLabelFormat.call(this, point): point.yValue;
333
- if(!text) return;
334
-
335
- // v如果指定了为控件,则直接加入
336
- if(text instanceof jmControl) {
337
- this.addShape(text);
338
- return text;
339
- }
340
-
341
- const style = this.graph.utils.clone(this.graph.style.itemLabel, {
342
- zIndex: 21,
343
- ...this.style.label
344
- });
345
-
346
- if(typeof style.fill === 'function') {
347
- style.fill = style.fill.call(this, point);
348
- }
349
-
350
- const barWidth = (this.barTotalWidth||0) / 2 - (this.barWidth||0) * (this.barIndex||0) - (this.barWidth||0) / 2;
351
- const baseOffset = point.y - this.baseY;
352
- const label = this.graph.createShape('label', {
353
- style,
354
- text: text,
355
- data: point,
356
- position: function() {
357
- const offh = style.offset || 5;
358
- const size = this.testSize();
359
- return {
360
- x: point.x - size.width / 2 - barWidth,
361
- y: baseOffset>0?(point.y + offh): (point.y - size.height - offh)
362
- }
363
- }
364
- });
365
-
366
- this.addShape(label);
367
- }
368
-
369
- /**
370
- * 在图上加下定制图形
371
- * @param {jmShape} shape 图形
372
- */
373
- addShape(shape) {
374
- this.graph.chartArea.children.add(shape);
375
- this.shapes.add(shape);
376
- return shape;
377
- }
378
-
379
- /**
380
- * 获取指定事件的集合
381
- * 比如mousedown,mouseup等
382
- *
383
- * @method getEvent
384
- * @param {string} name 事件名称
385
- * @return {list} 事件委托的集合
386
- */
387
- getEvent(name) {
388
-
389
- const event = this.option? this.option[name]: null;
390
- if(!event) {
391
- return super.getEvent(name);
392
- }
393
- else {
394
- const events = new jmList();
395
- events.add(event);
396
-
397
- const oldevents = super.getEvent(name);
398
- if(oldevents) {
399
- events.concat(oldevents);
400
- }
401
- return events;
402
- }
403
- }
404
- };
1
+
2
+ import { jmPath, jmList, jmControl } from 'jmgraph';
3
+ import utils from '../common/utils.js';
4
+
5
+ const ANIMATION_DATA_THRESHOLD = 100;
6
+
7
+ /**
8
+ * 图形基类
9
+ *
10
+ * @class jmSeries
11
+ * @module jmChart
12
+ * @param {jmChart} chart 当前图表
13
+ * @param {array} mappings 图形字段映射
14
+ * @param {style} style 样式
15
+ */
16
+
17
+ export default class jmSeries extends jmPath {
18
+ constructor(options) {
19
+ super(options);
20
+
21
+ this.option = options;
22
+
23
+ this.field = options.field || options.fields || '';
24
+ this.index = options.index || 1;
25
+ this.legendLabel = options.legendLabel || '';
26
+ this.___animateCounter = 0;
27
+ this._cache = new Map();
28
+
29
+ this.xAxis = this.graph.createXAxis();
30
+
31
+ this.yAxis = this.yAxis || this.graph.createYAxis({
32
+ index: this.index,
33
+ format: options.yLabelFormat || this.graph.option.yLabelFormat
34
+ });
35
+
36
+ this.yAxis.init({
37
+ field: Array.isArray(this.field) ? this.field[0] : 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
+ initDataPoint(...args) {
103
+ let dataChanged = false;
104
+
105
+ if(this.enableAnimate && this.data && this.data.length < ANIMATION_DATA_THRESHOLD) {
106
+ this.lastPoints = this.graph.utils.clone(this.dataPoints, null, true, (obj) => {
107
+ if(obj instanceof jmControl) return obj;
108
+ });
109
+
110
+ this.dataPoints = this.createPoints(...args);
111
+ dataChanged = utils.arrayIsChange(this.lastPoints, this.dataPoints, (s, t) => {
112
+ return s.x === t.x && s.y === t.y;
113
+ });
114
+
115
+ if(dataChanged) {
116
+ this.___animateCounter = 0;
117
+ }
118
+ } else {
119
+ this.dataPoints = this.createPoints(...args);
120
+ }
121
+
122
+ if(this.option && this.option.onInit) {
123
+ this.option.onInit.apply(this, args);
124
+ }
125
+
126
+ return {
127
+ dataChanged,
128
+ points: this.dataPoints
129
+ };
130
+ }
131
+
132
+ /**
133
+ * 根据X轴坐标,获取它最近的数据描点
134
+ * 离点最近的一个描点
135
+ * @param {number} x X轴坐标
136
+ */
137
+ getDataPointByX(x) {
138
+ if(!this.dataPoints) return null;
139
+ // 获取最近的那个
140
+ let prePoint = undefined, nextPoint = undefined; // 跟上一个点和下一个点的距离,哪个近用哪个
141
+ for(let i=0; i< this.dataPoints.length; i++) {
142
+ const p = this.dataPoints[i];
143
+ if(p.x == x) return p;
144
+
145
+ // 上一个点
146
+ if(p.x < x) {
147
+ if(i === this.dataPoints.length - 1) return p;
148
+ prePoint = p;
149
+ }
150
+
151
+ // 下一个点
152
+ if(typeof nextPoint === 'undefined' && p.x > x) {
153
+ // 没有上一个,只能返回这个了
154
+ if(prePoint && x - prePoint.x < p.x - x) return prePoint;
155
+ else return p
156
+ }
157
+ }
158
+ return null;
159
+ }
160
+
161
+ /**
162
+ * 根据X轴值获取数据点
163
+ * @param {number} xValue X轴值
164
+ */
165
+ getDataPointByXValue(xValue) {
166
+ if(!this.dataPoints) return null;
167
+
168
+ for(let i=0; i< this.dataPoints.length; i++) {
169
+ const p = this.dataPoints[i];
170
+ if(p.xValue == xValue) return p;
171
+ }
172
+ return null;
173
+ }
174
+
175
+
176
+ reset() {
177
+ var shape;
178
+ while(shape = this.shapes.shift()) {
179
+ shape && shape.remove();
180
+ }
181
+
182
+ this.initAxisValue();
183
+ this.createLegend();
184
+
185
+ return this.chartInfo = {
186
+ xAxis: this.xAxis,
187
+ yAxis: this.yAxis
188
+ };
189
+ }
190
+
191
+ initAxisValue() {
192
+ if(!this.data || !this.data.length) return;
193
+
194
+ for(var i = 0; i < this.data.length; i++) {
195
+ if(Array.isArray(this.field)) {
196
+ this.field.forEach((f) => {
197
+ const v = this.data[i][f];
198
+ if(v != null) {
199
+ this.yAxis.max(v);
200
+ this.yAxis.min(v);
201
+ }
202
+ });
203
+ } else {
204
+ const v = this.data[i][this.field];
205
+ if(v != null) {
206
+ this.yAxis.max(v);
207
+ this.yAxis.min(v);
208
+ }
209
+ }
210
+
211
+ const xv = this.data[i][this.xAxis.field];
212
+ if(xv != null) {
213
+ this.xAxis.max(xv);
214
+ this.xAxis.min(xv);
215
+ }
216
+ }
217
+ }
218
+
219
+ createPoints(data) {
220
+ data = data || this.data;
221
+ if(!data || !data.length) return [];
222
+
223
+ const xstep = this.xAxis.step();
224
+ const minY = this.yAxis.min();
225
+ const ystep = this.yAxis.step();
226
+
227
+ this.baseYValue = typeof this.graph.baseY === 'undefined' ? minY : (this.graph.baseY || 0);
228
+ this.baseYHeight = (this.baseYValue - minY) * ystep;
229
+ this.baseY = this.graph.chartArea.height - this.baseYHeight;
230
+
231
+ const fields = Array.isArray(this.field) ? this.field : [this.field];
232
+ const dataPoints = [];
233
+
234
+ for(let i = 0; i < data.length; i++) {
235
+ const s = data[i];
236
+ const xv = s[this.xAxis.field];
237
+
238
+ const p = {
239
+ data: s,
240
+ index: i,
241
+ xValue: xv,
242
+ xLabel: xv,
243
+ points: [],
244
+ style: this.graph.utils.clone(this.style),
245
+ };
246
+
247
+ p.x = xstep * i + this.xAxis.labelStart;
248
+
249
+ for(let j = 0; j < fields.length; j++) {
250
+ const f = fields[j];
251
+ const yv = s[f];
252
+ p.yLabel = p.yValue = yv;
253
+ p.height = (yv - this.baseYValue) * ystep;
254
+
255
+ const point = {
256
+ x: p.x,
257
+ height: p.height,
258
+ yValue: yv,
259
+ field: f
260
+ }
261
+
262
+ if(yv == null || typeof yv == 'undefined') {
263
+ point.m = p.m = true;
264
+ } else {
265
+ if(this.yAxis.dataType != 'number') {
266
+ yv = i;
267
+ }
268
+ point.y = p.y = this.baseY - point.height;
269
+ }
270
+ p.points.push(point);
271
+ }
272
+
273
+ if(typeof this.option.initItemHandler === 'function') {
274
+ this.option.initItem.call(this, p);
275
+ }
276
+
277
+ dataPoints.push(p);
278
+ }
279
+
280
+ this.dataPoints = dataPoints;
281
+ return this.dataPoints;
282
+ }
283
+
284
+ // 生成颜色
285
+ getColor(p) {
286
+ if(typeof this.style.color === 'function') {
287
+ return this.style.color.call(this, p);
288
+ }
289
+ else {
290
+ return this.style.color;
291
+ }
292
+ }
293
+
294
+ /**
295
+ * 生成图例
296
+ *
297
+ * @method createLegend
298
+ */
299
+ createLegend() {
300
+ //生成图例前的图标
301
+ const style = this.graph.utils.clone(this.style);
302
+ style.fill = this.getColor();
303
+ //delete style.stroke;
304
+ const shape = this.graph.createShape('rect',{
305
+ style
306
+ });
307
+ this.graph.legend.append(this, shape);
308
+ return shape;
309
+ }
310
+
311
+ // 生成柱图的标注
312
+ createItemLabel(point, position) {
313
+ if(!this.style.label || this.style.label.show !== true) return;
314
+
315
+ const text = this.option.itemLabelFormat?this.option.itemLabelFormat.call(this, point): point.yValue;
316
+ if(!text) return;
317
+
318
+ // v如果指定了为控件,则直接加入
319
+ if(text instanceof jmControl) {
320
+ this.addShape(text);
321
+ return text;
322
+ }
323
+
324
+ const style = this.graph.utils.clone(this.graph.style.itemLabel, {
325
+ zIndex: 21,
326
+ ...this.style.label
327
+ });
328
+
329
+ if(typeof style.fill === 'function') {
330
+ style.fill = style.fill.call(this, point);
331
+ }
332
+
333
+ const barWidth = (this.barTotalWidth||0) / 2 - (this.barWidth||0) * (this.barIndex||0) - (this.barWidth||0) / 2;
334
+ const baseOffset = point.y - this.baseY;
335
+ const label = this.graph.createShape('label', {
336
+ style,
337
+ text: text,
338
+ data: point,
339
+ position: function() {
340
+ const offh = style.offset || 5;
341
+ const size = this.testSize();
342
+ return {
343
+ x: point.x - size.width / 2 - barWidth,
344
+ y: baseOffset>0?(point.y + offh): (point.y - size.height - offh)
345
+ }
346
+ }
347
+ });
348
+
349
+ this.addShape(label);
350
+ }
351
+
352
+ /**
353
+ * 在图上加下定制图形
354
+ * @param {jmShape} shape 图形
355
+ */
356
+ addShape(shape) {
357
+ this.graph.chartArea.children.add(shape);
358
+ this.shapes.add(shape);
359
+ return shape;
360
+ }
361
+
362
+ /**
363
+ * 获取指定事件的集合
364
+ * 比如mousedown,mouseup等
365
+ *
366
+ * @method getEvent
367
+ * @param {string} name 事件名称
368
+ * @return {list} 事件委托的集合
369
+ */
370
+ getEvent(name) {
371
+
372
+ const event = this.option? this.option[name]: null;
373
+ if(!event) {
374
+ return super.getEvent(name);
375
+ }
376
+ else {
377
+ const events = new jmList();
378
+ events.add(event);
379
+
380
+ const oldevents = super.getEvent(name);
381
+ if(oldevents) {
382
+ events.concat(oldevents);
383
+ }
384
+ return events;
385
+ }
386
+ }
387
+ };