jmchart 1.2.12 → 1.2.15

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,191 @@
1
+ import jmSeries from './series.js';
2
+
3
+ /**
4
+ * 环形进度图
5
+ *
6
+ * 环形进度图用于展示单个或多个指标的完成进度或占比情况。
7
+ * 每个指标用一个环形表示,进度用填充的弧线表示。
8
+ *
9
+ * 数据格式要求:
10
+ * - field: 'value' - 数值字段
11
+ * - 多个指标时使用多个数据项
12
+ *
13
+ * 样式配置:
14
+ * - radius: 环形半径,默认自动计算
15
+ * - lineWidth: 环形线宽,默认 20
16
+ * - startAngle: 起始角度,默认 -90 (12点钟方向)
17
+ * - max: 最大值,默认 100
18
+ * - showLabel: 是否显示标签,默认 true
19
+ *
20
+ * @class jmRingProgressSeries
21
+ * @module jmChart
22
+ * @extends jmSeries
23
+ *
24
+ * @example
25
+ * chart.createSeries('ringProgress', {
26
+ * field: 'value',
27
+ * style: {
28
+ * lineWidth: 15,
29
+ * max: 100,
30
+ * showLabel: true
31
+ * }
32
+ * });
33
+ */
34
+ export default class jmRingProgressSeries extends jmSeries {
35
+ constructor(options) {
36
+ options.style = options.style || options.graph.style.ringProgress;
37
+ super(options);
38
+ }
39
+
40
+ /**
41
+ * 初始化环形进度图
42
+ */
43
+ init() {
44
+ const data = this.data;
45
+ if(!data || !data.length) return;
46
+
47
+ const chartWidth = this.graph.chartArea.width;
48
+ const chartHeight = this.graph.chartArea.height;
49
+ const centerX = chartWidth / 2;
50
+ const centerY = chartHeight / 2;
51
+
52
+ const maxRadius = Math.min(chartWidth, chartHeight) / 2 - 20;
53
+ const lineWidth = this.style.lineWidth || 20;
54
+ const startAngle = this.style.startAngle !== undefined ? this.style.startAngle : -90;
55
+ const maxValue = this.style.max || 100;
56
+ const field = this.field || 'value';
57
+
58
+ const len = data.length;
59
+ const ringGap = this.style.ringGap || 10;
60
+ const totalRingSpace = len * lineWidth + (len - 1) * ringGap;
61
+ const availableRadius = maxRadius - totalRingSpace / 2;
62
+
63
+ for(let i = 0; i < len; i++) {
64
+ const item = data[i];
65
+ const value = Math.max(0, Math.min(maxValue, item[field] || 0));
66
+ const ratio = value / maxValue;
67
+
68
+ const radius = availableRadius + (len - 1 - i) * (lineWidth + ringGap);
69
+
70
+ this.createRingBackground(centerX, centerY, radius, lineWidth, i);
71
+ this.createRingProgress(centerX, centerY, radius, lineWidth, startAngle, ratio, i);
72
+
73
+ if(this.style.showLabel !== false) {
74
+ this.createRingLabel(centerX, centerY, radius, value, maxValue, i, item);
75
+ }
76
+ }
77
+ }
78
+
79
+ /**
80
+ * 创建环形背景
81
+ */
82
+ createRingBackground(centerX, centerY, radius, lineWidth, index) {
83
+ const bgColor = this.style.backgroundColor || '#e0e0e0';
84
+
85
+ const bgArc = this.graph.createShape('arc', {
86
+ style: {
87
+ stroke: bgColor,
88
+ fill: 'transparent',
89
+ lineWidth: lineWidth,
90
+ zIndex: 0
91
+ },
92
+ center: { x: centerX, y: centerY },
93
+ radius: radius,
94
+ startAngle: 0,
95
+ endAngle: 360
96
+ });
97
+
98
+ this.addShape(bgArc);
99
+ }
100
+
101
+ /**
102
+ * 创建环形进度
103
+ */
104
+ createRingProgress(centerX, centerY, radius, lineWidth, startAngle, ratio, index) {
105
+ if(ratio <= 0) return;
106
+
107
+ const color = this.getColor(null, index);
108
+ const endAngle = startAngle + ratio * 360;
109
+
110
+ const progressArc = this.graph.createShape('arc', {
111
+ style: {
112
+ stroke: color,
113
+ fill: 'transparent',
114
+ lineWidth: lineWidth,
115
+ zIndex: 1,
116
+ ...this.style.progressStyle
117
+ },
118
+ center: { x: centerX, y: centerY },
119
+ radius: radius,
120
+ startAngle: startAngle,
121
+ endAngle: endAngle
122
+ });
123
+
124
+ this.addShape(progressArc);
125
+ }
126
+
127
+ /**
128
+ * 创建环形标签
129
+ */
130
+ createRingLabel(centerX, centerY, radius, value, maxValue, index, item) {
131
+ const text = this.option.itemLabelFormat
132
+ ? this.option.itemLabelFormat.call(this, { yValue: value, index, data: item })
133
+ : `${Math.round(value / maxValue * 100)}%`;
134
+
135
+ if(!text) return;
136
+
137
+ const label = this.graph.createShape('label', {
138
+ style: {
139
+ fill: this.style.labelColor || '#333',
140
+ font: this.style.labelFont || '14px Arial',
141
+ textAlign: 'center',
142
+ textBaseline: 'middle',
143
+ zIndex: 10
144
+ },
145
+ text: text,
146
+ position: {
147
+ x: centerX,
148
+ y: centerY
149
+ }
150
+ });
151
+
152
+ if(index === 0) {
153
+ this.addShape(label);
154
+ }
155
+ }
156
+
157
+ /**
158
+ * 获取颜色
159
+ */
160
+ getColor(point, index) {
161
+ if(this.style.colors && this.style.colors.length > 0) {
162
+ return this.style.colors[index % this.style.colors.length];
163
+ }
164
+ if(typeof this.style.color === 'function') {
165
+ return this.style.color.call(this, point, index);
166
+ }
167
+ return this.graph.getColor(index);
168
+ }
169
+
170
+ /**
171
+ * 生成图例
172
+ */
173
+ createLegend() {
174
+ const style = this.graph.utils.clone(this.style);
175
+ style.fill = this.style.color || this.graph.getColor(0);
176
+ style.stroke = style.fill;
177
+
178
+ const shape = this.graph.createShape('circle', {
179
+ style: style,
180
+ center: {
181
+ x: this.graph.style.legend.item.shape.width / 2,
182
+ y: this.graph.style.legend.item.shape.height / 2
183
+ },
184
+ radius: this.graph.style.legend.item.shape.height / 2
185
+ });
186
+
187
+ this.graph.legend.append(this, shape);
188
+ }
189
+ }
190
+
191
+ export { jmRingProgressSeries };
@@ -0,0 +1,125 @@
1
+ import jmSeries, { ANIMATION_DATA_THRESHOLD, DEFAULT_ANIMATION_COUNT } from './series.js';
2
+
3
+ /**
4
+ * 散点图
5
+ *
6
+ * @class jmScatterSeries
7
+ * @module jmChart
8
+ * @param {jmChart} chart 当前图表
9
+ * @param {array} mappings 图形字段映射
10
+ * @param {style} style 样式
11
+ */
12
+
13
+ export default class jmScatterSeries extends jmSeries {
14
+ constructor(options) {
15
+ options.style = options.style || options.graph.style.scatter;
16
+ super(options);
17
+ }
18
+
19
+ /**
20
+ * 初始化散点图
21
+ *
22
+ * @method init
23
+ * @for jmScatterSeries
24
+ */
25
+ init() {
26
+ const {
27
+ points,
28
+ dataChanged
29
+ } = this.initDataPoint();
30
+
31
+ const len = points.length;
32
+ if(!len) return;
33
+
34
+ this.style.stroke = this.style.color;
35
+ this.style.item.stroke = this.style.color;
36
+
37
+ const isRunningAni = this.enableAnimate && (dataChanged || this.___animateCounter > 0) && len < ANIMATION_DATA_THRESHOLD;
38
+
39
+ const aniCount = (this.style.aniCount || DEFAULT_ANIMATION_COUNT);
40
+ const aniStep = Math.floor(len / aniCount) || 1;
41
+
42
+ for(let i = 0; i < len; i++) {
43
+ const p = points[i];
44
+
45
+ if(typeof p.y === 'undefined' || p.y === null) {
46
+ continue;
47
+ }
48
+
49
+ if(isRunningAni && i > this.___animateCounter) {
50
+ break;
51
+ }
52
+
53
+ this.createPointItem(p);
54
+ this.createItemLabel(p);
55
+
56
+ this.emit('onPointCreated', p);
57
+ }
58
+
59
+ if(this.___animateCounter >= len - 1) {
60
+ this.___animateCounter = 0;
61
+ } else if(isRunningAni) {
62
+ this.___animateCounter += aniStep;
63
+ this.graph.utils.requestAnimationFrame(() => {
64
+ this.needUpdate = true;
65
+ });
66
+ }
67
+ }
68
+
69
+ /**
70
+ * 生成散点
71
+ *
72
+ * @method createPointItem
73
+ * @for jmScatterSeries
74
+ * @param {object} p 数据点
75
+ */
76
+ createPointItem(p) {
77
+ // 获取点的大小,可以从数据中指定或使用默认值
78
+ const radius = typeof p.size === 'number' ? p.size : (this.style.radius || 5);
79
+
80
+ // 获取点的颜色,可以从数据中指定或使用系列颜色
81
+ const color = p.color || this.style.color;
82
+
83
+ const pointStyle = this.graph.utils.clone(this.style.item, {
84
+ stroke: color,
85
+ fill: color
86
+ });
87
+
88
+ const pointShape = this.graph.createShape('circle', {
89
+ style: pointStyle,
90
+ center: p,
91
+ radius: radius
92
+ });
93
+
94
+ pointShape.zIndex = (pointShape.style.zIndex || 1) + 1;
95
+ return this.addShape(pointShape);
96
+ }
97
+
98
+ /**
99
+ * 生成图例
100
+ *
101
+ * @method createLegend
102
+ * @for jmScatterSeries
103
+ */
104
+ createLegend() {
105
+ // 生成图例前的图标
106
+ var style = this.graph.utils.clone(this.style);
107
+ style.stroke = style.color;
108
+ style.fill = style.color;
109
+
110
+ var shape = this.graph.createShape('circle', {
111
+ style: style,
112
+ center: {
113
+ x: this.graph.style.legend.item.shape.width / 2,
114
+ y: this.graph.style.legend.item.shape.height / 2
115
+ },
116
+ radius: this.style.radius || 5
117
+ });
118
+
119
+ this.graph.legend.append(this, shape);
120
+ }
121
+ }
122
+
123
+ export {
124
+ jmScatterSeries
125
+ }
@@ -1,19 +1,29 @@
1
-
2
1
  import { jmPath, jmList, jmControl } from 'jmgraph';
