Mercurial > octave-nkf
comparison scripts/plot/area.m @ 17437:cf5a8fccfc63
area.m: Overhaul function to fix color cycling.
* scripts/plot/area.m: Fix color cycling. Fix input validation and make it
more precise. Add %!error tests for input validation.
author | Rik <rik@octave.org> |
---|---|
date | Wed, 18 Sep 2013 19:43:09 -0700 |
parents | b81b9d079515 |
children | 76614e624818 |
comparison
equal
deleted
inserted
replaced
17436:47269b03a946 | 17437:cf5a8fccfc63 |
---|---|
63 | 63 |
64 if (nargin == 0) | 64 if (nargin == 0) |
65 print_usage (); | 65 print_usage (); |
66 endif | 66 endif |
67 | 67 |
68 idx = 1; | |
69 x = y = []; | 68 x = y = []; |
70 bv = 0; | 69 bv = 0; |
71 args = {}; | 70 |
72 ## Check for (X) or (X,Y) arguments and possible base value. | 71 num_numeric = find (cellfun ("isclass", varargin, "char"), 1) - 1; |
73 if (nargin >= idx && ismatrix (varargin{idx})) | 72 if (isempty (num_numeric)) |
74 y = varargin{idx}; | 73 num_numeric = nargin; |
75 idx++; | 74 endif |
76 if (nargin >= idx) | 75 |
77 if (isscalar (varargin{idx})) | 76 switch (num_numeric) |
78 bv = varargin{idx}; | 77 case 1 |
79 idx++; | 78 y = varargin{1}; |
80 elseif (ismatrix (varargin{idx})) | 79 case 2 |
81 x = y; | 80 if (isscalar (varargin{2})) |
82 y = varargin{idx}; | 81 y = varargin{1}; |
83 idx++; | 82 bv = varargin{2}; |
84 if (nargin >= idx && isscalar (varargin{idx})) | 83 else |
85 bv = varargin{idx}; | 84 x = varargin{1}; |
86 idx++; | 85 y = varargin{2}; |
87 endif | |
88 endif | 86 endif |
89 endif | 87 case 3 |
90 else | 88 x = varargin{1}; |
91 print_usage (); | 89 y = varargin{2}; |
92 endif | 90 bv = varargin{3}; |
93 ## Check for additional args. | 91 otherwise |
94 if (nargin >= idx) | 92 print_usage (); |
95 args = {varargin{idx:end}}; | 93 endswitch |
96 endif | 94 |
95 if (! isreal (x) || ! isreal (y)) | |
96 error ("area: X and Y must be real vectors or matrices"); | |
97 endif | |
98 if (! isreal (bv) || ! isscalar (bv)) | |
99 error ("area: LVL must be a real scalar"); | |
100 endif | |
101 | |
97 if (isvector (y)) | 102 if (isvector (y)) |
98 y = y(:); | 103 y = y(:); |
99 endif | 104 endif |
100 if (isempty (x)) | 105 if (isempty (x)) |
101 x = repmat ([1:rows(y)]', 1, columns (y)); | 106 x = repmat ([1:rows(y)]', 1, columns (y)); |
107 if (! isempty (hax)) | 112 if (! isempty (hax)) |
108 oldfig = get (0, "currentfigure"); | 113 oldfig = get (0, "currentfigure"); |
109 endif | 114 endif |
110 unwind_protect | 115 unwind_protect |
111 hax = newplot (hax); | 116 hax = newplot (hax); |
112 htmp = __area__ (hax, x, y, bv, args{:}); | 117 htmp = __area__ (hax, x, y, bv, varargin{num_numeric+1:end}); |
113 unwind_protect_cleanup | 118 unwind_protect_cleanup |
114 if (! isempty (oldfig)) | 119 if (! isempty (oldfig)) |
115 set (0, "currentfigure", oldfig); | 120 set (0, "currentfigure", oldfig); |
116 endif | 121 endif |
117 end_unwind_protect | 122 end_unwind_protect |
126 | 131 |
127 y0 = bv * ones (1, rows (y)); | 132 y0 = bv * ones (1, rows (y)); |
128 y0 = zeros (1, rows (y)); | 133 y0 = zeros (1, rows (y)); |
129 retval = []; | 134 retval = []; |
130 for i = 1: columns (y); | 135 for i = 1: columns (y); |
136 | |
137 lc = __next_line_color__ (); | |
138 | |
139 ## Must occur after __next_line_color__ in order to work correctly. | |
131 hg = hggroup (); | 140 hg = hggroup (); |
132 retval = [retval; hg]; | 141 retval = [retval; hg]; |
133 args = __add_datasource__ ("area", hg, {"x", "y"}, varargin{:}); | 142 args = __add_datasource__ ("area", hg, {"x", "y"}, varargin{:}); |
134 | 143 |
135 x1 = x(:, 1).'; | 144 x1 = x(:, 1)'; |
136 y1 = y (:, i).'; | 145 y1 = y(:, i)'; |
137 addproperty ("xdata", hg, "data", x1); | 146 addproperty ("xdata", hg, "data", x1); |
138 addproperty ("ydata", hg, "data", y1); | 147 addproperty ("ydata", hg, "data", y1); |
139 | 148 |
140 addlistener (hg, "xdata", @update_data); | 149 addlistener (hg, "xdata", @update_data); |
141 addlistener (hg, "ydata", @update_data); | 150 addlistener (hg, "ydata", @update_data); |
142 | 151 |
143 if (i == 1) | 152 if (i == 1) |
144 h = patch (ax, [x1(1), x1, fliplr(x1)], [bv, y1, bv*ones(1, length(y1))], | 153 h = patch (ax, [x1(1), x1, fliplr(x1)], [bv, y1, bv*ones(1, length(y1))], |
145 __next_line_color__ (), "parent", hg); | 154 lc, "parent", hg); |
146 else | 155 else |
147 y1 = y0 + y1; | 156 y1 = y0 + y1; |
148 h = patch (ax, [x1(1), x1, fliplr(x1)], [y0(1), y1, fliplr(y0)], | 157 h = patch (ax, [x1(1), x1, fliplr(x1)], [y0(1), y1, fliplr(y0)], |
149 __next_line_color__ (), "parent", hg); | 158 lc, "parent", hg); |
150 endif | 159 endif |
151 | 160 |
152 y0 = y1; | 161 y0 = y1; |
153 | 162 |
154 addproperty ("basevalue", hg, "data", bv); | 163 addproperty ("basevalue", hg, "data", bv); |
155 addlistener (hg, "basevalue", @move_baseline); | 164 addlistener (hg, "basevalue", @move_baseline); |
156 | 165 |
157 addproperty ("edgecolor", hg, "patchedgecolor", get (h, "edgecolor")); | 166 addproperty ("edgecolor", hg, "patchedgecolor", get (h, "edgecolor")); |
167 addproperty ("facecolor", hg, "patchfacecolor", get (h, "facecolor")); | |
168 addproperty ("linestyle", hg, "patchlinestyle", get (h, "linestyle")); | |
158 addproperty ("linewidth", hg, "patchlinewidth", get (h, "linewidth")); | 169 addproperty ("linewidth", hg, "patchlinewidth", get (h, "linewidth")); |
159 addproperty ("linestyle", hg, "patchlinestyle", get (h, "linestyle")); | |
160 addproperty ("facecolor", hg, "patchfacecolor", get (h, "facecolor")); | |
161 | 170 |
162 addlistener (hg, "edgecolor", @update_props); | 171 addlistener (hg, "edgecolor", @update_props); |
172 addlistener (hg, "facecolor", @update_props); | |
173 addlistener (hg, "linestyle", @update_props); | |
163 addlistener (hg, "linewidth", @update_props); | 174 addlistener (hg, "linewidth", @update_props); |
164 addlistener (hg, "linestyle", @update_props); | |
165 addlistener (hg, "facecolor", @update_props); | |
166 | 175 |
167 addproperty ("areagroup", hg, "data"); | 176 addproperty ("areagroup", hg, "data"); |
168 set (retval, "areagroup", retval); | 177 set (retval, "areagroup", retval); |
169 | 178 |
170 if (! isempty (args)) | 179 if (! isempty (args)) |
175 endfunction | 184 endfunction |
176 | 185 |
177 function update_props (h, d) | 186 function update_props (h, d) |
178 kids = get (h, "children"); | 187 kids = get (h, "children"); |
179 set (kids, "edgecolor", get (h, "edgecolor"), | 188 set (kids, "edgecolor", get (h, "edgecolor"), |
180 "linewidth", get (h, "linewidth"), | 189 "facecolor", get (h, "facecolor"), |
181 "linestyle", get (h, "linestyle"), | 190 "linestyle", get (h, "linestyle"), |
182 "facecolor", get (h, "facecolor")); | 191 "linewidth", get (h, "linewidth")); |
183 endfunction | 192 endfunction |
184 | 193 |
185 function move_baseline (h, d) | 194 function move_baseline (h, d) |
186 persistent recursion = false; | 195 persistent recursion = false; |
187 | 196 |
257 %! y = rand (size (x)); | 266 %! y = rand (size (x)); |
258 %! h = area (x, y); | 267 %! h = area (x, y); |
259 %! set (h, 'ydata', sort (get (h, 'ydata'))) | 268 %! set (h, 'ydata', sort (get (h, 'ydata'))) |
260 %! title ('area() plot of sorted data'); | 269 %! title ('area() plot of sorted data'); |
261 | 270 |
271 %% Test input validation | |
272 %!error area () | |
273 %!error area (1,2,3,4) | |
274 %!error <X and Y must be real vectors or matrices> area ({1}) | |
275 %!error <X and Y must be real vectors or matrices> area (1+i) | |
276 %!error <X and Y must be real vectors or matrices> area (1:2, {1, 2}) | |
277 %!error <X and Y must be real vectors or matrices> area (1:2, [1 1+i]) | |
278 %!error <LVL must be a real scalar> area (1, i) | |
279 %!error <LVL must be a real scalar> area (1, 2, ones (2,2)) | |
280 |