changelog shortlog tags changeset files revisions annotate raw

scripts/time/datetick.m

changeset 10289: 4b124317dc38
parent:16f53d29049f
author: John W. Eaton <jwe@octave.org>
date: Tue Feb 09 20:58:55 2010 -0500 (51 minutes ago)
permissions: -rw-r--r--
description: base_properties::set_children: account for hidden children
1## Copyright (C) 2008, 2009 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 ()
21## @deftypefnx {Function File} {} datetick (@var{form})
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{})
26## Adds date formatted tick labels to an axis. The axis the apply the
27## ticks to is determined by @var{axis} that can take the values "x",
28## "y" or "z". The default value is "x". The formatting of the labels is
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
35function datetick (varargin)
36
37 [h, varargin, nargin] = __plt_get_axis_arg__ ("datetick", varargin{:});
38
39 oldh = gca ();
40 unwind_protect
41 axes (h);
42 __datetick__ (varargin{:});
43 unwind_protect_cleanup
44 axes (oldh);
45 end_unwind_protect
46
47endfunction
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
58function __datetick__ (varargin)
59
60 keeplimits = false;
61 keeptick = false;
62 idx = [];
63 for i = 1 : nargin
64 arg = varargin {i};
65 if (ischar (arg))
66 if (strcmpi (arg, "keeplimits"))
67 keeplimits = true;
68 idx = [idx, i];
69 elseif (strcmpi (arg, "keeptick"))
70 keeptick = true;
71 idx = [idx, i];
72 endif
73 endif
74 endfor
75
76 varargin(idx) = [];
77 nargin = length (varargin);
78 form = [];
79 ax = "x";
80
81 if (nargin != 0)
82 arg = varargin{1};
83 if (ischar(arg) && (strcmp (arg, "x") || strcmp (arg, "y") ||
84 strcmp (arg, "z")))
85 ax = arg;
86 if (nargin > 1)
87 form = varargin{2};
88 varargin(1:2) = [];
89 else
90 varargin(1) = [];
91 endif
92 else
93 form = arg;
94 varargin(1) = [];
95 endif
96 endif
97
98 ## Don't publish the existence of this variable for use with dateaxis
99 if (length (varargin) > 0)
100 startdate = varargin{1};
101 else
102 startdate = [];
103 endif
104
105 if (! isempty (form))
106 if (isnumeric (form))
107 if (! isscalar (form) || floor (form) != form || form < 0)
108 error ("datetick: expecting form argument to be a positive integer");
109 endif
110 elseif (! ischar (form))
111 error ("datetick: expecting valid date format string");
112 endif
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 ## FIXME -- FORM should be 19 for European users who use dd/mm
196 ## instead of mm/dd. How can that be determined automatically?
197 ## months
198 form = 6;
199 elseif r < 90*12
200 ## quarters
201 form = 27;
202 else
203 ## years
204 form = 10;
205 endif
206 endif
207
208 if (length (ticks) == 6)
209 ## Careful that its not treated as a datevec
210 if (! isempty (startdate))
211 sticks = strvcat (datestr (ticks(1:end-1) - ticks(1) + startdate, form),
212 datestr (ticks(end) - ticks(1) + startdate, form));
213 else
214 sticks = strvcat (datestr (ticks(1:end-1), form),
215 datestr (ticks(end), form));
216 endif
217 else
218 if (! isempty (startdate))
219 sticks = datestr (ticks - ticks(1) + startdate, form);
220 else
221 sticks = datestr (ticks, form);
222 endif
223 endif
224
225 sticks = mat2cell (sticks, ones (rows (sticks), 1), columns (sticks));
226
227 if (keeptick)
228 if (keeplimits)
229 set (gca(), strcat (ax, "ticklabel"), sticks);
230 else
231 set (gca(), strcat (ax, "ticklabel"), sticks, strcat (ax, "lim"),
232 [min(ticks), max(ticks)]);
233 endif
234 else
235 if (keeplimits)
236 set (gca(), strcat (ax, "tick"), ticks, strcat (ax, "ticklabel"), sticks);
237 else
238 set (gca(), strcat (ax, "tick"), ticks, strcat (ax, "ticklabel"), sticks,
239 strcat (ax, "lim"), [min(ticks), max(ticks)]);
240 endif
241 endif
242endfunction
243
244function [a, b] = __magform__ (x)
245 if (x == 0)
246 a = 0;
247 b = 0;
248 else
249 l = log10 (abs (x));
250 r = fmod (l, 1);
251 a = 10 .^ r;
252 b = fix (l - r);
253 if (a < 1)
254 a *= 10;
255 b -= 1;
256 endif
257 if (x < 0)
258 a = -a;
259 endif
260 endif
261endfunction
262
263## A translation from Tom Holoryd's python code at
264## http://kurage.nimh.nih.gov/tomh/tics.py
265function sep = __calc_tick_sep__ (lo, hi)
266 persistent sqrt_2 = sqrt (2.0);
267 persistent sqrt_10 = sqrt (10.0);
268 persistent sqrt_50 = sqrt (50.0);
269
270 ticint = 5;
271
272 ## Reference: Lewart, C. R., "Algorithms SCALE1, SCALE2, and
273 ## SCALE3 for Determination of Scales on Computer Generated
274 ## Plots", Communications of the ACM, 10 (1973), 639-640.
275 ## Also cited as ACM Algorithm 463.
276
277 [a, b] = __magform__ ((hi - lo) / ticint);
278
279 if (a < sqrt_2)
280 x = 1;
281 elseif (a < sqrt_10)
282 x = 2;
283 elseif (a < sqrt_50)
284 x = 5;
285 else
286 x = 10;
287 endif
288 sep = x * 10 .^ b;
289endfunction
290