7017
|
1 ## Copyright (C) 1996, 1997, 2007 John W. Eaton |
6540
|
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 |
7016
|
7 ## the Free Software Foundation; either version 3 of the License, or (at |
|
8 ## your option) any later version. |
6540
|
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 |
7016
|
16 ## along with Octave; see the file COPYING. If not, see |
|
17 ## <http://www.gnu.org/licenses/>. |
6540
|
18 |
6895
|
19 ## Undocumented internal function. |
6540
|
20 |
|
21 ## Author: jwe |
|
22 |
|
23 function varargout = __bar__ (vertical, func, varargin) |
6886
|
24 |
7189
|
25 varargout = cell (nargout, 1); |
|
26 if (isscalar (varargin{1}) && ishandle (varargin{1})) |
|
27 h = varargin{1}; |
|
28 if (! strcmp (get (h, "type"), "axes")) |
|
29 error ("%s: expecting first argument to be an axes object", func); |
|
30 endif |
|
31 oldh = gca (); |
|
32 unwind_protect |
|
33 axes (h); |
|
34 [varargout{:}] = __bar2__ (h, vertical, func, varargin{2:end}); |
|
35 unwind_protect_cleanup |
|
36 axes (oldh); |
|
37 end_unwind_protect |
|
38 else |
|
39 [varargout{:}] = __bar2__ (gca(), vertical, func, varargin{:}); |
|
40 endif |
|
41 |
|
42 endfunction |
|
43 |
|
44 function varargout = __bar2__ (h, vertical, func, varargin) |
|
45 |
7148
|
46 ## Slightly smaller than 0.8 to avoid clipping issue in gnuplot 4.0 |
|
47 width = 0.8 - 10 * eps; |
6540
|
48 group = true; |
|
49 |
7189
|
50 if (nargin < 4) |
6886
|
51 print_usage (); |
6540
|
52 endif |
|
53 |
7189
|
54 if (nargin > 4 && isnumeric (varargin{2})) |
6540
|
55 x = varargin{1}; |
6886
|
56 if (isvector (x)) |
6540
|
57 x = x(:); |
|
58 endif |
|
59 y = varargin{2}; |
6886
|
60 if (isvector (y)) |
6540
|
61 y = y(:); |
|
62 endif |
6886
|
63 if (size (x, 1) != size (y, 1)) |
6540
|
64 y = varargin{1}; |
6886
|
65 if (isvector (y)) |
6540
|
66 y = y(:); |
|
67 endif |
|
68 x = [1:size(y,1)]'; |
|
69 idx = 2; |
|
70 else |
6886
|
71 if (! isvector (x)) |
6540
|
72 error ("%s: x must be a vector", func); |
|
73 endif |
|
74 idx = 3; |
|
75 endif |
|
76 else |
|
77 y = varargin{1}; |
6886
|
78 if (isvector (y)) |
6540
|
79 y = y(:); |
|
80 endif |
|
81 x = [1:size(y,1)]'; |
|
82 idx = 2; |
|
83 endif |
|
84 |
|
85 newargs = {}; |
6886
|
86 have_line_spec = false; |
7189
|
87 while (idx <= nargin - 3) |
6886
|
88 if (isstr (varargin{idx}) && strcmp (varargin{idx}, "grouped")) |
6540
|
89 group = true; |
|
90 idx++; |
6886
|
91 elseif (isstr (varargin{idx}) && strcmp (varargin{idx}, "stacked")) |
6540
|
92 group = false; |
|
93 idx++; |
|
94 else |
6886
|
95 if ((isstr (varargin{idx}) || iscell (varargin{idx})) |
|
96 && ! have_line_spec) |
6885
|
97 [linespec, valid] = __pltopt__ (func, varargin{idx}, false); |
6540
|
98 if (valid) |
6886
|
99 have_line_spec = true; |
6885
|
100 newargs = [{linespec.color}, newargs]; |
|
101 idx++; |
6540
|
102 continue; |
|
103 endif |
|
104 endif |
|
105 if (isscalar(varargin{idx})) |
|
106 width = varargin{idx++}; |
7189
|
107 elseif (idx == nargin - 3) |
6540
|
108 newargs = [newargs,varargin(idx++)]; |
|
109 else |
|
110 newargs = [newargs,varargin(idx:idx+1)]; |
|
111 idx += 2; |
|
112 endif |
|
113 endif |
|
114 endwhile |
|
115 |
|
116 xlen = size (x, 1); |
|
117 ylen = size (y, 1); |
|
118 |
|
119 if (xlen != ylen) |
|
120 error ("%s: length of x and y must be equal", func) |
|
121 endif |
|
122 if (any (x(2:end) < x(1:end-1))) |
|
123 error ("%s: x vector values must be in ascending order", func); |
|
124 endif |
|
125 |
|
126 ycols = size (y, 2); |
|
127 if (group) |
|
128 width = width / ycols; |
|
129 endif |
|
130 |
7189
|
131 cutoff = min (diff (double(x))) / 2; |
|
132 delta_p = delta_m = repmat (cutoff * width, size (x)); |
6540
|
133 x1 = (x - delta_m)(:)'; |
|
134 x2 = (x + delta_p)(:)'; |
6886
|
135 xb = repmat ([x1; x1; x2; x2](:), 1, ycols); |
6540
|
136 |
|
137 if (group) |
|
138 width = width / ycols; |
|
139 offset = ((delta_p + delta_m) * [-(ycols - 1) / 2 : (ycols - 1) / 2]); |
6885
|
140 xb(1:4:4*ylen,:) += offset; |
|
141 xb(2:4:4*ylen,:) += offset; |
|
142 xb(3:4:4*ylen,:) += offset; |
|
143 xb(4:4:4*ylen,:) += offset; |
6540
|
144 y0 = zeros (size (y)); |
|
145 y1 = y; |
|
146 else |
|
147 y1 = cumsum(y,2); |
|
148 y0 = [zeros(ylen,1), y1(:,1:end-1)]; |
|
149 endif |
|
150 |
6885
|
151 yb = zeros (4*ylen, ycols); |
|
152 yb(1:4:4*ylen,:) = y0; |
|
153 yb(2:4:4*ylen,:) = y1; |
|
154 yb(3:4:4*ylen,:) = y1; |
|
155 yb(4:4:4*ylen,:) = y0; |
|
156 |
7148
|
157 xb = reshape (xb, [4, numel(xb) / 4 / ycols, ycols]); |
|
158 yb = reshape (yb, [4, numel(yb) / 4 / ycols, ycols]); |
6885
|
159 |
7189
|
160 if (nargout < 2) |
|
161 newplot (); |
|
162 tmp = __bars__ (h, vertical, x, y, xb, yb, width, group, have_line_spec, newargs {:}); |
|
163 if (nargout == 1) |
|
164 varargout{1} = tmp; |
|
165 endif |
|
166 else |
|
167 if (vertical) |
6540
|
168 varargout{1} = xb; |
|
169 varargout{2} = yb; |
|
170 else |
|
171 varargout{1} = yb; |
|
172 varargout{2} = xb; |
|
173 endif |
6886
|
174 endif |
6540
|
175 endfunction |