57930c86e9e3d739baa9273ee994d63fe77b579f
[pspp-builds.git] / src / box-whisker.c
1 /* PSPP - computes sample statistics.
2    Copyright (C) 2004 Free Software Foundation, Inc.
3    Written by John Darrington <john@darrington.wattle.id.au>
4
5    This program is free software; you can redistribute it and/or
6    modify it under the terms of the GNU General Public License as
7    published by the Free Software Foundation; either version 2 of the
8    License, or (at your option) any later version.
9
10    This program 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 this program; if not, write to the Free Software
17    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
18    02111-1307, USA. */
19
20
21 #include "chart.h"
22 #include <math.h>
23 #include <assert.h>
24 #include "misc.h"
25
26 #include "factor_stats.h"
27
28
29 /* Draw a box-and-whiskers plot
30 */
31
32 /* Draw an outlier on the plot CH
33  * at CENTRELINE
34  * The outlier is in (*wvp)[idx]
35  * If EXTREME is non zero, then consider it to be an extreme
36  * value
37  */
38 void 
39 draw_outlier(struct chart *ch, double centreline, 
40              struct weighted_value **wvp, 
41              int idx,
42              short extreme);
43
44
45 void 
46 draw_outlier(struct chart *ch, double centreline, 
47              struct weighted_value **wvp, 
48              int idx,
49              short extreme
50              )
51 {
52   char label[10];
53
54 #define MARKER_CIRCLE 4
55 #define MARKER_STAR 3
56
57   pl_fmarker_r(ch->lp,
58                centreline,
59                ch->data_bottom + 
60                (wvp[idx]->v.f - ch->y_min ) * ch->ordinate_scale,
61                extreme?MARKER_STAR:MARKER_CIRCLE,
62                20);
63
64   pl_moverel_r(ch->lp, 10,0);
65
66   snprintf(label, 10, "%d", wvp[idx]->case_nos->num);
67   
68   pl_alabel_r(ch->lp, 'l', 'c', label);
69
70 }
71
72
73 void 
74 boxplot_draw_boxplot(struct chart *ch,
75                      double box_centre, 
76                      double box_width,
77                      struct metrics *m,
78                      const char *name)
79 {
80   double whisker[2];
81   int i;
82
83   assert(m);
84
85
86   const double *hinge = m->hinge;
87   struct weighted_value **wvp = m->wvp;
88   const int n_data = m->n_data;
89
90   const double step = (hinge[2] - hinge[0]) * 1.5;
91
92
93   const double box_left = box_centre - box_width / 2.0;
94
95   const double box_right = box_centre + box_width / 2.0;
96
97
98   const double box_bottom = 
99     ch->data_bottom + ( hinge[0] - ch->y_min ) * ch->ordinate_scale;
100
101
102   const double box_top = 
103     ch->data_bottom + ( hinge[2] - ch->y_min ) * ch->ordinate_scale;
104
105   /* Can't really draw a boxplot if there's no data */
106   if ( n_data == 0 ) 
107           return ;
108
109   whisker[1] = hinge[2];
110   whisker[0] = wvp[0]->v.f;
111
112   for ( i = 0 ; i < n_data ; ++i ) 
113     {
114       if ( hinge[2] + step >  wvp[i]->v.f) 
115         whisker[1] = wvp[i]->v.f;
116
117       if ( hinge[0] - step >  wvp[i]->v.f) 
118         whisker[0] = wvp[i]->v.f;
119     
120     }
121     
122   
123   const double bottom_whisker = 
124     ch->data_bottom + ( whisker[0] - ch->y_min ) * ch->ordinate_scale;
125
126   const double top_whisker = 
127     ch->data_bottom + ( whisker[1] - ch->y_min ) * ch->ordinate_scale;
128
129         
130   pl_savestate_r(ch->lp);
131
132
133   /* Draw the box */
134   pl_savestate_r(ch->lp);
135   pl_fillcolorname_r(ch->lp,ch->fill_colour);
136   pl_filltype_r(ch->lp,1);
137   pl_fbox_r(ch->lp, 
138             box_left,
139             box_bottom,
140             box_right,
141             box_top);
142
143   pl_restorestate_r(ch->lp);
144
145
146   
147   /* Draw the median */
148   pl_savestate_r(ch->lp);
149   pl_linewidth_r(ch->lp,5);
150   pl_fline_r(ch->lp, 
151              box_left, 
152              ch->data_bottom + ( hinge[1] - ch->y_min ) * ch->ordinate_scale,
153              box_right,   
154              ch->data_bottom + ( hinge[1] - ch->y_min ) * ch->ordinate_scale);
155   pl_restorestate_r(ch->lp);
156
157
158   /* Draw the bottom whisker */
159   pl_fline_r(ch->lp, 
160              box_left, 
161              bottom_whisker,
162              box_right,   
163              bottom_whisker);
164
165   /* Draw top whisker */
166   pl_fline_r(ch->lp, 
167              box_left, 
168              top_whisker,
169              box_right,   
170              top_whisker);
171
172
173
174   /* Draw centre line.
175      (bottom half) */
176   pl_fline_r(ch->lp, 
177              box_centre, bottom_whisker,
178              box_centre, box_bottom);
179
180   /* (top half) */
181   pl_fline_r(ch->lp, 
182              box_centre, top_whisker,
183              box_centre, box_top);
184
185   /* Draw outliers */
186   for ( i = 0 ; i < n_data ; ++i ) 
187     {
188       if ( wvp[i]->v.f >= hinge[2] + step ) 
189         draw_outlier(ch, box_centre, wvp, i, 
190                      ( wvp[i]->v.f > hinge[2] + 2 * step ) 
191                      );
192
193       if ( wvp[i]->v.f <= hinge[0] - step ) 
194         draw_outlier(ch, box_centre, wvp, i, 
195                      ( wvp[i]->v.f < hinge[0] - 2 * step )
196                      );
197     }
198
199
200   /* Draw  tick  mark on x axis */
201   draw_tick(ch, TICK_ABSCISSA, box_centre - ch->data_left, name);
202
203   pl_restorestate_r(ch->lp);
204
205 }
206
207
208
209 void
210 boxplot_draw_yscale(struct chart *ch , double y_max, double y_min)
211 {
212   double y_tick;
213   double d;
214
215   if ( !ch ) 
216      return ;
217
218   ch->y_max  = y_max;
219   ch->y_min  = y_min;
220
221   y_tick = chart_rounded_tick(fabs(ch->y_max - ch->y_min) / 5.0);
222
223   ch->y_min = (ceil( ch->y_min  / y_tick ) - 1.0  ) * y_tick;
224       
225   ch->y_max = ( floor( ch->y_max  / y_tick ) + 1.0  ) * y_tick;
226
227   ch->ordinate_scale = fabs(ch->data_top - ch->data_bottom) 
228     / fabs(ch->y_max - ch->y_min) ;
229
230
231   /* Move to data bottom-left */
232   pl_move_r(ch->lp, 
233             ch->data_left, ch->data_bottom);
234
235   for ( d = ch->y_min; d <= ch->y_max ; d += y_tick )
236     {
237       draw_tick (ch, TICK_ORDINATE, (d - ch->y_min ) * ch->ordinate_scale, "%g", d);
238     }
239
240 }