jmchart 1.1.81 → 1.1.91

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