1## Copyright (C) 2008 Bill Denney <bill@denney.ws>
2##
3## This program is free software; you can redistribute it and/or modify it under
4## the terms of the GNU General Public License as published by the Free Software
5## Foundation; either version 3 of the License, or (at your option) any later
6## version.
7##
8## This program is distributed in the hope that it will be useful, but WITHOUT
9## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
11## details.
12##
13## You should have received a copy of the GNU General Public License along with
14## this program; if not, see <http://www.gnu.org/licenses/>.
15
16## -*- texinfo -*-
17## @deftypefn {Function File} {} dateaxis ()
18## @deftypefnx {Function File} {} dateaxis (@var{ax})
19## @deftypefnx {Function File} {} dateaxis (@var{ax}, @var{dateform})
20## @deftypefnx {Function File} {} dateaxis (@var{ax}, @var{dateform}, @var{startdate})
21## @deftypefnx {Function File} {} dateaxis (@var{h}, ...)
22##
23## Convert the current axis tick labels (or the axis handle @var{h}) to
24## a date format.  The axis given by @var{ax} ("x", "y", or "z") will be
25## changed; the default is "x".  The date format, @var{dateform}, used
26## will be either auto-determined or an integer corresponding to the
27## date formats in datestr. If @var{startdate} is given, then the first
28## tick value on the given axis is assumed to be that date.
29##
30## @seealso{bolling, candle, highlow, movavg, pointfig}
31## @end deftypefn
32
33function dateaxis (varargin)
34
35  ## defaults
36  h         = [];
37  ax        = "x";
38  dateform  = [];
39  startdate = [];
40
41  ## check inputs
42  if nargin > 0
43    if ishandle(varargin{1})
44      h = varargin{1};
45      varargin(1) = [];
46    endif
47    if length(varargin) > 0
48      ax = varargin{1};
49    endif
50    if length(varargin) > 1
51      dateform = varargin{2};
52    endif
53    if length(varargin) > 2
54      startdate = varargin{3};
55      if ischar(startdate)
56        startdate = datenum(startdate);
57      elseif !isnumeric(startdate)
58        error ("dateaxis: startdate must be either a datenum or numeric")
59      endif
60    endif
61    if length(varargin) > 3
62      print_usage ();
63    endif
64  endif
65
66  if (isempty (h))
67    h = gca ();
68  endif
69
70  if isempty(dateform)
71    r = range(get(h, [ax "lim"]));
72    if r < 10/60/24
73      ## minutes and seconds
74      dateform = 13;
75    elseif r < 2
76      ## hours
77      dateform = 15;
78    elseif r < 15
79      ## days
80      dateform = 8;
81    elseif r < 365
82      ## months
83      dateform = 6;
84    elseif r < 90*12
85      ## quarters
86      dateform = 27;
87    else
88      ## years
89      dateform = 10;
90    endif
91  endif
92
93  ticks = get (h, [ax "tick"]);
94  if (!isempty (startdate))
95    ticks = ticks - ticks(1) + startdate;
96  endif
97  ticks = datestr(ticks, dateform);
98  ticks = mat2cell(ticks, ones(size(ticks,1),1), size(ticks,2));
99  set (h, [ax "ticklabel"], ticks);
100
101endfunction
102