Mercurial > hg > octave-nkf
annotate scripts/time/datetick.m @ 11294:e2a4f3478b7c
datetick.m: add missing semicolon
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Thu, 25 Nov 2010 02:18:58 -0500 |
parents | fe3c3dfc07eb |
children | 1740012184f9 |
rev | line source |
---|---|
9245 | 1 ## Copyright (C) 2008, 2009 David Bateman |
8112 | 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 -*- | |
10793
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
10369
diff
changeset
|
20 ## @deftypefn {Function File} {} datetick () |
10151
c2f1cdb59821
datetick: make form argument optional
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
21 ## @deftypefnx {Function File} {} datetick (@var{form}) |
8112 | 22 ## @deftypefnx {Function File} {} datetick (@var{axis}, @var{form}) |
23 ## @deftypefnx {Function File} {} datetick (@dots{}, "keeplimits") | |
24 ## @deftypefnx {Function File} {} datetick (@dots{}, "keepticks") | |
25 ## @deftypefnx {Function File} {} datetick (@dots{ax}, @dots{}) | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8325
diff
changeset
|
26 ## Adds date formatted tick labels to an axis. The axis the apply the |
8112 | 27 ## ticks to is determined by @var{axis} that can take the values "x", |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8325
diff
changeset
|
28 ## "y" or "z". The default value is "x". The formatting of the labels is |
8112 | 29 ## determined by the variable @var{form}, that can either be a string in |
30 ## the format needed by @code{dateform}, or a positive integer that can | |
31 ## be accepted by @code{datestr}. | |
32 ## @seealso{datenum, datestr} | |
33 ## @end deftypefn | |
34 | |
35 function datetick (varargin) | |
36 | |
37 [h, varargin, nargin] = __plt_get_axis_arg__ ("datetick", varargin{:}); | |
38 | |
10151
c2f1cdb59821
datetick: make form argument optional
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
39 oldh = gca (); |
c2f1cdb59821
datetick: make form argument optional
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
40 unwind_protect |
c2f1cdb59821
datetick: make form argument optional
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
41 axes (h); |
c2f1cdb59821
datetick: make form argument optional
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
42 __datetick__ (varargin{:}); |
c2f1cdb59821
datetick: make form argument optional
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
43 unwind_protect_cleanup |
c2f1cdb59821
datetick: make form argument optional
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
44 axes (oldh); |
c2f1cdb59821
datetick: make form argument optional
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
45 end_unwind_protect |
c2f1cdb59821
datetick: make form argument optional
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
46 |
8112 | 47 endfunction |
48 | |
49 %!demo | |
50 %! yr = 1900:10:2000; | |
51 %! pop = [76.094, 92.407, 106.461, 123.077 131.954, 151.868, 179.979, ... | |
52 %! 203.984, 227.225, 249.623, 282.224]; | |
53 %! plot (datenum (yr, 1, 1), pop); | |
54 %! title ("US population (millions)"); | |
55 %! xlabel ("Year"); | |
56 %! datetick ("x", "YYYY"); | |
57 | |
10369
3516a245d607
datetick.m: Fix 'keepticks' bug, and untabify.
Ben Abbott <bpabbott@mac.com>
parents:
10151
diff
changeset
|
58 %!demo |
3516a245d607
datetick.m: Fix 'keepticks' bug, and untabify.
Ben Abbott <bpabbott@mac.com>
parents:
10151
diff
changeset
|
59 %! yr =1988:2:2002; |
3516a245d607
datetick.m: Fix 'keepticks' bug, and untabify.
Ben Abbott <bpabbott@mac.com>
parents:
10151
diff
changeset
|
60 %! yr =datenum(yr,1,1); |
3516a245d607
datetick.m: Fix 'keepticks' bug, and untabify.
Ben Abbott <bpabbott@mac.com>
parents:
10151
diff
changeset
|
61 %! pr = [12.1 13.3 12.6 13.1 13.3 14.1 14.4 15.2]; |
3516a245d607
datetick.m: Fix 'keepticks' bug, and untabify.
Ben Abbott <bpabbott@mac.com>
parents:
10151
diff
changeset
|
62 %! plot(yr,pr); |
3516a245d607
datetick.m: Fix 'keepticks' bug, and untabify.
Ben Abbott <bpabbott@mac.com>
parents:
10151
diff
changeset
|
63 %! xlabel('year') |
3516a245d607
datetick.m: Fix 'keepticks' bug, and untabify.
Ben Abbott <bpabbott@mac.com>
parents:
10151
diff
changeset
|
64 %! ylabel('average price') |
3516a245d607
datetick.m: Fix 'keepticks' bug, and untabify.
Ben Abbott <bpabbott@mac.com>
parents:
10151
diff
changeset
|
65 %! ax=gca; |
3516a245d607
datetick.m: Fix 'keepticks' bug, and untabify.
Ben Abbott <bpabbott@mac.com>
parents:
10151
diff
changeset
|
66 %! set(ax,'xtick',datenum(1990:5:2005,1,1)) |
3516a245d607
datetick.m: Fix 'keepticks' bug, and untabify.
Ben Abbott <bpabbott@mac.com>
parents:
10151
diff
changeset
|
67 %! datetick(2,'keepticks') |
3516a245d607
datetick.m: Fix 'keepticks' bug, and untabify.
Ben Abbott <bpabbott@mac.com>
parents:
10151
diff
changeset
|
68 %! set(ax,'ytick',12:16) |
3516a245d607
datetick.m: Fix 'keepticks' bug, and untabify.
Ben Abbott <bpabbott@mac.com>
parents:
10151
diff
changeset
|
69 |
8112 | 70 function __datetick__ (varargin) |
71 | |
72 keeplimits = false; | |
10369
3516a245d607
datetick.m: Fix 'keepticks' bug, and untabify.
Ben Abbott <bpabbott@mac.com>
parents:
10151
diff
changeset
|
73 keepticks = false; |
8112 | 74 idx = []; |
75 for i = 1 : nargin | |
76 arg = varargin {i}; | |
77 if (ischar (arg)) | |
78 if (strcmpi (arg, "keeplimits")) | |
10369
3516a245d607
datetick.m: Fix 'keepticks' bug, and untabify.
Ben Abbott <bpabbott@mac.com>
parents:
10151
diff
changeset
|
79 keeplimits = true; |
3516a245d607
datetick.m: Fix 'keepticks' bug, and untabify.
Ben Abbott <bpabbott@mac.com>
parents:
10151
diff
changeset
|
80 idx = [idx, i]; |
3516a245d607
datetick.m: Fix 'keepticks' bug, and untabify.
Ben Abbott <bpabbott@mac.com>
parents:
10151
diff
changeset
|
81 elseif (strcmpi (arg, "keepticks")) |
3516a245d607
datetick.m: Fix 'keepticks' bug, and untabify.
Ben Abbott <bpabbott@mac.com>
parents:
10151
diff
changeset
|
82 keepticks = true; |
3516a245d607
datetick.m: Fix 'keepticks' bug, and untabify.
Ben Abbott <bpabbott@mac.com>
parents:
10151
diff
changeset
|
83 idx = [idx, i]; |
8112 | 84 endif |
85 endif | |
86 endfor | |
87 | |
88 varargin(idx) = []; | |
89 nargin = length (varargin); | |
90 form = []; | |
91 ax = "x"; | |
92 | |
93 if (nargin != 0) | |
94 arg = varargin{1}; | |
11149
fe3c3dfc07eb
style fix: break lines before && and ||, not after
John W. Eaton <jwe@octave.org>
parents:
10793
diff
changeset
|
95 if (ischar (arg) && (strcmp (arg, "x") || strcmp (arg, "y") |
fe3c3dfc07eb
style fix: break lines before && and ||, not after
John W. Eaton <jwe@octave.org>
parents:
10793
diff
changeset
|
96 || strcmp (arg, "z"))) |
8112 | 97 ax = arg; |
98 if (nargin > 1) | |
10369
3516a245d607
datetick.m: Fix 'keepticks' bug, and untabify.
Ben Abbott <bpabbott@mac.com>
parents:
10151
diff
changeset
|
99 form = varargin{2}; |
3516a245d607
datetick.m: Fix 'keepticks' bug, and untabify.
Ben Abbott <bpabbott@mac.com>
parents:
10151
diff
changeset
|
100 varargin(1:2) = []; |
8112 | 101 else |
10369
3516a245d607
datetick.m: Fix 'keepticks' bug, and untabify.
Ben Abbott <bpabbott@mac.com>
parents:
10151
diff
changeset
|
102 varargin(1) = []; |
8112 | 103 endif |
104 else | |
105 form = arg; | |
106 varargin(1) = []; | |
107 endif | |
108 endif | |
109 | |
110 ## Don't publish the existence of this variable for use with dateaxis | |
111 if (length (varargin) > 0) | |
112 startdate = varargin{1}; | |
113 else | |
114 startdate = []; | |
115 endif | |
116 | |
10151
c2f1cdb59821
datetick: make form argument optional
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
117 if (! isempty (form)) |
c2f1cdb59821
datetick: make form argument optional
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
118 if (isnumeric (form)) |
c2f1cdb59821
datetick: make form argument optional
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
119 if (! isscalar (form) || floor (form) != form || form < 0) |
c2f1cdb59821
datetick: make form argument optional
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
120 error ("datetick: expecting form argument to be a positive integer"); |
c2f1cdb59821
datetick: make form argument optional
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
121 endif |
c2f1cdb59821
datetick: make form argument optional
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
122 elseif (! ischar (form)) |
c2f1cdb59821
datetick: make form argument optional
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
123 error ("datetick: expecting valid date format string"); |
8112 | 124 endif |
125 endif | |
126 | |
10369
3516a245d607
datetick.m: Fix 'keepticks' bug, and untabify.
Ben Abbott <bpabbott@mac.com>
parents:
10151
diff
changeset
|
127 if (keepticks) |
11294
e2a4f3478b7c
datetick.m: add missing semicolon
John W. Eaton <jwe@octave.org>
parents:
11149
diff
changeset
|
128 ticks = get (gca (), strcat (ax, "tick")); |
8112 | 129 else |
130 ## Need to do our own axis tick position calculation as | |
131 ## year, etc, don't fallback on nice datenum values. | |
132 objs = findall (gca()); | |
133 xmax = NaN; | |
134 xmin = NaN; | |
135 for i = 1 : length (objs) | |
136 fld = get (objs (i)); | |
137 if (isfield (fld, strcat (ax, "data"))) | |
10369
3516a245d607
datetick.m: Fix 'keepticks' bug, and untabify.
Ben Abbott <bpabbott@mac.com>
parents:
10151
diff
changeset
|
138 xdata = getfield (fld, strcat (ax, "data"))(:); |
3516a245d607
datetick.m: Fix 'keepticks' bug, and untabify.
Ben Abbott <bpabbott@mac.com>
parents:
10151
diff
changeset
|
139 xmin = min (xmin, min (xdata)); |
3516a245d607
datetick.m: Fix 'keepticks' bug, and untabify.
Ben Abbott <bpabbott@mac.com>
parents:
10151
diff
changeset
|
140 xmax = max (xmax, max (xdata)); |
8112 | 141 endif |
142 endfor | |
143 | |
144 if (isnan (xmin) || isnan (xmax)) | |
145 xmin = 0; | |
146 xmax = 1; | |
147 elseif (xmin == xmax) | |
148 xmax = xmin + 1; | |
149 endif | |
150 | |
151 N = 3; | |
152 if (xmax - xmin < N) | |
153 ## Day scale or less | |
154 if (xmax - xmin < N / 24 / 60 / 60) | |
10369
3516a245d607
datetick.m: Fix 'keepticks' bug, and untabify.
Ben Abbott <bpabbott@mac.com>
parents:
10151
diff
changeset
|
155 scl = 1 / 24 / 60 / 60; |
8112 | 156 elseif (xmax - xmin < N / 24 / 6) |
10369
3516a245d607
datetick.m: Fix 'keepticks' bug, and untabify.
Ben Abbott <bpabbott@mac.com>
parents:
10151
diff
changeset
|
157 scl = 1 / 24 / 60; |
8112 | 158 else |
10369
3516a245d607
datetick.m: Fix 'keepticks' bug, and untabify.
Ben Abbott <bpabbott@mac.com>
parents:
10151
diff
changeset
|
159 scl = 1 / 24; |
8112 | 160 endif |
161 sep = __calc_tick_sep__ (xmin / scl , xmax / scl); | |
162 xmin = sep * floor (xmin / scl / sep); | |
163 xmax = sep * ceil (xmax / scl / sep); | |
164 nticks = (xmax - xmin) / sep + 1; | |
165 xmin *= scl; | |
166 xmax *= scl; | |
167 else | |
168 [ymin, mmin, dmin] = datevec (xmin); | |
169 [ymax, mmax, dmax] = datevec (xmax); | |
170 minyear = ymin + (mmin - 1) / 12 + (dmin - 1) / 12 / 30; | |
171 maxyear = ymax + (mmax - 1) / 12 + (dmax - 1) / 12 / 30; | |
172 minmonth = mmin + (dmin - 1) / 30; | |
173 maxmonth = (ymax - ymin) * 12 + mmax + (dmax - 1) / 30; | |
174 | |
175 if (maxmonth - minmonth < N) | |
10369
3516a245d607
datetick.m: Fix 'keepticks' bug, and untabify.
Ben Abbott <bpabbott@mac.com>
parents:
10151
diff
changeset
|
176 sep = __calc_tick_sep__ (xmin, xmax); |
3516a245d607
datetick.m: Fix 'keepticks' bug, and untabify.
Ben Abbott <bpabbott@mac.com>
parents:
10151
diff
changeset
|
177 xmin = sep * floor (xmin / sep); |
3516a245d607
datetick.m: Fix 'keepticks' bug, and untabify.
Ben Abbott <bpabbott@mac.com>
parents:
10151
diff
changeset
|
178 xmax = sep * ceil (xmax / sep); |
3516a245d607
datetick.m: Fix 'keepticks' bug, and untabify.
Ben Abbott <bpabbott@mac.com>
parents:
10151
diff
changeset
|
179 nticks = (xmax - xmin) / sep + 1; |
8112 | 180 elseif (maxyear - minyear < N) |
10369
3516a245d607
datetick.m: Fix 'keepticks' bug, and untabify.
Ben Abbott <bpabbott@mac.com>
parents:
10151
diff
changeset
|
181 sep = __calc_tick_sep__ (minmonth , maxmonth); |
3516a245d607
datetick.m: Fix 'keepticks' bug, and untabify.
Ben Abbott <bpabbott@mac.com>
parents:
10151
diff
changeset
|
182 xmin = datenum (ymin, sep * floor (minmonth / sep), 1); |
3516a245d607
datetick.m: Fix 'keepticks' bug, and untabify.
Ben Abbott <bpabbott@mac.com>
parents:
10151
diff
changeset
|
183 xmax = datenum (ymin, sep * ceil (maxmonth / sep), 1); |
3516a245d607
datetick.m: Fix 'keepticks' bug, and untabify.
Ben Abbott <bpabbott@mac.com>
parents:
10151
diff
changeset
|
184 nticks = ceil (maxmonth / sep) - floor (minmonth / sep) + 1; |
8112 | 185 else |
10369
3516a245d607
datetick.m: Fix 'keepticks' bug, and untabify.
Ben Abbott <bpabbott@mac.com>
parents:
10151
diff
changeset
|
186 sep = __calc_tick_sep__ (minyear , maxyear); |
3516a245d607
datetick.m: Fix 'keepticks' bug, and untabify.
Ben Abbott <bpabbott@mac.com>
parents:
10151
diff
changeset
|
187 xmin = datenum (sep * floor (minyear / sep), 1, 1); |
3516a245d607
datetick.m: Fix 'keepticks' bug, and untabify.
Ben Abbott <bpabbott@mac.com>
parents:
10151
diff
changeset
|
188 xmax = datenum (sep * ceil (maxyear / sep), 1, 1); |
3516a245d607
datetick.m: Fix 'keepticks' bug, and untabify.
Ben Abbott <bpabbott@mac.com>
parents:
10151
diff
changeset
|
189 nticks = ceil (maxyear / sep) - floor (minyear / sep) + 1; |
8112 | 190 endif |
191 endif | |
192 ticks = xmin + [0 : nticks - 1] / (nticks - 1) * (xmax - xmin); | |
193 endif | |
194 | |
195 if (isempty (form)) | |
196 r = max(ticks) - min(ticks); | |
197 if r < 10/60/24 | |
198 ## minutes and seconds | |
199 form = 13; | |
200 elseif r < 2 | |
201 ## hours | |
202 form = 15; | |
203 elseif r < 15 | |
204 ## days | |
205 form = 8; | |
206 elseif r < 365 | |
10151
c2f1cdb59821
datetick: make form argument optional
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
207 ## FIXME -- FORM should be 19 for European users who use dd/mm |
c2f1cdb59821
datetick: make form argument optional
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
208 ## instead of mm/dd. How can that be determined automatically? |
8112 | 209 ## months |
210 form = 6; | |
211 elseif r < 90*12 | |
212 ## quarters | |
213 form = 27; | |
214 else | |
215 ## years | |
216 form = 10; | |
217 endif | |
218 endif | |
219 | |
220 if (length (ticks) == 6) | |
221 ## Careful that its not treated as a datevec | |
222 if (! isempty (startdate)) | |
223 sticks = strvcat (datestr (ticks(1:end-1) - ticks(1) + startdate, form), | |
10369
3516a245d607
datetick.m: Fix 'keepticks' bug, and untabify.
Ben Abbott <bpabbott@mac.com>
parents:
10151
diff
changeset
|
224 datestr (ticks(end) - ticks(1) + startdate, form)); |
8112 | 225 else |
226 sticks = strvcat (datestr (ticks(1:end-1), form), | |
10369
3516a245d607
datetick.m: Fix 'keepticks' bug, and untabify.
Ben Abbott <bpabbott@mac.com>
parents:
10151
diff
changeset
|
227 datestr (ticks(end), form)); |
8112 | 228 endif |
229 else | |
230 if (! isempty (startdate)) | |
231 sticks = datestr (ticks - ticks(1) + startdate, form); | |
232 else | |
233 sticks = datestr (ticks, form); | |
234 endif | |
235 endif | |
236 | |
237 sticks = mat2cell (sticks, ones (rows (sticks), 1), columns (sticks)); | |
238 | |
10369
3516a245d607
datetick.m: Fix 'keepticks' bug, and untabify.
Ben Abbott <bpabbott@mac.com>
parents:
10151
diff
changeset
|
239 if (keepticks) |
8112 | 240 if (keeplimits) |
241 set (gca(), strcat (ax, "ticklabel"), sticks); | |
242 else | |
243 set (gca(), strcat (ax, "ticklabel"), sticks, strcat (ax, "lim"), | |
10369
3516a245d607
datetick.m: Fix 'keepticks' bug, and untabify.
Ben Abbott <bpabbott@mac.com>
parents:
10151
diff
changeset
|
244 [min(ticks), max(ticks)]); |
8112 | 245 endif |
246 else | |
247 if (keeplimits) | |
248 set (gca(), strcat (ax, "tick"), ticks, strcat (ax, "ticklabel"), sticks); | |
249 else | |
250 set (gca(), strcat (ax, "tick"), ticks, strcat (ax, "ticklabel"), sticks, | |
10369
3516a245d607
datetick.m: Fix 'keepticks' bug, and untabify.
Ben Abbott <bpabbott@mac.com>
parents:
10151
diff
changeset
|
251 strcat (ax, "lim"), [min(ticks), max(ticks)]); |
8112 | 252 endif |
253 endif | |
254 endfunction | |
255 | |
256 function [a, b] = __magform__ (x) | |
257 if (x == 0) | |
258 a = 0; | |
259 b = 0; | |
260 else | |
261 l = log10 (abs (x)); | |
262 r = fmod (l, 1); | |
263 a = 10 .^ r; | |
264 b = fix (l - r); | |
265 if (a < 1) | |
266 a *= 10; | |
267 b -= 1; | |
268 endif | |
269 if (x < 0) | |
270 a = -a; | |
271 endif | |
272 endif | |
273 endfunction | |
274 | |
275 ## A translation from Tom Holoryd's python code at | |
276 ## http://kurage.nimh.nih.gov/tomh/tics.py | |
277 function sep = __calc_tick_sep__ (lo, hi) | |
278 persistent sqrt_2 = sqrt (2.0); | |
279 persistent sqrt_10 = sqrt (10.0); | |
280 persistent sqrt_50 = sqrt (50.0); | |
281 | |
282 ticint = 5; | |
283 | |
284 ## Reference: Lewart, C. R., "Algorithms SCALE1, SCALE2, and | |
285 ## SCALE3 for Determination of Scales on Computer Generated | |
286 ## Plots", Communications of the ACM, 10 (1973), 639-640. | |
287 ## Also cited as ACM Algorithm 463. | |
288 | |
289 [a, b] = __magform__ ((hi - lo) / ticint); | |
290 | |
291 if (a < sqrt_2) | |
292 x = 1; | |
293 elseif (a < sqrt_10) | |
294 x = 2; | |
295 elseif (a < sqrt_50) | |
296 x = 5; | |
297 else | |
298 x = 10; | |
299 endif | |
300 sep = x * 10 .^ b; | |
301 endfunction | |
302 |