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,727 +1,727 @@
1
- import {
2
- jmArrowLine
3
- } from 'jmgraph';
4
-
5
- /**
6
- * 轴
7
- *
8
- * @class jmAxis
9
- * @module jmChart
10
- * @param {jmChart} chart 当前图表
11
- * @param {string} [type] 轴类型(x/y/radar),默认为x
12
- * @param {string} [dataType] 当前轴的数据类型(number/date/string),默认为 number
13
- * @param {object} [style] 样式
14
- */
15
-
16
- export default class jmAxis extends jmArrowLine {
17
- constructor(options) {
18
- super(options);
19
-
20
- //初始化不显示箭头
21
- this.arrowVisible = !!options.arrowVisible;
22
- this.zeroBase = options.zeroBase || false;
23
-
24
- this.labelCount = options.labelCount || 5;
25
- this.type = options.type || 'x';// 为横轴x或纵轴y
26
-
27
- if(this.type == 'x') {
28
- this.dataType = options.dataType || 'string';
29
- }
30
- else {
31
- this.dataType = options.dataType || 'number';
32
- }
33
-
34
- this.field = options.field || '';
35
- this.index = options.index || 0;
36
- this.gridLines = [];// 线条数组
37
-
38
- this.init(options);
39
- }
40
-
41
- // 初始化一些参数
42
- // 这个函数可能会重入。
43
- init(options) {
44
- options = options || {};
45
- // 深度组件默认样式
46
- if(options.style) this.graph.utils.clone(options.style, this.style, true);
47
-
48
- this.field = options.field || this.field || '';
49
- this.radarOption = options.radarOption;
50
-
51
- if(this.type == 'x') {
52
- if(typeof options.maxXValue !== 'undefined') this.maxValue = options.maxXValue; // 最大的值,如果指定了,则如果有数值比它大才会修改上限,否则以它为上限
53
- if(typeof options.minXValue !== 'undefined') this.minValue = options.minXValue;// 最小值,如果指定了,则轴的最小值为它或更小的值
54
- }
55
- else {
56
- if(typeof options.maxYValue !== 'undefined' && (options.maxYValue > this.maxValue || typeof this.maxValue === 'undefined')) this.maxValue = options.maxYValue; // 最大的值,如果指定了,则如果有数值比它大才会修改上限,否则以它为上限
57
- if(typeof options.minYValue !== 'undefined'&& (options.minYValue < this.minValue || typeof this.minValue === 'undefined')) this.minValue = options.minYValue;// 最小值,如果指定了,则轴的最小值为它或更小的值
58
- }
59
- }
60
-
61
- /**
62
- * 轴类型(x/y/radar),默认为x
63
- *
64
- * @property type
65
- * @type string
66
- */
67
- type = 'x';
68
-
69
- /**
70
- * 对应的字段
71
- */
72
- field = '';
73
-
74
- /**
75
- * 轴标签起始坐标
76
- *
77
- * @property labelStart
78
- * @type number
79
- */
80
- labelStart = 0;
81
-
82
- /**
83
- * 否从0开始
84
- *
85
- * @property type
86
- * @type bool
87
- * @for jmAxis
88
- */
89
- zeroBase = false;
90
-
91
- /**
92
- * 显示标签个数
93
- *
94
- * @property labelCount
95
- * @type number
96
- * @for jmAxis
97
- */
98
- labelCount = 1;
99
-
100
- /**
101
- * 轴上的刻度,由动态计算出
102
- */
103
- scalePoints = [];
104
-
105
- /**
106
- * 轴上的标签,只读
107
- */
108
- labels = [];
109
-
110
- /**
111
- * 关联访问的是chart的数据源
112
- */
113
- get data() {
114
- return this.graph.data;
115
- }
116
- set data(d) {
117
- this.graph.data = d;
118
- }
119
-
120
- // 生成绘制点,
121
- // 重写原函数
122
- initPoints() {
123
- // 如果是雷达图
124
- if(this.radarOption && this.type === 'x') {
125
- this.points = [];
126
- for(const axis of this.radarOption.yAxises) {
127
- axis.end && this.points.push(axis.end);
128
- }
129
- this.points.push(this.points[0]);
130
- return this.points;
131
- }
132
- else {
133
- return super.initPoints();
134
- }
135
- }
136
-
137
-
138
- /**
139
- * 计算当前轴的位置
140
- *
141
- * @method reset
142
- */
143
- reset() {
144
- const bounds = this.graph.chartArea.getBounds();// 获取画图区域
145
- switch(this.type) {
146
- case 'x' : {
147
- //初始化显示标签个数
148
- this.labelCount = this.style.xLabel.count || 5;
149
- // 如果是雷达图,则画栅格线
150
- if(this.radarOption) {
151
- if(this.style.grid && this.style.grid.x) {
152
- for(let i=1; i< this.labelCount+1; i++) {
153
- const points = [];
154
- const curRadius = this.radarOption.radius / this.labelCount * i;
155
- for(const axis of this.radarOption.yAxises) {
156
- if(!axis.radarOption) continue;
157
- const point = {};
158
- point.x = axis.radarOption.center.x + axis.radarOption.cos * curRadius + bounds.left;
159
- point.y = axis.radarOption.center.y - axis.radarOption.sin * curRadius + bounds.top;
160
- points.push(point);
161
- }
162
- // 画栅格线
163
- for(let j=0; j<points.length; j++) {
164
- const start = points[j];
165
- const end = points[j+1] || points[0];
166
- const gridLine = this.graph.createShape('line', {
167
- start,
168
- end,
169
- style: this.style.grid
170
- });
171
- this.parent.children.add(gridLine);
172
- this.gridLines.push(gridLine);
173
- }
174
- }
175
- }
176
- break;
177
- }
178
-
179
- this.start.x = bounds.left;
180
- this.start.y = bounds.bottom;
181
- this.end.x = bounds.right;
182
- this.end.y = bounds.bottom;
183
-
184
- // zeroBase 时才需要移到0位置,否则依然为沉底
185
- if(this.graph.baseY === 0) {
186
- const yAxis = this.graph.yAxises[1];
187
- if(!yAxis) return;
188
-
189
- this.value = 0;
190
- const y = this.start.y + yAxis.min() * yAxis.step();
191
- this.start.y = this.end.y = y;
192
- }
193
- break;
194
- }
195
- case 'y' : {
196
- //初始化显示标签个数
197
- this.labelCount = this.style.yLabel.count || 5;
198
-
199
- const index = this.index || 1;
200
- // 如果是雷达图,则画发散的线
201
- if(this.radarOption) {
202
- this.end.x = this.radarOption.center.x + this.radarOption.cos * this.radarOption.radius + bounds.left;;
203
- this.end.y = this.radarOption.center.y - this.radarOption.sin * this.radarOption.radius + bounds.top;
204
- this.start.x = this.radarOption.center.x + bounds.left;
205
- this.start.y = this.radarOption.center.y + bounds.top;
206
- }
207
- else {
208
- let xoffset = bounds.left;
209
-
210
- //多Y轴时,第二个为右边第一轴,其它的依此递推
211
- if(index == 2) {
212
- xoffset = bounds.right;
213
- }
214
- else if(index > 2) {
215
- xoffset = this.graph.yAxises[index-1].start.x + this.graph.yAxises[index-1].width + 10;
216
- }
217
-
218
- this.start.x = xoffset;
219
- this.start.y = bounds.bottom;
220
- this.end.x = this.start.x;
221
- this.end.y = bounds.top;
222
- }
223
- break;
224
- }
225
- }
226
- this.createLabel();
227
- }
228
-
229
- // 绘制完成后,生成label标签
230
- draw() {
231
- this.points.push(...this.scalePoints);// 把刻度也画出来
232
- super.draw();
233
- }
234
-
235
- /**
236
- * 生成轴标签
237
- *
238
- * @method createLabel
239
- */
240
- createLabel() {
241
- if(this.visible === false) return;
242
-
243
- // 雷达图的标签单独处理
244
- if(this.radarOption) {
245
- return this.createRadarLabel();
246
- }
247
-
248
- //如果是?X轴则执行X轴标签生成
249
- if(this.type == 'x') {
250
- this.createXLabel();
251
- }
252
- else if(this.type == 'y') {
253
- this.createYLabel();
254
- }
255
- }
256
-
257
- /**
258
- * 生成X轴标签
259
- *
260
- * @method createXLabel
261
- * @private
262
- */
263
- createXLabel() {
264
- //var max = this.max();
265
- //var min = this.min();
266
- const step = this.step();
267
- this.scalePoints = [];// 刻度点集合
268
- //最多显示标签个数
269
- //var count = this.style.xLabel.count || this.data.length;
270
- //字符串轴。则显示每个标签
271
- const format = this.option.format || this.format;
272
- const top = (this.style.xLabel.margin.top || 0);
273
- for(let i=0; i< this.data.length;i++) {
274
- const d = this.data[i];
275
- const v = d[this.field];
276
-
277
- // 不显示就不生成label。这里性能影响很大
278
- const text = format.call(this, v, d, i); // 格式化label
279
- if(!text) continue;
280
-
281
- /// 只有一条数据,就取这条数据就可以了
282
- const w = i * step;
283
-
284
- const label = this.graph.createShape('label', {
285
- style: this.style.xLabel
286
- });
287
- label.data = d; // 当前点的数据结构值
288
- label.text = text;
289
-
290
- this.labels.push(label);
291
- this.children.add(label);
292
-
293
- label.width = label.testSize().width + 2;
294
- label.height = 15;
295
-
296
- const pos = {
297
- x: this.labelStart + w,
298
- y: top
299
- };
300
-
301
- // 指定要显示网格
302
- if(this.style.grid && this.style.grid.y) {
303
- // 它的坐标是相对于轴的,所以Y轴会用负的区域高度
304
- const line = this.graph.createShape('line', {
305
- start: {
306
- x: pos.x,
307
- y: 0
308
- },
309
- end: {
310
- x: pos.x,
311
- y: -this.graph.chartArea.height
312
- },
313
- style: this.style.grid
314
- });
315
- this.children.add(line);
316
- }
317
-
318
- //在轴上画小标记m表示移至当前点开画
319
- this.scalePoints.push({
320
- x: pos.x + this.start.x,
321
- y: this.start.y,
322
- m: true
323
- });
324
- this.scalePoints.push({
325
- x: pos.x + this.start.x,
326
- y: this.start.y + (this.style.length || 5)
327
- });
328
-
329
- //如果进行了旋转,则处理位移
330
- const rotation = label.style.rotation;
331
- if(rotation && rotation.angle) {
332
- //设定旋转原点为label左上角
333
- rotation.point = pos;
334
- //当旋转后,其原点位移至左上角,所有当前控件必须反向移位其父容器位置
335
- label.position = {
336
- x: -this.graph.chartArea.position.x,
337
- y: -this.graph.chartArea.position.y
338
- };
339
- }
340
- else {
341
- // 如果标签居中,则把二头的标签左边的左对齐,右边的右对齐
342
- if(this.style.align === 'center' && this.data.length > 1 && (
343
- i === 0 || i === this.data.length - 1)
344
- ) {
345
- if(i === this.data.length - 1) {
346
- pos.x -= label.width;
347
- }
348
- }
349
- else {
350
- pos.x -= label.width / 2;//向左偏移半个label宽度
351
- }
352
- label.position = pos;
353
- }
354
- }
355
- }
356
-
357
- /**
358
- * 生成Y轴标签
359
- *
360
- * @method createYLabel
361
- * @private
362
- */
363
- createYLabel() {
364
- const max = this.max();
365
- const min = this.min();
366
- const step = this.step();
367
- const index = this.index || 1;
368
- this.scalePoints = [];// 刻度点集合
369
-
370
- let count = this.labelCount;
371
- const mm = max - min;
372
- let pervalue = (mm / count) || 1;
373
-
374
- const format = this.option.format || this.format;
375
- const marginLeft = this.style.yLabel.margin.left || 0;
376
- const marginRight = this.style.yLabel.margin.right || 0;
377
- let p = 0;
378
-
379
- for(let i=0; i<count+1; i++) {
380
- p = min + pervalue * i;
381
- if(p > max || i === count) p = max;
382
- const h = (p - min) * step; // 当前点的偏移高度
383
- const label = this.graph.createShape('label', {
384
- style: this.style.yLabel
385
- });
386
-
387
- label.text = format.call(this, p, label); // 格式化label
388
- this.labels.push(label);
389
- this.children.add(label);
390
-
391
- const w = label.testSize().width;
392
- const offy = this.height - h; // 刻度的偏移量
393
- // label的位置
394
- const pos = {
395
- x: 0,
396
- y: 0
397
- };
398
-
399
- //轴的宽度
400
- const axiswidth = marginLeft + marginRight + w;
401
- this.width = Math.max(axiswidth, this.width);
402
-
403
- //计算标签位置
404
- if(index <= 1) {
405
-
406
- pos.x = -w - marginRight;
407
- pos.y = offy - label.height / 2;
408
-
409
- //在轴上画小标记m表示移至当前点开画
410
- this.scalePoints.push({
411
- x: this.start.x,
412
- y: offy + this.end.y ,
413
- m: true
414
- });
415
- this.scalePoints.push({
416
- x:this.start.x,
417
- y: offy + this.end.y
418
- });
419
-
420
- // 指定要显示网格
421
- if(!this.radarOption && this.style.grid && this.style.grid.x) {
422
- // 它的坐标是相对于轴的,所以Y轴会用负的区域高度
423
- const line = this.graph.createShape('line', {
424
- start: {
425
- x: 0,
426
- y: offy
427
- },
428
- end: {
429
- x: this.graph.chartArea.width,
430
- y: offy
431
- },
432
- style: this.style.grid
433
- });
434
- this.children.add(line);
435
- }
436
- }
437
- else {
438
-
439
- pos.x = marginLeft
440
- pos.y = offy - label.height / 2;
441
-
442
- //在轴上画小标记m表示移至当前点开画
443
- this.scalePoints.push({
444
- x: this.start.x,
445
- y: offy + this.end.y,
446
- m: true
447
- });
448
- this.scalePoints.push({
449
- x: this.start.x,
450
- y: offy + this.end.y
451
- });
452
- }
453
-
454
- // label对齐方式
455
- switch(this.style.yLabel.textAlign) {
456
- case 'center': {
457
- pos.x = pos.x / 2 - w / 2;
458
- break;
459
- }
460
- case 'right': {
461
- if(index <= 1) pos.x = - axiswidth;
462
- else {
463
- // 轴在最右边时,轴宽减去label宽就是右对齐
464
- pos.x = axiswidth - w;
465
- }
466
- break;
467
- }
468
- // 默认就是左对齐,无需处理
469
- case 'left':
470
- default: {
471
- break;
472
- }
473
- }
474
-
475
- //如果进行了旋转,则处理位移
476
- const rotation = label.style.rotation;
477
- if(rotation && rotation.angle) {
478
- label.translate = pos;//先位移再旋转
479
- label.position = {x: -w / 2, y: 0};
480
- }
481
- else {
482
- label.position = pos;
483
- }
484
- }
485
- }
486
-
487
- /**
488
- * 生成雷达图的Y轴标签
489
- */
490
- createRadarLabel() {
491
- const format = this.option.format;
492
- const bounds = this.graph.chartArea.getBounds();// 获取画图区域
493
- const self = this;
494
- const label = this.graph.createShape('label', {
495
- style: this.style.yLabel,
496
- position: function() {
497
- // 因为axis是相对于chart的,而center是相对于chartArea的,所以要计算axis位置相对于chartArea来比较
498
- const pos = {
499
- x: self.end.x - bounds.left,
500
- y: self.end.y - bounds.top
501
- };
502
- const size = this.testSize();
503
- if(pos.x < self.radarOption.center.x) {
504
- pos.x -= size.width;
505
- }
506
-
507
- if(pos.y < self.radarOption.center.y) {
508
- pos.y -= size.height;
509
- }
510
- return pos;
511
- }
512
- });
513
-
514
- label.text = typeof format === 'function'? format.call(this, label): this.field; // 格式化label
515
- this.labels.push(label);
516
- this.graph.chartArea.children.add(label);
517
- }
518
-
519
- /**
520
- * 获取当前轴所占宽
521
- *
522
- * @method width
523
- */
524
- get width() {
525
- if(this._width) {
526
- return this._width;
527
- }
528
- return Math.abs(this.end.x - this.start.x);
529
- }
530
- set width(w) {
531
- this._width = w;
532
- }
533
-
534
- /**
535
- * 获取当前轴所占高
536
- *
537
- * @method height
538
- */
539
- get height() {
540
- return Math.abs(this.end.y - this.start.y);
541
- }
542
- // 这里设置高度没意义
543
- set height(h) {}
544
-
545
- /**
546
- * 获取或设置当前轴最大值
547
- *
548
- * @method max
549
- * @param {number/date/string} 当前轴的最大值
550
- * @return 当前轴的最大值
551
- */
552
- max(m) {
553
- if(typeof m !== 'undefined') {
554
- //如果为0为基线,则最小值不能大于0
555
- if(this.dataType == 'number' && m < 0 && this.zeroBase) {
556
- m = 0;
557
- }
558
- this._max = this._max != null && typeof(this._max) != 'undefined'? Math.max(m, this._max) : m;
559
- // 如果有指定默认最大值,则不超过它就采用它
560
- if(typeof this.maxValue != 'undefined') this._max = Math.max(this.maxValue, this._max);
561
- }
562
- //如果为字符串,则返回分类个数
563
- if(this.dataType == 'string' && this.data) {
564
- return this.data.length;
565
- }
566
-
567
- //如果是数字类型,则在最大值基础上加一定的值
568
- if(this.dataType == 'number') {
569
- m = this._max;
570
-
571
- // 如果有指定默认最大值,则不超过它就采用它
572
- if(typeof this.maxValue != 'undefined' && m <= this.maxValue) {
573
- return this.maxValue;
574
- }
575
-
576
- if(m <= 0) {
577
- if(m >= -10) m = 0;
578
- else m = -10;
579
- }
580
- else if(m > 500) {
581
- m = Math.ceil(m / 100);
582
- m = m * 100 + 100;
583
- }
584
- else if(m > 100) {
585
- m = Math.ceil(m / 50);
586
- m = m * 50 + 50;
587
- }
588
- else if(m > 10) {
589
- m = Math.ceil(m / 10);
590
- m = m * 10 + 10;
591
- }
592
- else {
593
- m = Math.ceil(m);
594
- }
595
-
596
- return m;
597
- }
598
-
599
- return this._max;
600
- }
601
-
602
- /**
603
- * 获取或设置当前轴最小值
604
- *
605
- * @method max
606
- * @param {number/date/string} 当前轴的最小值
607
- * @return 当前轴的最小值
608
- */
609
- min(m) {
610
- if(typeof m !== 'undefined') {
611
- //如果为0为基线,则最小值不能大于0
612
- if(this.dataType == 'number' && m > 0 && this.zeroBase) {
613
- m = 0;
614
- }
615
- this._min = this._min != null && typeof(this._min) != 'undefined'? Math.min(m, this._min) : m;
616
- // 如果有指定默认最小值,则不小于它就采用它
617
- if(typeof this.minValue != 'undefined') this._min = Math.min(this.minValue, this._min);
618
- }
619
-
620
- //如果是数字类型,则在最小值基础上减去一定的值
621
- if(this.dataType == 'number') {
622
- m = this._min;
623
-
624
- // 如果有指定默认最小值,则不小于它就采用它
625
- if(typeof this.minValue != 'undefined') {
626
- return typeof m !== 'undefined'? Math.min(this.minValue, m): this.minValue;
627
- }
628
-
629
- if(m >= 0) {
630
- if(m <= 10) m = 0;
631
- else {
632
- m = Math.floor(m / 10) * 10 - 10;
633
- }
634
- }
635
- else if(m < -500) {
636
- m = Math.floor(m / 100);
637
- m = m * 100 - 100;
638
- }
639
- else if(m < -100) {
640
- m = Math.floor(m / 50);
641
- m = m * 50 - 50;
642
- }
643
- else if(m < -10) {
644
- m = Math.floor(m / 10);
645
- m = m * 10 - 10;
646
- }
647
- else {
648
- m = Math.floor(m);
649
- }
650
-
651
- return m;
652
- }
653
- //如果为字符串则返回0
654
- return this.dataType == 'string'? 0: this._min;
655
- }
656
-
657
- /**
658
- * 清除一些属性
659
- *
660
- * @method clear
661
- */
662
- clear() {
663
- this._min = null;
664
- this._max = null;
665
- this.children.each((i, c) => {
666
- c.remove();
667
- }, true);
668
- // 清空栅格线
669
- this.gridLines && this.gridLines.forEach((line)=>{
670
- line.remove();
671
- });
672
- this.labels && this.labels.forEach((label)=>{
673
- label.remove();
674
- });
675
- this.labels = [];
676
- this.gridLines = [];
677
- }
678
-
679
- /**
680
- * 计算当前轴的单位偏移量
681
- *
682
- * @method step
683
- * @return {number} 单位偏移量
684
- */
685
- step() {
686
- if(this.type == 'x') {
687
- const w = this.radarOption? this.radarOption.radius : this.width;
688
-
689
- //如果排版为内联,则单位占宽减少一个单位,
690
- //也就是起始位从一个单位开始
691
- if(this.graph.style.layout == 'inside') {
692
- const sp = w / this.max();
693
- this.labelStart = sp / 2;
694
- return sp;
695
- }
696
- else {
697
- this.labelStart = 0;
698
- }
699
- let tmp = this.max() - 1;
700
- if(tmp === 0) tmp = 2; // 只有一个数据的情况,就直接居中
701
- return w / tmp;
702
-
703
- }
704
- else if(this.type == 'y') {
705
- const h = this.radarOption? this.radarOption.radius : this.height;
706
- switch(this.dataType) {
707
- case 'string': {
708
- return h / this.max();
709
- }
710
- case 'date':
711
- case 'number':
712
- default: {
713
- let tmp = Math.abs(this.max() - this.min());
714
- tmp = tmp || 1;
715
- return h / tmp;
716
- }
717
- }
718
- }
719
- }
720
- // 格式化标签值
721
- format(v, item) {
722
- return v + '';
723
- }
724
- }
725
-
726
-
727
-
1
+ import {
2
+ jmArrowLine
3
+ } from 'jmgraph';
4
+
5
+ const MAX_LABEL_COUNT = 10;
6
+
7
+ /**
8
+ * 轴
9
+ *
10
+ * @class jmAxis
11
+ * @module jmChart
12
+ * @param {jmChart} chart 当前图表
13
+ * @param {string} [type] 轴类型(x/y/radar),默认为x
14
+ * @param {string} [dataType] 当前轴的数据类型(number/date/string),默认为 number
15
+ * @param {object} [style] 样式
16
+ */
17
+
18
+ export default class jmAxis extends jmArrowLine {
19
+ constructor(options) {
20
+ super(options);
21
+
22
+ this.arrowVisible = !!options.arrowVisible;
23
+ this.zeroBase = options.zeroBase || false;
24
+
25
+ this.labelCount = Math.min(options.labelCount || 5, MAX_LABEL_COUNT);
26
+ this.type = options.type || 'x';
27
+
28
+ if(this.type == 'x') {
29
+ this.dataType = options.dataType || 'string';
30
+ } else {
31
+ this.dataType = options.dataType || 'number';
32
+ }
33
+
34
+ this.field = options.field || '';
35
+ this.index = options.index || 0;
36
+ this.gridLines = [];
37
+
38
+ this.init(options);
39
+ }
40
+
41
+ init(options) {
42
+ options = options || {};
43
+ if(options.style) this.graph.utils.clone(options.style, this.style, true);
44
+
45
+ this.field = options.field || this.field || '';
46
+ this.radarOption = options.radarOption;
47
+
48
+ if(this.type == 'x') {
49
+ if(typeof options.maxXValue !== 'undefined') this.maxValue = options.maxXValue;
50
+ if(typeof options.minXValue !== 'undefined') this.minValue = options.minXValue;
51
+ } else {
52
+ if(typeof options.maxYValue !== 'undefined' && (options.maxYValue > this.maxValue || typeof this.maxValue === 'undefined')) {
53
+ this.maxValue = options.maxYValue;
54
+ }
55
+ if(typeof options.minYValue !== 'undefined' && (options.minYValue < this.minValue || typeof this.minValue === 'undefined')) {
56
+ this.minValue = options.minYValue;
57
+ }
58
+ }
59
+ }
60
+
61
+ /**
62
+ * 轴类型(x/y/radar),默认为x
63
+ *
64
+ * @property type
65
+ * @type string
66
+ */
67
+ type = 'x';
68
+
69
+ /**
70
+ * 对应的字段
71
+ */
72
+ field = '';
73
+
74
+ /**
75
+ * 轴标签起始坐标
76
+ *
77
+ * @property labelStart
78
+ * @type number
79
+ */
80
+ labelStart = 0;
81
+
82
+ /**
83
+ * 否从0开始
84
+ *
85
+ * @property type
86
+ * @type bool
87
+ * @for jmAxis
88
+ */
89
+ zeroBase = false;
90
+
91
+ /**
92
+ * 显示标签个数
93
+ *
94
+ * @property labelCount
95
+ * @type number
96
+ * @for jmAxis
97
+ */
98
+ labelCount = 1;
99
+
100
+ /**
101
+ * 轴上的刻度,由动态计算出
102
+ */
103
+ scalePoints = [];
104
+
105
+ /**
106
+ * 轴上的标签,只读
107
+ */
108
+ labels = [];
109
+
110
+ /**
111
+ * 关联访问的是chart的数据源
112
+ */
113
+ get data() {
114
+ return this.graph.data;
115
+ }
116
+ set data(d) {
117
+ this.graph.data = d;
118
+ }
119
+
120
+ // 生成绘制点,
121
+ // 重写原函数
122
+ initPoints() {
123
+ // 如果是雷达图
124
+ if(this.radarOption && this.type === 'x') {
125
+ this.points = [];
126
+ for(const axis of this.radarOption.yAxises) {
127
+ axis.end && this.points.push(axis.end);
128
+ }
129
+ this.points.push(this.points[0]);
130
+ return this.points;
131
+ }
132
+ else {
133
+ return super.initPoints();
134
+ }
135
+ }
136
+
137
+
138
+ /**
139
+ * 计算当前轴的位置
140
+ *
141
+ * @method reset
142
+ */
143
+ reset() {
144
+ const bounds = this.graph.chartArea.getBounds();// 获取画图区域
145
+ switch(this.type) {
146
+ case 'x' : {
147
+ //初始化显示标签个数
148
+ this.labelCount = this.style.xLabel.count || 5;
149
+ // 如果是雷达图,则画栅格线
150
+ if(this.radarOption) {
151
+ if(this.style.grid && this.style.grid.x) {
152
+ for(let i=1; i< this.labelCount+1; i++) {
153
+ const points = [];
154
+ const curRadius = this.radarOption.radius / this.labelCount * i;
155
+ for(const axis of this.radarOption.yAxises) {
156
+ if(!axis.radarOption) continue;
157
+ const point = {};
158
+ point.x = axis.radarOption.center.x + axis.radarOption.cos * curRadius + bounds.left;
159
+ point.y = axis.radarOption.center.y - axis.radarOption.sin * curRadius + bounds.top;
160
+ points.push(point);
161
+ }
162
+ // 画栅格线
163
+ for(let j=0; j<points.length; j++) {
164
+ const start = points[j];
165
+ const end = points[j+1] || points[0];
166
+ const gridLine = this.graph.createShape('line', {
167
+ start,
168
+ end,
169
+ style: this.style.grid
170
+ });
171
+ this.parent.children.add(gridLine);
172
+ this.gridLines.push(gridLine);
173
+ }
174
+ }
175
+ }
176
+ break;
177
+ }
178
+
179
+ this.start.x = bounds.left;
180
+ this.start.y = bounds.bottom;
181
+ this.end.x = bounds.right;
182
+ this.end.y = bounds.bottom;
183
+
184
+ // zeroBase 时才需要移到0位置,否则依然为沉底
185
+ if(this.graph.baseY === 0) {
186
+ const yAxis = this.graph.yAxises[1];
187
+ if(!yAxis) return;
188
+
189
+ this.value = 0;
190
+ const y = this.start.y + yAxis.min() * yAxis.step();
191
+ this.start.y = this.end.y = y;
192
+ }
193
+ break;
194
+ }
195
+ case 'y' : {
196
+ //初始化显示标签个数
197
+ this.labelCount = this.style.yLabel.count || 5;
198
+
199
+ const index = this.index || 1;
200
+ // 如果是雷达图,则画发散的线
201
+ if(this.radarOption) {
202
+ this.end.x = this.radarOption.center.x + this.radarOption.cos * this.radarOption.radius + bounds.left;;
203
+ this.end.y = this.radarOption.center.y - this.radarOption.sin * this.radarOption.radius + bounds.top;
204
+ this.start.x = this.radarOption.center.x + bounds.left;
205
+ this.start.y = this.radarOption.center.y + bounds.top;
206
+ }
207
+ else {
208
+ let xoffset = bounds.left;
209
+
210
+ //多Y轴时,第二个为右边第一轴,其它的依此递推
211
+ if(index == 2) {
212
+ xoffset = bounds.right;
213
+ }
214
+ else if(index > 2) {
215
+ xoffset = this.graph.yAxises[index-1].start.x + this.graph.yAxises[index-1].width + 10;
216
+ }
217
+
218
+ this.start.x = xoffset;
219
+ this.start.y = bounds.bottom;
220
+ this.end.x = this.start.x;
221
+ this.end.y = bounds.top;
222
+ }
223
+ break;
224
+ }
225
+ }
226
+ this.createLabel();
227
+ }
228
+
229
+ // 绘制完成后,生成label标签
230
+ draw() {
231
+ this.points.push(...this.scalePoints);// 把刻度也画出来
232
+ super.draw();
233
+ }
234
+
235
+ /**
236
+ * 生成轴标签
237
+ *
238
+ * @method createLabel
239
+ */
240
+ createLabel() {
241
+ if(this.visible === false) return;
242
+
243
+ // 雷达图的标签单独处理
244
+ if(this.radarOption) {
245
+ return this.createRadarLabel();
246
+ }
247
+
248
+ //如果是?X轴则执行X轴标签生成
249
+ if(this.type == 'x') {
250
+ this.createXLabel();
251
+ }
252
+ else if(this.type == 'y') {
253
+ this.createYLabel();
254
+ }
255
+ }
256
+
257
+ /**
258
+ * 生成X轴标签
259
+ *
260
+ * @method createXLabel
261
+ * @private
262
+ */
263
+ createXLabel() {
264
+ //var max = this.max();
265
+ //var min = this.min();
266
+ const step = this.step();
267
+ this.scalePoints = [];// 刻度点集合
268
+ //最多显示标签个数
269
+ //var count = this.style.xLabel.count || this.data.length;
270
+ //字符串轴。则显示每个标签
271
+ const format = this.option.format || this.format;
272
+ const top = (this.style.xLabel.margin.top || 0);
273
+ for(let i=0; i< this.data.length;i++) {
274
+ const d = this.data[i];
275
+ const v = d[this.field];
276
+
277
+ // 不显示就不生成label。这里性能影响很大
278
+ const text = format.call(this, v, d, i); // 格式化label
279
+ if(!text) continue;
280
+
281
+ /// 只有一条数据,就取这条数据就可以了
282
+ const w = i * step;
283
+
284
+ const label = this.graph.createShape('label', {
285
+ style: this.style.xLabel
286
+ });
287
+ label.data = d; // 当前点的数据结构值
288
+ label.text = text;
289
+
290
+ this.labels.push(label);
291
+ this.children.add(label);
292
+
293
+ label.width = label.testSize().width + 2;
294
+ label.height = 15;
295
+
296
+ const pos = {
297
+ x: this.labelStart + w,
298
+ y: top
299
+ };
300
+
301
+ // 指定要显示网格
302
+ if(this.style.grid && this.style.grid.y) {
303
+ // 它的坐标是相对于轴的,所以Y轴会用负的区域高度
304
+ const line = this.graph.createShape('line', {
305
+ start: {
306
+ x: pos.x,
307
+ y: 0
308
+ },
309
+ end: {
310
+ x: pos.x,
311
+ y: -this.graph.chartArea.height
312
+ },
313
+ style: this.style.grid
314
+ });
315
+ this.children.add(line);
316
+ }
317
+
318
+ //在轴上画小标记m表示移至当前点开画
319
+ this.scalePoints.push({
320
+ x: pos.x + this.start.x,
321
+ y: this.start.y,
322
+ m: true
323
+ });
324
+ this.scalePoints.push({
325
+ x: pos.x + this.start.x,
326
+ y: this.start.y + (this.style.length || 5)
327
+ });
328
+
329
+ //如果进行了旋转,则处理位移
330
+ const rotation = label.style.rotation;
331
+ if(rotation && rotation.angle) {
332
+ //设定旋转原点为label左上角
333
+ rotation.point = pos;
334
+ //当旋转后,其原点位移至左上角,所有当前控件必须反向移位其父容器位置
335
+ label.position = {
336
+ x: -this.graph.chartArea.position.x,
337
+ y: -this.graph.chartArea.position.y
338
+ };
339
+ }
340
+ else {
341
+ // 如果标签居中,则把二头的标签左边的左对齐,右边的右对齐
342
+ if(this.style.align === 'center' && this.data.length > 1 && (
343
+ i === 0 || i === this.data.length - 1)
344
+ ) {
345
+ if(i === this.data.length - 1) {
346
+ pos.x -= label.width;
347
+ }
348
+ }
349
+ else {
350
+ pos.x -= label.width / 2;//向左偏移半个label宽度
351
+ }
352
+ label.position = pos;
353
+ }
354
+ }
355
+ }
356
+
357
+ /**
358
+ * 生成Y轴标签
359
+ *
360
+ * @method createYLabel
361
+ * @private
362
+ */
363
+ createYLabel() {
364
+ const max = this.max();
365
+ const min = this.min();
366
+ const step = this.step();
367
+ const index = this.index || 1;
368
+ this.scalePoints = [];// 刻度点集合
369
+
370
+ let count = this.labelCount;
371
+ const mm = max - min;
372
+ let pervalue = (mm / count) || 1;
373
+
374
+ const format = this.option.format || this.format;
375
+ const marginLeft = this.style.yLabel.margin.left || 0;
376
+ const marginRight = this.style.yLabel.margin.right || 0;
377
+ let p = 0;
378
+
379
+ for(let i=0; i<count+1; i++) {
380
+ p = min + pervalue * i;
381
+ if(p > max || i === count) p = max;
382
+ const h = (p - min) * step; // 当前点的偏移高度
383
+ const label = this.graph.createShape('label', {
384
+ style: this.style.yLabel
385
+ });
386
+
387
+ label.text = format.call(this, p, label); // 格式化label
388
+ this.labels.push(label);
389
+ this.children.add(label);
390
+
391
+ const w = label.testSize().width;
392
+ const offy = this.height - h; // 刻度的偏移量
393
+ // label的位置
394
+ const pos = {
395
+ x: 0,
396
+ y: 0
397
+ };
398
+
399
+ //轴的宽度
400
+ const axiswidth = marginLeft + marginRight + w;
401
+ this.width = Math.max(axiswidth, this.width);
402
+
403
+ //计算标签位置
404
+ if(index <= 1) {
405
+
406
+ pos.x = -w - marginRight;
407
+ pos.y = offy - label.height / 2;
408
+
409
+ //在轴上画小标记m表示移至当前点开画
410
+ this.scalePoints.push({
411
+ x: this.start.x,
412
+ y: offy + this.end.y ,
413
+ m: true
414
+ });
415
+ this.scalePoints.push({
416
+ x:this.start.x,
417
+ y: offy + this.end.y
418
+ });
419
+
420
+ // 指定要显示网格
421
+ if(!this.radarOption && this.style.grid && this.style.grid.x) {
422
+ // 它的坐标是相对于轴的,所以Y轴会用负的区域高度
423
+ const line = this.graph.createShape('line', {
424
+ start: {
425
+ x: 0,
426
+ y: offy
427
+ },
428
+ end: {
429
+ x: this.graph.chartArea.width,
430
+ y: offy
431
+ },
432
+ style: this.style.grid
433
+ });
434
+ this.children.add(line);
435
+ }
436
+ }
437
+ else {
438
+
439
+ pos.x = marginLeft
440
+ pos.y = offy - label.height / 2;
441
+
442
+ //在轴上画小标记m表示移至当前点开画
443
+ this.scalePoints.push({
444
+ x: this.start.x,
445
+ y: offy + this.end.y,
446
+ m: true
447
+ });
448
+ this.scalePoints.push({
449
+ x: this.start.x,
450
+ y: offy + this.end.y
451
+ });
452
+ }
453
+
454
+ // label对齐方式
455
+ switch(this.style.yLabel.textAlign) {
456
+ case 'center': {
457
+ pos.x = pos.x / 2 - w / 2;
458
+ break;
459
+ }
460
+ case 'right': {
461
+ if(index <= 1) pos.x = - axiswidth;
462
+ else {
463
+ // 轴在最右边时,轴宽减去label宽就是右对齐
464
+ pos.x = axiswidth - w;
465
+ }
466
+ break;
467
+ }
468
+ // 默认就是左对齐,无需处理
469
+ case 'left':
470
+ default: {
471
+ break;
472
+ }
473
+ }
474
+
475
+ //如果进行了旋转,则处理位移
476
+ const rotation = label.style.rotation;
477
+ if(rotation && rotation.angle) {
478
+ label.translate = pos;//先位移再旋转
479
+ label.position = {x: -w / 2, y: 0};
480
+ }
481
+ else {
482
+ label.position = pos;
483
+ }
484
+ }
485
+ }
486
+
487
+ /**
488
+ * 生成雷达图的Y轴标签
489
+ */
490
+ createRadarLabel() {
491
+ const format = this.option.format;
492
+ const bounds = this.graph.chartArea.getBounds();// 获取画图区域
493
+ const self = this;
494
+ const label = this.graph.createShape('label', {
495
+ style: this.style.yLabel,
496
+ position: function() {
497
+ // 因为axis是相对于chart的,而center是相对于chartArea的,所以要计算axis位置相对于chartArea来比较
498
+ const pos = {
499
+ x: self.end.x - bounds.left,
500
+ y: self.end.y - bounds.top
501
+ };
502
+ const size = this.testSize();
503
+ if(pos.x < self.radarOption.center.x) {
504
+ pos.x -= size.width;
505
+ }
506
+
507
+ if(pos.y < self.radarOption.center.y) {
508
+ pos.y -= size.height;
509
+ }
510
+ return pos;
511
+ }
512
+ });
513
+
514
+ label.text = typeof format === 'function'? format.call(this, label): this.field; // 格式化label
515
+ this.labels.push(label);
516
+ this.graph.chartArea.children.add(label);
517
+ }
518
+
519
+ /**
520
+ * 获取当前轴所占宽
521
+ *
522
+ * @method width
523
+ */
524
+ get width() {
525
+ if(this._width) {
526
+ return this._width;
527
+ }
528
+ return Math.abs(this.end.x - this.start.x);
529
+ }
530
+ set width(w) {
531
+ this._width = w;
532
+ }
533
+
534
+ /**
535
+ * 获取当前轴所占高
536
+ *
537
+ * @method height
538
+ */
539
+ get height() {
540
+ return Math.abs(this.end.y - this.start.y);
541
+ }
542
+ // 这里设置高度没意义
543
+ set height(h) {}
544
+
545
+ /**
546
+ * 获取或设置当前轴最大值
547
+ *
548
+ * @method max
549
+ * @param {number/date/string} 当前轴的最大值
550
+ * @return 当前轴的最大值
551
+ */
552
+ max(m) {
553
+ if(typeof m !== 'undefined') {
554
+ //如果为0为基线,则最小值不能大于0
555
+ if(this.dataType == 'number' && m < 0 && this.zeroBase) {
556
+ m = 0;
557
+ }
558
+ this._max = this._max != null && typeof(this._max) != 'undefined'? Math.max(m, this._max) : m;
559
+ // 如果有指定默认最大值,则不超过它就采用它
560
+ if(typeof this.maxValue != 'undefined') this._max = Math.max(this.maxValue, this._max);
561
+ }
562
+ //如果为字符串,则返回分类个数
563
+ if(this.dataType == 'string' && this.data) {
564
+ return this.data.length;
565
+ }
566
+
567
+ //如果是数字类型,则在最大值基础上加一定的值
568
+ if(this.dataType == 'number') {
569
+ m = this._max;
570
+
571
+ // 如果有指定默认最大值,则不超过它就采用它
572
+ if(typeof this.maxValue != 'undefined' && m <= this.maxValue) {
573
+ return this.maxValue;
574
+ }
575
+
576
+ if(m <= 0) {
577
+ if(m >= -10) m = 0;
578
+ else m = -10;
579
+ }
580
+ else if(m > 500) {
581
+ m = Math.ceil(m / 100);
582
+ m = m * 100 + 100;
583
+ }
584
+ else if(m > 100) {
585
+ m = Math.ceil(m / 50);
586
+ m = m * 50 + 50;
587
+ }
588
+ else if(m > 10) {
589
+ m = Math.ceil(m / 10);
590
+ m = m * 10 + 10;
591
+ }
592
+ else {
593
+ m = Math.ceil(m);
594
+ }
595
+
596
+ return m;
597
+ }
598
+
599
+ return this._max;
600
+ }
601
+
602
+ /**
603
+ * 获取或设置当前轴最小值
604
+ *
605
+ * @method max
606
+ * @param {number/date/string} 当前轴的最小值
607
+ * @return 当前轴的最小值
608
+ */
609
+ min(m) {
610
+ if(typeof m !== 'undefined') {
611
+ //如果为0为基线,则最小值不能大于0
612
+ if(this.dataType == 'number' && m > 0 && this.zeroBase) {
613
+ m = 0;
614
+ }
615
+ this._min = this._min != null && typeof(this._min) != 'undefined'? Math.min(m, this._min) : m;
616
+ // 如果有指定默认最小值,则不小于它就采用它
617
+ if(typeof this.minValue != 'undefined') this._min = Math.min(this.minValue, this._min);
618
+ }
619
+
620
+ //如果是数字类型,则在最小值基础上减去一定的值
621
+ if(this.dataType == 'number') {
622
+ m = this._min;
623
+
624
+ // 如果有指定默认最小值,则不小于它就采用它
625
+ if(typeof this.minValue != 'undefined') {
626
+ return typeof m !== 'undefined'? Math.min(this.minValue, m): this.minValue;
627
+ }
628
+
629
+ if(m >= 0) {
630
+ if(m <= 10) m = 0;
631
+ else {
632
+ m = Math.floor(m / 10) * 10 - 10;
633
+ }
634
+ }
635
+ else if(m < -500) {
636
+ m = Math.floor(m / 100);
637
+ m = m * 100 - 100;
638
+ }
639
+ else if(m < -100) {
640
+ m = Math.floor(m / 50);
641
+ m = m * 50 - 50;
642
+ }
643
+ else if(m < -10) {
644
+ m = Math.floor(m / 10);
645
+ m = m * 10 - 10;
646
+ }
647
+ else {
648
+ m = Math.floor(m);
649
+ }
650
+
651
+ return m;
652
+ }
653
+ //如果为字符串则返回0
654
+ return this.dataType == 'string'? 0: this._min;
655
+ }
656
+
657
+ /**
658
+ * 清除一些属性
659
+ *
660
+ * @method clear
661
+ */
662
+ clear() {
663
+ this._min = null;
664
+ this._max = null;
665
+ this.children.each((i, c) => {
666
+ c.remove();
667
+ }, true);
668
+ // 清空栅格线
669
+ this.gridLines && this.gridLines.forEach((line)=>{
670
+ line.remove();
671
+ });
672
+ this.labels && this.labels.forEach((label)=>{
673
+ label.remove();
674
+ });
675
+ this.labels = [];
676
+ this.gridLines = [];
677
+ }
678
+
679
+ /**
680
+ * 计算当前轴的单位偏移量
681
+ *
682
+ * @method step
683
+ * @return {number} 单位偏移量
684
+ */
685
+ step() {
686
+ if(this.type == 'x') {
687
+ const w = this.radarOption? this.radarOption.radius : this.width;
688
+
689
+ //如果排版为内联,则单位占宽减少一个单位,
690
+ //也就是起始位从一个单位开始
691
+ if(this.graph.style.layout == 'inside') {
692
+ const sp = w / this.max();
693
+ this.labelStart = sp / 2;
694
+ return sp;
695
+ }
696
+ else {
697
+ this.labelStart = 0;
698
+ }
699
+ let tmp = this.max() - 1;
700
+ if(tmp === 0) tmp = 2; // 只有一个数据的情况,就直接居中
701
+ return w / tmp;
702
+
703
+ }
704
+ else if(this.type == 'y') {
705
+ const h = this.radarOption? this.radarOption.radius : this.height;
706
+ switch(this.dataType) {
707
+ case 'string': {
708
+ return h / this.max();
709
+ }
710
+ case 'date':
711
+ case 'number':
712
+ default: {
713
+ let tmp = Math.abs(this.max() - this.min());
714
+ tmp = tmp || 1;
715
+ return h / tmp;
716
+ }
717
+ }
718
+ }
719
+ }
720
+ // 格式化标签值
721
+ format(v, item) {
722
+ return v + '';
723
+ }
724
+ }
725
+
726
+
727
+