1## Copyright (C) 2008, 2009 David Bateman
3## This file is part of Octave.
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.
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.
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/>.
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}
35function datetick (varargin)
37 [h, varargin, nargin] = __plt_get_axis_arg__ ("datetick", varargin{:});
42 __datetick__ (varargin{:});
43 unwind_protect_cleanup
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)");
56%! datetick ("x", "YYYY");
58function __datetick__ (varargin)
66 if (strcmpi (arg, "keeplimits"))
69 elseif (strcmpi (arg, "keeptick"))
77 nargin = length (varargin);
83 if (ischar(arg) && (strcmp (arg, "x") || strcmp (arg, "y") ||
98 ## Don't publish the existence of this variable for use with dateaxis
99 if (length (varargin) > 0)
100 startdate = varargin{1};
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");
110 elseif (! ischar (form))
111 error ("datetick: expecting valid date format string");
116 ticks = get (gca (), strcat (ax, "tick"))
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());
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));
132 if (isnan (xmin) || isnan (xmax))
135 elseif (xmin == xmax)
142 if (xmax - xmin < N / 24 / 60 / 60)
143 scl = 1 / 24 / 60 / 60;
144 elseif (xmax - xmin < N / 24 / 6)
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;
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;
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;
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;
180 ticks = xmin + [0 : nticks - 1] / (nticks - 1) * (xmax - xmin);
184 r = max(ticks) - min(ticks);
186 ## minutes and seconds
195 ## FIXME -- FORM should be 19 for European users who use dd/mm
196 ## instead of mm/dd. How can that be determined automatically?
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));
214 sticks = strvcat (datestr (ticks(1:end-1), form),
215 datestr (ticks(end), form));
218 if (! isempty (startdate))
219 sticks = datestr (ticks - ticks(1) + startdate, form);
221 sticks = datestr (ticks, form);
225 sticks = mat2cell (sticks, ones (rows (sticks), 1), columns (sticks));
229 set (gca(), strcat (ax, "ticklabel"), sticks);
231 set (gca(), strcat (ax, "ticklabel"), sticks, strcat (ax, "lim"),
232 [min(ticks), max(ticks)]);
236 set (gca(), strcat (ax, "tick"), ticks, strcat (ax, "ticklabel"), sticks);
238 set (gca(), strcat (ax, "tick"), ticks, strcat (ax, "ticklabel"), sticks,
239 strcat (ax, "lim"), [min(ticks), max(ticks)]);
244function [a, b] = __magform__ (x)
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);
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.
277 [a, b] = __magform__ ((hi - lo) / ticint);