Changed all the licence notices in all the files.
[pspp-builds.git] / src / piechart.c
1 /* PSPP - draws pie charts of sample statistics
2
3 Copyright (C) 2004 Free Software Foundation, Inc.
4 Written by John Darrington <john@darrington.wattle.id.au>
5
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 License, or (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301, USA. */
20
21
22 #include "config.h"
23 #include "chart.h"
24 #include <float.h>
25 #include <assert.h>
26 #include <math.h>
27 #include <stdio.h>
28 #include "str.h"
29 #include "value-labels.h"
30 #include "misc.h"
31
32
33 /* Pie charts of course need to know Pi :) */
34 #ifndef M_PI
35 #define M_PI ( 22.0 / 7.0 ) 
36 #endif
37
38
39
40 /* Draw a single slice of the pie */
41 static void
42 draw_segment(struct chart *ch, 
43              double centre_x, double centre_y, 
44              double radius,
45              double start_angle, double segment_angle,
46              const char *colour) ;
47
48
49
50 /* Draw a piechart */
51 void
52 piechart_plot(const char *title, const struct slice *slices, int n_slices)
53 {
54   int i;
55   double total_magnetude=0;
56
57   struct chart *ch;
58
59   ch = chart_create();
60
61   const double left_label = ch->data_left + 
62     (ch->data_right - ch->data_left)/10.0;
63
64   const double right_label = ch->data_right - 
65     (ch->data_right - ch->data_left)/10.0;
66
67   const double centre_x = (ch->data_right + ch->data_left ) / 2.0 ;
68   const double centre_y = (ch->data_top + ch->data_bottom ) / 2.0 ;
69
70   const double radius = min( 
71                             5.0 / 12.0 * (ch->data_top - ch->data_bottom),
72                             1.0 / 4.0 * (ch->data_right - ch->data_left)
73                             );
74
75
76   chart_write_title(ch, title);
77
78   for (i = 0 ; i < n_slices ; ++i ) 
79     total_magnetude += slices[i].magnetude;
80
81   for (i = 0 ; i < n_slices ; ++i ) 
82     {
83       static double angle=0.0;
84
85       const double segment_angle = 
86         slices[i].magnetude / total_magnetude * 2 * M_PI ;
87
88       const double label_x = centre_x - 
89         radius * sin(angle + segment_angle/2.0);
90
91       const double label_y = centre_y + 
92         radius * cos(angle + segment_angle/2.0);
93
94       /* Fill the segment */
95       draw_segment(ch,
96                    centre_x, centre_y, radius, 
97                    angle, segment_angle,
98                    data_colour[i]);
99         
100       /* Now add the labels */
101       if ( label_x < centre_x ) 
102         {
103           pl_line_r(ch->lp, label_x, label_y,
104                     left_label, label_y );
105           pl_moverel_r(ch->lp,0,5);
106           pl_alabel_r(ch->lp,0,0,slices[i].label);
107         }
108       else
109         {
110           pl_line_r(ch->lp, 
111                     label_x, label_y,
112                     right_label, label_y
113                     );
114           pl_moverel_r(ch->lp,0,5);
115           pl_alabel_r(ch->lp,'r',0,slices[i].label);
116         }
117
118       angle += segment_angle;
119
120     }
121
122   /* Draw an outline to the pie */
123   pl_filltype_r(ch->lp,0);
124   pl_fcircle_r (ch->lp, centre_x, centre_y, radius);
125
126   chart_submit(ch);
127 }
128
129 static void
130 fill_segment(struct chart *ch, 
131              double x0, double y0, 
132              double radius,
133              double start_angle, double segment_angle) ;
134
135
136 /* Fill a segment with the current fill colour */
137 static void
138 fill_segment(struct chart *ch, 
139              double x0, double y0, 
140              double radius,
141              double start_angle, double segment_angle)
142 {
143
144   const double start_x  = x0 - radius * sin(start_angle);
145   const double start_y  = y0 + radius * cos(start_angle);
146
147   const double stop_x   = 
148     x0 - radius * sin(start_angle + segment_angle); 
149
150   const double stop_y   = 
151     y0 + radius * cos(start_angle + segment_angle);
152
153   assert(segment_angle <= 2 * M_PI);
154   assert(segment_angle >= 0);
155
156   if ( segment_angle > M_PI ) 
157     {
158       /* Then we must draw it in two halves */
159       fill_segment(ch, x0, y0, radius, start_angle, segment_angle / 2.0 );
160       fill_segment(ch, x0, y0, radius, start_angle + segment_angle / 2.0,
161                    segment_angle / 2.0 );
162     }
163   else
164     {
165       pl_move_r(ch->lp, x0, y0);
166
167       pl_cont_r(ch->lp, stop_x, stop_y);
168       pl_cont_r(ch->lp, start_x, start_y);
169
170       pl_arc_r(ch->lp,
171                x0, y0,
172                stop_x, stop_y,
173                start_x, start_y
174                );
175
176       pl_endpath_r(ch->lp);
177     }
178 }
179
180
181
182 /* Draw a single slice of the pie */
183 static void
184 draw_segment(struct chart *ch, 
185              double x0, double y0, 
186              double radius,
187              double start_angle, double segment_angle, 
188              const char *colour)
189 {
190   const double start_x  = x0 - radius * sin(start_angle);
191   const double start_y  = y0 + radius * cos(start_angle);
192
193   pl_savestate_r(ch->lp);
194
195   pl_savestate_r(ch->lp);
196   pl_colorname_r(ch->lp, colour);
197   
198   pl_pentype_r(ch->lp,1);
199   pl_filltype_r(ch->lp,1);
200
201   fill_segment(ch, x0, y0, radius, start_angle, segment_angle);
202   pl_restorestate_r(ch->lp);
203
204   /* Draw line dividing segments */
205   pl_pentype_r(ch->lp, 1);
206   pl_fline_r(ch->lp, x0, y0, start_x, start_y);
207         
208
209   pl_restorestate_r(ch->lp);
210 }
211