1 // Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
2 // Copyright (C) 2004-2006 - INRIA - Farid Belahcene
3 // Copyright (C) 2012 - 2016 - Scilab Enterprises
5 // This file is hereby licensed under the terms of the GNU GPL v2.0,
6 // pursuant to article 5.3.4 of the CeCILL v.2.1.
7 // This file was originally licensed under the terms of the CeCILL v2.1,
8 // and continues to be available under such terms.
9 // For more information, see the COPYING file which you should have received
10 // along with this program.
12 function pie(varargin)
14 // This function draws a pie, if size of x is N then pie function draws a pie with N parts, the area of the ith part is equal to (x(i)/sum(x))*( surface of the unit cercle).
17 // syntax : pie(x[,sp[,txt]])
18 // Input : The input arguments must have the same size
19 // x : a scalar or a vector of positive reals.
20 // sp : a scalar or a vector of reals. The sp vector allows to cut one or several parts of the pie
21 // txt : a cell or a vector of strings. The txt vector allows to write a text for each part of the pie
24 // Input arguments must have the same length
27 if size(varlist(i-1),"*") <> size(varlist(i),"*") then
28 error(msprintf(gettext("%s: Wrong size for input arguments: Matrices of same size expected.\n"), "pie"));
35 // Detect and set the RHS arguments
37 if size(varlist) == 1 then
38 if type(varlist(1))==1 & and(varlist(1)>0) then
41 error(msprintf(gettext("%s: Wrong type for input argument #%d: A real scalar or vector expected.\n"), "pie", 1));
43 // case : pie(x,sp) or pie(x,txt)
44 elseif size(varlist) == 2 then
45 if type(varlist(1)) == 1 & and(varlist(1)>0) then
48 error(msprintf(gettext("%s: Wrong type for input argument #%d: A real scalar or vector expected.\n"), "pie", 1));
50 if type(varlist(2)) == 1 | type(varlist(2)) == 4
52 elseif type(varlist(2)) == 10 | iscellstr(varlist(2)) then
55 // case : pie(x,sp,txt)
56 elseif size(varlist) == 3 then
57 if type(varlist(1)) == 1 & and(varlist(1)>0) then
60 error(msprintf(gettext("%s: Wrong type for input argument #%d: A real scalar or vector expected.\n"), "pie", 1));
62 if type(varlist(2)) == 1 | type(varlist(2)) == 4
65 error(msprintf(gettext("%s: Wrong type for input argument #%d: A real scalar or vector expected.\n"), "pie", 2));
67 if type(varlist(3)) == 10
69 elseif iscellstr(varlist(3)) then
70 for j=1:size(varlist(3),"*")
75 error(msprintf(gettext("%s: Wrong size for input argument #%d: Vector of strings expected.\n"),"pie", 3));
78 error(msprintf(gettext("%s: Wrong number of input argument(s): %d to %d expected.\n"), "pie", 1, 3));
82 error(msprintf(gettext("%s: Wrong type for input argument #%d: A real scalar or vector expected.\n"), "pie", 1));
84 // xi and yi represents the coordinates of each polyline
85 // iesp is the index of the part of i which are separated of the pie
86 // teta_1 is the start angle of the arc polyline, teta_2 is the end angle of the arc polyline
87 iesp = find(esp>0 | esp<0);
93 initDrawingMode = gcf().immediate_drawing;
94 gcf().immediate_drawing = "off";
97 // Create a close polyline for every parts of pie, the polyline inside color is determinated by the plot colormap
102 teta_i = (x(i)/sum(x))*2*%pi;
103 teta_2 = teta_1 + teta_i
104 if size(x,"*") <> 1 then
113 inter = 1/(100*x(i)/sum(x));
116 xi($+1) = cos((1-k)*teta_1 +k*teta_2);
117 yi($+1) = sin((1-k)*teta_1 +k*teta_2);
120 xi($+1) = cos(teta_2);
121 yi($+1) = sin(teta_2);
126 if or(i == iesp) then
127 ei.x_shift = ones(1,size(xi,"*")) * (1/10) * cos((teta_2+teta_1)/2);
128 ei.y_shift = ones(1,size(yi,"*")) * (1/10) * sin((teta_2+teta_1)/2);
130 xstring(cos((teta_2+teta_1)/2)*1.2+ei.x_shift(1)-0.1*(cos((teta_2+teta_1)/2)<0),sin((teta_2+teta_1)/2)*1.2+ei.y_shift(1),txt(i));
132 xstring(cos((teta_2+teta_1)/2)*1.2+ei.x_shift(1)-0.1*(cos((teta_2+teta_1)/2)<0),sin((teta_2+teta_1)/2)*1.2+ei.y_shift(1),string(round((x(i)/sum(x))*100)) + "%");
136 xstring(cos((teta_2+teta_1)/2)*1.2-0.1*(cos((teta_2+teta_1)/2)<0),sin((teta_2+teta_1)/2)*1.2,txt(i));
138 xstring(cos((teta_2+teta_1)/2)*1.2-0.1*(cos((teta_2+teta_1)/2)<0),sin((teta_2+teta_1)/2)*1.2,string(round((x(i)/sum(x))*100)) + "%");
142 [Color,CurColor] = setDefaultColor(CurColor);
143 ei.background = Color;
145 // Update data_bounds
146 a.data_bounds = [min(-1.3,a.data_bounds(1,1)) min(-1.3,a.data_bounds(1,2));max(1.3,a.data_bounds(2,1)) max(1.3,a.data_bounds(2,2))];
150 a.axes_visible = "off";
152 gcf().immediate_drawing = initDrawingMode;