1 /* PSPP - computes sample statistics.
2 Copyright (C) 2004 Free Software Foundation, Inc.
3 Written by John Darrington <john@darrington.wattle.id.au>
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.
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.
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
22 #include "chart-geometry.h"
27 #include "factor-stats.h"
28 #include "box-whisker.h"
29 #include "plot-chart.h"
32 /* Draw a box-and-whiskers plot
35 /* Draw an outlier on the plot CH
37 * The outlier is in (*wvp)[idx]
38 * If EXTREME is non zero, then consider it to be an extreme
42 draw_outlier(struct chart *ch, double centreline,
43 struct weighted_value **wvp,
49 draw_outlier(struct chart *ch, double centreline,
50 struct weighted_value **wvp,
57 #define MARKER_CIRCLE 4
63 (wvp[idx]->v.f - ch->y_min ) * ch->ordinate_scale,
64 extreme?MARKER_STAR:MARKER_CIRCLE,
67 pl_moverel_r(ch->lp, 10,0);
69 snprintf(label, 10, "%d", wvp[idx]->case_nos->num);
71 pl_alabel_r(ch->lp, 'l', 'c', label);
77 boxplot_draw_boxplot(struct chart *ch,
86 const double *hinge = m->hinge;
87 struct weighted_value **wvp = m->wvp;
88 const int n_data = m->n_data;
90 const double step = (hinge[2] - hinge[0]) * 1.5;
93 const double box_left = box_centre - box_width / 2.0;
95 const double box_right = box_centre + box_width / 2.0;
98 const double box_bottom =
99 ch->data_bottom + ( hinge[0] - ch->y_min ) * ch->ordinate_scale;
102 const double box_top =
103 ch->data_bottom + ( hinge[2] - ch->y_min ) * ch->ordinate_scale;
107 /* Can't really draw a boxplot if there's no data */
111 whisker[1] = hinge[2];
112 whisker[0] = wvp[0]->v.f;
114 for ( i = 0 ; i < n_data ; ++i )
116 if ( hinge[2] + step > wvp[i]->v.f)
117 whisker[1] = wvp[i]->v.f;
119 if ( hinge[0] - step > wvp[i]->v.f)
120 whisker[0] = wvp[i]->v.f;
125 const double bottom_whisker =
126 ch->data_bottom + ( whisker[0] - ch->y_min ) * ch->ordinate_scale;
128 const double top_whisker =
129 ch->data_bottom + ( whisker[1] - ch->y_min ) * ch->ordinate_scale;
132 pl_savestate_r(ch->lp);
136 pl_savestate_r(ch->lp);
137 pl_fillcolorname_r(ch->lp,ch->fill_colour);
138 pl_filltype_r(ch->lp,1);
145 pl_restorestate_r(ch->lp);
149 /* Draw the median */
150 pl_savestate_r(ch->lp);
151 pl_linewidth_r(ch->lp,5);
154 ch->data_bottom + ( hinge[1] - ch->y_min ) * ch->ordinate_scale,
156 ch->data_bottom + ( hinge[1] - ch->y_min ) * ch->ordinate_scale);
157 pl_restorestate_r(ch->lp);
160 /* Draw the bottom whisker */
167 /* Draw top whisker */
179 box_centre, bottom_whisker,
180 box_centre, box_bottom);
184 box_centre, top_whisker,
185 box_centre, box_top);
189 for ( i = 0 ; i < n_data ; ++i )
191 if ( wvp[i]->v.f >= hinge[2] + step )
192 draw_outlier(ch, box_centre, wvp, i,
193 ( wvp[i]->v.f > hinge[2] + 2 * step )
196 if ( wvp[i]->v.f <= hinge[0] - step )
197 draw_outlier(ch, box_centre, wvp, i,
198 ( wvp[i]->v.f < hinge[0] - 2 * step )
203 /* Draw tick mark on x axis */
204 draw_tick(ch, TICK_ABSCISSA, box_centre - ch->data_left, name);
206 pl_restorestate_r(ch->lp);
213 boxplot_draw_yscale(struct chart *ch , double y_max, double y_min)
224 y_tick = chart_rounded_tick(fabs(ch->y_max - ch->y_min) / 5.0);
226 ch->y_min = (ceil( ch->y_min / y_tick ) - 1.0 ) * y_tick;
228 ch->y_max = ( floor( ch->y_max / y_tick ) + 1.0 ) * y_tick;
230 ch->ordinate_scale = fabs(ch->data_top - ch->data_bottom)
231 / fabs(ch->y_max - ch->y_min) ;
234 /* Move to data bottom-left */
236 ch->data_left, ch->data_bottom);
238 for ( d = ch->y_min; d <= ch->y_max ; d += y_tick )
240 draw_tick (ch, TICK_ORDINATE, (d - ch->y_min ) * ch->ordinate_scale, "%g", d);