File: datetick.m

package info (click to toggle)
octave 9.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 144,300 kB
  • sloc: cpp: 332,784; ansic: 77,239; fortran: 20,963; objc: 9,396; sh: 8,213; yacc: 4,925; lex: 4,389; perl: 1,544; java: 1,366; awk: 1,259; makefile: 648; xml: 189
file content (360 lines) | stat: -rw-r--r-- 10,207 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
########################################################################
##
## Copyright (C) 2008-2024 The Octave Project Developers
##
## See the file COPYRIGHT.md in the top-level directory of this
## distribution or <https://octave.org/copyright/>.
##
## This file is part of Octave.
##
## Octave is free software: you can redistribute it and/or modify it
## under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## Octave is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with Octave; see the file COPYING.  If not, see
## <https://www.gnu.org/licenses/>.
##
########################################################################

## -*- texinfo -*-
## @deftypefn  {} {} datetick ()
## @deftypefnx {} {} datetick (@var{axis_str})
## @deftypefnx {} {} datetick (@var{date_format})
## @deftypefnx {} {} datetick (@var{axis_str}, @var{date_format})
## @deftypefnx {} {} datetick (@dots{}, "keeplimits")
## @deftypefnx {} {} datetick (@dots{}, "keepticks")
## @deftypefnx {} {} datetick (@var{hax}, @dots{})
## Add date-formatted tick labels to an axis.
##
## The axis to apply the ticks to is determined by @var{axis_str} which can
## take the values @qcode{"x"}, @qcode{"y"}, or @qcode{"z"}.  The default
## value is @qcode{"x"}.
##
## The formatting of the labels is determined by the variable
## @var{date_format}, which can either be a string or positive integer that
## @code{datestr} accepts.
##
## If the first argument @var{hax} is an axes handle, then plot into this axes,
## rather than the current axes returned by @code{gca}.
##
## @seealso{datenum, datestr}
## @end deftypefn

function datetick (varargin)

  [hax, varargin, nargin] = __plt_get_axis_arg__ ("datetick", varargin{:});

  oldfig = [];
  if (! isempty (hax))
    oldfig = get (0, "currentfigure");
  endif
  if (isempty (hax))
    hax = gca ();
  endif

  unwind_protect
    ## FIXME: This will bring the axes to the top of the stack.
    ##        This may not be desirable if there are multiple axes objects,
    ##        such as can occur with plotyy.
    axes (hax);
    __datetick__ (varargin{:});
  unwind_protect_cleanup
    if (! isempty (oldfig))
      set (0, "currentfigure", oldfig);
    endif
  end_unwind_protect

endfunction


%!demo
%! clf;
%! yr = 1900:10:2000;
%! pop = [76.094, 92.407, 106.461, 123.077 131.954, 151.868, 179.979, ...
%!        203.984, 227.225, 249.623, 282.224];
%! plot (datenum (yr, 1, 1), pop);
%! xlabel ("Year");
%! ylabel ("US population (millions)");
%! title ("datetick() with 4-digit year format");
%! datetick ("x", "YYYY");

%!demo
%! clf;
%! yr = 1988:2:2002;
%! yr = datenum (yr,1,1);
%! pr = [12.1 13.3 12.6 13.1 13.3 14.1 14.4 15.2];
%! plot (yr, pr, "-o");
%! xlabel ("year");
%! ylabel ("average price");
%! title ("datetick() with MM/DD/YY format");
%! ax = gca ();
%! set (ax, "xtick", datenum (1990:5:2005,1,1));
%! datetick ("x", 2, "keepticks");
%! set (ax, "ytick", 12:16);

%!test
%! hf = figure ("visible", "off");
%! unwind_protect
%!   hax = axes ();
%!   plot ([213:364 0:28], randn (1,181));
%!   datetick ("x", 3);
%!   xticks = get (hax, "xtick");
%!   assert (xticks, [-30 32 92 153 214 275 336 398]);
%! unwind_protect_cleanup
%!   close (hf);
%! end_unwind_protect

