jmchart 1.2.9 → 1.2.11

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,337 +1,337 @@
1
- import {
2
- jmControl
3
- } from 'jmgraph';
4
- import jmSeries from './series.js';
5
-
6
- /**
7
- * 饼图
8
- *
9
- * @class jmPieSeries
10
- * @module jmChart
11
- * @param {jmChart} chart 当前图表
12
- * @param {array} mappings 图形字段映射
13
- * @param {style} style 样式
14
- */
15
-
16
- //构造函数
17
- export default class jmPieSeries extends jmSeries {
18
- constructor(options) {
19
- super(options);
20
-
21
- this.xAxis.visible = false;
22
- this.yAxis.visible = false;
23
- }
24
-
25
- // 重新初始化图形
26
- init() {
27
-
28
- //总和
29
- this.totalValue = 0;
30
- //计算最大值和最小值
31
- if(this.data) {
32
- for(const i in this.data) {
33
- const s = this.data[i];
34
- const vy = s[this.field];
35
- if(vy) {
36
- this.totalValue += Math.abs(vy);
37
- }
38
- }
39
- }
40
-
41
- const center = {
42
- x: this.graph.chartArea.width / 2,
43
- y: this.graph.chartArea.height / 2
44
- };
45
-
46
- const radius = Math.min(center.x - this.style.margin.left -
47
- this.style.margin.right,
48
- center.y - this.style.margin.top - this.style.margin.bottom);
49
-
50
-
51
- //生成描点位
52
- // super.init会把参数透传给 createPoints
53
- const { points, dataChanged } = this.initDataPoint(center, radius);
54
-
55
- // 是否正在动画中
56
- const isRunningAni = this.enableAnimate && (dataChanged || this.___animateCounter > 0 );
57
-
58
- // 在动画中,则一直刷新
59
- if(isRunningAni) {
60
- const aniCount = (this.style.aniCount || 20);
61
- let aniIsEnd = true;// 当次是否结束动画
62
- const len = points.length;
63
- for(let i=0; i<len; i++) {
64
-
65
- const p = points[i];
66
- const step = (p.y - p.shape.startAngle) / aniCount;
67
-
68
- p.shape.endAngle = p.shape.startAngle + this.___animateCounter * step;
69
-
70
- if(p.shape.endAngle >= p.y) {
71
- p.shape.endAngle = p.y;
72
- }
73
- else {
74
- aniIsEnd = false;
75
- }
76
- // p.shape.points = arc.initPoints();
77
- // p.shape.points.push(center);
78
- //绑定提示框
79
- //this.bindTooltip(p.shape, p);
80
- }
81
- // 所有动画都完成,则清空计数器
82
- if(aniIsEnd) {
83
- this.___animateCounter = 0;
84
- }
85
- else {
86
- this.___animateCounter ++;
87
- // next tick 再次刷新
88
- this.graph.utils.requestAnimationFrame(()=>{
89
- this.needUpdate = true;//需要刷新
90
- });
91
- }
92
- }
93
- }
94
-
95
- // 当前总起画角度
96
- get startAngle() {
97
- return this.option.startAngle || 0;
98
- }
99
- set startAngle(v) {
100
- this.option.startAngle = v;
101
- }
102
-
103
- /**
104
- * 生成序列图描点
105
- *
106
- * @method createPoints
107
- */
108
- createPoints(center, radius) {
109
- if(!this.data) return [];
110
-
111
- const points = [];
112
- let index = 0;
113
-
114
- let startAni = this.startAngle; // 总起始角度
115
- if(typeof startAni === 'function') {
116
- startAni = startAni.call(this, this.data);
117
- }
118
-
119
- let cm = Math.PI * 2;
120
- //规定应该逆时针还是顺时针绘图 false 顺时针,true 逆时针
121
- const anticlockwise = this.option.anticlockwise || false;
122
- // 每项之间的间隔角度 顺时钟为正,否则为负
123
- const marginAngle = (Number(this.style.marginAngle) || 0);
124
-
125
- for(var i=0;i< this.data.length;i++) {
126
- const s = this.data[i];
127
-
128
- const yv = s[this.field];
129
-
130
- //如果Y值不存在。则此点无效,不画图
131
- if(yv == null || typeof yv == 'undefined') {
132
- continue;
133
- }
134
- else {
135
- const p = {
136
- data: s,
137
- x: i,
138
- yValue: yv,
139
- yLabel: yv,
140
- step: Math.abs(yv / this.totalValue),// 每个数值点比
141
- style: this.graph.utils.clone(this.style),
142
- anticlockwise
143
- };
144
- points.push(p);
145
- //p.style.color = this.graph.getColor(index);
146
- if(p.style.color && typeof p.style.color === 'function') {
147
- p.style.fill = p.style.color.call(this, p);
148
- }
149
- else {
150
- p.style.fill = this.graph.getColor(index);
151
- }
152
-
153
- const start = startAni;// 上一个扇形的结束角度为当前的起始角度
154
- // 计算当前结束角度, 同时也是下一个的起始角度
155
- p.y = startAni + p.step * cm;
156
- startAni = p.y;
157
- p.startAngle = start + marginAngle;
158
- p.endAngle = p.y;
159
-
160
- if(center && radius) {
161
- const arcWidth = this.style.arcWidth || radius*0.2;
162
- p.radius = radius;
163
- // 如果有指定动态半径,则调用
164
- if(typeof this.option.radius === 'function') {
165
- p.radius = this.option.radius.call(this, p, radius, i);
166
- }
167
- p.maxRadius = p.radius;
168
- // 如果有指定动态半径,则调用
169
- if(typeof this.option.maxRadius === 'function') {
170
- p.maxRadius = this.option.maxRadius.call(this, p, p.maxRadius, i);
171
- }
172
- p.minRadius = p.radius - arcWidth;
173
- // 如果有指定动态半径,则调用
174
- if(typeof this.option.minRadius === 'function') {
175
- p.minRadius = this.option.minRadius.call(this, p, p.minRadius, i);
176
- }
177
- p.center = center;
178
- // 如果有指定动态半径,则调用
179
- if(typeof this.option.center === 'function') {
180
- p.center = this.option.center.call(this, p, p.center, i);
181
- }
182
- p.shape = this.graph.createShape(this.style.isHollow? 'harc' : 'arc', {
183
- style: p.style,
184
- startAngle: p.startAngle,
185
- endAngle: p.endAngle,
186
- anticlockwise: anticlockwise,
187
- isFan: true, // 表示画扇形
188
- center: p.center,
189
- radius: p.radius,
190
- maxRadius: p.maxRadius,
191
- minRadius: p.minRadius
192
- });
193
-
194
- /**
195
- * 因为jmgraph是按图形形状来计算所占区域和大小的, 这里我们把扇形占区域改为整个图圆。这样计算大小和渐变时才好闭合。
196
- */
197
- p.shape.getLocation = function() {
198
- const local = this.location = {
199
- left: 0,
200
- top: 0,
201
- width: 0,
202
- height: 0,
203
- center: this.center,
204
- radius: p.radius
205
- };
206
-
207
- local.left = this.center.x - p.radius;
208
- local.top = this.center.y - p.radius;
209
- local.width = local.height = p.radius * 2;
210
-
211
- return local;
212
- }
213
- p.shape.getBounds = function() {
214
- return this.getLocation();
215
- }
216
-
217
- this.addShape(p.shape);
218
-
219
- // 如果有点击事件
220
- if(this.option.onClick) {
221
- p.shape.on('click', (e) => {
222
- return this.option.onClick.call(this, p, e);
223
- });
224
- }
225
- if(this.option.onOver) {
226
- p.shape.on('mouseover touchover', (e) => {
227
- return this.option.onOver.call(this, p, e);
228
- });
229
- }
230
- if(this.option.onLeave) {
231
- p.shape.on('mouseleave touchleave', (e) => {
232
- return this.option.onLeave.call(this, p, e);
233
- });
234
- }
235
-
236
- this.createLabel(p);// 生成标签
237
- // 生成标点的回调
238
- this.emit('onPointCreated', p);
239
- }
240
- index++;
241
- }
242
- }
243
-
244
- return points;
245
- }
246
-
247
- // 生成图的标注
248
- createLabel(point) {
249
- if(this.style.label && this.style.label.show === false) return;
250
-
251
- const text = this.option.itemLabelFormat?this.option.itemLabelFormat.call(this, point): point.step;
252
- if(!text) return;
253
-
254
- // v如果指定了为控件,则直接加入
255
- if(text instanceof jmControl) {
256
- point.shape.children.add(text);
257
- return text;
258
- }
259
- const self = this;
260
-
261
- const label = this.graph.createShape('label', {
262
- style: this.style.label,
263
- text: text,
264
- position: function() {
265
- if(!this.parent || !this.parent.points || !this.parent.points.length) return {x: 0, y: 0};
266
-
267
- // 动态计算位置
268
- const parentRect = this.parent.getBounds();
269
- //const rect = this.getBounds.call(this.parent);
270
-
271
- // 圆弧的中间位,离圆心最近和最远的二个点
272
- let centerMaxPoint = this.parent.points[Math.floor(this.parent.points.length / 2)];
273
- let centerMinPoint = this.parent.center;
274
- // 如果是空心圆,则要计算 1/4 和 3/4位的点。顺时针和逆时针二个点大小不一样,这里只取,大小计算时处理
275
- if(self.style.isHollow) {
276
- centerMaxPoint = this.parent.points[Math.floor(this.parent.points.length * 0.25)];
277
- centerMinPoint = this.parent.points[Math.floor(this.parent.points.length * 0.75)];
278
- }
279
- const centerMinX = Math.min(centerMaxPoint.x, centerMinPoint.x);
280
- const centerMaxX = Math.max(centerMaxPoint.x, centerMinPoint.x);
281
- const centerMinY = Math.min(centerMaxPoint.y, centerMinPoint.y);
282
- const centerMaxY = Math.max(centerMaxPoint.y, centerMinPoint.y);
283
-
284
- // 中心点
285
- const center = {
286
- x: (centerMaxX - centerMinX) / 2 + centerMinX,
287
- y: (centerMaxY - centerMinY) / 2 + centerMinY,
288
- };
289
-
290
- const size = this.testSize();
291
-
292
- // 取图形中间的二个点
293
- // rect是相对于图形坐标点形图的图形的左上角,而parentRect是图形重新指定的整圆区域。减去整圆区域左上角就是相对于整圆区域坐标
294
- return {
295
- x: center.x - parentRect.left - size.width / 2,
296
- y: center.y - parentRect.top - size.height / 2
297
- }
298
- }
299
- });
300
-
301
- point.shape.children.add(label);
302
- }
303
- }
304
-
305
-
306
-
307
- /**
308
- * 生成图例
309
- *
310
- * @method createLegend
311
- */
312
- jmPieSeries.prototype.createLegend = function() {
313
-
314
- const points = this.createPoints();
315
- if(!points || !points.length) return;
316
-
317
- for(let k=0; k < points.length; k++) {
318
- const p = points[k];
319
- if(!p) continue;
320
-
321
- //生成图例前的图标
322
- const style = this.graph.utils.clone(p.style);
323
- style.fill = style.fill;
324
- //delete style.stroke;
325
- const shape = this.graph.createShape('rect',{
326
- style: style,
327
- position : {x: 0, y: 0}
328
- });
329
- //shape.targetShape = p.shape;
330
- //此处重写图例事件
331
- this.graph.legend.append(this, shape, {
332
- name: this.legendLabel,
333
- data: this.data[k]
334
- });
335
- }
336
- }
337
-
1
+ import {
2
+ jmControl
3
+ } from 'jmgraph';
4
+ import jmSeries from './series.js';
5
+
6
+ /**
7
+ * 饼图
8
+ *
9
+ * @class jmPieSeries
10
+ * @module jmChart
11
+ * @param {jmChart} chart 当前图表
12
+ * @param {array} mappings 图形字段映射
13
+ * @param {style} style 样式
14
+ */
15
+
16
+ //构造函数
17
+ export default class jmPieSeries extends jmSeries {
18
+ constructor(options) {
19
+ super(options);
20
+
21
+ this.xAxis.visible = false;
22
+ this.yAxis.visible = false;
23
+ }
24
+
25
+ // 重新初始化图形
26
+ init() {
27
+
28
+ //总和
29
+ this.totalValue = 0;
30
+ //计算最大值和最小值
31
+ if(this.data) {
32
+ for(const i in this.data) {
33
+ const s = this.data[i];
34
+ const vy = s[this.field];
35
+ if(vy) {
36
+ this.totalValue += Math.abs(vy);
37
+ }
38
+ }
39
+ }
40
+
41
+ const center = {
42
+ x: this.graph.chartArea.width / 2,
43
+ y: this.graph.chartArea.height / 2
44
+ };
45
+
46
+ const radius = Math.min(center.x - this.style.margin.left -
47
+ this.style.margin.right,
48
+ center.y - this.style.margin.top - this.style.margin.bottom);
49
+
50
+
51
+ //生成描点位
52
+ // super.init会把参数透传给 createPoints
53
+ const { points, dataChanged } = this.initDataPoint(center, radius);
54
+
55
+ // 是否正在动画中
56
+ const isRunningAni = this.enableAnimate && (dataChanged || this.___animateCounter > 0 );
57
+
58
+ // 在动画中,则一直刷新
59
+ if(isRunningAni) {
60
+ const aniCount = (this.style.aniCount || 20);
61
+ let aniIsEnd = true;// 当次是否结束动画
62
+ const len = points.length;
63
+ for(let i=0; i<len; i++) {
64
+
65
+ const p = points[i];
66
+ const step = (p.y - p.shape.startAngle) / aniCount;
67
+
68
+ p.shape.endAngle = p.shape.startAngle + this.___animateCounter * step;
69
+
70
+ if(p.shape.endAngle >= p.y) {
71
+ p.shape.endAngle = p.y;
72
+ }
73
+ else {
74
+ aniIsEnd = false;
75
+ }
76
+ // p.shape.points = arc.initPoints();
77
+ // p.shape.points.push(center);
78
+ //绑定提示框
79
+ //this.bindTooltip(p.shape, p);
80
+ }
81
+ // 所有动画都完成,则清空计数器
82
+ if(aniIsEnd) {
83
+ this.___animateCounter = 0;
84
+ }
85
+ else {
86
+ this.___animateCounter ++;
87
+ // next tick 再次刷新
88
+ this.graph.utils.requestAnimationFrame(()=>{
89
+ this.needUpdate = true;//需要刷新
90
+ });
91
+ }
92
+ }
93
+ }
94
+
95
+ // 当前总起画角度
96
+ get startAngle() {
97
+ return this.option.startAngle || 0;
98
+ }
99
+ set startAngle(v) {
100
+ this.option.startAngle = v;
101
+ }
102
+
103
+ /**
104
+ * 生成序列图描点
105
+ *
106
+ * @method createPoints
107
+ */
108
+ createPoints(center, radius) {
109
+ if(!this.data) return [];
110
+
111
+ const points = [];
112
+ let index = 0;
113
+
114
+ let startAni = this.startAngle; // 总起始角度
115
+ if(typeof startAni === 'function') {
116
+ startAni = startAni.call(this, this.data);
117
+ }
118
+
119
+ let cm = Math.PI * 2;
120
+ //规定应该逆时针还是顺时针绘图 false 顺时针,true 逆时针
121
+ const anticlockwise = this.option.anticlockwise || false;
122
+ // 每项之间的间隔角度 顺时钟为正,否则为负
123
+ const marginAngle = (Number(this.style.marginAngle) || 0);
124
+
125
+ for(var i=0;i< this.data.length;i++) {
126
+ const s = this.data[i];
127
+
128
+ const yv = s[this.field];
129
+
130
+ //如果Y值不存在。则此点无效,不画图
131
+ if(yv == null || typeof yv == 'undefined') {
132
+ continue;
133
+ }
134
+ else {
135
+ const p = {
136
+ data: s,
137
+ x: i,
138
+ yValue: yv,
139
+ yLabel: yv,
140
+ step: Math.abs(yv / this.totalValue),// 每个数值点比
141
+ style: this.graph.utils.clone(this.style),
142
+ anticlockwise
143
+ };
144
+ points.push(p);
145
+ //p.style.color = this.graph.getColor(index);
146
+ if(p.style.color && typeof p.style.color === 'function') {
147
+ p.style.fill = p.style.color.call(this, p);
148
+ }
149
+ else {
150
+ p.style.fill = this.graph.getColor(index);
151
+ }
152
+
153
+ const start = startAni;// 上一个扇形的结束角度为当前的起始角度
154
+ // 计算当前结束角度, 同时也是下一个的起始角度
155
+ p.y = startAni + p.step * cm;
156
+ startAni = p.y;
157
+ p.startAngle = start + marginAngle;
158
+ p.endAngle = p.y;
159
+
160
+ if(center && radius) {
161
+ const arcWidth = this.style.arcWidth || radius*0.2;
162
+ p.radius = radius;
163
+ // 如果有指定动态半径,则调用
164
+ if(typeof this.option.radius === 'function') {
165
+ p.radius = this.option.radius.call(this, p, radius, i);
166
+ }
167
+ p.maxRadius = p.radius;
168
+ // 如果有指定动态半径,则调用
169
+ if(typeof this.option.maxRadius === 'function') {
170
+ p.maxRadius = this.option.maxRadius.call(this, p, p.maxRadius, i);
171
+ }
172
+ p.minRadius = p.radius - arcWidth;
173
+ // 如果有指定动态半径,则调用
174
+ if(typeof this.option.minRadius === 'function') {
175
+ p.minRadius = this.option.minRadius.call(this, p, p.minRadius, i);
176
+ }
177
+ p.center = center;
178
+ // 如果有指定动态半径,则调用
179
+ if(typeof this.option.center === 'function') {
180
+ p.center = this.option.center.call(this, p, p.center, i);
181
+ }
182
+ p.shape = this.graph.createShape(this.style.isHollow? 'harc' : 'arc', {
183
+ style: p.style,
184
+ startAngle: p.startAngle,
185
+ endAngle: p.endAngle,
186
+ anticlockwise: anticlockwise,
187
+ isFan: true, // 表示画扇形
188
+ center: p.center,
189
+ radius: p.radius,
190
+ maxRadius: p.maxRadius,
191
+ minRadius: p.minRadius
192
+ });
193
+
194
+ /**
195
+ * 因为jmgraph是按图形形状来计算所占区域和大小的, 这里我们把扇形占区域改为整个图圆。这样计算大小和渐变时才好闭合。
196
+ */
197
+ p.shape.getLocation = function() {
198
+ const local = this.location = {
199
+ left: 0,
200
+ top: 0,
201
+ width: 0,
202
+ height: 0,
203
+ center: this.center,
204
+ radius: p.radius
205
+ };
206
+
207
+ local.left = this.center.x - p.radius;
208
+ local.top = this.center.y - p.radius;
209
+ local.width = local.height = p.radius * 2;
210
+
211
+ return local;
212
+ }
213
+ p.shape.getBounds = function() {
214
+ return this.getLocation();
215
+ }
216
+
217
+ this.addShape(p.shape);
218
+
219
+ // 如果有点击事件
220
+ if(this.option.onClick) {
221
+ p.shape.on('click', (e) => {
222
+ return this.option.onClick.call(this, p, e);
223
+ });
224
+ }
225
+ if(this.option.onOver) {
226
+ p.shape.on('mouseover touchover', (e) => {
227
+ return this.option.onOver.call(this, p, e);
228
+ });
229
+ }
230
+ if(this.option.onLeave) {
231
+ p.shape.on('mouseleave touchleave', (e) => {
232
+ return this.option.onLeave.call(this, p, e);
233
+ });
234
+ }
235
+
236
+ this.createLabel(p);// 生成标签
237
+ // 生成标点的回调
238
+ this.emit('onPointCreated', p);
239
+ }
240
+ index++;
241
+ }
242
+ }
243
+
244
+ return points;
245
+ }
246
+
247
+ // 生成图的标注
248
+ createLabel(point) {
249
+ if(this.style.label && this.style.label.show === false) return;
250
+
251
+ const text = this.option.itemLabelFormat?this.option.itemLabelFormat.call(this, point): point.step;
252
+ if(!text) return;
253
+
254
+ // v如果指定了为控件,则直接加入
255
+ if(text instanceof jmControl) {
256
+ point.shape.children.add(text);
257
+ return text;
258
+ }
259
+ const self = this;
260
+
261
+ const label = this.graph.createShape('label', {
262
+ style: this.style.label,
263
+ text: text,
264
+ position: function() {
265
+ if(!this.parent || !this.parent.points || !this.parent.points.length) return {x: 0, y: 0};
266
+
267
+ // 动态计算位置
268
+ const parentRect = this.parent.getBounds();
269
+ //const rect = this.getBounds.call(this.parent);
270
+
271
+ // 圆弧的中间位,离圆心最近和最远的二个点
272
+ let centerMaxPoint = this.parent.points[Math.floor(this.parent.points.length / 2)];
273
+ let centerMinPoint = this.parent.center;
274
+ // 如果是空心圆,则要计算 1/4 和 3/4位的点。顺时针和逆时针二个点大小不一样,这里只取,大小计算时处理
275
+ if(self.style.isHollow) {
276
+ centerMaxPoint = this.parent.points[Math.floor(this.parent.points.length * 0.25)];
277
+ centerMinPoint = this.parent.points[Math.floor(this.parent.points.length * 0.75)];
278
+ }
279
+ const centerMinX = Math.min(centerMaxPoint.x, centerMinPoint.x);
280
+ const centerMaxX = Math.max(centerMaxPoint.x, centerMinPoint.x);
281
+ const centerMinY = Math.min(centerMaxPoint.y, centerMinPoint.y);
282
+ const centerMaxY = Math.max(centerMaxPoint.y, centerMinPoint.y);
283
+
284
+ // 中心点
285
+ const center = {
286
+ x: (centerMaxX - centerMinX) / 2 + centerMinX,
287
+ y: (centerMaxY - centerMinY) / 2 + centerMinY,
288
+ };
289
+
290
+ const size = this.testSize();
291
+
292
+ // 取图形中间的二个点
293
+ // rect是相对于图形坐标点形图的图形的左上角,而parentRect是图形重新指定的整圆区域。减去整圆区域左上角就是相对于整圆区域坐标
294
+ return {
295
+ x: center.x - parentRect.left - size.width / 2,
296
+ y: center.y - parentRect.top - size.height / 2
297
+ }
298
+ }
299
+ });
300
+
301
+ point.shape.children.add(label);
302
+ }
303
+ }
304
+
305
+
306
+
307
+ /**
308
+ * 生成图例
309
+ *
310
+ * @method createLegend
311
+ */
312
+ jmPieSeries.prototype.createLegend = function() {
313
+
314
+ const points = this.createPoints();
315
+ if(!points || !points.length) return;
316
+
317
+ for(let k=0; k < points.length; k++) {
318
+ const p = points[k];
319
+ if(!p) continue;
320
+
321
+ //生成图例前的图标
322
+ const style = this.graph.utils.clone(p.style);
323
+ style.fill = style.fill;
324
+ //delete style.stroke;
325
+ const shape = this.graph.createShape('rect',{
326
+ style: style,
327
+ position : {x: 0, y: 0}
328
+ });
329
+ //shape.targetShape = p.shape;
330
+ //此处重写图例事件
331
+ this.graph.legend.append(this, shape, {
332
+ name: this.legendLabel,
333
+ data: this.data[k]
334
+ });
335
+ }
336
+ }
337
+