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 |
7215
|
25 [h, varargin] = __plt_get_axis_arg__ ((nargout > 1), func, varargin{:}); |
7189
|
26 |
7148
|
27 ## Slightly smaller than 0.8 to avoid clipping issue in gnuplot 4.0 |
|
28 width = 0.8 - 10 * eps; |
6540
|
29 group = true; |
|
30 |
7215
|
31 if (nargin < 3) |
6886
|
32 print_usage (); |
6540
|
33 endif |
|
34 |
7215
|
35 if (nargin > 3 && isnumeric (varargin{2})) |
6540
|
36 x = varargin{1}; |
6886
|
37 if (isvector (x)) |
6540
|
38 x = x(:); |
|
39 endif |
|
40 y = varargin{2}; |
6886
|
41 if (isvector (y)) |
6540
|
42 y = y(:); |
|
43 endif |
6886
|
44 if (size (x, 1) != size (y, 1)) |
6540
|
45 y = varargin{1}; |
6886
|
46 if (isvector (y)) |
6540
|
47 y = y(:); |
|
48 endif |
|
49 x = [1:size(y,1)]'; |
|
50 idx = 2; |
|
51 else |
6886
|
52 if (! isvector (x)) |
6540
|
53 error ("%s: x must be a vector", func); |
|
54 endif |
|
55 idx = 3; |
|
56 endif |
|
57 else |
|
58 y = varargin{1}; |
6886
|
59 if (isvector (y)) |
6540
|
60 y = y(:); |
|
61 endif |
|
62 x = [1:size(y,1)]'; |
|
63 idx = 2; |
|
64 endif |
|
65 |
|
66 newargs = {}; |
6886
|
67 have_line_spec = false; |
7215
|
68 while (idx <= nargin - 2) |
6886
|
69 if (isstr (varargin{idx}) && strcmp (varargin{idx}, "grouped")) |
6540
|
70 group = true; |
|
71 idx++; |
6886
|
72 elseif (isstr (varargin{idx}) && strcmp (varargin{idx}, "stacked")) |
6540
|
73 group = false; |
|
74 idx++; |
|
75 else |
6886
|
76 if ((isstr (varargin{idx}) || iscell (varargin{idx})) |
|
77 && ! have_line_spec) |
6885
|
78 [linespec, valid] = __pltopt__ (func, varargin{idx}, false); |
6540
|
79 if (valid) |
6886
|
80 have_line_spec = true; |
6885
|
81 newargs = [{linespec.color}, newargs]; |
|
82 idx++; |
6540
|
83 continue; |
|
84 endif |
|
85 endif |
|
86 if (isscalar(varargin{idx})) |
|
87 width = varargin{idx++}; |
7215
|
88 elseif (idx == nargin - 2) |
6540
|
89 newargs = [newargs,varargin(idx++)]; |
|
90 else |
|
91 newargs = [newargs,varargin(idx:idx+1)]; |
|
92 idx += 2; |
|
93 endif |
|
94 endif |
|
95 endwhile |
|
96 |
|
97 xlen = size (x, 1); |
|
98 ylen = size (y, 1); |
|
99 |
|
100 if (xlen != ylen) |
|
101 error ("%s: length of x and y must be equal", func) |
|
102 endif |
|
103 if (any (x(2:end) < x(1:end-1))) |
|
104 error ("%s: x vector values must be in ascending order", func); |
|
105 endif |
|
106 |
|
107 ycols = size (y, 2); |
|
108 if (group) |
|
109 width = width / ycols; |
|
110 endif |
|
111 |
7189
|
112 cutoff = min (diff (double(x))) / 2; |
|
113 delta_p = delta_m = repmat (cutoff * width, size (x)); |
6540
|
114 x1 = (x - delta_m)(:)'; |
|
115 x2 = (x + delta_p)(:)'; |
6886
|
116 xb = repmat ([x1; x1; x2; x2](:), 1, ycols); |
6540
|
117 |
|
118 if (group) |
|
119 width = width / ycols; |
|
120 offset = ((delta_p + delta_m) * [-(ycols - 1) / 2 : (ycols - 1) / 2]); |
6885
|
121 xb(1:4:4*ylen,:) += offset; |
|
122 xb(2:4:4*ylen,:) += offset; |
|
123 xb(3:4:4*ylen,:) += offset; |
|
124 xb(4:4:4*ylen,:) += offset; |
6540
|
125 y0 = zeros (size (y)); |
|
126 y1 = y; |
|
127 else |
|
128 y1 = cumsum(y,2); |
|
129 y0 = [zeros(ylen,1), y1(:,1:end-1)]; |
|
130 endif |
|
131 |
6885
|
132 yb = zeros (4*ylen, ycols); |
|
133 yb(1:4:4*ylen,:) = y0; |
|
134 yb(2:4:4*ylen,:) = y1; |
|
135 yb(3:4:4*ylen,:) = y1; |
|
136 yb(4:4:4*ylen,:) = y0; |
|
137 |
7148
|
138 xb = reshape (xb, [4, numel(xb) / 4 / ycols, ycols]); |
|
139 yb = reshape (yb, [4, numel(yb) / 4 / ycols, ycols]); |
6885
|
140 |
7189
|
141 if (nargout < 2) |
7215
|
142 oldh = gca (); |
|
143 unwind_protect |
|
144 axes (h); |
|
145 newplot (); |
|
146 |
|
147 tmp = __bars__ (h, vertical, x, y, xb, yb, width, group, |
|
148 have_line_spec, newargs{:}); |
|
149 if (nargout == 1) |
|
150 varargout{1} = tmp; |
|
151 endif |
|
152 unwind_protect_cleanup |
|
153 axes (oldh); |
|
154 end_unwind_protect |
7189
|
155 else |
|
156 if (vertical) |
6540
|
157 varargout{1} = xb; |
|
158 varargout{2} = yb; |
|
159 else |
|
160 varargout{1} = yb; |
|
161 varargout{2} = xb; |
|
162 endif |
6886
|
163 endif |
7191
|
164 |
6540
|
165 endfunction |