function __datetick__ (varargin)

  keeplimits = false;
  idx = strcmpi (varargin, "keeplimits");
  if (any (idx))
    keeplimits = true;
    varargin = varargin(! idx);
  endif
  keepticks = false;
  idx = strcmpi (varargin, "keepticks");
  if (any (idx))
    keepticks = true;
    varargin = varargin(! idx);
  endif

  nargin = numel (varargin);
  form = [];
  ax = "x";

  if (nargin != 0)
    arg = varargin{1};
    if (ischar (arg) && any (strcmpi (arg, {"x", "y", "z"})))
      ax = lower (arg);
      if (nargin > 1)
        form = varargin{2};
        varargin(1:2) = [];
      else
        varargin(1) = [];
      endif
    else
      form = arg;
      varargin(1) = [];
    endif
  endif

  ## Don't publish the existence of this variable for use with dateaxis
  if (length (varargin) > 0)
    startdate = varargin{1};
  else
    startdate = [];
  endif

  if (! isempty (form))
    if (isnumeric (form))
      if (! isscalar (form) || form < 0 || form != fix (form))
        error ("datetick: FORM argument must be a positive integer");
      endif
    elseif (! ischar (form))
      error ("datetick: FORM argument must be a valid date format string");
    endif
  endif

  if (keepticks)
    ticks = get (gca (), [ax "tick"]);
  else
    ## Need to do our own axis tick position calculation as
    ## year, etc., don't fall back to nice datenum values.
    if (keeplimits)
      limits = get (gca (), [ax "lim"]);
      xmin = limits(1);
      xmax = limits(2);
    else
      objs = findall (gca ());
      xmin = xmax = NaN;
      for i = 1 : numel (objs)
        fld = get (objs(i));
        if (isfield (fld, [ax "data"]))
          xdata = getfield (fld, [ax "data"])(:);
          xmin = min (xmin, min (xdata));
          xmax = max (xmax, max (xdata));
        endif
      endfor
    endif

    if (isnan (xmin) || isnan (xmax))
      xmin = 0;
      xmax = 1;
    elseif (xmin == xmax)
      xmax = xmin + 1;
    endif

    N = 3;
    if (xmax - xmin < N)
      ## Day scale or less
      if (xmax - xmin < N / 24 / 60 / 60)
        scl = 1 / 24 / 60 / 60;
      elseif (xmax - xmin < N / 24 / 60)
        scl = 1 / 24 / 60;
      else
        scl = 1 / 24;
      endif
      sep = __calc_tick_sep__ (xmin / scl , xmax / scl);
      xmin = sep * floor (xmin / scl / sep);
      xmax = sep * ceil (xmax / scl / sep);
      nticks = (xmax - xmin) / sep + 1;
      xmin *= scl;
      xmax *= scl;
      ticks = xmin + [0 : nticks - 1] / (nticks - 1) * (xmax - xmin);
    else
      [ymin, mmin, dmin] = datevec (xmin);
      [ymax, mmax, dmax] = datevec (xmax);
      minyear = ymin + (mmin - 1) / 12 + (dmin - 1) / 12 / 30.5;
      maxyear = ymax + (mmax - 1) / 12 + (dmax - 1) / 12 / 30.5;
      minmonth = mmin + (dmin - 1) / 30.5;
      maxmonth = (ymax  - ymin) * 12 + mmax + (dmax - 1) / 30.5;

      if (maxmonth - minmonth < N)
        sep = __calc_tick_sep__ (xmin, xmax);
        xmin = sep * floor (xmin / sep);
        xmax = sep * ceil (xmax / sep);
        nticks = (xmax - xmin) / sep + 1;
        ticks = xmin + [0 : nticks - 1] / (nticks - 1) * (xmax - xmin);
      elseif (maxyear - minyear < N)
        sep = __calc_tick_sep__ (minmonth, maxmonth);
        minyear = floor (minyear);
        minmonth = sep * floor (minmonth / sep);
        minmonth = ifelse (minmonth == 0, 1, minmonth);
        maxmonth = sep * ceil (maxmonth / sep);
        rangemonth = (minmonth:sep:maxmonth)';
        tickdays = round (1 + 28*mod (rangemonth, 1));
        ticks = datenum ([repmat(minyear, size(rangemonth)), ...
                          floor(rangemonth), ...
                          tickdays]);
      else
        sep = __calc_tick_sep__ (minyear, maxyear);
        minyear = sep * floor (minyear / sep);
        maxyear = sep * ceil (maxyear / sep);
        rangeyear = (minyear:sep:maxyear)';
        tickmonth = round (1 + 12*mod (rangeyear, 1));
        ticks = datenum ([floor(rangeyear), ...
                          tickmonth, ...
                          ones(rows (rangeyear), 1)]);
      endif
    endif
  endif

  if (isempty (form))
    r = max (ticks) - min (ticks);
    if (r < 10/60/24)
      ## minutes and seconds
      form = 13;
    elseif (r < 2)
      ## hours
      form = 15;
    elseif (r < 15)
      ## days
      form = 8;
    elseif (r < 365)
      ## FIXME: FORM should be 19 for European users who use dd/mm
      ## instead of mm/dd.  How can that be determined automatically?
      ## months
      form = 6;
    elseif (r < 90*12)
      ## quarters
      form = 27;
    else
      ## years
      form = 10;
    endif
  endif

  if (length (ticks) == 6)
    ## Careful that its not treated as a datevec
    if (! isempty (startdate))
      sticks = strvcat (datestr (ticks(1:end-1) - ticks(1) + startdate, form),
      datestr (ticks(end) - ticks(1) + startdate, form));
    else
      sticks = strvcat (datestr (ticks(1:end-1), form),
      datestr (ticks(end), form));
    endif
  else
    if (! isempty (startdate))
      sticks = datestr (ticks - ticks(1) + startdate, form);
    else
      sticks = datestr (ticks, form);
    endif
  endif

  sticks = mat2cell (sticks, ones (rows (sticks), 1), columns (sticks));

  if (keepticks)
    if (keeplimits)
      set (gca (), [ax "ticklabel"], sticks);
    else
      set (gca (), [ax "ticklabel"], sticks,
                   [ax "lim"], [min(ticks), max(ticks)]);
    endif
  else
    if (keeplimits)
      set (gca (), [ax "tick"], ticks, [ax "ticklabel"], sticks);
    else
      set (gca (), [ax "tick"], ticks, [ax "ticklabel"], sticks,
                   [ax "lim"], [min(ticks), max(ticks)]);
    endif
  endif

