summaryrefslogtreecommitdiff
path: root/modules/graphics/macros/legend.sci
blob: b58ee6081c77df2d85095646aeddb6ffcb944ecf (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
// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
// Copyright (C) 2007-2010 - INRIA - Serge Steer <serge.steer@inria.fr>
// 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 c=legend(varargin)

    if size(varargin)<1 then
        error(msprintf(gettext("%s: Wrong number of input argument(s): At least %d expected.\n"), "legend", 1));
    end

    options_names=["in_upper_right";"in_upper_left";"in_lower_left";
    "in_lower_right";"by_coordinates";"out_upper_right";
    "out_upper_left";"out_lower_left";"out_lower_right";
    "upper_caption";"lower_caption"];

    options_codes=[1;2;3;
    4;5;-1;
    -2;-3;-4;
    -5;-6];
    f        = gcf();
    vis      = f.immediate_drawing;
    vis_on   = vis == "on"; // current figure draw status (to decide if drawnow or standby)
    narg     = size(varargin);
    k0       = 1;
    H        = [];
    leg      = [];
    Acur     = gca();
    A        = Acur;
    kopt      = 1;
    with_box = %T;
    coords=[];

    if type(varargin(narg))==4 then
        with_box=varargin(narg)
        narg=narg-1
    end
    if type(varargin(narg))==1 then
        pos=varargin(narg)
        if size(pos,"*")==2 then //position given by coordinates
            coords=pos;
            narg=narg-1
        elseif size(pos,"*")==1 then //position given by code
            if or(pos==options_codes) then
                kopt=find(pos==options_codes)
                narg=narg-1
            else
                error(msprintf(_("%s: Wrong value for input argument #%d: Must be in the set {%s}.\n"),..
                "legend",narg,strcat(string(options_codes),",")));
            end
        else
            error(msprintf(_("%s: Wrong size for input argument #%d."),"legend",narg));
        end
    elseif  type(varargin(narg))==10 then
        k=find(varargin(narg)==options_names)
        if ~isempty(k) then
            kopt=k
            narg=narg-1
        end
    end

    while type(varargin(k0))==9 then //a handle that could be an Axes, Agreg. or Polyline handle.
        tmpH=varargin(k0)
        if tmpH.type=="Axes" then
            A = tmpH;
        end
        tmpH=tmpH($:-1:1);
        H=[getvalidchildren(tmpH);H];
        k0 = k0+1;
    end

    if H==[] then
        //walk subtree to get all proper children
        H=getvalidchildren(A)
    end

    if H==[] then
        error(msprintf(_("%s: No ''%s'' handle found.\n"), "legend","Polyline"));
    end

    //get all labels
    for k=k0:narg
        if type(varargin(k))<>10 then
            break;
        end
        vk=varargin(k)
        leg=[leg; vk(:)]
    end
    nleg=size(leg,"*")


    //upper left coordinates
    if ~isempty(coords) then
        bnds=get(gca(),"axes_bounds");
        as=get(gcf(),"axes_size");

        [x1,y1,rect]=xchange(coords(1),coords(2),"f2i")
        x1=x1-as(1)*bnds(1)
        y1=y1-as(2)*bnds(2)
        pos=[x1/(as(1)*bnds(3)), y1/(as(2)*bnds(4))]
        kopt=5
    elseif kopt==5 then
        pos=[]
    end
    // the number of labels might be lower than the number of polylines
    nbLeg = min(size(H, "*"), size(leg, "*"));
    first_handle=size(H, "*")-nbLeg+1;
    H = H(first_handle:size(H, "*"));
    leg = leg(1:nbLeg);


    drawlater()
    c=captions(H,leg)
    if options_codes(kopt)<0 then
        c.background=f.background
    else
        c.background=Acur.background
    end
    if with_box then c.line_mode="on",else c.line_mode="off",end
    c.legend_location=options_names(kopt)
    if kopt==5 then
        if pos<>[] then
            c.position=pos;
        else
            if vis_on then drawnow(); end    // draw if figure status allows it (otherwise standby)
            bnds=get(gca(),"axes_bounds");
            as=get(gcf(),"axes_size");
            while %t
                rep=xgetmouse()
                if rep(3)<> -1 then break,end
                [x1,y1,rect]=xchange(rep(1),rep(2),"f2i")
                x1=x1-as(1)*bnds(1)
                y1=y1-as(2)*bnds(2)
                pos=[x1/(as(1)*bnds(3)), y1/(as(2)*bnds(4))]
                c.position=pos;
            end
        end
    end
    if vis_on then drawnow(); end       // draw if figure status allows it (otherwise standbye)
endfunction

function h=getvalidchildren(A)
    h=[]
    for k=1:size(A,"*")
        a=A(k)
        select a.type
        case "Polyline" then
            h=[h;a]
        case "Axes"
            ax=a.children
            h=[h;getvalidchildren(ax)]
        case "Compound"
            for k=1:1:size(a.children,"*")
                h=[h;getvalidchildren(a.children(k))]
            end
        end
    end
endfunction