1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2004, 2008, 2009 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU 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, see <http://www.gnu.org/licenses/>. */
20 #include <output/charts/box-whisker.h>
25 #include <libpspp/misc.h>
26 #include <math/chart-geometry.h>
27 #include <math/box-whisker.h>
28 #include <output/chart.h>
29 #include <output/chart-provider.h>
30 #include <output/charts/plot-chart.h>
32 /* Draw a box-and-whiskers plot
37 struct box_whisker *bw;
48 size_t n_boxes, boxes_allocated;
51 static const struct chart_class boxplot_chart_class;
54 boxplot_create (double y_min, double y_max, const char *title)
56 struct boxplot *boxplot = xmalloc (sizeof *boxplot);
57 chart_init (&boxplot->chart, &boxplot_chart_class);
58 boxplot->y_min = y_min;
59 boxplot->y_max = y_max;
60 boxplot->title = xstrdup (title);
61 boxplot->boxes = NULL;
62 boxplot->n_boxes = boxplot->boxes_allocated = 0;
67 boxplot_add_box (struct boxplot *boxplot,
68 struct box_whisker *bw, const char *label)
71 if (boxplot->n_boxes >= boxplot->boxes_allocated)
72 boxplot->boxes = x2nrealloc (boxplot->boxes, &boxplot->boxes_allocated,
73 sizeof *boxplot->boxes);
74 box = &boxplot->boxes[boxplot->n_boxes++];
76 box->label = xstrdup (label);
80 boxplot_get_chart (struct boxplot *boxplot)
82 return &boxplot->chart;
85 /* Draw an OUTLIER on the plot CH
89 draw_case (plPlotter *lp, const struct chart_geometry *geom, double centreline,
90 const struct outlier *outlier)
93 #define MARKER_CIRCLE 4
98 geom->data_bottom + (outlier->value - geom->y_min) * geom->ordinate_scale,
99 outlier->extreme ? MARKER_STAR : MARKER_CIRCLE,
102 pl_moverel_r(lp, 10,0);
104 pl_alabel_r(lp, 'l', 'c', ds_cstr (&outlier->label));
108 boxplot_draw_box (plPlotter *lp, const struct chart_geometry *geom,
111 const struct box_whisker *bw,
118 const struct ll_list *outliers;
120 const double box_left = box_centre - box_width / 2.0;
122 const double box_right = box_centre + box_width / 2.0;
126 double bottom_whisker ;
129 box_whisker_whiskers (bw, whisker);
130 box_whisker_hinges (bw, hinge);
132 box_bottom = geom->data_bottom + (hinge[0] - geom->y_min ) * geom->ordinate_scale;
134 box_top = geom->data_bottom + (hinge[2] - geom->y_min ) * geom->ordinate_scale;
136 bottom_whisker = geom->data_bottom + (whisker[0] - geom->y_min) *
137 geom->ordinate_scale;
139 top_whisker = geom->data_bottom + (whisker[1] - geom->y_min) * geom->ordinate_scale;
145 pl_fillcolorname_r (lp, geom->fill_colour);
146 pl_filltype_r (lp,1);
153 pl_restorestate_r (lp);
155 /* Draw the median */
157 pl_linewidth_r (lp, 5);
160 geom->data_bottom + (hinge[1] - geom->y_min) * geom->ordinate_scale,
162 geom->data_bottom + (hinge[1] - geom->y_min) * geom->ordinate_scale);
163 pl_restorestate_r (lp);
165 /* Draw the bottom whisker */
172 /* Draw top whisker */
183 box_centre, bottom_whisker,
184 box_centre, box_bottom);
188 box_centre, top_whisker,
189 box_centre, box_top);
191 outliers = box_whisker_outliers (bw);
192 for (ll = ll_head (outliers);
193 ll != ll_null (outliers); ll = ll_next (ll))
195 const struct outlier *outlier = ll_data (ll, struct outlier, ll);
196 draw_case (lp, geom, box_centre, outlier);
199 /* Draw tick mark on x axis */
200 draw_tick(lp, geom, TICK_ABSCISSA, box_centre - geom->data_left, "%s", name);
202 pl_restorestate_r(lp);
206 boxplot_draw_yscale (plPlotter *lp, struct chart_geometry *geom,
207 double y_max, double y_min)
215 y_tick = chart_rounded_tick (fabs (geom->y_max - geom->y_min) / 5.0);
217 geom->y_min = (ceil (geom->y_min / y_tick) - 1.0) * y_tick;
219 geom->y_max = (floor (geom->y_max / y_tick) + 1.0) * y_tick;
221 geom->ordinate_scale = (fabs (geom->data_top - geom->data_bottom)
222 / fabs (geom->y_max - geom->y_min));
224 /* Move to data bottom-left */
225 pl_move_r (lp, geom->data_left, geom->data_bottom);
227 for (d = geom->y_min; d <= geom->y_max; d += y_tick)
228 draw_tick (lp, geom, TICK_ORDINATE,
229 (d - geom->y_min) * geom->ordinate_scale, "%g", d);
233 boxplot_chart_draw (const struct chart *chart, plPlotter *lp)
235 const struct boxplot *boxplot = (struct boxplot *) chart;
236 struct chart_geometry geom;
240 chart_geometry_init (lp, &geom);
241 boxplot_draw_yscale (lp, &geom, boxplot->y_max, boxplot->y_min);
242 chart_write_title (lp, &geom, "%s", boxplot->title);
244 box_width = (geom.data_right - geom.data_left) / boxplot->n_boxes / 2.0;
245 for (i = 0; i < boxplot->n_boxes; i++)
247 const struct box *box = &boxplot->boxes[i];
248 const double box_centre = (i * 2 + 1) * box_width + geom.data_left;
249 boxplot_draw_box (lp, &geom, box_centre, box_width, box->bw, box->label);
252 chart_geometry_free (lp);
256 boxplot_chart_destroy (struct chart *chart)
258 struct boxplot *boxplot = (struct boxplot *) chart;
261 free (boxplot->title);
262 for (i = 0; i < boxplot->n_boxes; i++)
264 struct box *box = &boxplot->boxes[i];
265 struct statistic *statistic = &box->bw->parent.parent;
266 statistic->destroy (statistic);
269 free (boxplot->boxes);
273 static const struct chart_class boxplot_chart_class =
276 boxplot_chart_destroy