]> git.proxmox.com Git - extjs.git/blob - extjs/packages/charts/src/chart/axis/segmenter/Time.js
add extjs 6.0.1 sources
[extjs.git] / extjs / packages / charts / src / chart / axis / segmenter / Time.js
1 /**
2 * @class Ext.chart.axis.segmenter.Time
3 * @extends Ext.chart.axis.segmenter.Segmenter
4 *
5 * Time data type.
6 */
7 Ext.define('Ext.chart.axis.segmenter.Time', {
8 extend: 'Ext.chart.axis.segmenter.Segmenter',
9 alias: 'segmenter.time',
10
11 config: {
12 /**
13 * @cfg {Object} step
14 * If specified, the will override the result of {@link #preferredStep}.
15 */
16 step: null
17 },
18
19 renderer: function (value, context) {
20 var ExtDate = Ext.Date;
21 switch (context.majorTicks.unit) {
22 case 'y':
23 return ExtDate.format(value, 'Y');
24 case 'mo':
25 return ExtDate.format(value, 'Y-m');
26 case 'd':
27 return ExtDate.format(value, 'Y-m-d');
28 }
29 return ExtDate.format(value, 'Y-m-d\nH:i:s');
30 },
31
32 from: function (value) {
33 return new Date(value);
34 },
35
36 diff: function (min, max, unit) {
37 if (isFinite(min)) {
38 min = new Date(min);
39 }
40 if (isFinite(max)) {
41 max = new Date(max);
42 }
43 return Ext.Date.diff(min, max, unit);
44 },
45
46 align: function (date, step, unit) {
47 if (unit === 'd' && step >= 7) {
48 date = Ext.Date.align(date, 'd', step);
49 date.setDate(date.getDate() - date.getDay() + 1);
50 return date;
51 } else {
52 return Ext.Date.align(date, unit, step);
53 }
54 },
55
56 add: function (value, step, unit) {
57 return Ext.Date.add(new Date(value), unit, step);
58 },
59
60 stepUnits: [
61 [Ext.Date.YEAR, 1, 2, 5, 10, 20, 50, 100, 200, 500],
62 [Ext.Date.MONTH, 1, 3, 6],
63 [Ext.Date.DAY, 1, 7, 14],
64 [Ext.Date.HOUR, 1, 6, 12],
65 [Ext.Date.MINUTE, 1, 5, 15, 30],
66 [Ext.Date.SECOND, 1, 5, 15, 30],
67 [Ext.Date.MILLI, 1, 2, 5, 10, 20, 50, 100, 200, 500]
68 ],
69
70 preferredStep: function (min, estStepSize) {
71 if (this.getStep()) {
72 return this.getStep();
73 }
74 var from = new Date(+min),
75 to = new Date(+min + Math.ceil(estStepSize)),
76 units = this.stepUnits,
77 result, unit, diff,
78 i, j;
79
80 for (i = 0; i < units.length; i++) {
81 unit = units[i][0];
82 diff = this.diff(from, to, unit);
83
84 if (diff > 0) {
85 for (j = 1; j < units[i].length; j++) {
86 if (diff <= units[i][j]) {
87 result = {
88 unit: unit,
89 step: units[i][j]
90 };
91 break;
92 }
93 }
94 if (!result) {
95 i--;
96 result = {
97 unit: units[i][0],
98 step: 1
99 };
100 }
101 break;
102 }
103 }
104 if (!result) {
105 result = {unit: Ext.Date.DAY, step: 1}; // Default step is one Day.
106 }
107 return result;
108 }
109 });