1 /* PSPP - computes sample statistics.
2 Copyright (C) 2004 Free Software Foundation, Inc.
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.
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.
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
22 #include <libpspp/misc.h>
24 #include <output/charts/box-whisker.h>
25 #include <output/charts/plot-chart.h>
27 #include <output/chart.h>
28 #include <math/chart-geometry.h>
29 #include <math/factor-stats.h>
34 /* Draw a box-and-whiskers plot
37 /* Draw an outlier on the plot CH
39 * The outlier is in (*wvp)[idx]
40 * If EXTREME is non zero, then consider it to be an extreme
44 draw_outlier(struct chart *ch, double centreline,
45 struct weighted_value **wvp,
51 draw_outlier(struct chart *ch, double centreline,
52 struct weighted_value **wvp,
59 #define MARKER_CIRCLE 4
65 (wvp[idx]->v.f - ch->y_min ) * ch->ordinate_scale,
66 extreme?MARKER_STAR:MARKER_CIRCLE,
69 pl_moverel_r(ch->lp, 10,0);
71 snprintf(label, 10, "%d", wvp[idx]->case_nos->num);
73 pl_alabel_r(ch->lp, 'l', 'c', label);
79 boxplot_draw_boxplot(struct chart *ch,
88 const double *hinge = m->hinge;
89 struct weighted_value **wvp = m->wvp;
90 const int n_data = m->n_data;
92 const double step = (hinge[2] - hinge[0]) * 1.5;
95 const double box_left = box_centre - box_width / 2.0;
97 const double box_right = box_centre + box_width / 2.0;
100 const double box_bottom =
101 ch->data_bottom + ( hinge[0] - ch->y_min ) * ch->ordinate_scale;
104 const double box_top =
105 ch->data_bottom + ( hinge[2] - ch->y_min ) * ch->ordinate_scale;
109 /* Can't really draw a boxplot if there's no data */
113 whisker[1] = hinge[2];
114 whisker[0] = wvp[0]->v.f;
116 for ( i = 0 ; i < n_data ; ++i )
118 if ( hinge[2] + step > wvp[i]->v.f)
119 whisker[1] = wvp[i]->v.f;
121 if ( hinge[0] - step > wvp[i]->v.f)
122 whisker[0] = wvp[i]->v.f;
127 const double bottom_whisker =
128 ch->data_bottom + ( whisker[0] - ch->y_min ) * ch->ordinate_scale;
130 const double top_whisker =
131 ch->data_bottom + ( whisker[1] - ch->y_min ) * ch->ordinate_scale;
134 pl_savestate_r(ch->lp);
138 pl_savestate_r(ch->lp);
139 pl_fillcolorname_r(ch->lp,ch->fill_colour);
140 pl_filltype_r(ch->lp,1);
147 pl_restorestate_r(ch->lp);
151 /* Draw the median */
152 pl_savestate_r(ch->lp);
153 pl_linewidth_r(ch->lp,5);
156 ch->data_bottom + ( hinge[1] - ch->y_min ) * ch->ordinate_scale,
158 ch->data_bottom + ( hinge[1] - ch->y_min ) * ch->ordinate_scale);
159 pl_restorestate_r(ch->lp);
162 /* Draw the bottom whisker */
169 /* Draw top whisker */
181 box_centre, bottom_whisker,
182 box_centre, box_bottom);
186 box_centre, top_whisker,
187 box_centre, box_top);
191 for ( i = 0 ; i < n_data ; ++i )
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 )
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 )
205 /* Draw tick mark on x axis */
206 draw_tick(ch, TICK_ABSCISSA, box_centre - ch->data_left, name);
208 pl_restorestate_r(ch->lp);
215 boxplot_draw_yscale(struct chart *ch , double y_max, double y_min)
226 y_tick = chart_rounded_tick(fabs(ch->y_max - ch->y_min) / 5.0);
228 ch->y_min = (ceil( ch->y_min / y_tick ) - 1.0 ) * y_tick;
230 ch->y_max = ( floor( ch->y_max / y_tick ) + 1.0 ) * y_tick;
232 ch->ordinate_scale = fabs(ch->data_top - ch->data_bottom)
233 / fabs(ch->y_max - ch->y_min) ;
236 /* Move to data bottom-left */
238 ch->data_left, ch->data_bottom);
240 for ( d = ch->y_min; d <= ch->y_max ; d += y_tick )
242 draw_tick (ch, TICK_ORDINATE, (d - ch->y_min ) * ch->ordinate_scale, "%g", d);