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