endfunction

function [a, b] = __magform__ (x)

  if (x == 0)
    a = b = 0;
  else
    l = log10 (abs (x));
    r = rem (l, 1);
    a = 10 .^ r;
    b = fix (l - r);
    if (a < 1)
      a *= 10;
      b -= 1;
    endif
    if (x < 0)
      a = -a;
    endif
  endif

endfunction

## A translation from Tom Holoryd's python code at
## http://kurage.nimh.nih.gov/tomh/tics.py
function sep = __calc_tick_sep__ (lo, hi)
  persistent sqrt_2  = sqrt (2.0);
  persistent sqrt_10 = sqrt (10.0);
  persistent sqrt_50 = sqrt (50.0);

  ticint = 5;

  ## Reference: Lewart, C. R., "Algorithms SCALE1, SCALE2, and
  ## SCALE3 for Determination of Scales on Computer Generated
  ## Plots", Communications of the ACM, 10 (1973), 639-640.
  ## Also cited as ACM Algorithm 463.

  [a, b] = __magform__ ((hi - lo) / ticint);

  if (a < sqrt_2)
    x = 1;
  elseif (a < sqrt_10)
    x = 2;
  elseif (a < sqrt_50)
    x = 5;
  else
    x = 10;
  endif

  sep = x * 10 .^ b;

endfunction