jmchart 1.2.12 → 1.2.13

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.
@@ -0,0 +1,128 @@
1
+ import jmSeries from './series.js';
2
+
3
+ const ANIMATION_DATA_THRESHOLD = 100;
4
+ const DEFAULT_ANIMATION_COUNT = 10;
5
+
6
+ /**
7
+ * 散点图
8
+ *
9
+ * @class jmScatterSeries
10
+ * @module jmChart
11
+ * @param {jmChart} chart 当前图表
12
+ * @param {array} mappings 图形字段映射
13
+ * @param {style} style 样式
14
+ */
15
+
16
+ export default class jmScatterSeries extends jmSeries {
17
+ constructor(options) {
18
+ options.style = options.style || options.graph.style.scatter;
19
+ super(options);
20
+ }
21
+
22
+ /**
23
+ * 初始化散点图
24
+ *
25
+ * @method init
26
+ * @for jmScatterSeries
27
+ */
28
+ init() {
29
+ const {
30
+ points,
31
+ dataChanged
32
+ } = this.initDataPoint();
33
+
34
+ const len = points.length;
35
+ if(!len) return;
36
+
37
+ this.style.stroke = this.style.color;
38
+ this.style.item.stroke = this.style.color;
39
+
40
+ const isRunningAni = this.enableAnimate && (dataChanged || this.___animateCounter > 0) && len < ANIMATION_DATA_THRESHOLD;
41
+
42
+ const aniCount = (this.style.aniCount || DEFAULT_ANIMATION_COUNT);
43
+ const aniStep = Math.floor(len / aniCount) || 1;
44
+
45
+ for(let i = 0; i < len; i++) {
46
+ const p = points[i];
47
+
48
+ if(typeof p.y === 'undefined' || p.y === null) {
49
+ continue;
50
+ }
51
+
52
+ if(isRunningAni && i > this.___animateCounter) {
53
+ break;
54
+ }
55
+
56
+ this.createPointItem(p);
57
+ this.createItemLabel(p);
58
+
59
+ this.emit('onPointCreated', p);
60
+ }
61
+
62
+ if(this.___animateCounter >= len - 1) {
63
+ this.___animateCounter = 0;
64
+ } else if(isRunningAni) {
65
+ this.___animateCounter += aniStep;
66
+ this.graph.utils.requestAnimationFrame(() => {
67
+ this.needUpdate = true;
68
+ });
69
+ }
70
+ }
71
+
72
+ /**
73
+ * 生成散点
74
+ *
75
+ * @method createPointItem
76
+ * @for jmScatterSeries
77
+ * @param {object} p 数据点
78
+ */
79
+ createPointItem(p) {
80
+ // 获取点的大小,可以从数据中指定或使用默认值
81
+ const radius = typeof p.size === 'number' ? p.size : (this.style.radius || 5);
82
+
83
+ // 获取点的颜色,可以从数据中指定或使用系列颜色
84
+ const color = p.color || this.style.color;
85
+
86
+ const pointStyle = this.graph.utils.clone(this.style.item, {
87
+ stroke: color,
88
+ fill: color
89
+ });
90
+
91
+ const pointShape = this.graph.createShape('circle', {
92
+ style: pointStyle,
93
+ center: p,
94
+ radius: radius
95
+ });
96
+
97
+ pointShape.zIndex = (pointShape.style.zIndex || 1) + 1;
98
+ return this.addShape(pointShape);
99
+ }
100
+
101
+ /**
102
+ * 生成图例
103
+ *
104
+ * @method createLegend
105
+ * @for jmScatterSeries
106
+ */
107
+ createLegend() {
108
+ // 生成图例前的图标
109
+ var style = this.graph.utils.clone(this.style);
110
+ style.stroke = style.color;
111
+ style.fill = style.color;
112
+
113
+ var shape = this.graph.createShape('circle', {
114
+ style: style,
115
+ center: {
116
+ x: this.graph.style.legend.item.shape.width / 2,
117
+ y: this.graph.style.legend.item.shape.height / 2
118
+ },
119
+ radius: this.style.radius || 5
120
+ });
121
+
122
+ this.graph.legend.append(this, shape);
123
+ }
124
+ }
125
+
126
+ export {
127
+ jmScatterSeries
128
+ }
@@ -1,19 +1,27 @@
1
-
2
1
  import { jmPath, jmList, jmControl } from 'jmgraph';
3
2
  import utils from '../common/utils.js';
4
3
 
5
4
  const ANIMATION_DATA_THRESHOLD = 100;
6
5
 
