]> git.proxmox.com Git - extjs.git/blob - extjs/packages/charts/src/chart/axis/segmenter/Time.js
bump version to 7.0.0-4
[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 * @cfg {String} step.unit The unit of the step (Ext.Date.DAY, Ext.Date.MONTH, etc).
15 * @cfg {Number} step.step The number of units for the step (1, 2, etc).
16 * If specified, will override the result of {@link #preferredStep}.
17 * For example:
18 *
19 * step: {
20 * unit: Ext.Date.HOUR,
21 * step: 1
22 * }
23 */
24 step: null
25 },
26
27 renderer: function(value, context) {
28 var ExtDate = Ext.Date;
29
30 switch (context.majorTicks.unit) {
31 case 'y':
32 return ExtDate.format(value, 'Y');
33
34 case 'mo':
35 return ExtDate.format(value, 'Y-m');
36
37 case 'd':
38 return ExtDate.format(value, 'Y-m-d');
39 }
40
41 return ExtDate.format(value, 'Y-m-d\nH:i:s');
42 },
43
44 from: function(value) {
45 return new Date(value);
46 },
47
48 diff: function(min, max, unit) {
49 if (isFinite(min)) {
50 min = new Date(min);
51 }
52
53 if (isFinite(max)) {
54 max = new Date(max);
55 }
56
57 return Ext.Date.diff(min, max, unit);
58 },
59
60 updateStep: function() {
61 var axis = this.getAxis();
62
63 if (axis && !this.isConfiguring) {
64 axis.performLayout();
65 }
66 },
67
68 align: function(date, step, unit) {
69 if (unit === 'd' && step >= 7) {
70 date = Ext.Date.align(date, 'd', step);
71 date.setDate(date.getDate() - date.getDay() + 1);
72
73 return date;
74 }
75 else {
76 return Ext.Date.align(date, unit, step);
77 }
78 },
79
80 add: function(value, step, unit) {
81 return Ext.Date.add(new Date(value), unit, step);
82 },
83
84 timeBuckets: [
85 {
86 unit: Ext.Date.YEAR,
87 steps: [1, 2, 5, 10, 20, 50, 100, 200, 500]
88 },
89 {
90 unit: Ext.Date.MONTH,
91 steps: [1, 3, 6]
92 },
93 {
94 unit: Ext.Date.DAY,
95 steps: [1, 7, 14]
96 },
97 {
98 unit: Ext.Date.HOUR,
99 steps: [1, 6, 12]
100 },
101 {
102 unit: Ext.Date.MINUTE,
103 steps: [1, 5, 15, 30]
104 },
105 {
106 unit: Ext.Date.SECOND,
107 steps: [1, 5, 15, 30]
108 },
109 {
110 unit: Ext.Date.MILLI,
111 steps: [1, 2, 5, 10, 20, 50, 100, 200, 500]
112 }
113 ],
114
115 /**
116 * @private
117 * Takes a time interval and figures out what is the smallest nice number of which
118 * units (years, months, days, etc.) that can fully encompass that interval.
119 * @param {Date} min
120 * @param {Date} max
121 * @return {Object}
122 * @return {String} return.unit The unit.
123 * @return {Number} return.step The number of units.
124 */
125 getTimeBucket: function(min, max) {
126 var buckets = this.timeBuckets,
127 unit, unitCount,
128 steps, step,
129 result,
130 i, j;
131
132 for (i = 0; i < buckets.length; i++) {
133 unit = buckets[i].unit;
134 unitCount = this.diff(min, max, unit);
135
136 if (unitCount > 0) {
137 steps = buckets[i].steps;
138
139 for (j = 0; j < steps.length; j++) {
140 step = steps[j];
141
142 if (unitCount <= step) {
143 break;
144 }
145 }
146
147 result = {
148 unit: unit,
149 step: step
150 };
151 break;
152 }
153 }
154
155 // If the interval is smaller then one millisecond ...
156 if (!result) {
157 // ... we can't go smaller than one millisecond.
158 result = {
159 unit: Ext.Date.MILLI,
160 step: 1
161 };
162 }
163
164 return result;
165 },
166
167 preferredStep: function(min, estStepSize) {
168 var step = this.getStep();
169
170 return step
171 ? step
172 : this.getTimeBucket(
173 new Date(+min),
174 new Date(+min + Math.ceil(estStepSize))
175 );
176 }
177 });