changelog shortlog tags changeset files revisions annotate raw

scripts/plot/pie.m

changeset 10289: 4b124317dc38
parent:ff89a265592b
author: John W. Eaton <jwe@octave.org>
date: Tue Feb 09 20:58:55 2010 -0500 (71 minutes ago)
permissions: -rw-r--r--
description: base_properties::set_children: account for hidden children
1## Copyright (C) 2007, 2008, 2009 David Bateman
2##
3## This file is part of Octave.
4##
5## Octave is free software; you can redistribute it and/or modify it
6## under the terms of the GNU General Public License as published by
7## the Free Software Foundation; either version 3 of the License, or (at
8## your option) any later version.
9##
10## Octave is distributed in the hope that it will be useful, but
11## WITHOUT ANY WARRANTY; without even the implied warranty of
12## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13## General Public License for more details.
14##
15## You should have received a copy of the GNU General Public License
16## along with Octave; see the file COPYING. If not, see
17## <http://www.gnu.org/licenses/>.
18
19## -*- texinfo -*-
20## @deftypefn {Function File} {} pie (@var{y})
21## @deftypefnx {Function File} {} pie (@var{y}, @var{explode})
22## @deftypefnx {Function File} {} pie (@dots{}, @var{labels})
23## @deftypefnx {Function File} {} pie (@var{h}, @dots{});
24## @deftypefnx {Function File} {@var{h} =} pie (@dots{});
25## Produce a pie chart.
26##
27## Called with a single vector argument, produces a pie chart of the
28## elements in @var{x}, with the size of the slice determined by percentage
29## size of the values of @var{x}.
30##
31## The variable @var{explode} is a vector of the same length as @var{x} that
32## if non zero 'explodes' the slice from the pie chart.
33##
34## If given @var{labels} is a cell array of strings of the same length as
35## @var{x}, giving the labels of each of the slices of the pie chart.
36##
37## The optional return value @var{h} provides a handle to the patch object.
38##
39## @seealso{bar, stem}
40## @end deftypefn
41
42## Very roughly based on pie.m from octave-forge whose author was
43## Daniel Heiserer <Daniel.heiserer@physik.tu-muenchen.de>
44
45function retval = pie (varargin)
46
47 [h, varargin] = __plt_get_axis_arg__ ("pie", varargin{:});
48
49 if (nargin < 1)
50 print_usage ();
51 else
52 oldh = gca ();
53 unwind_protect
54 axes (h);
55 newplot ();
56 tmp = __pie__ (h, varargin{:});
57 unwind_protect_cleanup
58 axes (oldh);
59 end_unwind_protect
60 endif
61
62 if (nargout > 0)
63 retval = tmp;
64 endif
65
66endfunction
67
68function hlist = __pie__ (varargin)
69
70 h = varargin{1};
71 x = abs (varargin{2});
72 iarg = 3;
73
74 if (! isvector (x))
75 error ("pie: expecting vector argument");
76 endif
77
78 len = length (x);
79
80 have_explode = false;
81 have_labels = false;
82
83 while (iarg <= nargin)
84 arg = varargin{iarg++};
85 if (iscell (arg))
86 labels = arg;
87 have_labels = true;
88 if (numel (x) != numel (labels))
89 error ("pie: mismatch in number of labels and data");
90 endif
91 elseif (isnumeric (arg))
92 explode = arg;
93 have_explode = true;
94 if (! size_equal (x, explode))
95 error ("pie: mismatch in number of elements in explode and data");
96 endif
97 endif
98 endwhile
99
100 if (! have_explode)
101 explode = zeros (size (x));
102 endif
103
104 if (! have_labels)
105 xp = round (100 * x ./ sum (x));
106 for i = 1:len
107 labels{i} = sprintf ("%d%%", xp(i));
108 endfor
109 endif
110
111 hlist = [];
112 refinement = 90;
113 phi = 0:refinement:360;
114 xphi = cumsum (x / sum (x) * 360);
115 for i = 1:len
116 if (i == 1)
117 xn = 0 : 360 / refinement : xphi(i);
118 else
119 xn = xphi(i-1) : 360 / refinement : xphi(i);
120 endif
121
122 if (xn(end) != xphi(i))
123 xn = [xn, xphi(i)];
124 endif
125
126 xn2 = (xn(1) + xn(end)) / 2;
127 if (explode (i))
128 xoff = - 0.1 * sind (xn2);
129 yoff = 0.1 * cosd (xn2);
130 else
131 xoff = 0;
132 yoff = 0;
133 endif
134 xt = - 1.2 * sind (xn2);
135 yt = 1.2 * cosd (xn2);
136 if (xt > 0)
137 align = "left";
138 else
139 align = "right";
140 endif
141
142 hlist = [hlist; patch(xoff + [0, - sind(xn)], yoff + [0, cosd(xn)], i);
143 text(xt, yt, labels{i}, "horizontalalignment", align)];
144 endfor
145
146 if (len == 1)
147 set (h, "clim", [1, 2]);
148 else
149 set (h, "clim", [1, len]);
150 endif
151
152 axis ([-1.5, 1.5, -1.5, 1.5], "square");
153
154endfunction
155
156%!demo
157%! pie ([3, 2, 1], [0, 0, 1]);
158%! colormap([1,0,0;0,1,0;0,0,1;1,1,0;1,0,1;0,1,1]);
159
160%!demo
161%! pie ([3, 2, 1], [0, 0, 1], {"Cheddar", "Swiss", "Camembert"});
162%! colormap([1,0,0;0,1,0;0,0,1;1,1,0;1,0,1;0,1,1]);
163%! axis ([-2,2,-2,2]);