Instead of making system or portable file readers responsible for
[pspp] / 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., 59 Temple Place - Suite 330, Boston, MA
19 02111-1307, 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
31
32 /* Pie charts of course need to know Pi :) */
33 #ifndef M_PI
34 #define M_PI ( 22.0 / 7.0 ) 
35 #endif
36
37
38 #define min(A,B) ((A>B)?B:A)
39
40
41 /* Draw a single slice of the pie */
42 void
43 draw_segment(struct chart *ch, 
44              double centre_x, double centre_y, 
45              double radius,
46              double start_angle, double segment_angle,
47              const char *colour) ;
48
49
50 /* Draw a pie chart */
51 void
52 draw_piechart(struct chart *ch, const struct variable *var,
53               const struct freq_tab *frq_tab)
54 {
55   int i;
56
57   const int n_data = frq_tab->n_valid;
58   const double left_label = ch->data_left + 
59     (ch->data_right - ch->data_left)/10.0;
60
61   const double right_label = ch->data_right - 
62     (ch->data_right - ch->data_left)/10.0;
63
64   const double centre_x = (ch->data_right + ch->data_left ) / 2.0 ;
65   const double centre_y = (ch->data_top + ch->data_bottom ) / 2.0 ;
66
67   const double radius = min( 
68                             5.0 / 12.0 * (ch->data_top - ch->data_bottom),
69                             1.0 / 4.0 * (ch->data_right - ch->data_left)
70                             );
71
72
73   chart_write_title(ch, var->label ? var->label: var->name);
74
75     
76   for (i = 0 ; i < n_data ; ++i ) 
77     {
78       static double angle=0.0;
79       const struct freq frq = frq_tab->valid[i];
80
81       const double segment_angle = 
82         frq.c / frq_tab->valid_cases * 2 * M_PI ;
83
84       char *label = val_labs_find (var->val_labs, frq.v );
85       if ( !label ) 
86         {
87           static char l[20];
88           snprintf(l,20,"%g",frq.v.f);
89           label = l;
90         }
91
92       const double label_x = centre_x - 
93         radius * sin(angle + segment_angle/2.0);
94
95       const double label_y = centre_y + 
96         radius * cos(angle + segment_angle/2.0);
97
98       /* Fill the segment */
99       draw_segment(ch,
100                    centre_x, centre_y, radius, 
101                    angle, segment_angle,
102                    data_colour[i]);
103         
104       /* Now add the labels */
105       if ( label_x < centre_x ) 
106         {
107           pl_line_r(ch->lp, label_x, label_y,
108                     left_label, label_y );
109           pl_moverel_r(ch->lp,0,5);
110           pl_alabel_r(ch->lp,0,0,label);
111         }
112       else
113         {
114           pl_line_r(ch->lp, 
115                     label_x, label_y,
116                     right_label, label_y
117                     );
118           pl_moverel_r(ch->lp,0,5);
119           pl_alabel_r(ch->lp,'r',0,label);
120         }
121
122       angle += segment_angle;
123
124     }
125
126   /* Draw an outline to the pie */
127   pl_filltype_r(ch->lp,0);
128   pl_fcircle_r (ch->lp, centre_x, centre_y, radius);
129
130 }
131
132
133
134 void
135 fill_segment(struct chart *ch, 
136              double x0, double y0, 
137              double radius,
138              double start_angle, double segment_angle) ;
139
140
141 /* Fill a segment with the current fill colour */
142 void
143 fill_segment(struct chart *ch, 
144              double x0, double y0, 
145              double radius,
146              double start_angle, double segment_angle)
147 {
148
149   const double start_x  = x0 - radius * sin(start_angle);
150   const double start_y  = y0 + radius * cos(start_angle);
151
152   const double stop_x   = 
153     x0 - radius * sin(start_angle + segment_angle); 
154
155   const double stop_y   = 
156     y0 + radius * cos(start_angle + segment_angle);
157
158   assert(segment_angle <= 2 * M_PI);
159   assert(segment_angle >= 0);
160
161   if ( segment_angle > M_PI ) 
162     {
163       /* Then we must draw it in two halves */
164       fill_segment(ch, x0, y0, radius, start_angle, segment_angle / 2.0 );
165       fill_segment(ch, x0, y0, radius, start_angle + segment_angle / 2.0,
166                    segment_angle / 2.0 );
167     }
168   else
169     {
170       pl_move_r(ch->lp, x0, y0);
171
172       pl_cont_r(ch->lp, stop_x, stop_y);
173       pl_cont_r(ch->lp, start_x, start_y);
174
175       pl_arc_r(ch->lp,
176                x0, y0,
177                stop_x, stop_y,
178                start_x, start_y
179                );
180
181       pl_endpath_r(ch->lp);
182     }
183 }
184
185
186
187 /* Draw a single slice of the pie */
188 void
189 draw_segment(struct chart *ch, 
190              double x0, double y0, 
191              double radius,
192              double start_angle, double segment_angle, 
193              const char *colour)
194 {
195   const double start_x  = x0 - radius * sin(start_angle);
196   const double start_y  = y0 + radius * cos(start_angle);
197
198   pl_savestate_r(ch->lp);
199
200   pl_savestate_r(ch->lp);
201   pl_colorname_r(ch->lp, colour);
202   
203   pl_pentype_r(ch->lp,1);
204   pl_filltype_r(ch->lp,1);
205
206   fill_segment(ch, x0, y0, radius, start_angle, segment_angle);
207   pl_restorestate_r(ch->lp);
208
209   /* Draw line dividing segments */
210   pl_pentype_r(ch->lp, 1);
211   pl_fline_r(ch->lp, x0, y0, start_x, start_y);
212         
213
214   pl_restorestate_r(ch->lp);
215 }