1 // Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
2 // Copyright (C) 2005 - INRIA - Farid Belahcene
3 // Copyright (C) 2012 - Michael Baudin
4 // Copyright (C) 2012 - 2016 - Scilab Enterprises
6 // This file is hereby licensed under the terms of the GNU GPL v2.0,
7 // pursuant to article 5.3.4 of the CeCILL v.2.1.
8 // This file was originally licensed under the terms of the CeCILL v2.1,
9 // and continues to be available under such terms.
10 // For more information, see the COPYING file which you should have received
11 // along with this program.
14 function barh(varargin)
16 // barh(x,y,width,style,color)
18 // x : a scalar or a vector of reals
19 // y : a scalar, a vector or a matrix of reals
20 // width : a double, the bar width, it's the percentage (0<width<1) of the max width of one bar which is wanted (default: width=0.8)
21 // style : a string, 'grouped' or 'stacked' (default: style='grouped')
23 if and(size(varargin)<>[1:5]) then
24 error(msprintf(gettext("%s: Wrong number of input argument(s): %d to %d expected.\n"), "barh", 1, 5));
27 styletab=["grouped","stacked"]
35 // Check RHS arguments
38 // Detect and set the current axes now:
39 if type(ListArg(1)) == 9
41 if (hdle.type == "Axes")
43 ListArg(1) = null(); // remove this parameter from the list
45 warning(msprintf(gettext("%s: Wrong type for input argument #%d: Axes handle expected.\n"),"barh",1));
49 if size(ListArg) == 4 then
51 if type(COLOR) <> 10 then
52 error(msprintf(gettext("%s: Wrong type for input argument #%d: string expected.\n"),"barh",4));
55 if size(ListArg) == 5 then
57 if type(STYLE) <> 10 then
58 error(msprintf(gettext("%s: Wrong type for input argument #%d: string expected.\n"),"barh",5));
67 T(k) = type(ListArg(k))
73 if size(argdb,"*")<> argdb($) then
74 error(msprintf(gettext("%s: Wrong type for input arguments: Matrix expected for %s, %s and %s.\n"),"barh", "x", "y", "width"));
77 if size(argstr,"*") <> nv-argdb($) then
78 error(msprintf(gettext("%s: Wrong type for input arguments: String expected for %s and %s.\n"),"barh", "color", "style"));
81 // Set the double argument : x,y,width
85 if or(size(Y)==1) then
92 if size(ListArg(2),"*")==1 then
94 if size(ListArg(1),"*")==1 then
101 if or(size(Y)==1) then
110 if or(size(X)==1) then
111 if size(X,"*")<>1 then // X is a vector
112 if or(size(Y)==1) then // Y is a vector
115 if size(X,"*")<>size(Y,1)
116 error(msprintf(gettext("%s: Wrong size for input arguments #%d and #%d: The number of rows of argument #%d must be equal to the size of argument #%d.\n"),"bar",1, 2, 2, 1));
118 elseif size(Y,1)>1 then
119 error(msprintf(gettext("%s: Wrong size for input arguments #%d: A scalar or a column vector expected.\n"),"bar",2));
122 error(msprintf(gettext("%s: Wrong type for input argument #%d: A scalar or a vector expected.\n"),"barh",1));
127 // barh(x,y,width,...)
128 if size(argdb,"*")==3
132 if size(WIDTH,"*")<>1 then
133 error(msprintf(gettext("%s: Wrong type for input argument #%d: A scalar expected.\n"),"barh",3));
134 elseif or(size(X)==1) then
135 if size(X,"*")<>1 then // X is a vector
136 if or(size(Y)==1) then // Y is a vector
139 if size(X,"*")<>size(Y,1)
140 error(msprintf(gettext("%s: Wrong size for input arguments #%d and #%d: The number of rows of argument #%d must be equal to the size of argument #%d.\n"),"bar",1, 2, 2, 1));
142 elseif size(Y,1)>1 then
143 error(msprintf(gettext("%s: Wrong size for input arguments #%d: A scalar or a column vector expected.\n"),"bar",2));
146 error(msprintf(gettext("%s: Wrong type for input argument #%d: A scalar or a vector expected.\n"),"barh",1));
152 // Set the string argument
153 for i=1:size(argstr,"*")
155 if or(ListArg(argstr(i))==styletab) then
156 STYLE=ListArg(argstr(i))
158 COLOR=ListArg(argstr(i))
163 // Verify if there are data bounds which are defined before creation the horizontal bars creation, in order to merge the data bounds
165 if size(a.children)<>0 then
166 gca_children_empty=%t
167 a_data_bounds=a.data_bounds
169 gca_children_empty=%f
174 immediate_drawing = curFig.immediate_drawing;
177 plot(X,Y,COLOR); // plot manages immediate_drawing property itself to avoid flickering
179 plot(X,Y); // plot manages immediate_drawing property itself to avoid flickering
182 curFig.immediate_drawing = "off";
184 barh_number=size(Y,2)
186 if size(X,"*")>1 then
187 Xtemp=gsort(X,"r","i")
188 inter=Xtemp(2)-Xtemp(1)
189 for i=2:size(Xtemp,"*")-1
190 inter=min(Xtemp(i+1)-Xtemp(i),inter)
199 wmax=inter/barh_number
200 y_shift=zeros(size(X,"*"),1)
201 bar_number= size(Y,2)
206 for i=bar_number:-1:1
211 if modulo(bar_number,2)==0 then
212 x_shift=(-i+bar_number/2)*wmax+0.4*wmax
213 elseif modulo(bar_number,2)==1 then
214 x_shift=(-i+1+floor(bar_number/2))*wmax
218 if i==bar_number then
219 y_shift=zeros(size(X,"*"),1)
221 y_shift=Y(:,bar_number-i)+y_shift
224 // Update axes data bounds
227 if i <> bar_number then
228 ymin=min(a.data_bounds(1,1),min(Y(:,bar_number-i+1)),0)
229 xmin=min(a.data_bounds(1,2),min(X)+x_shift-0.4*wmax)
230 ymax=max(a.data_bounds(2,1),max(Y(:,bar_number-i+1)),0)
231 xmax=max(a.data_bounds(2,2),max(X)+x_shift+0.4*wmax)
233 if ~gca_children_empty
234 ymin=min(min(Y(:,bar_number-i+1)),0)
235 xmin=min(X)+x_shift-0.4*wmax
236 ymax=max(max(Y(:,bar_number-i+1)),0)
237 xmax=max(X)+x_shift+0.4*wmax
239 ymin=min(a_data_bounds(1,1),min(Y(:,bar_number-i+1)),0)
240 xmin=min(a_data_bounds(1,2),min(X)+x_shift-0.4*wmax)
241 ymax=max(a_data_bounds(2,1),max(Y(:,bar_number-i+1)),0)
242 xmax=max(a_data_bounds(2,2),max(X)+x_shift+0.4*wmax)
245 a.data_bounds=[ymin xmin;ymax xmax]
246 ei.x_shift=x_shift*ones(size(X,"*"),1)
247 else // case 'stacked'
249 if i <> bar_number then
250 ymin=min(a.data_bounds(1,1),min(Y(:,bar_number-i+1)+y_shift))
251 xmin=min(a.data_bounds(1,2),0,min(X-0.4*wmax))
252 ymax=max(a.data_bounds(2,1),max(Y(:,bar_number-i+1)+y_shift))
253 xmax=max(a.data_bounds(2,2),0,max(X+0.4*wmax))
255 if ~gca_children_empty
256 ymin=min(Y(:,bar_number-i+1)+y_shift)
257 xmin=min(0,min(X-0.4*wmax))
258 ymax=max(Y(:,bar_number-i+1)+y_shift)
259 xmax=max(0,max(X+0.4*wmax))
261 ymin=min(a_data_bounds(1,1),min(Y(:,bar_number-i+1)+y_shift))
262 xmin=min(a_data_bounds(1,2),0,min(X-0.4*wmax))
263 ymax=max(a_data_bounds(2,1),max(Y(:,bar_number-i+1)+y_shift))
264 xmax=max(a_data_bounds(2,2),0,max(X+0.4*wmax))
267 a.data_bounds=[ymin xmin; ymax xmax]
271 a.y_ticks=tlist("ticks",Xtemp,string(Xtemp))
274 ei.background=ei.foreground
275 ei.polyline_style=7; // bar type
276 ei.background=ei.foreground
277 ei.foreground = -1; // black by default
282 curFig.immediate_drawing = immediate_drawing;