3
2
  import utils from '../common/utils.js';
4
3
 
5
- const ANIMATION_DATA_THRESHOLD = 100;
4
+ // 公共常量提取到基类,避免每个子类重复定义
5
+ export const ANIMATION_DATA_THRESHOLD = 100;
6
+ export const DEFAULT_ANIMATION_COUNT = 10;
6
7
 
7
8
  /**
8
- * 图形基类
9
+ * 图表系列基类
10
+ *
11
+ * 所有图表类型的基类,提供通用的数据点生成、坐标轴管理、动画控制和图例生成功能。
12
+ * 子类需要实现 init() 方法来定义具体的图表绘制逻辑。
9
13
  *
10
14
  * @class jmSeries
11
15
  * @module jmChart
12
- * @param {jmChart} chart 当前图表
13
- * @param {array} mappings 图形字段映射
14
- * @param {style} style 样式
16
+ * @extends jmPath
17
+ *
18
+ * @example
19
+ * // 创建自定义图表系列
20
+ * class MySeries extends jmSeries {
21
+ * init() {
22
+ * const { points } = this.initDataPoint();
23
+ * // 绘制图形...
24
+ * }
25
+ * }
15
26
  */
16
-
17
27
  export default class jmSeries extends jmPath {
18
28
  constructor(options) {
19
29
  super(options);
@@ -27,6 +37,9 @@ export default class jmSeries extends jmPath {
27
37
  this._cache = new Map();
28
38
 
29
39
  this.xAxis = this.graph.createXAxis();
40
+ this.xAxis.init({
41
+ field: options.xField
42
+ });
30
43
 
31
44
  this.yAxis = this.yAxis || this.graph.createYAxis({
32
45
  index: this.index,
@@ -42,6 +55,7 @@ export default class jmSeries extends jmPath {
42
55
 
43
56
  /**
44
57
  * 关联访问的是chart的数据源
58
+ * @type {Array}
45
59
  */
46
60
  get data() {
47
61
  return this.graph.data;
@@ -50,7 +64,10 @@ export default class jmSeries extends jmPath {
50
64
  this.graph.data = d;
51
65
  }
52
66
 
53
- //是否启用动画效果
67
+ /**
68
+ * 是否启用动画效果
69
+ * @type {boolean}
70
+ */
54
71
  get enableAnimate() {
55
72
  if(typeof this.option.enableAnimate !== 'undefined') return !!this.option.enableAnimate;
56
73
  else {
@@ -63,54 +80,80 @@ export default class jmSeries extends jmPath {
63
80
 
64
81
  /**
65
82
  * 图例名称
66
- *
67
- * @property legendLabel
68
- * @type string
83
+ * @type {string}
69
84
  */
70
85
  legendLabel = '';
71
86
 
72
87
  /**
73
- * 当前图形下的所有子图
88
+ * 当前图形下的所有子图形状
89
+ * @type {jmList}
74
90
  */
75
91
  shapes = new jmList();
76
92
 
77
93
  /**
78
- * 关健点集合
94
+ * 关键点集合,用于交互和提示
95
+ * @type {Array}
79
96
  */
80
97
  keyPoints = [];
81
98
 
82
99
  /**
83
100
  * 标注集合
101
+ * @type {Array}
84
102
  */
85
103
  labels = [];
86
104
 
87
- // 图绑定的属性名
105
+ /**
106
+ * 图表绑定的数据字段名
107
+ * @type {string|string[]}
108
+ */
88
109
  field = '';
110
+
89
111
  /**
90
112
  * Y轴的基线跟最底层的高度
113
+ * @type {number}
91
114
  */
92
115
  baseYHeight = 0;
116
+
93
117
  /**
94
118
  * Y轴基线的Y坐标
119
+ * @type {number}
95
120
  */
96
121
  baseY = 0;
122
+
97
123
  /**
98
124
  * 当前基线Y的值,不给basey就会默认采用当前Y轴最小值
125
+ * @type {number}
99
126
  */
100
127
  baseYValue = 0;
101
128
 
129
+ /**
130
+ * 初始化数据点
131
+ *
132
+ * 根据数据生成图表的数据点,支持动画效果。
133
+ * 当数据量小于阈值(100)且启用动画时,会记录上一次的数据点用于动画过渡。
134
+ *
135
+ * @param {...any} args 传递给 createPoints 的参数
136
+ * @returns {Object} 包含 points 和 dataChanged 的对象
137
+ */
102
138
  initDataPoint(...args) {
103
139
  let dataChanged = false;
104
140
 
105
141
  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
- });
142
+ // 仅在数据引用变化时才做深度比较
143
+ if(this._lastData !== this.data) {
144
+ this.lastPoints = this.graph.utils.clone(this.dataPoints, null, true, (obj) => {
145
+ if(obj instanceof jmControl) return obj;
146
+ });
109
147
 
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
- });
148
+ this.dataPoints = this.createPoints(...args);
149
+ dataChanged = utils.arrayIsChange(this.lastPoints, this.dataPoints, (s, t) => {
150
+ return s.x === t.x && s.y === t.y;
151
+ });
152
+ this._lastData = this.data;
153
+ }
154
+ else {
155
+ this.dataPoints = this.createPoints(...args);
156
+ }
114
157
 
115
158
  if(dataChanged) {
116
159
  this.___animateCounter = 0;
@@ -130,27 +173,25 @@ export default class jmSeries extends jmPath {
130
173
  }
131
174
 
132
175
  /**
133
- * 根据X轴坐标,获取它最近的数据描点
134
- * 离点最近的一个描点
135
- * @param {number} x X轴坐标
176
+ * 根据X轴坐标获取最近的数据描点
177
+ *
178
+ * @param {number} x X轴坐标
179
+ * @returns {Object|null} 最近的数据点对象
136
180
  */
137
181
  getDataPointByX(x) {
138
182
  if(!this.dataPoints) return null;
139
- // 获取最近的那个
140
- let prePoint = undefined, nextPoint = undefined; // 跟上一个点和下一个点的距离,哪个近用哪个
183
+
184
+ let prePoint = undefined, nextPoint = undefined;
141
185
  for(let i=0; i< this.dataPoints.length; i++) {
142
186
  const p = this.dataPoints[i];
143
187
  if(p.x == x) return p;
144
188
 
145
- // 上一个点
146
189
  if(p.x < x) {
147
190
  if(i === this.dataPoints.length - 1) return p;
148
191
  prePoint = p;
149
192
  }
150
193
 
151
- // 下一个点
152
194
  if(typeof nextPoint === 'undefined' && p.x > x) {
153
- // 没有上一个,只能返回这个了
154
195
  if(prePoint && x - prePoint.x < p.x - x) return prePoint;
155
196
  else return p
156
197
  }
@@ -160,7 +201,9 @@ export default class jmSeries extends jmPath {
160
201
 
161
202
  /**
162
203
  * 根据X轴值获取数据点
163
- * @param {number} xValue X轴值
204
+ *
205
+ * @param {any} xValue X轴值
206
+ * @returns {Object|null} 数据点对象
164
207
  */
165
208
  getDataPointByXValue(xValue) {
166
209
  if(!this.dataPoints) return null;
@@ -172,7 +215,13 @@ export default class jmSeries extends jmPath {
172
215
  return null;
173
216
  }
174
217
 
175
-
218
+ /**
219
+ * 重置图表系列
220
+ *
221
+ * 清除所有形状,重新初始化坐标轴和图例
222
+ *
223
+ * @returns {Object} 包含 xAxis 和 yAxis 的信息对象
224
+ */
176
225
  reset() {
177
226
  var shape;
178
227
  while(shape = this.shapes.shift()) {
@@ -188,6 +237,11 @@ export default class jmSeries extends jmPath {
188
237
  };
189
238
  }
190
239
 
240
+ /**
241
+ * 初始化坐标轴的值范围
242
+ *
243
+ * 遍历数据,计算X轴和Y轴的最大最小值
244
+ */
191
245
  initAxisValue() {
192
246
  if(!this.data || !this.data.length) return;
193
247
 
@@ -216,6 +270,14 @@ export default class jmSeries extends jmPath {
216
270
  }
217
271
  }
218
272
 
273
+ /**
274
+ * 创建数据点
275
+ *
276
+ * 根据数据源和坐标轴配置,计算每个数据点的屏幕坐标位置
277
+ *
278
+ * @param {Array} data 数据源,默认使用 this.data
279
+ * @returns {Array} 数据点数组
280
+ */
219
281
  createPoints(data) {
220
282
  data = data || this.data;
221
283
  if(!data || !data.length) return [];
@@ -241,7 +303,7 @@ export default class jmSeries extends jmPath {
241
303
  xValue: xv,
242
304
  xLabel: xv,
243
305
  points: [],
244
- style: this.graph.utils.clone(this.style),
306
+ style: this.style, // 共享引用,避免深拷贝;子类如需修改应自行创建副本
245
307
  };
246
308
 
247
309
  p.x = xstep * i + this.xAxis.labelStart;
@@ -281,7 +343,12 @@ export default class jmSeries extends jmPath {
281
343
  return this.dataPoints;
282
344
  }
283
345
 
284
- // 生成颜色
346
+ /**
347
+ * 生成颜色
348
+ *
349
+ * @param {Object} p 数据点对象
350
+ * @returns {string} 颜色值
351
+ */
285
352
  getColor(p) {
286
353
  if(typeof this.style.color === 'function') {
287
354
  return this.style.color.call(this, p);
@@ -293,14 +360,12 @@ export default class jmSeries extends jmPath {
293
360
 
294
361
  /**
295
362
  * 生成图例
296
- *
297
- * @method createLegend
363
+ *
364
+ * @returns {jmShape} 图例形状
298
365
  */
299
366
  createLegend() {
300
- //生成图例前的图标
301
367
  const style = this.graph.utils.clone(this.style);
302
368
  style.fill = this.getColor();
303
- //delete style.stroke;
304
369
  const shape = this.graph.createShape('rect',{
305
370
  style
306
371
  });
@@ -308,14 +373,20 @@ export default class jmSeries extends jmPath {
308
373
  return shape;
309
374
  }
310
375
 
311
- // 生成柱图的标注
376
+ /**
377
+ * 生成数据项标签
378
+ *
379
+ * 在数据点上方或指定位置显示数值标签
380
+ *
381
+ * @param {Object} point 数据点对象
382
+ * @param {string} position 标签位置 (top/bottom/left/right/inside)
383
+ */
312
384
  createItemLabel(point, position) {
313
385
  if(!this.style.label || this.style.label.show !== true) return;
314
386
 
315
387
  const text = this.option.itemLabelFormat?this.option.itemLabelFormat.call(this, point): point.yValue;
316
388
  if(!text) return;
317
389
 
318
- // v如果指定了为控件,则直接加入
319
390
  if(text instanceof jmControl) {
320
391
  this.addShape(text);
321
392
  return text;
@@ -339,9 +410,39 @@ export default class jmSeries extends jmPath {
339
410
  position: function() {
340
411
  const offh = style.offset || 5;
341
412
  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)
413
+ const position = style.position || (baseOffset > 0 ? 'top' : 'bottom');
414
+
415
+ switch(position) {
416
+ case 'top':
417
+ return {
418
+ x: point.x - size.width / 2 - barWidth,
419
+ y: point.y - size.height - offh
420
+ };
421
+ case 'bottom':
422
+ return {
423
+ x: point.x - size.width / 2 - barWidth,
424
+ y: point.y + offh
425
+ };
426
+ case 'left':
427
+ return {
428
+ x: point.x - size.width - offh - barWidth,
429
+ y: point.y - size.height / 2
430
+ };
431
+ case 'right':
432
+ return {
433
+ x: point.x + offh - barWidth,
434
+ y: point.y - size.height / 2
435
+ };
436
+ case 'inside':
437
+ return {
438
+ x: point.x - size.width / 2 - barWidth,
439
+ y: point.y + (baseOffset > 0 ? -size.height / 2 : size.height / 2)
440
+ };
441
+ default:
442
+ return {
443
+ x: point.x - size.width / 2 - barWidth,
444
+ y: baseOffset>0?(point.y - size.height - offh): (point.y + offh)
445
+ };
345
446
  }
346
447
  }
347
448
  });
@@ -350,8 +451,10 @@ export default class jmSeries extends jmPath {
350
451
  }
351
452
 
352
453
  /**
353
- * 在图上加下定制图形
354
- * @param {jmShape} shape 图形
454
+ * 在图表上添加形状
455
+ *
456
+ * @param {jmShape} shape 图形对象
457
+ * @returns {jmShape} 添加的图形对象
355
458
  */
356
459
  addShape(shape) {
357
460
  this.graph.chartArea.children.add(shape);
@@ -361,14 +464,11 @@ export default class jmSeries extends jmPath {
361
464
 
362
465
  /**
363
466
  * 获取指定事件的集合
364
- * 比如mousedown,mouseup等
365
- *
366
- * @method getEvent
467
+ *
367
468
  * @param {string} name 事件名称
368
- * @return {list} 事件委托的集合
469
+ * @returns {jmList} 事件委托的集合
369
470
  */
370
471
  getEvent(name) {
371
-
372
472
  const event = this.option? this.option[name]: null;
373
473
  if(!event) {
374
474
  return super.getEvent(name);