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,273 @@
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 jmAreaSeries
10
+ * @module jmChart
11
+ * @param {jmChart} chart 当前图表
12
+ * @param {array} mappings 图形字段映射
13
+ * @param {style} style 样式
14
+ */
15
+
16
+ export default class jmAreaSeries extends jmSeries {
17
+ constructor(options) {
18
+ options.style = options.style || options.graph.style.area;
19
+ super(options);
20
+ // 面积图默认显示面积填充
21
+ this.style.area = true;
22
+ }
23
+
24
+ /**
25
+ * 初始化面积图
26
+ *
27
+ * @method init
28
+ * @for jmAreaSeries
29
+ */
30
+ init() {
31
+ const {
32
+ points,
33
+ dataChanged
34
+ } = this.initDataPoint();
35
+
36
+ const len = points.length;
37
+ if(!len) return;
38
+
39
+ this.style.stroke = this.style.color;
40
+ this.style.item.stroke = this.style.color;
41
+
42
+ const isRunningAni = this.enableAnimate && (dataChanged || this.___animateCounter > 0) && len < ANIMATION_DATA_THRESHOLD;
43
+
44
+ let shapePoints = [];
45
+ const aniCount = (this.style.aniCount || DEFAULT_ANIMATION_COUNT);
46
+ const aniStep = Math.floor(len / aniCount) || 1;
47
+
48
+ for(let i = 0; i < len; i++) {
49
+ const p = points[i];
50
+
51
+ if(typeof p.y === 'undefined' || p.y === null) {
52
+ continue;
53
+ }
54
+
55
+ if(isRunningAni && i > this.___animateCounter) {
56
+ break;
57
+ }
58
+
59
+ if(this.style.showItem) {
60
+ this.createPointItem(p);
61
+ }
62
+
63
+ if(this.style.curve) {
64
+ shapePoints = this.createCurePoints(shapePoints, p);
65
+ } else if(this.style.lineType === 'dotted') {
66
+ shapePoints = this.createDotLine(shapePoints, p);
67
+ }
68
+
69
+ shapePoints.push(p);
70
+
71
+ this.createItemLabel(p);
72
+
73
+ this.emit('onPointCreated', p);
74
+ }
75
+
76
+ if(this.___animateCounter >= len - 1) {
77
+ this.___animateCounter = 0;
78
+ } else if(isRunningAni) {
79
+ this.___animateCounter += aniStep;
80
+ this.graph.utils.requestAnimationFrame(() => {
81
+ this.needUpdate = true;
82
+ });
83
+ }
84
+
85
+ this.points = shapePoints;
86
+ this.createArea(shapePoints);
87
+ }
88
+
89
+ /**
90
+ * 生成点的小圆圈
91
+ *
92
+ * @method createPointItem
93
+ * @for jmAreaSeries
94
+ * @param {object} p 数据点
95
+ */
96
+ createPointItem(p) {
97
+ const pointShape = this.graph.createShape('circle', {
98
+ style: this.style.item,
99
+ center: p,
100
+ radius: this.style.radius || 3
101
+ });
102
+
103
+ pointShape.zIndex = (pointShape.style.zIndex || 1) + 1;
104
+ return this.addShape(pointShape);
105
+ }
106
+
107
+ /**
108
+ * 创建曲线点
109
+ *
110
+ * @method createCurePoints
111
+ * @for jmAreaSeries
112
+ * @param {array} shapePoints 形状点数组
113
+ * @param {object} p 当前点
114
+ * @return {array} 新的形状点数组
115
+ */
116
+ createCurePoints(shapePoints, p) {
117
+ const startPoint = shapePoints[shapePoints.length - 1];
118
+
119
+ if(!startPoint || !p) return shapePoints;
120
+
121
+ if(startPoint.x === undefined || startPoint.x === null ||
122
+ startPoint.y === undefined || startPoint.y === null ||
123
+ p.x === undefined || p.x === null ||
124
+ p.y === undefined || p.y === null) {
125
+ return shapePoints;
126
+ }
127
+
128
+ const p1 = {x: startPoint.x + (p.x - startPoint.x) / 5, y: startPoint.y};
129
+ const p2 = {x: startPoint.x + (p.x - startPoint.x) / 2, y: p.y - (p.y - startPoint.y) / 2};
130
+ const p3 = {x: p.x - (p.x - startPoint.x) / 5, y: p.y};
131
+
132
+ this.__bezier = this.__bezier || this.graph.createShape('bezier');
133
+ this.__bezier.cpoints = [
134
+ startPoint,
135
+ p1,
136
+ p2,
137
+ p3,
138
+ p
139
+ ];
140
+
141
+ const bzpoints = this.__bezier.initPoints();
142
+ if(bzpoints && bzpoints.length) {
143
+ shapePoints = shapePoints.concat(bzpoints);
144
+ }
145
+
146
+ return shapePoints;
147
+ }
148
+
149
+ /**
150
+ * 创建虚线点
151
+ *
152
+ * @method createDotLine
153
+ * @for jmAreaSeries
154
+ * @param {array} shapePoints 形状点数组
155
+ * @param {object} p 当前点
156
+ * @return {array} 新的形状点数组
157
+ */
158
+ createDotLine(shapePoints, p) {
159
+ const startPoint = shapePoints[shapePoints.length - 1];
160
+
161
+ if(!startPoint || !p) return shapePoints;
162
+
163
+ if(startPoint.x === undefined || startPoint.x === null ||
164
+ startPoint.y === undefined || startPoint.y === null ||
165
+ p.x === undefined || p.x === null ||
166
+ p.y === undefined || p.y === null) {
167
+ return shapePoints;
168
+ }
169
+
170
+ this.__line = this.__line || this.graph.createShape('line', {
171
+ style: this.style,
172
+ });
173
+ this.__line.start = startPoint;
174
+ this.__line.end = p;
175
+
176
+ const dots = this.__line.initPoints();
177
+ if(dots && dots.length) {
178
+ shapePoints = shapePoints.concat(dots);
179
+ }
180
+
181
+ return shapePoints;
182
+ }
183
+
184
+ /**
185
+ * 生成面积效果
186
+ *
187
+ * @method createArea
188
+ * @for jmAreaSeries
189
+ * @param {array} points 数据点数组
190
+ * @param {boolean} needClosePoint 是否需要闭合点
191
+ */
192
+ createArea(points, needClosePoint = true) {
193
+ // 有指定绘制区域效果才展示
194
+ if(!this.style.area || points.length < 2) return;
195
+
196
+ const start = points[0];
197
+ const end = points[points.length - 1];
198
+
199
+ const style = this.graph.utils.clone(this.style.area, {}, true);
200
+ // 连框颜色如果没指定,就透明
201
+ style.stroke = style.stroke || 'transparent';
202
+
203
+ if(!style.fill) {
204
+ const color = this.graph.utils.hexToRGBA(this.style.stroke);
205
+ style.fill = `linear-gradient(50% 0 50% 100%,
206
+ rgba(${color.r},${color.g},${color.b}, 0) 1,
207
+ rgba(${color.r},${color.g},${color.b}, 0.1) 0.7,
208
+ rgba(${color.r},${color.g},${color.b}, 0.3) 0)`;
209
+ }
210
+ else if(typeof style.fill === 'function') {
211
+ style.fill = style.fill.call(this, style);
212
+ }
213
+ const area = this.graph.createShape('path', {
214
+ points: this.graph.utils.clone(points, true),
215
+ style,
216
+ width: this.graph.chartArea.width,
217
+ height: this.graph.chartArea.height
218
+ });
219
+
220
+ // 在点集合前后加上落地到X轴的点就可以组成一个封闭的图形area
221
+ if(needClosePoint) {
222
+ area.points.unshift({
223
+ x: start.x,
224
+ y: this.baseY
225
+ });
226
+ area.points.push({
227
+ x: end.x,
228
+ y: this.baseY
229
+ });
230
+ }
231
+
232
+ this.addShape(area);
233
+ }
234
+
235
+ /**
236
+ * 生成图例
237
+ *
238
+ * @method createLegend
239
+ * @for jmAreaSeries
240
+ */
241
+ createLegend() {
242
+ // 生成图例前的图标
243
+ var style = this.graph.utils.clone(this.style);
244
+ style.stroke = style.color;
245
+ var shape = this.graph.createShape('path',{style:style});
246
+
247
+ if(this.curve || this.style.curve) {
248
+ var p1 = {x:0,y: this.graph.style.legend.item.shape.height};
249
+ var p2 = {x:this.graph.style.legend.item.shape.width / 3,y:this.graph.style.legend.item.shape.height/3};
250
+ var p3 = {x:this.graph.style.legend.item.shape.width / 3 * 2,y:this.graph.style.legend.item.shape.height/3*2};
251
+ var p4 = {x:this.graph.style.legend.item.shape.width,y:0};
252
+
253
+ this.__bezier = this.__bezier || this.graph.createShape('bezier');
254
+ this.__bezier.cpoints = [
255
+ p1,p2,p3,p4
256
+ ];//设置控制点
257
+
258
+ shape.points = this.__bezier.initPoints();
259
+ }
260
+ else {
261
+ shape.points = [{
262
+ x:0,y: this.graph.style.legend.item.shape.height/2
263
+ },{
264
+ x: this.graph.style.legend.item.shape.width,y: this.graph.style.legend.item.shape.height/2
265
+ }];
266
+ }
267
+ this.graph.legend.append(this, shape);
268
+ }
269
+ }
270
+
271
+ export {
272
+ jmAreaSeries
273
+ }
@@ -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,134 @@
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 jmBubbleSeries
10
+ * @module jmChart
11
+ * @param {jmChart} chart 当前图表
12
+ * @param {array} mappings 图形字段映射
13
+ * @param {style} style 样式
14
+ */
15
+
16
+ export default class jmBubbleSeries extends jmSeries {
17
+ constructor(options) {
18
+ options.style = options.style || options.graph.style.bubble;
19
+ super(options);
20
+ }
21
+
22
+ /**
23
+ * 初始化气泡图
24
+ *
25
+ * @method init
26
+ * @for jmBubbleSeries
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.createBubbleItem(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 createBubbleItem
76
+ * @for jmBubbleSeries
77
+ * @param {object} p 数据点
78
+ */
79
+ createBubbleItem(p) {
80
+ // 获取气泡的大小,从数据中指定
81
+ const size = typeof p.size === 'number' ? p.size : 10;
82
+ const radius = Math.sqrt(size) * (this.style.radiusScale || 1);
83
+
84
+ // 获取气泡的颜色,可以从数据中指定或使用系列颜色
85
+ const color = p.color || this.style.color;
86
+
87
+ // 获取气泡的透明度,可以从数据中指定或使用默认值
88
+ const opacity = typeof p.opacity === 'number' ? p.opacity : (this.style.opacity || 0.6);
89
+
90
+ const bubbleStyle = this.graph.utils.clone(this.style.item, {
91
+ stroke: color,
92
+ fill: color,
93
+ opacity: opacity
94
+ });
95
+
96
+ const bubbleShape = this.graph.createShape('circle', {
97
+ style: bubbleStyle,
98
+ center: p,
99
+ radius: radius
100
+ });
101
+
102
+ bubbleShape.zIndex = (bubbleShape.style.zIndex || 1) + 1;
103
+ return this.addShape(bubbleShape);
104
+ }
105
+
106
+ /**
107
+ * 生成图例
108
+ *
109
+ * @method createLegend
110
+ * @for jmBubbleSeries
111
+ */
112
+ createLegend() {
113
+ // 生成图例前的图标
114
+ var style = this.graph.utils.clone(this.style);
115
+ style.stroke = style.color;
116
+ style.fill = style.color;
117
+ style.opacity = style.opacity || 0.6;
118
+
119
+ var shape = this.graph.createShape('circle', {
120
+ style: style,
121
+ center: {
122
+ x: this.graph.style.legend.item.shape.width / 2,
123
+ y: this.graph.style.legend.item.shape.height / 2
124
+ },
125
+ radius: this.style.radius || 5
126
+ });
127
+
128
+ this.graph.legend.append(this, shape);
129
+ }
130
+ }
131
+
132
+ export {
133
+ jmBubbleSeries
134
+ }