jmchart 1.2.11 → 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.
@@ -1,143 +1,19 @@
1
-
2
- import jmLineSeries from './lineSeries.js';
1
+ import jmRangeSeries from './rangeSeries.js';
3
2
 
4
3
  /**
5
- * 二条线组成的区域图表
6
- *
4
+ * 堆叠折线图(已废弃,请使用范围图)
5
+ *
6
+ * @deprecated 此类已废弃,请使用 jmRangeSeries(范围图)代替
7
7
  * @class jmStackLineSeries
8
8
  * @module jmChart
9
- * @param {jmChart} chart 当前图表
10
- * @param {array} mappings 图形字段映射
11
- * @param {style} style 样式
9
+ * @extends jmRangeSeries
12
10
  */
13
11
 
14
- //构造函数
15
- export default class jmStackLineSeries extends jmLineSeries {
12
+ export default class jmStackLineSeries extends jmRangeSeries {
16
13
  constructor(options) {
17
- options.style = options.style || options.graph.style.stackLine;
14
+ console.warn('jmStackLineSeries 已废弃,请使用 jmRangeSeries(范围图)代替');
18
15
  super(options);
19
16
  }
20
-
21
- /**
22
- * 绘制图形前 初始化线条
23
- *
24
- * @method preDraw
25
- * @for jmLineSeries
26
- */
27
- init() {
28
- //生成描点位
29
- const {
30
- points,
31
- dataChanged
32
- } = this.initDataPoint();
33
-
34
- //去除多余的线条
35
- //当数据源线条数比现有的少时,删除多余的线条
36
- const len = points.length;
37
-
38
- //设定其填充颜色
39
- //if(!this.style.fill) this.style.fill = jmUtils.toColor(this.style.stroke,null,null,20);
40
- this.style.stroke = this.style.color;
41
- //是否启用动画效果
42
- //var ani = typeof this.enableAnimate === 'undefined'? this.graph.enableAnimate: this.enableAnimate;
43
- this.style.item.stroke = this.style.color;
44
-
45
- // 是否正在动画中
46
- // 如果数据点多于100 个,暂时不启用动画,太慢了
47
- const isRunningAni = this.enableAnimate && (dataChanged || this.___animateCounter > 0 );
48
-
49
- let startShapePoints = []; // 计算出来的曲线点集合
50
- let endShapePoints = []; // 计算出来的曲线点集合
51
- const aniCount = (this.style.aniCount || 10);
52
- const aniStep = Math.floor(len / aniCount) || 1;// 每次动画播放点个数
53
-
54
- for(let i=0; i< len;i++) {
55
- const p = points[i];
56
-
57
- if(isRunningAni) {
58
- if(i > this.___animateCounter) {
59
- break;
60
- }
61
- }
62
-
63
- // 是否显示数值点圆
64
- if(this.style.showItem) {
65
- this.createPointItem(p.points[0]);
66
- this.createPointItem(p.points[1]);
67
- }
68
- // 平滑曲线
69
- if(this.style.curve) {
70
- startShapePoints = this.createCurePoints(startShapePoints, p.points[0]);
71
- endShapePoints = this.createCurePoints(endShapePoints, p.points[1]);
72
- }
73
- // 如果是虚线
74
- else if(this.style.lineType === 'dotted') {
75
- startShapePoints = this.createDotLine(startShapePoints, p.points[0]);
76
- endShapePoints = this.createDotLine(endShapePoints, p.points[1]);
77
- }
78
-
79
- startShapePoints.push(p.points[0]);
80
- endShapePoints.push(p.points[1]);
81
-
82
-
83
- // 生成标点的回调
84
- this.emit('onPointCreated', p);
85
- }
86
-
87
- // 如果所有都已经结束,则重置成初始化状态
88
- if(this.___animateCounter >= len - 1) {
89
- this.___animateCounter = 0;
90
- }
91
- else if(isRunningAni) {
92
- this.___animateCounter += aniStep;
93
- // next tick 再次刷新
94
- this.graph.utils.requestAnimationFrame(()=>{
95
- this.needUpdate = true;//需要刷新
96
- });
97
- }
98
- if(endShapePoints.length) endShapePoints[0].m = true;// 第二条线重新开始画
99
- this.points = startShapePoints.concat(endShapePoints);
100
- // 仓建区域效果 这里的endShapePoints要倒过来画,才能形成一个封闭区域
101
- const areaPoints = startShapePoints.concat(endShapePoints.reverse());
102
- const areaEnd = areaPoints[areaPoints.length - 1] = this.graph.utils.clone(areaPoints[areaPoints.length - 1]);
103
- areaEnd.m = false;
104
- this.createArea(areaPoints, false);
105
- }
106
-
107
- /**
108
- * 生成图例
109
- *
110
- * @method createLegend
111
- */
112
- createLegend() {
113
-
114
- //生成图例前的图标
115
- var style = this.graph.utils.clone(this.style);
116
- style.stroke = style.color;
117
- var shape = this.graph.createShape('path',{style:style});
118
-
119
- if(this.curve || this.style.curve) {
120
- var p1 = {x:0,y: this.graph.style.legend.item.shape.height};
121
- var p2 = {x:this.graph.style.legend.item.shape.width / 3,y:this.graph.style.legend.item.shape.height/3};
122
- var p3 = {x:this.graph.style.legend.item.shape.width / 3 * 2,y:this.graph.style.legend.item.shape.height/3*2};
123
- var p4 = {x:this.graph.style.legend.item.shape.width,y:0};
124
-
125
- this.__bezier = this.__bezier || this.graph.createShape('bezier');
126
- this.__bezier.cpoints = [
127
- p1,p2,p3,p4
128
- ];//设置控制点
129
-
130
- shape.points = this.__bezier.initPoints();
131
- }
132
- else {
133
- shape.points = [{
134
- x:0,y: this.graph.style.legend.item.shape.height/2
135
- },{
136
- x: this.graph.style.legend.item.shape.width,y: this.graph.style.legend.item.shape.height/2
137
- }];
138
- }
139
- this.graph.legend.append(this, shape);
140
- }
141
17
  }
142
18
 
143
19
  export {
@@ -0,0 +1,240 @@
1
+ import jmSeries from './series.js';
2
+
3
+ /**
4
+ * 旭日图
5
+ *
6
+ * 旭日图用于展示层级数据的占比关系,由多个同心圆环组成,每个圆环代表一个层级。
7
+ * 内层圆环是外层圆环的父级,扇形角度代表数据占比。
8
+ *
9
+ * 数据格式要求:
10
+ * - 树形结构数据,每个节点包含 name、value 和 children
11
+ *
12
+ * 样式配置:
13
+ * - innerRadius: 内圆半径,默认 0
14
+ * - startAngle: 起始角度,默认 0
15
+ * - showLabels: 是否显示标签,默认 true
16
+ *
17
+ * @class jmSunburstSeries
18
+ * @module jmChart
19
+ * @extends jmSeries
20
+ *
21
+ * @example
22
+ * chart.createSeries('sunburst', {
23
+ * data: [{
24
+ * name: 'Root',
25
+ * children: [
26
+ * { name: 'A', value: 30 },
27
+ * { name: 'B', value: 70 }
28
+ * ]
29
+ * }]
30
+ * });
31
+ */
32
+ export default class jmSunburstSeries extends jmSeries {
33
+ constructor(options) {
34
+ options.style = options.style || options.graph.style.sunburst;
35
+ super(options);
36
+ this.maxDepth = 0;
37
+ }
38
+
39
+ /**
40
+ * 初始化旭日图
41
+ */
42
+ init() {
43
+ const data = this.option.data || this.data;
44
+ if(!data || !data.length) return;
45
+
46
+ const chartWidth = this.graph.chartArea.width;
47
+ const chartHeight = this.graph.chartArea.height;
48
+ const centerX = chartWidth / 2;
49
+ const centerY = chartHeight / 2;
50
+ const maxRadius = Math.min(chartWidth, chartHeight) / 2 - 20;
51
+
52
+ this.maxDepth = this.calculateMaxDepth(data);
53
+ if(this.maxDepth === 0) return;
54
+
55
+ const ringWidth = maxRadius / this.maxDepth;
56
+ const innerRadius = this.style.innerRadius || 0;
57
+
58
+ this.drawLevel(data, centerX, centerY, innerRadius, ringWidth, 0, 360, 0);
59
+
60
+ if(this.style.showCenter !== false && innerRadius > 0) {
61
+ this.drawCenter(centerX, centerY, innerRadius);
62
+ }
63
+ }
64
+
65
+ /**
66
+ * 计算最大深度
67
+ */
68
+ calculateMaxDepth(data, depth = 1) {
69
+ let maxDepth = depth;
70
+ for(const node of data) {
71
+ if(node.children && node.children.length > 0) {
72
+ const childDepth = this.calculateMaxDepth(node.children, depth + 1);
73
+ maxDepth = Math.max(maxDepth, childDepth);
74
+ }
75
+ }
76
+ return maxDepth;
77
+ }
78
+
79
+ /**
80
+ * 计算节点总值
81
+ */
82
+ calculateTotalValue(data) {
83
+ let total = 0;
84
+ for(const node of data) {
85
+ if(node.children && node.children.length > 0) {
86
+ total += this.calculateTotalValue(node.children);
87
+ } else {
88
+ total += node.value || 0;
89
+ }
90
+ }
91
+ return total;
92
+ }
93
+
94
+ /**
95
+ * 绘制层级
96
+ */
97
+ drawLevel(data, centerX, centerY, innerRadius, ringWidth, startAngle, totalAngle, level) {
98
+ const total = this.calculateTotalValue(data);
99
+ if(total === 0) return;
100
+
101
+ let currentAngle = startAngle;
102
+ const outerRadius = innerRadius + ringWidth;
103
+
104
+ for(const node of data) {
105
+ let nodeValue = 0;
106
+ if(node.children && node.children.length > 0) {
107
+ nodeValue = this.calculateTotalValue(node.children);
108
+ } else {
109
+ nodeValue = node.value || 0;
110
+ }
111
+
112
+ const angleRatio = nodeValue / total;
113
+ const nodeAngle = totalAngle * angleRatio;
114
+ const endAngle = currentAngle + nodeAngle;
115
+
116
+ if(nodeAngle > 0.1) {
117
+ const color = this.getColor(node, level);
118
+
119
+ this.drawArc(centerX, centerY, innerRadius, outerRadius, currentAngle, endAngle, color, node);
120
+
121
+ if(this.style.showLabels !== false && nodeAngle > 10) {
122
+ this.drawLabel(centerX, centerY, innerRadius, outerRadius, currentAngle, endAngle, node);
123
+ }
124
+
125
+ if(node.children && node.children.length > 0) {
126
+ this.drawLevel(node.children, centerX, centerY, outerRadius, ringWidth, currentAngle, nodeAngle, level + 1);
127
+ }
128
+ }
129
+
130
+ currentAngle = endAngle;
131
+ }
132
+ }
133
+
134
+ /**
135
+ * 绘制扇形
136
+ */
137
+ drawArc(centerX, centerY, innerRadius, outerRadius, startAngle, endAngle, color, node) {
138
+ const arc = this.graph.createShape('arc', {
139
+ style: {
140
+ fill: color,
141
+ stroke: this.style.stroke || '#fff',
142
+ lineWidth: this.style.lineWidth || 1,
143
+ zIndex: 1
144
+ },
145
+ center: { x: centerX, y: centerY },
146
+ radius: outerRadius,
147
+ innerRadius: innerRadius,
148
+ startAngle: startAngle - 90,
149
+ endAngle: endAngle - 90
150
+ });
151
+
152
+ this.addShape(arc);
153
+ node.shape = arc;
154
+ }
155
+
156
+ /**
157
+ * 绘制标签
158
+ */
159
+ drawLabel(centerX, centerY, innerRadius, outerRadius, startAngle, endAngle, node) {
160
+ const midAngle = (startAngle + endAngle) / 2 - 90;
161
+ const midRadius = (innerRadius + outerRadius) / 2;
162
+
163
+ const x = centerX + midRadius * Math.cos(midAngle * Math.PI / 180);
164
+ const y = centerY + midRadius * Math.sin(midAngle * Math.PI / 180);
165
+
166
+ const text = node.name || '';
167
+ if(!text) return;
168
+
169
+ const label = this.graph.createShape('label', {
170
+ style: {
171
+ fill: this.style.labelColor || '#fff',
172
+ font: this.style.labelFont || '12px Arial',
173
+ textAlign: 'center',
174
+ textBaseline: 'middle',
175
+ zIndex: 10
176
+ },
177
+ text: text,
178
+ position: { x, y }
179
+ });
180
+
181
+ this.addShape(label);
182
+ }
183
+
184
+ /**
185
+ * 绘制中心
186
+ */
187
+ drawCenter(centerX, centerY, innerRadius) {
188
+ const centerCircle = this.graph.createShape('circle', {
189
+ style: {
190
+ fill: this.style.centerFill || '#fff',
191
+ stroke: this.style.centerStroke || '#e0e0e0',
192
+ lineWidth: 1,
193
+ zIndex: 0
194
+ },
195
+ center: { x: centerX, y: centerY },
196
+ radius: innerRadius
197
+ });
198
+
199
+ this.addShape(centerCircle);
200
+ }
201
+
202
+ /**
203
+ * 获取颜色
204
+ */
205
+ getColor(node, level) {
206
+ if(node.color) return node.color;
207
+
208
+ if(this.style.colors && this.style.colors.length > 0) {
209
+ return this.style.colors[level % this.style.colors.length];
210
+ }
211
+
212
+ if(typeof this.style.color === 'function') {
213
+ return this.style.color.call(this, node, level);
214
+ }
215
+
216
+ return this.graph.getColor(level);
217
+ }
218
+
219
+ /**
220
+ * 生成图例
221
+ */
222
+ createLegend() {
223
+ const style = this.graph.utils.clone(this.style);
224
+ style.fill = this.style.color || this.graph.getColor(0);
225
+ style.stroke = style.fill;
226
+
227
+ const shape = this.graph.createShape('circle', {
228
+ style: style,
229
+ center: {
230
+ x: this.graph.style.legend.item.shape.width / 2,
231
+ y: this.graph.style.legend.item.shape.height / 2
232
+ },
233
+ radius: this.graph.style.legend.item.shape.height / 2
234
+ });
235
+
236
+ this.graph.legend.append(this, shape);
237
+ }
238
+ }
239
+
240
+ export { jmSunburstSeries };
@@ -0,0 +1,182 @@
1
+ import jmSeries from './series.js';
2
+
3
+ const ANIMATION_DATA_THRESHOLD = 100;
4
+ const DEFAULT_ANIMATION_COUNT = 10;
5
+
6
+ export default class jmWaterfallSeries extends jmSeries {
7
+ constructor(options) {
8
+ options.style = options.style || options.graph.style.waterfall;
9
+ super(options);
10
+ }
11
+
12
+ init() {
13
+ const {
14
+ points,
15
+ dataChanged
16
+ } = this.initDataPoint();
17
+
18
+ const len = points.length;
19
+ if(!len) return;
20
+
21
+ this.pointsLen = len;
22
+ this.style.stroke = this.style.color;
23
+
24
+ let cumulativeValue = 0;
25
+ const processedPoints = [];
26
+
27
+ for(let i = 0; i < len; i++) {
28
+ const p = points[i];
29
+ if(typeof p.y === 'undefined' || p.y === null) {
30
+ continue;
31
+ }
32
+
33
+ p.originalValue = p.yValue;
34
+ p.startValue = cumulativeValue;
35
+ p.endValue = cumulativeValue + p.yValue;
36
+ cumulativeValue = p.endValue;
37
+
38
+ processedPoints.push(p);
39
+ }
40
+
41
+ let minValue = Infinity;
42
+ let maxValue = -Infinity;
43
+
44
+ for(const p of processedPoints) {
45
+ minValue = Math.min(minValue, p.startValue, p.endValue);
46
+ maxValue = Math.max(maxValue, p.startValue, p.endValue);
47
+ }
48
+
49
+ if(this.yAxis) {
50
+ this.yAxis.min(minValue);
51
+ this.yAxis.max(maxValue);
52
+ }
53
+
54
+ const minY = this.yAxis ? this.yAxis.min() : 0;
55
+ const ystep = this.yAxis ? this.yAxis.step() : 1;
56
+ const chartHeight = this.graph.chartArea.height;
57
+
58
+ const isRunningAni = this.enableAnimate && (dataChanged || this.___animateCounter > 0) && len < ANIMATION_DATA_THRESHOLD;
59
+
60
+ const aniCount = (this.style.aniCount || DEFAULT_ANIMATION_COUNT);
61
+ const aniStep = Math.floor(len / aniCount) || 1;
62
+
63
+ for(let i = 0; i < processedPoints.length; i++) {
64
+ const p = processedPoints[i];
65
+
66
+ if(isRunningAni && i > this.___animateCounter) {
67
+ break;
68
+ }
69
+
70
+ const startY = chartHeight - (p.startValue - minY) * ystep;
71
+ const endY = chartHeight - (p.endValue - minY) * ystep;
72
+ const height = Math.abs(endY - startY);
73
+
74
+ let barColor = this.style.color;
75
+ if(p.yValue > 0) {
76
+ barColor = this.style.increaseColor || '#52c41a';
77
+ } else if(p.yValue < 0) {
78
+ barColor = this.style.decreaseColor || '#f5222d';
79
+ }
80
+
81
+ this.createWaterfallBar(p, startY, endY, height, barColor);
82
+
83
+ if(i < processedPoints.length - 1) {
84
+ this.createConnectorLine(p, processedPoints[i + 1], chartHeight, minY, ystep);
85
+ }
86
+
87
+ this.createItemLabel(p);
88
+
89
+ this.emit('onPointCreated', p);
90
+ }
91
+
92
+ if(this.___animateCounter >= len - 1) {
93
+ this.___animateCounter = 0;
94
+ } else if(isRunningAni) {
95
+ this.___animateCounter += aniStep;
96
+ this.graph.utils.requestAnimationFrame(() => {
97
+ this.needUpdate = true;
98
+ });
99
+ }
100
+ }
101
+
102
+ createWaterfallBar(p, startY, endY, height, color) {
103
+ const barWidth = this.style.perWidth || 0.5;
104
+ const chartWidth = this.graph.chartArea.width;
105
+ const barX = p.x - (chartWidth * barWidth / this.pointsLen) / 2;
106
+ const barHeight = height;
107
+
108
+ const barStyle = {
109
+ stroke: color,
110
+ fill: color,
111
+ zIndex: 1
112
+ };
113
+
114
+ const barShape = this.graph.createShape('rect', {
115
+ style: barStyle,
116
+ position: {
117
+ x: barX,
118
+ y: Math.min(startY, endY)
119
+ },
120
+ width: chartWidth * barWidth / this.pointsLen,
121
+ height: barHeight
122
+ });
123
+
124
+ this.addShape(barShape);
125
+ }
126
+
127
+ createConnectorLine(currentPoint, nextPoint, chartHeight, minY, ystep) {
128
+ const startY = chartHeight - (currentPoint.endValue - minY) * ystep;
129
+ const chartWidth = this.graph.chartArea.width;
130
+ const barWidth = this.style.perWidth || 0.5;
131
+ const nextX = nextPoint.x - (chartWidth * barWidth / this.pointsLen) / 2;
132
+
133
+ const horizontalLine = this.graph.createShape('line', {
134
+ style: {
135
+ stroke: this.style.connectorColor || '#999',
136
+ lineWidth: 1,
137
+ lineType: 'dotted',
138
+ zIndex: 0
139
+ },
140
+ start: { x: currentPoint.x, y: startY },
141
+ end: { x: nextX, y: startY }
142
+ });
143
+
144
+ this.addShape(horizontalLine);
145
+
146
+ const nextStartY = chartHeight - (nextPoint.startValue - minY) * ystep;
147
+
148
+ const verticalLine = this.graph.createShape('line', {
149
+ style: {
150
+ stroke: this.style.connectorColor || '#999',
151
+ lineWidth: 1,
152
+ zIndex: 0
153
+ },
154
+ start: { x: nextX, y: startY },
155
+ end: { x: nextX, y: nextStartY }
156
+ });
157
+
158
+ this.addShape(verticalLine);
159
+ }
160
+
161
+ createLegend() {
162
+ const style = this.graph.utils.clone(this.style);
163
+ style.stroke = this.style.color;
164
+ style.fill = this.style.color;
165
+
166
+ const shape = this.graph.createShape('rect', {
167
+ style: style,
168
+ position: {
169
+ x: 0,
170
+ y: 0
171
+ },
172
+ width: this.graph.style.legend.item.shape.width,
173
+ height: this.graph.style.legend.item.shape.height
174
+ });
175
+
176
+ this.graph.legend.append(this, shape);
177
+ }
178
+ }
179
+
180
+ export {
181
+ jmWaterfallSeries
182
+ }