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
24 #include <libpspp/misc.h>
26 #include <output/charts/box-whisker.h>
27 #include <output/charts/plot-chart.h>
29 #include <output/chart.h>
30 #include <math/chart-geometry.h>
31 #include <math/factor-stats.h>
36 /* Draw a box-and-whiskers plot
39 /* Draw an outlier on the plot CH
41 * The outlier is in (*wvp)[idx]
42 * If EXTREME is non zero, then consider it to be an extreme
46 draw_outlier(struct chart *ch, double centreline,
47 struct weighted_value **wvp,
53 draw_outlier(struct chart *ch, double centreline,
54 struct weighted_value **wvp,
61 #define MARKER_CIRCLE 4
67 (wvp[idx]->v.f - ch->y_min ) * ch->ordinate_scale,
68 extreme?MARKER_STAR:MARKER_CIRCLE,
71 pl_moverel_r(ch->lp, 10,0);
73 snprintf(label, 10, "%d", wvp[idx]->case_nos->num);
75 pl_alabel_r(ch->lp, 'l', 'c', label);
81 boxplot_draw_boxplot(struct chart *ch,
90 const double *hinge = m->hinge;
91 struct weighted_value **wvp = m->wvp;
92 const int n_data = m->n_data;
94 const double step = (hinge[2] - hinge[0]) * 1.5;
97 const double box_left = box_centre - box_width / 2.0;
99 const double box_right = box_centre + box_width / 2.0;
102 const double box_bottom =
103 ch->data_bottom + ( hinge[0] - ch->y_min ) * ch->ordinate_scale;
106 const double box_top =
107 ch->data_bottom + ( hinge[2] - ch->y_min ) * ch->ordinate_scale;
111 /* Can't really draw a boxplot if there's no data */
115 whisker[1] = hinge[2];
116 whisker[0] = wvp[0]->v.f;
118 for ( i = 0 ; i < n_data ; ++i )
120 if ( hinge[2] + step > wvp[i]->v.f)
121 whisker[1] = wvp[i]->v.f;
123 if ( hinge[0] - step > wvp[i]->v.f)
124 whisker[0] = wvp[i]->v.f;
129 const double bottom_whisker =
130 ch->data_bottom + ( whisker[0] - ch->y_min ) * ch->ordinate_scale;
132 const double top_whisker =
133 ch->data_bottom + ( whisker[1] - ch->y_min ) * ch->ordinate_scale;
136 pl_savestate_r(ch->lp);
140 pl_savestate_r(ch->lp);
141 pl_fillcolorname_r(ch->lp,ch->fill_colour);
142 pl_filltype_r(ch->lp,1);
149 pl_restorestate_r(ch->lp);
153 /* Draw the median */
154 pl_savestate_r(ch->lp);
155 pl_linewidth_r(ch->lp,5);
158 ch->data_bottom + ( hinge[1] - ch->y_min ) * ch->ordinate_scale,
160 ch->data_bottom + ( hinge[1] - ch->y_min ) * ch->ordinate_scale);
161 pl_restorestate_r(ch->lp);
164 /* Draw the bottom whisker */
171 /* Draw top whisker */
183 box_centre, bottom_whisker,
184 box_centre, box_bottom);
188 box_centre, top_whisker,
189 box_centre, box_top);
193 for ( i = 0 ; i < n_data ; ++i )
195 if ( wvp[i]->v.f >= hinge[2] + step )
196 draw_outlier(ch, box_centre, wvp, i,
197 ( wvp[i]->v.f > hinge[2] + 2 * step )
200 if ( wvp[i]->v.f <= hinge[0] - step )
201 draw_outlier(ch, box_centre, wvp, i,
202 ( wvp[i]->v.f < hinge[0] - 2 * step )
207 /* Draw tick mark on x axis */
208 draw_tick(ch, TICK_ABSCISSA, box_centre - ch->data_left, name);
210 pl_restorestate_r(ch->lp);
217 boxplot_draw_yscale(struct chart *ch , double y_max, double y_min)
228 y_tick = chart_rounded_tick(fabs(ch->y_max - ch->y_min) / 5.0);
230 ch->y_min = (ceil( ch->y_min / y_tick ) - 1.0 ) * y_tick;
232 ch->y_max = ( floor( ch->y_max / y_tick ) + 1.0 ) * y_tick;
234 ch->ordinate_scale = fabs(ch->data_top - ch->data_bottom)
235 / fabs(ch->y_max - ch->y_min) ;
238 /* Move to data bottom-left */
240 ch->data_left, ch->data_bottom);
242 for ( d = ch->y_min; d <= ch->y_max ; d += y_tick )
244 draw_tick (ch, TICK_ORDINATE, (d - ch->y_min ) * ch->ordinate_scale, "%g", d);