summaryrefslogtreecommitdiff
path: root/modules/graphics/macros/barh.sci
blob: 8d6e43e110a5cc4a06922d2868913acd268456d1 (plain)
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
// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
// Copyright (C) 2005 - INRIA - Farid Belahcene
// Copyright (C) 2012 - Michael Baudin
// This file must be used under the terms of the CeCILL.
// This source file is licensed as described in the file COPYING, which
// you should have received as part of this distribution.  The terms
// are also available at
// http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt


function  barh(varargin)

    // barh(x,y,width,style,color)
    // Input :
    // x : a scalar or a vector of reals
    // y : a sclar, a vector or a matrix of reals
    // 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)
    // style : a string, 'grouped' or 'stacked' (default: style='grouped')

    if and(size(varargin)<>[1:5]) then
        error(msprintf(gettext("%s: Wrong number of input argument(s): %d to %d expected.\n"), "barh", 1, 5));
    end

    styletab=["grouped","stacked"]
    COLORBOOL=%f

    // Default values
    STYLE="grouped"
    WIDTH=0.8
    COLORBOOL=%f

    // Check RHS arguments
    ListArg = varargin;

    // Detect and set the current axes now:
    if type(ListArg(1)) == 9
        hdle = ListArg(1);
        if (hdle.type == "Axes")
            sca(ListArg(1));
            ListArg(1) = null(); // remove this parameter from the list
        else
            warning(msprintf(gettext("%s: Wrong type for input argument #%d: Axes handle expected.\n"),"barh",1));
            return;
        end
    end
    if size(ListArg) == 4 then
        COLOR=ListArg(4);
        if type(COLOR) <> 10 then
            error(msprintf(gettext("%s: Wrong type for input argument #%d: A string expected.\n"),"barh",4));
        end
    end
    if size(ListArg) == 5 then
        STYLE=ListArg(5);
        if type(STYLE) <> 10 then
            error(msprintf(gettext("%s: Wrong type for input argument #%d: A string expected.\n"),"barh",5));
        end
    end

    nv = size(ListArg)

    T=[];

    for k=1:nv
        T(k) = type(ListArg(k))
    end

    argdb=find(T==1)
    argstr=find(T==10)

    if size(argdb,"*")<> argdb($) then
        error(msprintf(gettext("%s: Wrong type for input arguments: Matrix expected for %s, %s and %s.\n"),"barh", "x", "y", "width"));
    end

    if size(argstr,"*") <> nv-argdb($) then
        error(msprintf(gettext("%s: Wrong type for input arguments: String expected for %s and %s.\n"),"barh", "color", "style"));
    end

    // Set the double argument : x,y,width
    // barh(y,...)
    if size(argdb,"*")==1
        Y=ListArg(1)
        if or(size(Y)==1) then
            Y=Y(:)
        end
        X=1:size(Y,1)
    end

    if size(argdb,"*")==2
        if size(ListArg(2),"*")==1 then
            // barh(x,y,...)
            if size(ListArg(1),"*")==1 then
                X=ListArg(1)
                Y=ListArg(2)
            else
                //barh(y,width,...)
                WIDTH=ListArg(2)
                Y=ListArg(1)
                if or(size(Y)==1) then
                    Y=Y(:)
                end
                X=1:size(Y,1)
            end
        else
            // barh(x,y,...)
            X=ListArg(1)
            Y=ListArg(2)
            if or(size(X)==1) then
                if size(X,"*")<>1 then // X is a vector
                    if or(size(Y)==1) then // Y is a vector
                        Y=Y(:)
                    end
                    if size(X,"*")<>size(Y,1)
                        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));
                    end
                elseif size(Y,1)>1 then
                    error(msprintf(gettext("%s: Wrong size for input arguments #%d: A scalar or a column vector expected.\n"),"bar",2));
                end
            else
                error(msprintf(gettext("%s: Wrong type for input argument #%d: A scalar or a vector expected.\n"),"barh",1));
            end
        end
    end

    // barh(x,y,width,...)
    if size(argdb,"*")==3
        X=ListArg(1)
        Y=ListArg(2)
        WIDTH=ListArg(3)
        if size(WIDTH,"*")<>1 then
            error(msprintf(gettext("%s: Wrong type for input argument #%d: A scalar expected.\n"),"barh",3));
        elseif or(size(X)==1) then
            if size(X,"*")<>1 then // X is a vector
                if or(size(Y)==1) then // Y is a vector
                    Y=Y(:)
                end
                if size(X,"*")<>size(Y,1)
                    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));
                end
            elseif size(Y,1)>1 then
                error(msprintf(gettext("%s: Wrong size for input arguments #%d: A scalar or a column vector expected.\n"),"bar",2));
            end
        else
            error(msprintf(gettext("%s: Wrong type for input argument #%d: A scalar or a vector expected.\n"),"barh",1));
        end
    end

    X=X(:)

    // Set the string argument
    for i=1:size(argstr,"*")
        // barh(...,style)
        if or(ListArg(argstr(i))==styletab) then
            STYLE=ListArg(argstr(i))
        else
            COLOR=ListArg(argstr(i))
            COLORBOOL=%t
        end
    end

    // Verify if there are data bounds which are defined before creation the horizontal bars creation, in order to merge the data bounds
    a=gca()
    if size(a.children)<>0 then
        gca_children_empty=%t
        a_data_bounds=a.data_bounds
    else
        gca_children_empty=%f
    end

    //drawlater
    curFig = gcf();
    immediate_drawing = curFig.immediate_drawing;

    if COLORBOOL
        plot(X,Y,COLOR); // plot manages immediate_drawing property itself to avoid flickering
    else
        plot(X,Y); // plot manages immediate_drawing property itself to avoid flickering
    end

    curFig.immediate_drawing = "off";

    barh_number=size(Y,2)

    if size(X,"*")>1 then
        Xtemp=gsort(X,"r","i")
        inter=Xtemp(2)-Xtemp(1)
        for i=2:size(Xtemp,"*")-1
            inter=min(Xtemp(i+1)-Xtemp(i),inter)
        end
        if barh_number>1
            inter=inter*0.9
        end
    else
        inter=1
    end

    wmax=inter/barh_number
    y_shift=zeros(size(X,"*"),1)
    bar_number= size(Y,2)
    e=gce()
    a=gca()
    a.sub_ticks(2) = 0

    for i=bar_number:-1:1

        ei = e.children(i)

        // Perform x_shift
        if modulo(bar_number,2)==0 then
            x_shift=(-i+bar_number/2)*wmax+0.4*wmax
        elseif modulo(bar_number,2)==1 then
            x_shift=(-i+1+floor(bar_number/2))*wmax
        end

        // Perform y_shift
        if i==bar_number then
            y_shift=zeros(size(X,"*"),1)
        else
            y_shift=Y(:,bar_number-i)+y_shift
        end

        // Update axes data bounds
        // case 'grouped'
        if STYLE=="grouped"
            if i <> bar_number then
                ymin=min(a.data_bounds(1,1),min(Y(:,bar_number-i+1)),0)
                xmin=min(a.data_bounds(1,2),min(X)+x_shift-0.4*wmax)
                ymax=max(a.data_bounds(2,1),max(Y(:,bar_number-i+1)),0)
                xmax=max(a.data_bounds(2,2),max(X)+x_shift+0.4*wmax)
            else
                if ~gca_children_empty
                    ymin=min(min(Y(:,bar_number-i+1)),0)
                    xmin=min(X)+x_shift-0.4*wmax
                    ymax=max(max(Y(:,bar_number-i+1)),0)
                    xmax=max(X)+x_shift+0.4*wmax
                else
                    ymin=min(a_data_bounds(1,1),min(Y(:,bar_number-i+1)),0)
                    xmin=min(a_data_bounds(1,2),min(X)+x_shift-0.4*wmax)
                    ymax=max(a_data_bounds(2,1),max(Y(:,bar_number-i+1)),0)
                    xmax=max(a_data_bounds(2,2),max(X)+x_shift+0.4*wmax)
                end
            end
            a.data_bounds=[ymin xmin;ymax xmax]
            ei.x_shift=x_shift*ones(size(X,"*"),1)
        else  // case 'stacked'
            wmax=inter
            if i <> bar_number then
                ymin=min(a.data_bounds(1,1),min(Y(:,bar_number-i+1)+y_shift))
                xmin=min(a.data_bounds(1,2),0,min(X-0.4*wmax))
                ymax=max(a.data_bounds(2,1),max(Y(:,bar_number-i+1)+y_shift))
                xmax=max(a.data_bounds(2,2),0,max(X+0.4*wmax))
            else
                if ~gca_children_empty
                    ymin=min(Y(:,bar_number-i+1)+y_shift)
                    xmin=min(0,min(X-0.4*wmax))
                    ymax=max(Y(:,bar_number-i+1)+y_shift)
                    xmax=max(0,max(X+0.4*wmax))
                else
                    ymin=min(a_data_bounds(1,1),min(Y(:,bar_number-i+1)+y_shift))
                    xmin=min(a_data_bounds(1,2),0,min(X-0.4*wmax))
                    ymax=max(a_data_bounds(2,1),max(Y(:,bar_number-i+1)+y_shift))
                    xmax=max(a_data_bounds(2,2),0,max(X+0.4*wmax))
                end
            end
            a.data_bounds=[ymin xmin; ymax xmax]
            ei.y_shift=y_shift
        end

        a.y_ticks=tlist("ticks",Xtemp,string(Xtemp))
        w=WIDTH*wmax
        ei.bar_width=w
        ei.background=ei.foreground
        ei.polyline_style=7; // bar type
        ei.background=ei.foreground
        ei.foreground = -1; // black by default
        ei.line_mode="off";
    end

    //drawnow
    curFig.immediate_drawing = immediate_drawing;

endfunction