output: Use Cairo and Pango to draw charts, instead of libplot.
[pspp-builds.git] / src / output / charts / box-whisker.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2004, 2008, 2009 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>. */
16
17
18 #include <config.h>
19
20 #include <output/charts/box-whisker.h>
21
22 #include <math.h>
23 #include <assert.h>
24 #include <cairo/cairo.h>
25
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>
32
33 /* Draw a box-and-whiskers plot
34 */
35
36 struct box
37   {
38     struct box_whisker *bw;
39     char *label;
40   };
41
42 struct boxplot
43   {
44     struct chart chart;
45     double y_min;
46     double y_max;
47     char *title;
48     struct box *boxes;
49     size_t n_boxes, boxes_allocated;
50   };
51
52 static const struct chart_class boxplot_chart_class;
53
54 struct boxplot *
55 boxplot_create (double y_min, double y_max, const char *title)
56 {
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;
64   return boxplot;
65 }
66
67 void
68 boxplot_add_box (struct boxplot *boxplot,
69                  struct box_whisker *bw, const char *label)
70 {
71   struct box *box;
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++];
76   box->bw = bw;
77   box->label = xstrdup (label);
78 }
79
80 struct chart *
81 boxplot_get_chart (struct boxplot *boxplot)
82 {
83   return &boxplot->chart;
84 }
85
86 /* Draw an OUTLIER on the plot CH
87  * at CENTRELINE
88  */
89 static void
90 draw_case (cairo_t *cr, const struct chart_geometry *geom, double centreline,
91            const struct outlier *outlier)
92 {
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,
96                      20);
97
98   cairo_move_to (cr, centreline + 10, y);
99   chart_label (cr, 'l', 'c', ds_cstr (&outlier->label));
100 }
101
102 static void
103 boxplot_draw_box (cairo_t *cr, const struct chart_geometry *geom,
104                   double box_centre,
105                   double box_width,
106                   const struct box_whisker *bw,
107                   const char *name)
108 {
109   double whisker[2];
110   double hinge[3];
111   struct ll *ll;
112
113   const struct ll_list *outliers;
114
115   const double box_left = box_centre - box_width / 2.0;
116
117   const double box_right = box_centre + box_width / 2.0;
118
119   double box_bottom ;
120   double box_top ;
121   double bottom_whisker ;
122   double top_whisker ;
123
124   box_whisker_whiskers (bw, whisker);
125   box_whisker_hinges (bw, hinge);
126
127   box_bottom = geom->data_bottom + (hinge[0] - geom->y_min ) * geom->ordinate_scale;
128
129   box_top = geom->data_bottom + (hinge[2] - geom->y_min ) * geom->ordinate_scale;
130
131   bottom_whisker = geom->data_bottom + (whisker[0] - geom->y_min) *
132     geom->ordinate_scale;
133
134   top_whisker = geom->data_bottom + (whisker[1] - geom->y_min) * geom->ordinate_scale;
135
136   /* Draw the box */
137   cairo_rectangle (cr,
138                    box_left,
139                    box_bottom,
140                    box_right - box_left,
141                    box_top - box_bottom);
142   cairo_save (cr);
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);
147   cairo_fill (cr);
148   cairo_restore (cr);
149   cairo_stroke (cr);
150
151   /* Draw the median */
152   cairo_save (cr);
153   cairo_set_line_width (cr, cairo_get_line_width (cr) * 5);
154   cairo_move_to (cr,
155                  box_left,
156                  geom->data_bottom + (hinge[1] - geom->y_min) * geom->ordinate_scale);
157   cairo_line_to (cr,
158                  box_right,
159                  geom->data_bottom + (hinge[1] - geom->y_min) * geom->ordinate_scale);
160   cairo_stroke (cr);
161   cairo_restore (cr);
162
163   /* Draw the bottom whisker */
164   cairo_move_to (cr, box_left, bottom_whisker);
165   cairo_line_to (cr, box_right, bottom_whisker);
166   cairo_stroke (cr);
167
168   /* Draw top whisker */
169   cairo_move_to (cr, box_left, top_whisker);
170   cairo_line_to (cr, box_right, top_whisker);
171   cairo_stroke (cr);
172
173   /* Draw centre line.
174      (bottom half) */
175   cairo_move_to (cr, box_centre, bottom_whisker);
176   cairo_line_to (cr, box_centre, box_bottom);
177   cairo_stroke (cr);
178
179   /* (top half) */
180   cairo_move_to (cr, box_centre, top_whisker);
181   cairo_line_to (cr, box_centre, box_top);
182   cairo_stroke (cr);
183
184   outliers = box_whisker_outliers (bw);
185   for (ll = ll_head (outliers);
186        ll != ll_null (outliers); ll = ll_next (ll))
187     {
188       const struct outlier *outlier = ll_data (ll, struct outlier, ll);
189       draw_case (cr, geom, box_centre, outlier);
190     }
191
192   /* Draw  tick  mark on x axis */
193   draw_tick(cr, geom, TICK_ABSCISSA, box_centre - geom->data_left, "%s", name);
194 }
195
196 static void
197 boxplot_draw_yscale (cairo_t *cr, struct chart_geometry *geom,
198                      double y_max, double y_min)
199 {
200   double y_tick;
201   double d;
202
203   geom->y_max = y_max;
204   geom->y_min = y_min;
205
206   y_tick = chart_rounded_tick (fabs (geom->y_max - geom->y_min) / 5.0);
207
208   geom->y_min = (ceil (geom->y_min / y_tick) - 1.0) * y_tick;
209
210   geom->y_max = (floor (geom->y_max / y_tick) + 1.0) * y_tick;
211
212   geom->ordinate_scale = (fabs (geom->data_top - geom->data_bottom)
213                           / fabs (geom->y_max - geom->y_min));
214
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);
218 }
219
220 static void
221 boxplot_chart_draw (const struct chart *chart, cairo_t *cr,
222                     struct chart_geometry *geom)
223 {
224   const struct boxplot *boxplot = (struct boxplot *) chart;
225   double box_width;
226   size_t i;
227
228   boxplot_draw_yscale (cr, geom, boxplot->y_max, boxplot->y_min);
229   chart_write_title (cr, geom, "%s", boxplot->title);
230
231   box_width = (geom->data_right - geom->data_left) / boxplot->n_boxes / 2.0;
232   for (i = 0; i < boxplot->n_boxes; i++)
233     {
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);
237     }
238 }
239
240 static void
241 boxplot_chart_destroy (struct chart *chart)
242 {
243   struct boxplot *boxplot = (struct boxplot *) chart;
244   size_t i;
245
246   free (boxplot->title);
247   for (i = 0; i < boxplot->n_boxes; i++)
248     {
249       struct box *box = &boxplot->boxes[i];
250       struct statistic *statistic = &box->bw->parent.parent;
251       statistic->destroy (statistic);
252       free (box->label);
253     }
254   free (boxplot->boxes);
255   free (boxplot);
256 }
257
258 static const struct chart_class boxplot_chart_class =
259   {
260     boxplot_chart_draw,
261     boxplot_chart_destroy
262   };