8112
|
1 ## Copyright (C) 2008 David Bateman |
|
2 ## |
|
3 ## This file is part of Octave. |
|
4 ## |
|
5 ## Octave is free software; you can redistribute it and/or modify it |
|
6 ## under the terms of the GNU General Public License as published by |
|
7 ## the Free Software Foundation; either version 3 of the License, or (at |
|
8 ## your option) any later version. |
|
9 ## |
|
10 ## Octave is distributed in the hope that it will be useful, but |
|
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
13 ## General Public License for more details. |
|
14 ## |
|
15 ## You should have received a copy of the GNU General Public License |
|
16 ## along with Octave; see the file COPYING. If not, see |
|
17 ## <http://www.gnu.org/licenses/>. |
|
18 |
|
19 ## -*- texinfo -*- |
|
20 ## @deftypefn {Function File} {} datetick (@var{form}) |
|
21 ## @deftypefnx {Function File} {} datetick (@var{axis}, @var{form}) |
|
22 ## @deftypefnx {Function File} {} datetick (@dots{}, "keeplimits") |
|
23 ## @deftypefnx {Function File} {} datetick (@dots{}, "keepticks") |
|
24 ## @deftypefnx {Function File} {} datetick (@dots{ax}, @dots{}) |
|
25 ## Adds date formatted tick labels to an axis. The axis the apply the |
|
26 ## ticks to is determined by @var{axis} that can take the values "x", |
|
27 ## "y" or "z". The default value is "x". The formating of the labels is |
|
28 ## determined by the variable @var{form}, that can either be a string in |
|
29 ## the format needed by @code{dateform}, or a positive integer that can |
|
30 ## be accepted by @code{datestr}. |
|
31 ## @seealso{datenum, datestr} |
|
32 ## @end deftypefn |
|
33 |
|
34 function datetick (varargin) |
|
35 |
|
36 [h, varargin, nargin] = __plt_get_axis_arg__ ("datetick", varargin{:}); |
|
37 |
|
38 if (nargin < 1) |
|
39 print_usage (); |
|
40 else |
|
41 oldh = gca (); |
|
42 unwind_protect |
|
43 axes (h); |
|
44 __datetick__ (varargin{:}); |
|
45 unwind_protect_cleanup |
|
46 axes (oldh); |
|
47 end_unwind_protect |
|
48 endif |
|
49 endfunction |
|
50 |
|
51 %!demo |
|
52 %! yr = 1900:10:2000; |
|
53 %! pop = [76.094, 92.407, 106.461, 123.077 131.954, 151.868, 179.979, ... |
|
54 %! 203.984, 227.225, 249.623, 282.224]; |
|
55 %! plot (datenum (yr, 1, 1), pop); |
|
56 %! title ("US population (millions)"); |
|
57 %! xlabel ("Year"); |
|
58 %! datetick ("x", "YYYY"); |
|
59 |
|
60 function __datetick__ (varargin) |
|
61 |
|
62 keeplimits = false; |
|
63 keeptick = false; |
|
64 idx = []; |
|
65 for i = 1 : nargin |
|
66 arg = varargin {i}; |
|
67 if (ischar (arg)) |
|
68 if (strcmpi (arg, "keeplimits")) |
|
69 keeplimits = true; |
|
70 idx = [idx, i]; |
|
71 elseif (strcmpi (arg, "keeptick")) |
|
72 keeptick = true; |
|
73 idx = [idx, i]; |
|
74 endif |
|
75 endif |
|
76 endfor |
|
77 |
|
78 varargin(idx) = []; |
|
79 nargin = length (varargin); |
|
80 form = []; |
|
81 ax = "x"; |
|
82 |
|
83 if (nargin != 0) |
|
84 arg = varargin{1}; |
|
85 if (ischar(arg) && (strcmp (arg, "x") || strcmp (arg, "y") || |
|
86 strcmp (arg, "z"))) |
|
87 ax = arg; |
|
88 if (nargin > 1) |
|
89 form = varargin{2}; |
|
90 varargin(1:2) = []; |
|
91 else |
|
92 varargin(1) = []; |
|
93 endif |
|
94 else |
|
95 form = arg; |
|
96 varargin(1) = []; |
|
97 endif |
|
98 endif |
|
99 |
|
100 ## Don't publish the existence of this variable for use with dateaxis |
|
101 if (length (varargin) > 0) |
|
102 startdate = varargin{1}; |
|
103 else |
|
104 startdate = []; |
|
105 endif |
|
106 |
|
107 if (isnumeric (form)) |
|
108 if (! isscalar (form) || floor (form) != form || form < 0) |
|
109 error ("datetick: expecting form argument to be a positive integer"); |
|
110 endif |
|
111 elseif (! ischar (form) && !isempty (form)) |
|
112 error ("datetick: expecting valid date format string"); |
|
113 endif |
|
114 |
|
115 if (keeptick) |
|
116 ticks = get (gca (), strcat (ax, "tick")) |
|
117 else |
|
118 ## Need to do our own axis tick position calculation as |
|
119 ## year, etc, don't fallback on nice datenum values. |
|
120 objs = findall (gca()); |
|
121 xmax = NaN; |
|
122 xmin = NaN; |
|
123 for i = 1 : length (objs) |
|
124 fld = get (objs (i)); |
|
125 if (isfield (fld, strcat (ax, "data"))) |
|
126 xdata = getfield (fld, strcat (ax, "data"))(:); |
|
127 xmin = min (xmin, min (xdata)); |
|
128 xmax = max (xmax, max (xdata)); |
|
129 endif |
|
130 endfor |
|
131 |
|
132 if (isnan (xmin) || isnan (xmax)) |
|
133 xmin = 0; |
|
134 xmax = 1; |
|
135 elseif (xmin == xmax) |
|
136 xmax = xmin + 1; |
|
137 endif |
|
138 |
|
139 N = 3; |
|
140 if (xmax - xmin < N) |
|
141 ## Day scale or less |
|
142 if (xmax - xmin < N / 24 / 60 / 60) |
|
143 scl = 1 / 24 / 60 / 60; |
|
144 elseif (xmax - xmin < N / 24 / 6) |
|
145 scl = 1 / 24 / 60; |
|
146 else |
|
147 scl = 1 / 24; |
|
148 endif |
|
149 sep = __calc_tick_sep__ (xmin / scl , xmax / scl); |
|
150 xmin = sep * floor (xmin / scl / sep); |
|
151 xmax = sep * ceil (xmax / scl / sep); |
|
152 nticks = (xmax - xmin) / sep + 1; |
|
153 xmin *= scl; |
|
154 xmax *= scl; |
|
155 else |
|
156 [ymin, mmin, dmin] = datevec (xmin); |
|
157 [ymax, mmax, dmax] = datevec (xmax); |
|
158 minyear = ymin + (mmin - 1) / 12 + (dmin - 1) / 12 / 30; |
|
159 maxyear = ymax + (mmax - 1) / 12 + (dmax - 1) / 12 / 30; |
|
160 minmonth = mmin + (dmin - 1) / 30; |
|
161 maxmonth = (ymax - ymin) * 12 + mmax + (dmax - 1) / 30; |
|
162 |
|
163 if (maxmonth - minmonth < N) |
|
164 sep = __calc_tick_sep__ (xmin, xmax); |
|
165 xmin = sep * floor (xmin / sep); |
|
166 xmax = sep * ceil (xmax / sep); |
|
167 nticks = (xmax - xmin) / sep + 1; |
|
168 elseif (maxyear - minyear < N) |
|
169 sep = __calc_tick_sep__ (minmonth , maxmonth); |
|
170 xmin = datenum (ymin, sep * floor (minmonth / sep), 1); |
|
171 xmax = datenum (ymin, sep * ceil (maxmonth / sep), 1); |
|
172 nticks = ceil (maxmonth / sep) - floor (minmonth / sep) + 1; |
|
173 else |
|
174 sep = __calc_tick_sep__ (minyear , maxyear); |
|
175 xmin = datenum (sep * floor (minyear / sep), 1, 1); |
|
176 xmax = datenum (sep * ceil (maxyear / sep), 1, 1); |
|
177 nticks = ceil (maxyear / sep) - floor (minyear / sep) + 1; |
|
178 endif |
|
179 endif |
|
180 ticks = xmin + [0 : nticks - 1] / (nticks - 1) * (xmax - xmin); |
|
181 endif |
|
182 |
|
183 if (isempty (form)) |
|
184 r = max(ticks) - min(ticks); |
|
185 if r < 10/60/24 |
|
186 ## minutes and seconds |
|
187 form = 13; |
|
188 elseif r < 2 |
|
189 ## hours |
|
190 form = 15; |
|
191 elseif r < 15 |
|
192 ## days |
|
193 form = 8; |
|
194 elseif r < 365 |
|
195 ## months |
|
196 form = 6; |
|
197 elseif r < 90*12 |
|
198 ## quarters |
|
199 form = 27; |
|
200 else |
|
201 ## years |
|
202 form = 10; |
|
203 endif |
|
204 endif |
|
205 |
|
206 if (length (ticks) == 6) |
|
207 ## Careful that its not treated as a datevec |
|
208 if (! isempty (startdate)) |
|
209 sticks = strvcat (datestr (ticks(1:end-1) - ticks(1) + startdate, form), |
|
210 datestr (ticks(end) - ticks(1) + startdate, form)); |
|
211 else |
|
212 sticks = strvcat (datestr (ticks(1:end-1), form), |
|
213 datestr (ticks(end), form)); |
|
214 endif |
|
215 else |
|
216 if (! isempty (startdate)) |
|
217 sticks = datestr (ticks - ticks(1) + startdate, form); |
|
218 else |
|
219 sticks = datestr (ticks, form); |
|
220 endif |
|
221 endif |
|
222 |
|
223 sticks = mat2cell (sticks, ones (rows (sticks), 1), columns (sticks)); |
|
224 |
|
225 if (keeptick) |
|
226 if (keeplimits) |
|
227 set (gca(), strcat (ax, "ticklabel"), sticks); |
|
228 else |
|
229 set (gca(), strcat (ax, "ticklabel"), sticks, strcat (ax, "lim"), |
|
230 [min(ticks), max(ticks)]); |
|
231 endif |
|
232 else |
|
233 if (keeplimits) |
|
234 set (gca(), strcat (ax, "tick"), ticks, strcat (ax, "ticklabel"), sticks); |
|
235 else |
|
236 set (gca(), strcat (ax, "tick"), ticks, strcat (ax, "ticklabel"), sticks, |
|
237 strcat (ax, "lim"), [min(ticks), max(ticks)]); |
|
238 endif |
|
239 endif |
|
240 endfunction |
|
241 |
|
242 function [a, b] = __magform__ (x) |
|
243 if (x == 0) |
|
244 a = 0; |
|
245 b = 0; |
|
246 else |
|
247 l = log10 (abs (x)); |
|
248 r = fmod (l, 1); |
|
249 a = 10 .^ r; |
|
250 b = fix (l - r); |
|
251 if (a < 1) |
|
252 a *= 10; |
|
253 b -= 1; |
|
254 endif |
|
255 if (x < 0) |
|
256 a = -a; |
|
257 endif |
|
258 endif |
|
259 endfunction |
|
260 |
|
261 ## A translation from Tom Holoryd's python code at |
|
262 ## http://kurage.nimh.nih.gov/tomh/tics.py |
|
263 function sep = __calc_tick_sep__ (lo, hi) |
|
264 persistent sqrt_2 = sqrt (2.0); |
|
265 persistent sqrt_10 = sqrt (10.0); |
|
266 persistent sqrt_50 = sqrt (50.0); |
|
267 |
|
268 ticint = 5; |
|
269 |
|
270 ## Reference: Lewart, C. R., "Algorithms SCALE1, SCALE2, and |
|
271 ## SCALE3 for Determination of Scales on Computer Generated |
|
272 ## Plots", Communications of the ACM, 10 (1973), 639-640. |
|
273 ## Also cited as ACM Algorithm 463. |
|
274 |
|
275 [a, b] = __magform__ ((hi - lo) / ticint); |
|
276 |
|
277 if (a < sqrt_2) |
|
278 x = 1; |
|
279 elseif (a < sqrt_10) |
|
280 x = 2; |
|
281 elseif (a < sqrt_50) |
|
282 x = 5; |
|
283 else |
|
284 x = 10; |
|
285 endif |
|
286 sep = x * 10 .^ b; |
|
287 endfunction |
|
288 |