LIST: Remove WEIGHT subcommand.
[pspp-builds.git] / src / output / charts / plot-chart.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2004, 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 #include <config.h>
18
19 #include <output/charts/plot-chart.h>
20
21 #include <assert.h>
22 #include <float.h>
23 #include <math.h>
24 #include <pango/pango-font.h>
25 #include <pango/pango-layout.h>
26 #include <pango/pango.h>
27 #include <pango/pangocairo.h>
28 #include <stdarg.h>
29 #include <stdio.h>
30 #include <stdio.h>
31 #include <string.h>
32
33 #include <libpspp/assertion.h>
34 #include <libpspp/str.h>
35 #include <math/chart-geometry.h>
36 #include <output/chart-provider.h>
37 #include <output/manager.h>
38 #include <output/output.h>
39
40 #include "xalloc.h"
41
42 #if ! PANGO_VERSION_CHECK (2, 22, 0)
43 int pango_layout_get_baseline (PangoLayout    *layout);
44
45 /* Shamelessly copied from the pango source */
46 int
47 pango_layout_get_baseline (PangoLayout    *layout)
48 {
49   int baseline;
50
51   /* XXX this is so inefficient */
52   PangoLayoutIter *iter = pango_layout_get_iter (layout);
53   baseline = pango_layout_iter_get_baseline (iter);
54   pango_layout_iter_free (iter);
55
56   return baseline;
57 }
58 #endif
59
60
61
62 const struct chart_colour data_colour[N_CHART_COLOURS] =
63   {
64     { 165, 42, 42 },            /* brown */
65     { 255, 0, 0 },              /* red */
66     { 255, 165, 0 },            /* orange */
67     { 255, 255, 0 },            /* yellow */
68     { 0, 255, 0 },              /* green */
69     { 0, 0, 255 },              /* blue */
70     { 238, 130, 238 },          /* violet */
71     { 190, 190, 190 },          /* grey */
72     { 255, 192, 203 },          /* pink */
73   };
74
75 void
76 chart_draw_marker (cairo_t *cr, double x, double y, enum marker_type marker,
77                    double size)
78 {
79   cairo_save (cr);
80   cairo_translate (cr, x, y);
81   cairo_scale (cr, size / 2.0, size / 2.0);
82   cairo_set_line_width (cr, cairo_get_line_width (cr) / (size / 2.0));
83   switch (marker)
84     {
85     case MARKER_CIRCLE:
86       cairo_arc (cr, 0, 0, 1.0, 0, 2 * M_PI);
87       cairo_stroke (cr);
88       break;
89
90     case MARKER_ASTERISK:
91       cairo_move_to (cr, 0, -1.0); /* | */
92       cairo_line_to (cr, 0, 1.0);
93       cairo_move_to (cr, -M_SQRT1_2, -M_SQRT1_2); /* / */
94       cairo_line_to (cr, M_SQRT1_2, M_SQRT1_2);
95       cairo_move_to (cr, -M_SQRT1_2, M_SQRT1_2); /* \ */
96       cairo_line_to (cr, M_SQRT1_2, -M_SQRT1_2);
97       cairo_stroke (cr);
98       break;
99
100     case MARKER_SQUARE:
101       cairo_rectangle (cr, -1.0, -1.0, 2.0, 2.0);
102       cairo_stroke (cr);
103       break;
104     }
105   cairo_restore (cr);
106 }
107
108 void
109 chart_label (cairo_t *cr, int horz_justify, int vert_justify, double font_size,
110              const char *string)
111 {
112   PangoFontDescription *desc;
113   PangoLayout *layout;
114   double x, y;
115
116   desc = pango_font_description_from_string ("sans serif");
117   if (desc == NULL)
118     {
119       cairo_new_path (cr);
120       return;
121     }
122   pango_font_description_set_absolute_size (desc, font_size * PANGO_SCALE);
123
124   cairo_save (cr);
125   cairo_get_current_point (cr, &x, &y);
126   cairo_translate (cr, x, y);
127   cairo_move_to (cr, 0, 0);
128   cairo_scale (cr, 1.0, -1.0);
129
130   layout = pango_cairo_create_layout (cr);
131   pango_layout_set_font_description (layout, desc);
132   pango_layout_set_text (layout, string, -1);
133   if (horz_justify != 'l')
134     {
135       int width_pango;
136       double width;
137
138       pango_layout_get_size (layout, &width_pango, NULL);
139       width = (double) width_pango / PANGO_SCALE;
140       if (horz_justify == 'r')
141         cairo_rel_move_to (cr, -width, 0);
142       else
143         cairo_rel_move_to (cr, -width / 2.0, 0);
144     }
145   if (vert_justify == 'x')
146     {
147       int baseline_pango = pango_layout_get_baseline (layout);
148       double baseline = (double) baseline_pango / PANGO_SCALE;
149       cairo_rel_move_to (cr, 0, -baseline);
150     }
151   else if (vert_justify != 't')
152     {
153       int height_pango;
154       double height;
155
156       pango_layout_get_size (layout, NULL, &height_pango);
157       height = (double) height_pango / PANGO_SCALE;
158       if (vert_justify == 'b')
159         cairo_rel_move_to (cr, 0, -height);
160       else if (vert_justify == 'c')
161         cairo_rel_move_to (cr, 0, -height / 2.0);
162     }
163   pango_cairo_show_layout (cr, layout);
164   g_object_unref (layout);
165
166   cairo_restore (cr);
167
168   cairo_new_path (cr);
169
170   pango_font_description_free (desc);
171 }
172
173 /* Draw a tick mark at position
174    If label is non zero, then print it at the tick mark
175 */
176 void
177 draw_tick (cairo_t *cr, const struct chart_geometry *geom,
178            enum tick_orientation orientation,
179            double position,
180            const char *label, ...)
181 {
182   const int tickSize = 10;
183   double x, y;
184
185   cairo_move_to (cr, geom->data_left, geom->data_bottom);
186
187   if (orientation == TICK_ABSCISSA)
188     {
189       cairo_rel_move_to (cr, position, 0);
190       cairo_rel_line_to (cr, 0, -tickSize);
191     }
192   else if (orientation == TICK_ORDINATE)
193     {
194       cairo_rel_move_to (cr, 0, position);
195       cairo_rel_line_to (cr, -tickSize, 0);
196     }
197   else
198     NOT_REACHED ();
199   cairo_get_current_point (cr, &x, &y);
200
201   cairo_stroke (cr);
202
203   if (label != NULL)
204     {
205       va_list ap;
206       char *s;
207
208       cairo_move_to (cr, x, y);
209
210       va_start (ap, label);
211       s = xvasprintf (label, ap);
212       if (orientation == TICK_ABSCISSA)
213         chart_label (cr, 'c', 't', geom->font_size, s);
214       else if (orientation == TICK_ORDINATE)
215         {
216           if (fabs (position) < DBL_EPSILON)
217             cairo_rel_move_to (cr, 0, 10);
218           chart_label (cr, 'r', 'c', geom->font_size, s);
219         }
220       free (s);
221       va_end (ap);
222     }
223 }
224
225
226 /* Write the title on a chart*/
227 void
228 chart_write_title (cairo_t *cr, const struct chart_geometry *geom,
229                    const char *title, ...)
230 {
231   va_list ap;
232   char *s;
233
234   cairo_save (cr);
235   cairo_move_to (cr, geom->data_left, geom->title_bottom);
236
237   va_start(ap, title);
238   s = xvasprintf (title, ap);
239   chart_label (cr, 'l', 'x', geom->font_size * 1.5, s);
240   free (s);
241   va_end (ap);
242
243   cairo_restore (cr);
244 }
245
246
247 /* Set the scale for the abscissa */
248 void
249 chart_write_xscale (cairo_t *cr, struct chart_geometry *geom,
250                     double min, double max, int ticks)
251 {
252   double x;
253
254   const double tick_interval =
255     chart_rounded_tick ((max - min) / (double) ticks);
256
257   geom->x_max = ceil (max / tick_interval) * tick_interval;
258   geom->x_min = floor (min / tick_interval) * tick_interval;
259   geom->abscissa_scale = fabs(geom->data_right - geom->data_left) /
260     fabs(geom->x_max - geom->x_min);
261
262   for (x = geom->x_min; x <= geom->x_max; x += tick_interval)
263     draw_tick (cr, geom, TICK_ABSCISSA,
264                (x - geom->x_min) * geom->abscissa_scale, "%g", x);
265 }
266
267
268 /* Set the scale for the ordinate */
269 void
270 chart_write_yscale (cairo_t *cr, struct chart_geometry *geom,
271                     double smin, double smax, int ticks)
272 {
273   double y;
274
275   const double tick_interval =
276     chart_rounded_tick ((smax - smin) / (double) ticks);
277
278   geom->y_max = ceil (smax / tick_interval) * tick_interval;
279   geom->y_min = floor (smin / tick_interval) * tick_interval;
280
281   geom->ordinate_scale =
282     (fabs (geom->data_top - geom->data_bottom)
283      / fabs (geom->y_max - geom->y_min));
284
285   for (y = geom->y_min; y <= geom->y_max; y += tick_interval)
286     draw_tick (cr, geom, TICK_ORDINATE,
287                (y - geom->y_min) * geom->ordinate_scale, "%g", y);
288 }
289
290 /* Write the abscissa label */
291 void
292 chart_write_xlabel (cairo_t *cr, const struct chart_geometry *geom,
293                     const char *label)
294 {
295   cairo_move_to (cr, geom->data_left, geom->abscissa_top);
296   chart_label (cr, 'l', 't', geom->font_size, label);
297 }
298
299 /* Write the ordinate label */
300 void
301 chart_write_ylabel (cairo_t *cr, const struct chart_geometry *geom,
302                     const char *label)
303 {
304   cairo_save (cr);
305   cairo_translate (cr, -geom->data_bottom, -geom->ordinate_right);
306   cairo_move_to (cr, 0, 0);
307   cairo_rotate (cr, M_PI / 2.0);
308   chart_label (cr, 'l', 'x', geom->font_size, label);
309   cairo_restore (cr);
310 }
311
312
313 void
314 chart_write_legend (cairo_t *cr, const struct chart_geometry *geom)
315 {
316   int i;
317   const int vstep = geom->font_size * 2;
318   const int xpad = 10;
319   const int ypad = 10;
320   const int swatch = 20;
321   const int legend_top = geom->data_top;
322   const int legend_bottom = legend_top -
323     (vstep * geom->n_datasets + 2 * ypad );
324
325   cairo_save (cr);
326
327   cairo_rectangle (cr, geom->legend_left, legend_top,
328                    geom->legend_right - xpad - geom->legend_left,
329                    legend_bottom - legend_top);
330   cairo_stroke (cr);
331
332   for (i = 0 ; i < geom->n_datasets ; ++i )
333     {
334       const int ypos = legend_top - vstep * (i + 1);
335       const int xpos = geom->legend_left + xpad;
336       const struct chart_colour *colour;
337
338       cairo_move_to (cr, xpos, ypos);
339
340       cairo_save (cr);
341       colour = &data_colour [ i % N_CHART_COLOURS];
342       cairo_set_source_rgb (cr,
343                             colour->red / 255.0,
344                             colour->green / 255.0,
345                             colour->blue / 255.0);
346       cairo_rectangle (cr, xpos, ypos, swatch, swatch);
347       cairo_fill_preserve (cr);
348       cairo_stroke (cr);
349       cairo_restore (cr);
350
351       cairo_move_to (cr, xpos + swatch * 1.5, ypos);
352       chart_label (cr, 'l', 'x', geom->font_size, geom->dataset[i]);
353     }
354
355   cairo_restore (cr);
356 }