forked from Mirkes/ElMap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrawPieChart.m
62 lines (59 loc) · 1.66 KB
/
drawPieChart.m
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
function drawPieChart(x, y, radius, Proportions, Colors)
%drawPieChart draw one pie chart with specified properties:
% x and y are coordinates of centre of circle
% radius is radius of circle
% Proportions ia vector of couns of casses for each color
% Colors is vector of colors
% Transform counts to proportions
prop = Proportions / sum(Proportions);
% Starts from zero angle
angle = 0;
% Draw chart
if size(prop, 2) == 1
% One class chart
for i = 1:size(Proportions, 2)
color = Colors(i,:);
plot_circle(x, y, radius, color);
end
else
% Several classes case
for i=1:size(Proportions,2)
th1 = angle;
th2 = angle+prop(i)*2*pi;
color = Colors(i,:);
plot_arc(th1,th2,x,y,radius,color);
angle = th2;
end
end
end
function plot_circle(x,y,r,color)
% Plot a circular arc as a pie wedge.
% a is start of arc in radians,
% b is end of arc in radians,
% (h,k) is the center of the circle.
% r is the radius.
% Try this: plot_arc(pi/4,3*pi/4,9,-4,3)
% Author: Matt Fig
t = 0:0.05:2*pi;
xp = r*cos(t) + x;
yp = r*sin(t) + y;
%x = [x h x(1)];
%y = [y k y(1)];
fill(xp,yp,color);
end
function plot_arc(a,b,h,k,r,color)
% Plot a circular arc as a pie wedge.
% a is start of arc in radians,
% b is end of arc in radians,
% (h,k) is the center of the circle.
% r is the radius.
% Try this: plot_arc(pi/4,3*pi/4,9,-4,3)
% Author: Matt Fig
t = linspace(a,b);
x = r*cos(t) + h;
y = r*sin(t) + k;
x = [x h x(1)];
y = [y k y(1)];
fill(x, y, color);
% axis([h-r-1 h+r+1 k-r-1 k+r+1])
end