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