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.
@@ -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++) {
@@ -42,42 +60,41 @@ export default class jmStackBarSeries extends jmBarSeries {
42
60
  let topStartY = this.baseY;
43
61
  let bottomStartY = this.baseY;
44
62
  for(let index=0; index < point.points.length; index ++) {
45
- const style = this.graph.utils.clone(this.style);
46
63
  const p = point.points[index];
47
64
 
48
- if(style.color && typeof style.color === 'function') {
49
- style.fill = style.color.call(this, {
65
+ let fillColor;
66
+ if(this.style.color && typeof this.style.color === 'function') {
67
+ fillColor = this.style.color.call(this, {
50
68
  index,
51
69
  point: p
52
70
  });
53
71
  }
54
72
  else {
55
- style.fill = this.graph.getColor(index);
73
+ fillColor = this.graph.getColor(index);
56
74
  }
75
+ // 使用浅拷贝而非深拷贝
76
+ const style = Object.assign({}, this.style, { fill: fillColor });
57
77
  const sp = this.addShape(this.graph.createPath(null, style));
58
78
 
59
79
  let startY = topStartY;
60
80
  if(p.yValue < this.baseYValue) startY = bottomStartY;
61
81
 
62
- //首先确定p1和p4,因为他们是底脚。会固定
63
82
  const p1 = {x: p.x - this.barTotalWidth / 2, y: startY };
64
83
  const p4 = {x: p1.x + this.barWidth, y: p1.y };
65
84
 
66
85
  const p2 = {x: p1.x, y: p1.y };
67
86
  const p3 = {x: p4.x, y: p1.y };
68
87
 
69
- // 如果要动画。则动态改变高度
70
88
  if(isRunningAni) {
71
89
  const step = p.height / aniCount;
72
- const offHeight = step * this.___animateCounter;// 动态计算当前高度
73
- p2.y = startY - offHeight;// 计算高度
90
+ const offHeight = step * this.___animateCounter;
91
+ p2.y = startY - offHeight;
74
92
 
75
- // 当次动画完成
76
93
  if((step >= 0 && offHeight >= p.height) || (step < 0 && offHeight <= p.height)) {
77
94
  p2.y = startY - p.height;
78
95
  }
79
96
  else {
80
- aniIsEnd = false;// 只要有一个没完成,就还没有完成动画
97
+ aniIsEnd = false;
81
98
  }
82
99
 
83
100
  p.y = p3.y = p2.y;
@@ -87,7 +104,7 @@ export default class jmStackBarSeries extends jmBarSeries {
87
104
  p.y = p3.y = p2.y;
88
105
  }
89
106
 
90
- if(p.yValue < this.baseYValue) bottomStartY = p2.y;// 下一个又从它顶部开始画
107
+ if(p.yValue < this.baseYValue) bottomStartY = p2.y;
91
108
  else topStartY = p2.y;
92
109
 
93
110
  sp.points.push(p1);
@@ -96,7 +113,6 @@ export default class jmStackBarSeries extends jmBarSeries {
96
113
  sp.points.push(p4);
97
114
  }
98
115
 
99
- // 生成标点的回调
100
116
  this.emit('onPointCreated', point);
101
117
  }
102
118
 
@@ -105,17 +121,19 @@ export default class jmStackBarSeries extends jmBarSeries {
105
121
  }
106
122
  else {
107
123
  this.___animateCounter++;
108
- // next tick 再次刷新
109
124
  this.graph.utils.requestAnimationFrame(()=>{
110
- this.needUpdate = true;//需要刷新
125
+ this.needUpdate = true;
111
126
  });
112
127
  }
113
128
  }
114
129
 
115
- // 计算最大值和最小值,一般图形直接采用最大最小值即可,有些需要多值叠加
130
+ /**
131
+ * 计算最大值和最小值
132
+ *
133
+ * 对于堆叠柱状图,需要计算所有字段的累加值
134
+ * 正值累加得到最大值,负值累加得到最小值
135
+ */
116
136
  initAxisValue() {
117
- // 计算最大最小值
118
- // 当前需要先更新axis的边界值,轴好画图
119
137
  const fields = Array.isArray(this.field)? this.field: [this.field];
120
138
 
121
139
  for(const row of this.data) {
@@ -147,4 +165,4 @@ export default class jmStackBarSeries extends jmBarSeries {
147
165
  this.xAxis.min(xv);
148
166
  }
149
167
  }
150
- }
168
+ }
@@ -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 };