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.
package/src/jmChart.js CHANGED
@@ -13,7 +13,19 @@ import jmPieSeries from './series/pieSeries.js';
13
13
  import jmRadarSeries from './series/radarSeries.js';
14
14
  import jmLineSeries from './series/lineSeries.js';
15
15
  import jmStackLineSeries from './series/stackLineSeries.js';
16
+ import jmRangeSeries from './series/rangeSeries.js';
16
17
  import jmCandlestickSeries from './series/candlestickSeries.js';
18
+ import jmScatterSeries from './series/scatterSeries.js';
19
+ import jmBubbleSeries from './series/bubbleSeries.js';
20
+ import jmHeatmapSeries from './series/heatmapSeries.js';
21
+ import jmGaugeSeries from './series/gaugeSeries.js';
22
+ import jmAreaSeries from './series/areaSeries.js';
23
+ import jmWaterfallSeries from './series/waterfallSeries.js';
24
+ import jmFunnelSeries from './series/funnelSeries.js';
25
+ import jmRingProgressSeries from './series/ringProgressSeries.js';
26
+ import jmBoxPlotSeries from './series/boxPlotSeries.js';
27
+ import jmWordCloudSeries from './series/wordCloudSeries.js';
28
+ import jmSunburstSeries from './series/sunburstSeries.js';
17
29
  import jmMarkLineManager from './core/markLine/manager';
18
30
 
19
31
  const ANIMATION_DATA_THRESHOLD = 100;
@@ -127,6 +139,33 @@ export default class jmChart extends jmGraph {
127
139
  //this.chartArea.children.add(this.tooltip);
128
140
 
129
141
  this.createXAxis();// 生成X轴
142
+
143
+ // 绑定点击事件
144
+ this.on('click', (args) => {
145
+ if(!this.option.onClick) return;
146
+ // 找到点击位置的数据点
147
+ let closestPoint = null;
148
+ let minDistance = 20; // 20px 范围,同时作为阈值,无需再比较小于阈值
149
+
150
+ this.series.each((i, serie) => {
151
+ if(!serie.dataPoints) return;
152
+ const points = serie.dataPoints;
153
+ for(let j = 0, len = points.length; j < len; j++) {
154
+ const point = points[j];
155
+ const dx = args.position.x - point.x;
156
+ const dy = args.position.y - point.y;
157
+ const distance = dx * dx + dy * dy; // 避免开方,直接比较平方
158
+ if(distance < minDistance * minDistance) {
159
+ minDistance = distance;
160
+ closestPoint = point;
161
+ }
162
+ }
163
+ });
164
+
165
+ if(closestPoint) {
166
+ this.option.onClick(closestPoint);
167
+ }
168
+ });
130
169
  }
131
170
 
132
171
  // 创建一个操作层,以免每次刷新
@@ -198,8 +237,9 @@ export default class jmChart extends jmGraph {
198
237
  delete this.xAxis;
199
238
  }