7
6
  /**
8
- * 图形基类
7
+ * 图表系列基类
8
+ *
9
+ * 所有图表类型的基类,提供通用的数据点生成、坐标轴管理、动画控制和图例生成功能。
10
+ * 子类需要实现 init() 方法来定义具体的图表绘制逻辑。
9
11
  *
10
12
  * @class jmSeries
11
13
  * @module jmChart
12
- * @param {jmChart} chart 当前图表
13
- * @param {array} mappings 图形字段映射
14
- * @param {style} style 样式
14
+ * @extends jmPath
15
+ *
16
+ * @example
17
+ * // 创建自定义图表系列
18
+ * class MySeries extends jmSeries {
19
+ * init() {
20
+ * const { points } = this.initDataPoint();
21
+ * // 绘制图形...
22
+ * }
23
+ * }
15
24
  */
16
-
17
25
  export default class jmSeries extends jmPath {
18
26
  constructor(options) {
19
27
  super(options);
@@ -27,6 +35,9 @@ export default class jmSeries extends jmPath {
27
35
  this._cache = new Map();
28
36
 
29
37
  this.xAxis = this.graph.createXAxis();
38
+ this.xAxis.init({
39
+ field: options.xField
40
+ });
30
41
 
31
42
  this.yAxis = this.yAxis || this.graph.createYAxis({
32
43
  index: this.index,
@@ -42,6 +53,7 @@ export default class jmSeries extends jmPath {
42
53
 
43
54
  /**
44
55
  * 关联访问的是chart的数据源
56
+ * @type {Array}
45
57
  */
46
58
  get data() {
47
59
  return this.graph.data;
@@ -50,7 +62,10 @@ export default class jmSeries extends jmPath {
50
62
  this.graph.data = d;
51
63
  }
52
64
 
53
- //是否启用动画效果
65
+ /**
66
+ * 是否启用动画效果
67
+ * @type {boolean}
68
+ */
54
69
  get enableAnimate() {
55
70
  if(typeof this.option.enableAnimate !== 'undefined') return !!this.option.enableAnimate;
56
71
  else {
@@ -63,42 +78,61 @@ export default class jmSeries extends jmPath {
63
78
 
64
79
  /**
65
80
  * 图例名称
66
- *
67
- * @property legendLabel
68
- * @type string
81
+ * @type {string}
69
82
  */
70
83
  legendLabel = '';
71
84
 
72
85
  /**
73
- * 当前图形下的所有子图
86
+ * 当前图形下的所有子图形状
87
+ * @type {jmList}
74
88
  */
75
89
  shapes = new jmList();
76
90
 
77
91
  /**
78
- * 关健点集合
92
+ * 关键点集合,用于交互和提示
93
+ * @type {Array}
79
94
  */
80
95
  keyPoints = [];
81
96
 
82
97
  /**
83
98
  * 标注集合
99
+ * @type {Array}
84
100
  */
85
101
  labels = [];
86
102
 
87
- // 图绑定的属性名
103
+ /**
104
+ * 图表绑定的数据字段名
105
+ * @type {string|string[]}
106
+ */
88
107
  field = '';
108
+
89
109
  /**
90
110
  * Y轴的基线跟最底层的高度
111
+ * @type {number}
91
112
  */
92
113
  baseYHeight = 0;
114
+
93
115
  /**
94
116
  * Y轴基线的Y坐标
117
+ * @type {number}
95
118
  */
96
119
  baseY = 0;
120
+
97
121
  /**
98
122
  * 当前基线Y的值,不给basey就会默认采用当前Y轴最小值
123
+ * @type {number}
99
124
  */
100
125
  baseYValue = 0;
101
126
 
127
+ /**
128
+ * 初始化数据点
129
+ *
130
+ * 根据数据生成图表的数据点,支持动画效果。
131
+ * 当数据量小于阈值(100)且启用动画时,会记录上一次的数据点用于动画过渡。
132
+ *
133
+ * @param {...any} args 传递给 createPoints 的参数
134
+ * @returns {Object} 包含 points 和 dataChanged 的对象
135
+ */
102
136
  initDataPoint(...args) {
103
137
  let dataChanged = false;
104
138
 
@@ -130,27 +164,25 @@ export default class jmSeries extends jmPath {
130
164
  }
131
165
 
132
166
  /**
133
- * 根据X轴坐标,获取它最近的数据描点
134
- * 离点最近的一个描点
135
- * @param {number} x X轴坐标
167
+ * 根据X轴坐标获取最近的数据描点
168
+ *
169
+ * @param {number} x X轴坐标
170
+ * @returns {Object|null} 最近的数据点对象
136
171
  */
137
172
  getDataPointByX(x) {
138
173
  if(!this.dataPoints) return null;
139
- // 获取最近的那个
140
- let prePoint = undefined, nextPoint = undefined; // 跟上一个点和下一个点的距离,哪个近用哪个
174
+
175
+ let prePoint = undefined, nextPoint = undefined;
141
176
  for(let i=0; i< this.dataPoints.length; i++) {
142
177
  const p = this.dataPoints[i];
143
178
  if(p.x == x) return p;
144
179
 
145
- // 上一个点
146
180
  if(p.x < x) {
147
181
  if(i === this.dataPoints.length - 1) return p;
148
182
  prePoint = p;
149
183
  }
150
184
 
151
- // 下一个点
152
185
  if(typeof nextPoint === 'undefined' && p.x > x) {
153
- // 没有上一个,只能返回这个了
154
186
  if(prePoint && x - prePoint.x < p.x - x) return prePoint;
155
187
  else return p
156
188
  }
@@ -160,7 +192,9 @@ export default class jmSeries extends jmPath {
160
192
 
161
193
  /**
162
194
  * 根据X轴值获取数据点
163
- * @param {number} xValue X轴值
195
+ *
196
+ * @param {any} xValue X轴值
197
+ * @returns {Object|null} 数据点对象
164
198
  */
165
199
  getDataPointByXValue(xValue) {
166
200
  if(!this.dataPoints) return null;
@@ -172,7 +206,13 @@ export default class jmSeries extends jmPath {
172
206
  return null;
173
207
  }
174
208
 
175
-
209
+ /**
210
+ * 重置图表系列
211
+ *
212
+ * 清除所有形状,重新初始化坐标轴和图例
213
+ *
214
+ * @returns {Object} 包含 xAxis 和 yAxis 的信息对象
215
+ */
176
216
  reset() {
177
217
  var shape;
178
218
  while(shape = this.shapes.shift()) {
@@ -188,6 +228,11 @@ export default class jmSeries extends jmPath {
188
228
  };
189
229
  }
190
230
 
231
+ /**
232
+ * 初始化坐标轴的值范围
233
+ *
234
+ * 遍历数据,计算X轴和Y轴的最大最小值
235
+ */
191
236
  initAxisValue() {
192
237
  if(!this.data || !this.data.length) return;
193
238
 
@@ -216,6 +261,14 @@ export default class jmSeries extends jmPath {
216
261
  }
217
262
  }
218
263
 
264
+ /**
265
+ * 创建数据点
266
+ *
267
+ * 根据数据源和坐标轴配置,计算每个数据点的屏幕坐标位置
268
+ *
269
+ * @param {Array} data 数据源,默认使用 this.data
270
+ * @returns {Array} 数据点数组
271
+ */
219
272
  createPoints(data) {
220
273
  data = data || this.data;
221
274
  if(!data || !data.length) return [];
@@ -281,7 +334,12 @@ export default class jmSeries extends jmPath {
281
334
  return this.dataPoints;
282
335
  }
283
336
 
284
- // 生成颜色
337
+ /**
338
+ * 生成颜色
339
+ *
340
+ * @param {Object} p 数据点对象
341
+ * @returns {string} 颜色值
342
+ */
285
343
  getColor(p) {
286
344
  if(typeof this.style.color === 'function') {
287
345
  return this.style.color.call(this, p);
@@ -293,14 +351,12 @@ export default class jmSeries extends jmPath {
293
351
 
294
352
  /**
295
353
  * 生成图例
296
- *
297
- * @method createLegend
354
+ *
355
+ * @returns {jmShape} 图例形状
298
356
  */
299
357
  createLegend() {
300
- //生成图例前的图标
301
358
  const style = this.graph.utils.clone(this.style);
302
359
  style.fill = this.getColor();
303
- //delete style.stroke;
304
360
  const shape = this.graph.createShape('rect',{
305
361
  style
306
362
  });
@@ -308,14 +364,20 @@ export default class jmSeries extends jmPath {
308
364
  return shape;
309
365
  }
310
366
 
311
- // 生成柱图的标注
367
+ /**
368
+ * 生成数据项标签
369
+ *
370
+ * 在数据点上方或指定位置显示数值标签
371
+ *
372
+ * @param {Object} point 数据点对象
373
+ * @param {string} position 标签位置 (top/bottom/left/right/inside)
374
+ */
312
375
  createItemLabel(point, position) {
313
376
  if(!this.style.label || this.style.label.show !== true) return;
314
377
 
315
378
  const text = this.option.itemLabelFormat?this.option.itemLabelFormat.call(this, point): point.yValue;
316
379
  if(!text) return;
317
380
 
318
- // v如果指定了为控件,则直接加入
319
381
  if(text instanceof jmControl) {
320
382
  this.addShape(text);
321
383
  return text;
@@ -339,9 +401,39 @@ export default class jmSeries extends jmPath {
339
401
  position: function() {
340
402
  const offh = style.offset || 5;
341
403
  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)
404
+ const position = style.position || (baseOffset > 0 ? 'top' : 'bottom');
405
+
406
+ switch(position) {
407
+ case 'top':
408
+ return {
409
+ x: point.x - size.width / 2 - barWidth,
410
+ y: point.y - size.height - offh
411
+ };
412
+ case 'bottom':
413
+ return {
414
+ x: point.x - size.width / 2 - barWidth,
415
+ y: point.y + offh
416
+ };
417
+ case 'left':
418
+ return {
419
+ x: point.x - size.width - offh - barWidth,
420
+ y: point.y - size.height / 2
421
+ };
422
+ case 'right':
423
+ return {
424
+ x: point.x + offh - barWidth,
425
+ y: point.y - size.height / 2
426
+ };
427
+ case 'inside':
428
+ return {
429
+ x: point.x - size.width / 2 - barWidth,
430
+ y: point.y + (baseOffset > 0 ? -size.height / 2 : size.height / 2)
431
+ };
432
+ default:
433
+ return {
434
+ x: point.x - size.width / 2 - barWidth,
435
+ y: baseOffset>0?(point.y - size.height - offh): (point.y + offh)
436
+ };
345
437
  }
346
438
  }
347
439
  });
@@ -350,8 +442,10 @@ export default class jmSeries extends jmPath {
350
442
  }
351
443
 
352
444
  /**
353
- * 在图上加下定制图形
354
- * @param {jmShape} shape 图形
445
+ * 在图表上添加形状
446
+ *
447
+ * @param {jmShape} shape 图形对象
448
+ * @returns {jmShape} 添加的图形对象
355
449
  */
356
450
  addShape(shape) {
357
451
  this.graph.chartArea.children.add(shape);
@@ -361,14 +455,11 @@ export default class jmSeries extends jmPath {
361
455
 
362
456
  /**
363
457
  * 获取指定事件的集合
364
- * 比如mousedown,mouseup等
365
- *
366
- * @method getEvent
458
+ *
367
459
  * @param {string} name 事件名称
368
- * @return {list} 事件委托的集合
460
+ * @returns {jmList} 事件委托的集合
369
461
  */
370
462
  getEvent(name) {
371
-
372
463
  const event = this.option? this.option[name]: null;
373
464
  if(!event) {
374
465
  return super.getEvent(name);
@@ -1,39 +1,57 @@
1
1
  import jmBarSeries from './barSeries.js';
2
2
 
3
-
4
3
  /**
5
- * 柱图
4
+ * 堆叠柱状图
5
+ *
6
+ * 堆叠柱状图用于显示多个数据系列的累积效果。
7
+ * 每个柱子由多个部分堆叠而成,每个部分代表一个数据系列。
8
+ *
9
+ * 数据格式要求:
10
+ * - field: ['value1', 'value2', 'value3'] - 多个数据字段
11
+ * - xField: 'category' - X轴字段
12
+ *
13
+ * 样式配置:
14
+ * - barWidth: 柱子宽度(像素)
15
+ * - perWidth: 柱子宽度占比(0-1),默认0.5
16
+ * - color: 颜色函数,可以根据索引返回不同颜色
17
+ *
18
+ * 特点:
19
+ * - 支持正负值堆叠
20
+ * - 正值从基线向上堆叠
21
+ * - 负值从基线向下堆叠
22
+ * - 支持动画效果
6
23
  *
7
- * @class jmBarSeries
24
+ * @class jmStackBarSeries
8
25
  * @module jmChart
26
+ * @extends jmBarSeries
9
27
  * @param {jmChart} chart 当前图表
10
28
  * @param {array} mappings 图形字段映射
11
29
  * @param {style} style 样式
12
30
  */
13
31
 
14
- //构造函数
15
32
  export default class jmStackBarSeries extends jmBarSeries {
16
33
  constructor(options) {
17
34
  super(options);
18
35
  }
36
+
19
37
  /**
20
- * 绘制当前图形
21
- *
22
- * @method beginDraw
23
- * @for jmBarSeries
38
+ * 初始化堆叠柱状图
39
+ *
40
+ * 绘制逻辑:
41
+ * 1. 遍历所有数据点
42
+ * 2. 对每个数据点的多个字段进行堆叠
43
+ * 3. 正值向上堆叠,负值向下堆叠
44
+ * 4. 支持动画效果
24
45
  */
25
46
  init() {
26
- //生成描点位
27
47
  const {points, dataChanged} = this.initDataPoint();
28
48
 
29
49
  const len = points.length;
30
50
 
31
51
  this.initWidth(len);
32
52
 
33
- // 是否正在动画中
34
- // 如果数据点多于100 个,暂时不启用动画,太慢了
35
53
  const isRunningAni = this.enableAnimate && (dataChanged || this.___animateCounter > 0 ) && len < 100;
36
- let aniIsEnd = true;// 当次是否结束动画
54
+ let aniIsEnd = true;
37
55
  const aniCount = (this.style.aniCount || 10);
38
56
 
39
57
  for(let i=0; i<len; i++) {
@@ -59,25 +77,22 @@ export default class jmStackBarSeries extends jmBarSeries {
59
77
  let startY = topStartY;
60
78
  if(p.yValue < this.baseYValue) startY = bottomStartY;
61
79
 
62
- //首先确定p1和p4,因为他们是底脚。会固定
63
80
  const p1 = {x: p.x - this.barTotalWidth / 2, y: startY };
64
81
  const p4 = {x: p1.x + this.barWidth, y: p1.y };
65
82
 
66
83
  const p2 = {x: p1.x, y: p1.y };
67
84
  const p3 = {x: p4.x, y: p1.y };
68
85
 
69
- // 如果要动画。则动态改变高度
70
86
  if(isRunningAni) {
71
87
  const step = p.height / aniCount;
72
- const offHeight = step * this.___animateCounter;// 动态计算当前高度
73
- p2.y = startY - offHeight;// 计算高度
88
+ const offHeight = step * this.___animateCounter;
89
+ p2.y = startY - offHeight;
74
90
 
75
- // 当次动画完成
76
91
  if((step >= 0 && offHeight >= p.height) || (step < 0 && offHeight <= p.height)) {
77
92
  p2.y = startY - p.height;
78
93
  }
79
94
  else {
80
- aniIsEnd = false;// 只要有一个没完成,就还没有完成动画
95
+ aniIsEnd = false;
81
96
  }
82
97
 
83
98
  p.y = p3.y = p2.y;
@@ -87,7 +102,7 @@ export default class jmStackBarSeries extends jmBarSeries {
87
102
  p.y = p3.y = p2.y;
88
103
  }
89
104
 
90
- if(p.yValue < this.baseYValue) bottomStartY = p2.y;// 下一个又从它顶部开始画
105
+ if(p.yValue < this.baseYValue) bottomStartY = p2.y;
91
106
  else topStartY = p2.y;
92
107
 
93
108
  sp.points.push(p1);
@@ -96,7 +111,6 @@ export default class jmStackBarSeries extends jmBarSeries {
96
111
  sp.points.push(p4);
97
112
  }
98
113
 
99
- // 生成标点的回调
100
114
  this.emit('onPointCreated', point);
101
115
  }
102
116
 
@@ -105,17 +119,19 @@ export default class jmStackBarSeries extends jmBarSeries {
105
119
  }
106
120
  else {
107
121
  this.___animateCounter++;
108
- // next tick 再次刷新
109
122
  this.graph.utils.requestAnimationFrame(()=>{
110
- this.needUpdate = true;//需要刷新
123
+ this.needUpdate = true;
111
124
  });
112
125
  }
113
126
  }
114
127
 
115
- // 计算最大值和最小值,一般图形直接采用最大最小值即可,有些需要多值叠加
128
+ /**
129
+ * 计算最大值和最小值
130
+ *
131
+ * 对于堆叠柱状图,需要计算所有字段的累加值
132
+ * 正值累加得到最大值,负值累加得到最小值
133
+ */
116
134
  initAxisValue() {
117
- // 计算最大最小值
118
- // 当前需要先更新axis的边界值,轴好画图
119
135
  const fields = Array.isArray(this.field)? this.field: [this.field];
120
136
 
121
137
  for(const row of this.data) {
@@ -147,4 +163,4 @@ export default class jmStackBarSeries extends jmBarSeries {
147
163
  this.xAxis.min(xv);
148
164
  }
149
165
  }
150
- }
166
+ }