Added Boxplots to the EXAMINE subcommand repertoire
[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 "misc.h"
24
25 #include "factor_stats.h"
26
27
28 /* Draw a box-and-whiskers plot
29 */
30
31 /* Draw an outlier on the plot CH
32  * at CENTRELINE
33  * The outlier is in (*wvp)[idx]
34  * If EXTREME is non zero, then consider it to be an extreme
35  * value
36  */
37 void 
38 draw_outlier(struct chart *ch, double centreline, 
39              struct weighted_value **wvp, 
40              int idx,
41              short extreme);
42
43
44 void 
45 draw_outlier(struct chart *ch, double centreline, 
46              struct weighted_value **wvp, 
47              int idx,
48              short extreme
49              )
50 {
51   char label[10];
52
53 #define MARKER_CIRCLE 4
54 #define MARKER_STAR 3
55
56   pl_fmarker_r(ch->lp,
57                centreline,
58                ch->data_bottom + 
59                (wvp[idx]->v.f - ch->y_min ) * ch->ordinate_scale,
60                extreme?MARKER_STAR:MARKER_CIRCLE,
61                20);
62
63   pl_moverel_r(ch->lp, 10,0);
64
65   snprintf(label, 10, "%d", wvp[idx]->case_nos->num);
66   
67   pl_alabel_r(ch->lp, 'l', 'c', label);
68
69 }
70
71
72 void 
73 boxplot_draw_boxplot(struct chart *ch,
74                      double box_centre, 
75                      double box_width,
76                      struct metrics *m,
77                      /* 
78                      const double hinge[3],
79                      struct weighted_value **wvp,
80                      int n_data,
81                      */
82                      const char *name)
83 {
84   double whisker[2];
85   int i;
86
87
88   const double *hinge = m->hinge;
89   struct weighted_value **wvp = m->wvp;
90   const int n_data = m->n_data;
91
92   const double step = (hinge[2] - hinge[0]) * 1.5;
93
94
95   const double box_left = box_centre - box_width / 2.0;
96
97   const double box_right = box_centre + box_width / 2.0;
98
99
100   const double box_bottom = 
101     ch->data_bottom + ( hinge[0] - ch->y_min ) * ch->ordinate_scale;
102
103
104   const double box_top = 
105     ch->data_bottom + ( hinge[2] - ch->y_min ) * ch->ordinate_scale;
106
107   whisker[1] = hinge[2];
108   whisker[0] = wvp[0]->v.f;
109
110   for ( i = 0 ; i < n_data ; ++i ) 
111     {
112       if ( hinge[2] + step >  wvp[i]->v.f) 
113         whisker[1] = wvp[i]->v.f;
114
115       if ( hinge[0] - step >  wvp[i]->v.f) 
116         whisker[0] = wvp[i]->v.f;
117     
118     }
119     
120   
121   const double bottom_whisker = 
122     ch->data_bottom + ( whisker[0] - ch->y_min ) * ch->ordinate_scale;
123
124   const double top_whisker = 
125     ch->data_bottom + ( whisker[1] - ch->y_min ) * ch->ordinate_scale;
126
127         
128   pl_savestate_r(ch->lp);
129
130
131   /* Draw the box */
132   pl_savestate_r(ch->lp);
133   pl_fillcolorname_r(ch->lp,ch->fill_colour);
134   pl_filltype_r(ch->lp,1);
135   pl_fbox_r(ch->lp, 
136             box_left,
137             box_bottom,
138             box_right,
139             box_top);
140
141   pl_restorestate_r(ch->lp);
142
143
144   
145   /* Draw the median */
146   pl_savestate_r(ch->lp);
147   pl_linewidth_r(ch->lp,5);
148   pl_fline_r(ch->lp, 
149              box_left, 
150              ch->data_bottom + ( hinge[1] - ch->y_min ) * ch->ordinate_scale,
151              box_right,   
152              ch->data_bottom + ( hinge[1] - ch->y_min ) * ch->ordinate_scale);
153   pl_restorestate_r(ch->lp);
154
155
156   /* Draw the bottom whisker */
157   pl_fline_r(ch->lp, 
158              box_left, 
159              bottom_whisker,
160              box_right,   
161              bottom_whisker);
162
163   /* Draw top whisker */
164   pl_fline_r(ch->lp, 
165              box_left, 
166              top_whisker,
167              box_right,   
168              top_whisker);
169
170
171
172   /* Draw centre line.
173      (bottom half) */
174   pl_fline_r(ch->lp, 
175              box_centre, bottom_whisker,
176              box_centre, box_bottom);
177
178   /* (top half) */
179   pl_fline_r(ch->lp, 
180              box_centre, top_whisker,
181              box_centre, box_top);
182
183   /* Draw outliers */
184   for ( i = 0 ; i < n_data ; ++i ) 
185     {
186       if ( wvp[i]->v.f >= hinge[2] + step ) 
187         draw_outlier(ch, box_centre, wvp, i, 
188                      ( wvp[i]->v.f > hinge[2] + 2 * step ) 
189                      );
190
191       if ( wvp[i]->v.f <= hinge[0] - step ) 
192         draw_outlier(ch, box_centre, wvp, i, 
193                      ( wvp[i]->v.f < hinge[0] - 2 * step )
194                      );
195     }
196
197
198   /* Draw  tick  mark on x axis */
199   draw_tick(ch, TICK_ABSCISSA, box_centre - ch->data_left, name);
200
201   pl_restorestate_r(ch->lp);
202
203 }
204
205
206
207 void
208 boxplot_draw_yscale(struct chart *ch , double y_max, double y_min)
209 {
210   double y_tick;
211   double d;
212
213   ch->y_max  = y_max;
214   ch->y_min  = y_min;
215
216   y_tick = chart_rounded_tick(fabs(ch->y_max - ch->y_min) / 5.0);
217
218   ch->y_min = (ceil( ch->y_min  / y_tick ) - 1.0  ) * y_tick;
219       
220   ch->y_max = ( floor( ch->y_max  / y_tick ) + 1.0  ) * y_tick;
221
222   ch->ordinate_scale = fabs(ch->data_top - ch->data_bottom) 
223     / fabs(ch->y_max - ch->y_min) ;
224
225
226   /* Move to data bottom-left */
227   pl_move_r(ch->lp, 
228             ch->data_left, ch->data_bottom);
229
230   for ( d = ch->y_min; d <= ch->y_max ; d += y_tick )
231     {
232       draw_tick (ch, TICK_ORDINATE, (d - ch->y_min ) * ch->ordinate_scale, "%g", d);
233     }
234
235 }