200
239
  if(this.yAxises) {
201
- for(let i in this.yAxises) {
202
- this.yAxises[i].remove();
240
+ const keys = Object.keys(this.yAxises);
241
+ for(let k = 0; k < keys.length; k++) {
242
+ this.yAxises[keys[k]].remove();
203
243
  }
204
244
  delete this.yAxises;
205
245
  }
@@ -211,20 +251,9 @@ export default class jmChart extends jmGraph {
211
251
  * @param {int} index 颜色索引
212
252
  */
213
253
  getColor(index) {
214
- const cacheKey = `color_${index}`;
215
- if(this._cache.has(cacheKey)) {
216
- return this._cache.get(cacheKey);
217
- }
218
-
219
- let color;
220
- if(index >= this.style.chartColors.length) {
221
- color = this.style.chartColors[index % this.style.chartColors.length];
222
- } else {
223
- color = this.style.chartColors[index];
224
- }
225
-
226
- this._cache.set(cacheKey, color);
227
- return color;
254
+ const colors = this.style.chartColors;
255
+ const colorIndex = index % colors.length;
256
+ return colors[colorIndex];
228
257
  }
229
258
 
230
259
  /**
@@ -245,28 +274,29 @@ export default class jmChart extends jmGraph {
245
274
  }
246
275
 
247
276
  if(this.yAxises) {
248
- for(let i in this.yAxises) {
249
- this.yAxises[i].clear();
277
+ const keys = Object.keys(this.yAxises);
278
+ for(let k = 0; k < keys.length; k++) {
279
+ this.yAxises[keys[k]].clear();
250
280
  }
251
281
  }
252
282
 
253
283
  this.barSeriesCount = 0;
284
+ // 合并两次 series 遍历为一次,减少遍历开销
285
+ const seriesInited = [];
254
286
  this.series.each((i, serie) => {
255
287
  if(!serie.style.color) {
256
288
  serie.style.color = serie.graph.getColor(i);
257
289
  }
258
290
 
259
- if(serie.graph.style.layout != 'inside') {
260
- if(serie instanceof jmBarSeries) {
291
+ if(serie instanceof jmBarSeries) {
292
+ if(serie.graph.style.layout != 'inside') {
261
293
  serie.graph.style.layout = 'inside';
262
294
  }
263
- }
264
-
265
- if(serie instanceof jmBarSeries) {
266
295
  serie.barIndex = serie.graph.barSeriesCount;
267
296
  serie.graph.barSeriesCount++;
268
297
  }
269
298
  serie.reset();
299
+ seriesInited.push(serie);
270
300
  });
271
301
 
272
302
  if(this.legend) {
@@ -274,8 +304,9 @@ export default class jmChart extends jmGraph {
274
304
  }
275
305
 
276
306
  if(this.yAxises) {
277
- for(let i in this.yAxises) {
278
- this.yAxises[i].reset();
307
+ const keys = Object.keys(this.yAxises);
308
+ for(let k = 0; k < keys.length; k++) {
309
+ this.yAxises[keys[k]].reset();
279
310
  }
280
311
  }
281
312
 
@@ -283,9 +314,9 @@ export default class jmChart extends jmGraph {
283
314
  this.xAxis.reset();
284
315
  }
285
316
 
286
- this.series.each((i, serie) => {
287
- serie.init && serie.init();
288
- });
317
+ for(let i = 0; i < seriesInited.length; i++) {
318
+ seriesInited[i].init && seriesInited[i].init();
319
+ }
289
320
  }
290
321
 
291
322
  /**
@@ -401,7 +432,19 @@ export default class jmChart extends jmGraph {
401
432
  'pie' : jmPieSeries,
402
433
  'radar' : jmRadarSeries,
403
434
  'stackLine' : jmStackLineSeries,
404
- 'candlestick': jmCandlestickSeries
435
+ 'range' : jmRangeSeries,
436
+ 'candlestick': jmCandlestickSeries,
437
+ 'scatter': jmScatterSeries,
438
+ 'bubble': jmBubbleSeries,
439
+ 'heatmap': jmHeatmapSeries,
440
+ 'gauge': jmGaugeSeries,
441
+ 'area': jmAreaSeries,
442
+ 'waterfall': jmWaterfallSeries,
443
+ 'funnel': jmFunnelSeries,
444
+ 'ringProgress': jmRingProgressSeries,
445
+ 'boxPlot': jmBoxPlotSeries,
446
+ 'wordCloud': jmWordCloudSeries,
447
+ 'sunburst': jmSunburstSeries
405
448
  };
406
449
  }
407
450
 
@@ -0,0 +1,50 @@
1
+ import jmLineSeries from './lineSeries.js';
2
+
3
+ /**
4
+ * 面积图
5
+ * 继承自折线图,默认启用区域填充
6
+ *
7
+ * @class jmAreaSeries
8
+ * @module jmChart
9
+ * @extends jmLineSeries
10
+ */
11
+ export default class jmAreaSeries extends jmLineSeries {
12
+ constructor(options) {
13
+ options.style = options.style || options.graph.style.area;
14
+ super(options);
15
+ // 面积图默认显示面积填充
16
+ this.style.area = true;
17
+ }
18
+
19
+ /**
20
+ * 生成图例
21
+ */
22
+ createLegend() {
23
+ var style = this.graph.utils.clone(this.style);
24
+ style.stroke = style.color;
25
+ style.fill = style.color;
26
+ var shape = this.graph.createShape('path',{style:style});
27
+
28
+ if(this.curve || this.style.curve) {
29
+ var p1 = {x:0,y: this.graph.style.legend.item.shape.height};
30
+ var p2 = {x:this.graph.style.legend.item.shape.width / 3,y:this.graph.style.legend.item.shape.height/3};
31
+ var p3 = {x:this.graph.style.legend.item.shape.width / 3 * 2,y:this.graph.style.legend.item.shape.height/3*2};
32
+ var p4 = {x:this.graph.style.legend.item.shape.width,y:0};
33
+
34
+ this.__bezier = this.__bezier || this.graph.createShape('bezier');
35
+ this.__bezier.cpoints = [
36
+ p1,p2,p3,p4
37
+ ];
38
+
39
+ shape.points = this.__bezier.initPoints();
40
+ }
41
+ else {
42
+ shape.points = [{
43
+ x:0,y: this.graph.style.legend.item.shape.height/2
44
+ },{
45
+ x: this.graph.style.legend.item.shape.width,y: this.graph.style.legend.item.shape.height/2
46
+ }];
47
+ }
48
+ this.graph.legend.append(this, shape);
49
+ }
50
+ }
@@ -1,7 +1,4 @@
1
- import jmSeries from './series.js';
2
-
3
- const ANIMATION_DATA_THRESHOLD = 100;
4
- const DEFAULT_ANIMATION_COUNT = 10;
1
+ import jmSeries, { ANIMATION_DATA_THRESHOLD, DEFAULT_ANIMATION_COUNT } from './series.js';
5
2
 
6
3
  /**
7
4
  * 柱图
@@ -43,8 +40,8 @@ export default class jmBarSeries extends jmSeries {
43
40
  continue;
44
41
  }
45
42
 
46
- point.style.fill = this.getColor(point);
47
- const sp = this.addShape(this.graph.createPath(null, point.style));
43
+ const style = Object.assign({}, point.style, { fill: this.getColor(point) });
44
+ const sp = this.addShape(this.graph.createPath(null, style));
48
45
 
49
46
  const p1 = {x: point.x - this.barTotalWidth / 2 + this.barWidth * this.barIndex, y: this.baseY };
50
47
  const p4 = {x: p1.x + this.barWidth, y: p1.y };
@@ -0,0 +1,254 @@
1
+ import jmSeries from './series.js';
2
+
3
+ /**
4
+ * 箱线图
5
+ *
6
+ * 箱线图用于展示数据的分布情况,包括最小值、第一四分位数(Q1)、中位数(Q2)、第三四分位数(Q3)和最大值。
7
+ * 可以直观地显示数据的集中趋势、离散程度和异常值。
8
+ *
9
+ * 数据格式要求:
10
+ * - field: ['min', 'q1', 'median', 'q3', 'max'] - 五个数值字段
11
+ * - xField: 'category' - 分类字段
12
+ *
13
+ * 样式配置:
14
+ * - boxWidth: 箱体宽度,默认自动计算
15
+ * - whiskerWidth: 须线宽度,默认 1
16
+ * - showOutliers: 是否显示异常值,默认 false
17
+ *
18
+ * @class jmBoxPlotSeries
19
+ * @module jmChart
20
+ * @extends jmSeries
21
+ *
22
+ * @example
23
+ * chart.createSeries('boxPlot', {
24
+ * field: ['min', 'q1', 'median', 'q3', 'max'],
25
+ * xField: 'category'
26
+ * });
27
+ */
28
+ export default class jmBoxPlotSeries extends jmSeries {
29
+ constructor(options) {
30
+ options.style = options.style || options.graph.style.boxPlot;
31
+ super(options);
32
+ }
33
+
34
+ /**
35
+ * 初始化箱线图
36
+ */
37
+ init() {
38
+ const data = this.data;
39
+ if(!data || !data.length) return;
40
+
41
+ const chartWidth = this.graph.chartArea.width;
42
+ const chartHeight = this.graph.chartArea.height;
43
+
44
+ const fields = Array.isArray(this.field) ? this.field : [this.field];
45
+ if(fields.length < 5) {
46
+ console.warn('箱线图需要5个字段(min, q1, median, q3, max)');
47
+ return;
48
+ }
49
+
50
+ let allValues = [];
51
+ for(const item of data) {
52
+ for(const f of fields) {
53
+ const v = item[f];
54
+ if(v != null && !isNaN(v)) {
55
+ allValues.push(v);
56
+ }
57
+ }
58
+ }
59
+
60
+ if(allValues.length === 0) return;
61
+
62
+ const minValue = Math.min(...allValues);
63
+ const maxValue = Math.max(...allValues);
64
+ const valueRange = maxValue - minValue || 1;
65
+
66
+ const boxWidth = this.style.boxWidth || (chartWidth / data.length * 0.5);
67
+ const whiskerWidth = this.style.whiskerWidth || 1;
68
+ const xField = this.option.xField || 'category';
69
+
70
+ const step = chartWidth / data.length;
71
+
72
+ for(let i = 0; i < data.length; i++) {
73
+ const item = data[i];
74
+
75
+ const min = item[fields[0]];
76
+ const q1 = item[fields[1]];
77
+ const median = item[fields[2]];
78
+ const q3 = item[fields[3]];
79
+ const max = item[fields[4]];
80
+
81
+ if([min, q1, median, q3, max].some(v => v == null || isNaN(v))) {
82
+ continue;
83
+ }
84
+
85
+ const minYPos = chartHeight - ((min - minValue) / valueRange) * chartHeight;
86
+ const q1YPos = chartHeight - ((q1 - minValue) / valueRange) * chartHeight;
87
+ const medianYPos = chartHeight - ((median - minValue) / valueRange) * chartHeight;
88
+ const q3YPos = chartHeight - ((q3 - minValue) / valueRange) * chartHeight;
89
+ const maxYPos = chartHeight - ((max - minValue) / valueRange) * chartHeight;
90
+
91
+ const centerX = step * i + step / 2;
92
+ const leftX = centerX - boxWidth / 2;
93
+ const rightX = centerX + boxWidth / 2;
94
+
95
+ const color = this.getColor(item, i);
96
+
97
+ this.createBox(leftX, rightX, q1YPos, q3YPos, color);
98
+ this.createMedianLine(leftX, rightX, medianYPos, color);
99
+ this.createWhisker(centerX, minYPos, q1YPos, maxYPos, q3YPos, color, whiskerWidth);
100
+
101
+ if(this.style.label && this.style.label.show) {
102
+ this.createBoxLabel(item, centerX, medianYPos, xField);
103
+ }
104
+ }
105
+ }
106
+
107
+ /**
108
+ * 创建箱体
109
+ */
110
+ createBox(leftX, rightX, q1Y, q3Y, color) {
111
+ const box = this.graph.createShape('rect', {
112
+ style: {
113
+ fill: this.style.boxFill || 'transparent',
114
+ stroke: color,
115
+ lineWidth: this.style.lineWidth || 1,
116
+ zIndex: 1
117
+ },
118
+ position: {
119
+ x: leftX,
120
+ y: Math.min(q1Y, q3Y)
121
+ },
122
+ width: rightX - leftX,
123
+ height: Math.abs(q3Y - q1Y)
124
+ });
125
+
126
+ this.addShape(box);
127
+ }
128
+
129
+ /**
130
+ * 创建中位数线
131
+ */
132
+ createMedianLine(leftX, rightX, medianY, color) {
133
+ const line = this.graph.createShape('line', {
134
+ style: {
135
+ stroke: color,
136
+ lineWidth: (this.style.lineWidth || 1) * 2,
137
+ zIndex: 2
138
+ },
139
+ start: { x: leftX, y: medianY },
140
+ end: { x: rightX, y: medianY }
141
+ });
142
+
143
+ this.addShape(line);
144
+ }
145
+
146
+ /**
147
+ * 创建须线
148
+ */
149
+ createWhisker(centerX, minY, q1Y, maxY, q3Y, color, whiskerWidth) {
150
+ const whiskerLength = this.style.whiskerLength || 20;
151
+
152
+ const lowerWhisker = this.graph.createShape('line', {
153
+ style: {
154
+ stroke: color,
155
+ lineWidth: whiskerWidth,
156
+ zIndex: 0
157
+ },
158
+ start: { x: centerX, y: q1Y },
159
+ end: { x: centerX, y: minY }
160
+ });
161
+ this.addShape(lowerWhisker);
162
+
163
+ const lowerCap = this.graph.createShape('line', {
164
+ style: {
165
+ stroke: color,
166
+ lineWidth: whiskerWidth,
167
+ zIndex: 0
168
+ },
169
+ start: { x: centerX - whiskerLength / 2, y: minY },
170
+ end: { x: centerX + whiskerLength / 2, y: minY }
171
+ });
172
+ this.addShape(lowerCap);
173
+
174
+ const upperWhisker = this.graph.createShape('line', {
175
+ style: {
176
+ stroke: color,
177
+ lineWidth: whiskerWidth,
178
+ zIndex: 0
179
+ },
180
+ start: { x: centerX, y: q3Y },
181
+ end: { x: centerX, y: maxY }
182
+ });
183
+ this.addShape(upperWhisker);
184
+
185
+ const upperCap = this.graph.createShape('line', {
186
+ style: {
187
+ stroke: color,
188
+ lineWidth: whiskerWidth,
189
+ zIndex: 0
190
+ },
191
+ start: { x: centerX - whiskerLength / 2, y: maxY },
192
+ end: { x: centerX + whiskerLength / 2, y: maxY }
193
+ });
194
+ this.addShape(upperCap);
195
+ }
196
+
197
+ /**
198
+ * 创建箱线图标签
199
+ */
200
+ createBoxLabel(item, centerX, medianY, xField) {
201
+ const text = this.option.itemLabelFormat
202
+ ? this.option.itemLabelFormat.call(this, { data: item })
203
+ : item[xField];
204
+
205
+ if(!text) return;
206
+
207
+ const label = this.graph.createShape('label', {
208
+ style: {
209
+ fill: this.style.label.fill || '#333',
210
+ font: this.style.label.font || '12px Arial',
211
+ textAlign: 'center',
212
+ textBaseline: 'top',
213
+ zIndex: 10
214
+ },
215
+ text: text,
216
+ position: {
217
+ x: centerX,
218
+ y: medianY + 5
219
+ }
220
+ });
221
+
222
+ this.addShape(label);
223
+ }
224
+
225
+ /**
226
+ * 获取颜色
227
+ */
228
+ getColor(item, index) {
229
+ if(typeof this.style.color === 'function') {
230
+ return this.style.color.call(this, item, index);
231
+ }
232
+ return this.style.color || this.graph.getColor(index);
233
+ }
234
+
235
+ /**
236
+ * 生成图例
237
+ */
238
+ createLegend() {
239
+ const style = this.graph.utils.clone(this.style);
240
+ style.fill = this.style.boxFill || 'transparent';
241
+ style.stroke = this.style.color || this.graph.getColor(0);
242
+
243
+ const shape = this.graph.createShape('rect', {
244
+ style: style,
245
+ position: { x: 0, y: 0 },
246
+ width: this.graph.style.legend.item.shape.width,
247
+ height: this.graph.style.legend.item.shape.height
248
+ });
249
+
250
+ this.graph.legend.append(this, shape);
251
+ }
252
+ }
253
+
254
+ export { jmBoxPlotSeries };
@@ -0,0 +1,131 @@
1
+ import jmSeries, { ANIMATION_DATA_THRESHOLD, DEFAULT_ANIMATION_COUNT } from './series.js';
2
+
3
+ /**
4
+ * 气泡图
5
+ *
6
+ * @class jmBubbleSeries
7
+ * @module jmChart
8
+ * @param {jmChart} chart 当前图表
9
+ * @param {array} mappings 图形字段映射
10
+ * @param {style} style 样式
11
+ */
12
+
13
+ export default class jmBubbleSeries extends jmSeries {
14
+ constructor(options) {
15
+ options.style = options.style || options.graph.style.bubble;
16
+ super(options);
17
+ }
18
+
19
+ /**
20
+ * 初始化气泡图
21
+ *
22
+ * @method init
23
+ * @for jmBubbleSeries
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.createBubbleItem(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 createBubbleItem
73
+ * @for jmBubbleSeries
74
+ * @param {object} p 数据点
75
+ */
76
+ createBubbleItem(p) {
77
+ // 获取气泡的大小,从数据中指定
78
+ const size = typeof p.size === 'number' ? p.size : 10;
79
+ const radius = Math.sqrt(size) * (this.style.radiusScale || 1);
80
+
81
+ // 获取气泡的颜色,可以从数据中指定或使用系列颜色
82
+ const color = p.color || this.style.color;
83
+
84
+ // 获取气泡的透明度,可以从数据中指定或使用默认值
85
+ const opacity = typeof p.opacity === 'number' ? p.opacity : (this.style.opacity || 0.6);
86
+
87
+ const bubbleStyle = this.graph.utils.clone(this.style.item, {
88
+ stroke: color,
89
+ fill: color,
90
+ opacity: opacity
91
+ });
92
+
93
+ const bubbleShape = this.graph.createShape('circle', {
94
+ style: bubbleStyle,
95
+ center: p,
96
+ radius: radius
97
+ });
98
+
99
+ bubbleShape.zIndex = (bubbleShape.style.zIndex || 1) + 1;
100
+ return this.addShape(bubbleShape);
101
+ }
102
+
103
+ /**
104
+ * 生成图例
105
+ *
106
+ * @method createLegend
107
+ * @for jmBubbleSeries
108
+ */
109
+ createLegend() {
110
+ // 生成图例前的图标
111
+ var style = this.graph.utils.clone(this.style);
112
+ style.stroke = style.color;
113
+ style.fill = style.color;
114
+ style.opacity = style.opacity || 0.6;
115
+
116
+ var shape = this.graph.createShape('circle', {
117
+ style: style,
118
+ center: {
119
+ x: this.graph.style.legend.item.shape.width / 2,
120
+ y: this.graph.style.legend.item.shape.height / 2
121
+ },
122
+ radius: this.style.radius || 5
123
+ });
124
+
125
+ this.graph.legend.append(this, shape);
126
+ }
127
+ }
128
+
129
+ export {
130
+ jmBubbleSeries
131
+ }