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>
24 #include <cairo/cairo.h>
26 #include <libpspp/misc.h>
27 #include <math/chart-geometry.h>
28 #include <math/box-whisker.h>
29 #include <output/chart.h>
30 #include <output/chart-provider.h>
31 #include <output/charts/plot-chart.h>
33 /* Draw a box-and-whiskers plot
38 struct box_whisker *bw;
49 size_t n_boxes, boxes_allocated;
52 static const struct chart_class boxplot_chart_class;
55 boxplot_create (double y_min, double y_max, const char *title)
57 struct boxplot *boxplot = xmalloc (sizeof *boxplot);
58 chart_init (&boxplot->chart, &boxplot_chart_class);
59 boxplot->y_min = y_min;
60 boxplot->y_max = y_max;
61 boxplot->title = xstrdup (title);
62 boxplot->boxes = NULL;
63 boxplot->n_boxes = boxplot->boxes_allocated = 0;
68 boxplot_add_box (struct boxplot *boxplot,
69 struct box_whisker *bw, const char *label)
72 if (boxplot->n_boxes >= boxplot->boxes_allocated)
73 boxplot->boxes = x2nrealloc (boxplot->boxes, &boxplot->boxes_allocated,
74 sizeof *boxplot->boxes);
75 box = &boxplot->boxes[boxplot->n_boxes++];
77 box->label = xstrdup (label);
81 boxplot_get_chart (struct boxplot *boxplot)
83 return &boxplot->chart;
86 /* Draw an OUTLIER on the plot CH
90 draw_case (cairo_t *cr, const struct chart_geometry *geom, double centreline,
91 const struct outlier *outlier)
93 double y = geom->data_bottom + (outlier->value - geom->y_min) * geom->ordinate_scale;
94 chart_draw_marker (cr, centreline, y,
95 outlier->extreme ? MARKER_ASTERISK : MARKER_CIRCLE,
98 cairo_move_to (cr, centreline + 10, y);
99 chart_label (cr, 'l', 'c', geom->font_size, ds_cstr (&outlier->label));
103 boxplot_draw_box (cairo_t *cr, const struct chart_geometry *geom,
106 const struct box_whisker *bw,
113 const struct ll_list *outliers;
115 const double box_left = box_centre - box_width / 2.0;
117 const double box_right = box_centre + box_width / 2.0;
121 double bottom_whisker ;
124 box_whisker_whiskers (bw, whisker);
125 box_whisker_hinges (bw, hinge);
127 box_bottom = geom->data_bottom + (hinge[0] - geom->y_min ) * geom->ordinate_scale;
129 box_top = geom->data_bottom + (hinge[2] - geom->y_min ) * geom->ordinate_scale;
131 bottom_whisker = geom->data_bottom + (whisker[0] - geom->y_min) *
132 geom->ordinate_scale;
134 top_whisker = geom->data_bottom + (whisker[1] - geom->y_min) * geom->ordinate_scale;
140 box_right - box_left,
141 box_top - box_bottom);
143 cairo_set_source_rgb (cr,
144 geom->fill_colour.red / 255.0,
145 geom->fill_colour.green / 255.0,
146 geom->fill_colour.blue / 255.0);
151 /* Draw the median */
153 cairo_set_line_width (cr, cairo_get_line_width (cr) * 5);
156 geom->data_bottom + (hinge[1] - geom->y_min) * geom->ordinate_scale);
159 geom->data_bottom + (hinge[1] - geom->y_min) * geom->ordinate_scale);
163 /* Draw the bottom whisker */
164 cairo_move_to (cr, box_left, bottom_whisker);
165 cairo_line_to (cr, box_right, bottom_whisker);
168 /* Draw top whisker */
169 cairo_move_to (cr, box_left, top_whisker);
170 cairo_line_to (cr, box_right, top_whisker);
175 cairo_move_to (cr, box_centre, bottom_whisker);
176 cairo_line_to (cr, box_centre, box_bottom);
180 cairo_move_to (cr, box_centre, top_whisker);
181 cairo_line_to (cr, box_centre, box_top);
184 outliers = box_whisker_outliers (bw);
185 for (ll = ll_head (outliers);
186 ll != ll_null (outliers); ll = ll_next (ll))
188 const struct outlier *outlier = ll_data (ll, struct outlier, ll);
189 draw_case (cr, geom, box_centre, outlier);
192 /* Draw tick mark on x axis */
193 draw_tick(cr, geom, TICK_ABSCISSA, box_centre - geom->data_left, "%s", name);
197 boxplot_draw_yscale (cairo_t *cr, struct chart_geometry *geom,
198 double y_max, double y_min)
206 y_tick = chart_rounded_tick (fabs (geom->y_max - geom->y_min) / 5.0);
208 geom->y_min = (ceil (geom->y_min / y_tick) - 1.0) * y_tick;
210 geom->y_max = (floor (geom->y_max / y_tick) + 1.0) * y_tick;
212 geom->ordinate_scale = (fabs (geom->data_top - geom->data_bottom)
213 / fabs (geom->y_max - geom->y_min));
215 for (d = geom->y_min; d <= geom->y_max; d += y_tick)
216 draw_tick (cr, geom, TICK_ORDINATE,
217 (d - geom->y_min) * geom->ordinate_scale, "%g", d);
221 boxplot_chart_draw (const struct chart *chart, cairo_t *cr,
222 struct chart_geometry *geom)
224 const struct boxplot *boxplot = (struct boxplot *) chart;
228 boxplot_draw_yscale (cr, geom, boxplot->y_max, boxplot->y_min);
229 chart_write_title (cr, geom, "%s", boxplot->title);
231 box_width = (geom->data_right - geom->data_left) / boxplot->n_boxes / 2.0;
232 for (i = 0; i < boxplot->n_boxes; i++)
234 const struct box *box = &boxplot->boxes[i];
235 const double box_centre = (i * 2 + 1) * box_width + geom->data_left;
236 boxplot_draw_box (cr, geom, box_centre, box_width, box->bw, box->label);
241 boxplot_chart_destroy (struct chart *chart)
243 struct boxplot *boxplot = (struct boxplot *) chart;
246 free (boxplot->title);
247 for (i = 0; i < boxplot->n_boxes; i++)
249 struct box *box = &boxplot->boxes[i];
250 struct statistic *statistic = &box->bw->parent.parent;
251 statistic->destroy (statistic);
254 free (boxplot->boxes);
258 static const struct chart_class boxplot_chart_class =
261 boxplot_chart_destroy