Change many %g format specifiers to %.*g with precision DBL_DIG + 1.
[pspp] / src / output / cairo-chart.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2004, 2009, 2010, 2011, 2014 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/cairo-chart.h"
20
21 #include <assert.h>
22 #include <cairo/cairo.h>
23 #include <pango/pango.h>
24 #include <pango/pangocairo.h>
25 #include <float.h>
26 #include <math.h>
27 #include <stdarg.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31
32 #include "libpspp/assertion.h"
33 #include "math/chart-geometry.h"
34 #include "output/cairo.h"
35 #include "output/chart-item.h"
36
37 #include "gl/xalloc.h"
38 #include "gl/xvasprintf.h"
39
40 #include "gettext.h"
41 #define _(msgid) gettext (msgid)
42
43 void
44 xrchart_geometry_init (cairo_t *cr, struct xrchart_geometry *geom,
45                        double width, double length)
46 {
47   /* Set default chart geometry. */
48   geom->axis[SCALE_ORDINATE].data_max = 0.900 * length;
49   geom->axis[SCALE_ORDINATE].data_min = 0.120 * length;
50
51   geom->axis[SCALE_ABSCISSA].data_min = 0.150 * width;
52   geom->axis[SCALE_ABSCISSA].data_max = 0.800 * width;
53   geom->abscissa_bottom = 0.070 * length;
54   geom->ordinate_left = 0.050 * width;
55   geom->title_bottom = 0.920 * length;
56   geom->legend_left = 0.810 * width;
57   geom->legend_right = width;
58   geom->font_size = 15.0;
59   geom->in_path = false;
60   geom->dataset = NULL;
61   geom->n_datasets = 0;
62
63   geom->fill_colour = data_colour[0];
64
65   cairo_set_line_width (cr, 1.0);
66
67   cairo_rectangle (cr, geom->axis[SCALE_ABSCISSA].data_min, geom->axis[SCALE_ORDINATE].data_min,
68                    geom->axis[SCALE_ABSCISSA].data_max - geom->axis[SCALE_ABSCISSA].data_min,
69                    geom->axis[SCALE_ORDINATE].data_max - geom->axis[SCALE_ORDINATE].data_min);
70   cairo_stroke (cr);
71 }
72
73 void
74 xrchart_geometry_free (cairo_t *cr UNUSED, struct xrchart_geometry *geom)
75 {
76   int i;
77
78   for (i = 0 ; i < geom->n_datasets; ++i)
79     free (geom->dataset[i]);
80   free (geom->dataset);
81 }
82
83 #if ! PANGO_VERSION_CHECK (1, 22, 0)
84 int pango_layout_get_baseline (PangoLayout    *layout);
85
86 /* Shamelessly copied from the pango source */
87 int
88 pango_layout_get_baseline (PangoLayout    *layout)
89 {
90   int baseline;
91
92   /* XXX this is so inefficient */
93   PangoLayoutIter *iter = pango_layout_get_iter (layout);
94   baseline = pango_layout_iter_get_baseline (iter);
95   pango_layout_iter_free (iter);
96
97   return baseline;
98 }
99 #endif
100
101 /*  
102     These colours come from: 
103     http://tango.freedesktop.org/static/cvs/tango-art-tools/palettes/Tango-Palette.gpl */
104 const struct xrchart_colour data_colour[XRCHART_N_COLOURS] =
105   {
106     {252, 233,  79},    /* Butter 1 */
107     {138, 226,  52},    /* Chameleon 1 */
108     {252, 175,  62},    /* Orange 1 */
109     {114, 159, 207},    /* Sky Blue 1 */
110     {173, 127, 168},    /* Plum 1 */
111     {233, 185, 110},    /* Chocolate 1 */
112     {239,  41,  41},    /* Scarlet Red 1 */
113     {238, 238, 236},    /* Aluminium 1 */
114
115     {237, 212,   0},    /* Butter 2 */
116     {115, 210,  22},    /* Chameleon 2 */
117     {245, 121,   0},    /* Orange 2 */
118     {52,  101, 164},    /* Sky Blue 2 */
119     {117,  80, 123},    /* Plum 2 */
120     {193, 125,  17},    /* Chocolate 2 */
121     {204,   0,   0},    /* Scarlet Red 2 */
122
123     {136, 138, 133},    /* Aluminium 4 */
124
125     {196, 160,   0},    /* Butter 3 */
126     {78,  154,   6},    /* Chameleon 3 */
127     {206,  92,   0},    /* Orange 3 */
128     {32,   74, 135},    /* Sky Blue 3 */
129     {92,   53, 102},    /* Plum 3 */
130     {143,  89,   2},    /* Chocolate 3 */
131     {164,   0,   0},    /* Scarlet Red 3 */
132     {85,   87,  83},    /* Aluminium 5 */
133
134     {211, 215, 207},    /* Aluminium 2 */
135     {186, 189, 182},    /* Aluminium 3 */
136     {46,   52,  54},    /* Aluminium 6 */
137   };
138
139 void
140 xrchart_draw_marker (cairo_t *cr, double x, double y,
141                      enum xrmarker_type marker, double size)
142 {
143   cairo_save (cr);
144   cairo_translate (cr, x, y);
145   cairo_scale (cr, size / 2.0, size / 2.0);
146   cairo_set_line_width (cr, cairo_get_line_width (cr) / (size / 2.0));
147   switch (marker)
148     {
149     case XRMARKER_CIRCLE:
150       cairo_arc (cr, 0, 0, 1.0, 0, 2 * M_PI);
151       cairo_stroke (cr);
152       break;
153
154     case XRMARKER_ASTERISK:
155       cairo_move_to (cr, 0, -1.0); /* | */
156       cairo_line_to (cr, 0, 1.0);
157       cairo_move_to (cr, -M_SQRT1_2, -M_SQRT1_2); /* / */
158       cairo_line_to (cr, M_SQRT1_2, M_SQRT1_2);
159       cairo_move_to (cr, -M_SQRT1_2, M_SQRT1_2); /* \ */
160       cairo_line_to (cr, M_SQRT1_2, -M_SQRT1_2);
161       cairo_stroke (cr);
162       break;
163
164     case XRMARKER_SQUARE:
165       cairo_rectangle (cr, -1.0, -1.0, 2.0, 2.0);
166       cairo_stroke (cr);
167       break;
168     }
169   cairo_restore (cr);
170 }
171
172 void
173 xrchart_label_rotate (cairo_t *cr, int horz_justify, int vert_justify,
174                       double font_size, const char *string, double angle)
175 {
176   PangoFontDescription *desc;
177   PangoLayout *layout;
178   double x, y;
179
180   desc = pango_font_description_from_string ("sans serif");
181   if (desc == NULL)
182     {
183       cairo_new_path (cr);
184       return;
185     }
186   pango_font_description_set_absolute_size (desc, font_size * PANGO_SCALE);
187
188   cairo_save (cr);
189   cairo_rotate (cr, angle);
190   cairo_get_current_point (cr, &x, &y);
191   cairo_translate (cr, x, y);
192   cairo_move_to (cr, 0, 0);
193   cairo_scale (cr, 1.0, -1.0);
194
195   layout = pango_cairo_create_layout (cr);
196   pango_layout_set_font_description (layout, desc);
197   pango_layout_set_text (layout, string, -1);
198   if (horz_justify != 'l')
199     {
200       int width_pango;
201       double width;
202
203       pango_layout_get_size (layout, &width_pango, NULL);
204       width = (double) width_pango / PANGO_SCALE;
205       if (horz_justify == 'r')
206         cairo_rel_move_to (cr, -width, 0);
207       else
208         cairo_rel_move_to (cr, -width / 2.0, 0);
209     }
210   if (vert_justify == 'x')
211     {
212       int baseline_pango = pango_layout_get_baseline (layout);
213       double baseline = (double) baseline_pango / PANGO_SCALE;
214       cairo_rel_move_to (cr, 0, -baseline);
215     }
216   else if (vert_justify != 't')
217     {
218       int height_pango;
219       double height;
220
221       pango_layout_get_size (layout, NULL, &height_pango);
222       height = (double) height_pango / PANGO_SCALE;
223       if (vert_justify == 'b')
224         cairo_rel_move_to (cr, 0, -height);
225       else if (vert_justify == 'c')
226         cairo_rel_move_to (cr, 0, -height / 2.0);
227     }
228   pango_cairo_show_layout (cr, layout);
229   g_object_unref (layout);
230
231   cairo_restore (cr);
232
233   cairo_new_path (cr);
234
235   pango_font_description_free (desc);
236 }
237
238 void
239 xrchart_label (cairo_t *cr, int horz_justify, int vert_justify,
240                double font_size, const char *string)
241 {
242   xrchart_label_rotate (cr, horz_justify, vert_justify, font_size, string, 0);
243 }
244
245
246 /* Draw a tick mark at position
247    If label is non null, then print it at the tick mark
248 */
249 static void
250 draw_tick_internal (cairo_t *cr, const struct xrchart_geometry *geom,
251                     enum tick_orientation orientation,
252                     bool rotated,
253                     double position,
254                     const char *s);
255
256 void
257 draw_tick (cairo_t *cr, const struct xrchart_geometry *geom,
258            enum tick_orientation orientation,
259            bool rotated,
260            double position,
261            const char *label, ...)
262 {
263   va_list ap;
264   char *s;
265   va_start (ap, label);
266   s = xvasprintf (label, ap);
267
268   if (fabs (position) < DBL_EPSILON)
269     position = 0;
270
271   draw_tick_internal (cr, geom, orientation, rotated, position, s);
272   free (s);
273   va_end (ap);
274 }
275
276
277 static void
278 draw_tick_internal (cairo_t *cr, const struct xrchart_geometry *geom,
279                     enum tick_orientation orientation,
280                     bool rotated,
281                     double position,
282                     const char *s)
283 {
284   const int tickSize = 10;
285   double x, y;
286
287   cairo_move_to (cr, geom->axis[SCALE_ABSCISSA].data_min, geom->axis[SCALE_ORDINATE].data_min);
288
289   if (orientation == SCALE_ABSCISSA)
290     {
291       cairo_rel_move_to (cr, position, 0);
292       cairo_rel_line_to (cr, 0, -tickSize);
293     }
294   else if (orientation == SCALE_ORDINATE)
295     {
296       cairo_rel_move_to (cr, 0, position);
297       cairo_rel_line_to (cr, -tickSize, 0);
298     }
299   else
300     NOT_REACHED ();
301   cairo_get_current_point (cr, &x, &y);
302
303   cairo_stroke (cr);
304
305   if (s != NULL)
306     {
307       cairo_move_to (cr, x, y);
308
309       if (orientation == SCALE_ABSCISSA)
310         {
311           if ( rotated) 
312             xrchart_label_rotate (cr, 'l', 'c', geom->font_size, s, -G_PI_4);
313           else
314             xrchart_label (cr, 'c', 't', geom->font_size, s);
315         }
316       else if (orientation == SCALE_ORDINATE)
317         {
318           if (fabs (position) < DBL_EPSILON)
319             cairo_rel_move_to (cr, 0, 10);
320           xrchart_label (cr, 'r', 'c', geom->font_size, s);
321         }
322     }
323 }
324
325
326 /* Write the title on a chart*/
327 void
328 xrchart_write_title (cairo_t *cr, const struct xrchart_geometry *geom,
329                    const char *title, ...)
330 {
331   va_list ap;
332   char *s;
333
334   cairo_save (cr);
335   cairo_move_to (cr, geom->axis[SCALE_ABSCISSA].data_min, geom->title_bottom);
336
337   va_start(ap, title);
338   s = xvasprintf (title, ap);
339   xrchart_label (cr, 'l', 'x', geom->font_size * 1.5, s);
340   free (s);
341   va_end (ap);
342
343   cairo_restore (cr);
344 }
345
346
347
348 static void
349 xrchart_write_scale (cairo_t *cr, struct xrchart_geometry *geom,
350                      double smin, double smax, int ticks, enum tick_orientation orient)
351 {
352   int s;
353
354   const double tick_interval =
355     chart_rounded_tick ((smax - smin) / (double) ticks);
356
357   int upper = ceil (smax / tick_interval);
358   int lower = floor (smin / tick_interval);
359
360   geom->axis[orient].max = tick_interval * upper;
361   geom->axis[orient].min = tick_interval * lower;
362
363   geom->axis[orient].scale = (fabs (geom->axis[orient].data_max - geom->axis[orient].data_min)
364      / fabs (geom->axis[orient].max - geom->axis[orient].min));
365
366   for (s = 0 ; s < upper - lower; ++s)
367     {
368       double pos = (s + lower) * tick_interval;
369       draw_tick (cr, geom, orient, false,
370                  s * tick_interval * geom->axis[orient].scale, "%.*g",
371                  DBL_DIG + 1, pos);
372     }
373 }
374
375 /* Set the scale for the ordinate */
376 void
377 xrchart_write_yscale (cairo_t *cr, struct xrchart_geometry *geom,
378                     double smin, double smax, int ticks)
379 {
380   xrchart_write_scale (cr, geom, smin, smax, ticks, SCALE_ORDINATE);
381 }
382
383 /* Set the scale for the abscissa */
384 void
385 xrchart_write_xscale (cairo_t *cr, struct xrchart_geometry *geom,
386                     double smin, double smax, int ticks)
387 {
388   xrchart_write_scale (cr, geom, smin, smax, ticks, SCALE_ABSCISSA);
389 }
390
391
392 /* Write the abscissa label */
393 void
394 xrchart_write_xlabel (cairo_t *cr, const struct xrchart_geometry *geom,
395                     const char *label)
396 {
397   cairo_move_to (cr, geom->axis[SCALE_ABSCISSA].data_min, geom->abscissa_bottom);
398   xrchart_label (cr, 'l', 't', geom->font_size, label);
399 }
400
401 /* Write the ordinate label */
402 void
403 xrchart_write_ylabel (cairo_t *cr, const struct xrchart_geometry *geom,
404                     const char *label)
405 {
406   cairo_save (cr);
407   cairo_translate (cr, geom->ordinate_left,   geom->axis[SCALE_ORDINATE].data_min);
408   cairo_rotate (cr, M_PI / 2.0);
409
410   xrchart_label (cr, 'l', 'x', geom->font_size, label);
411   cairo_restore (cr);
412 }
413
414
415 void
416 xrchart_write_legend (cairo_t *cr, const struct xrchart_geometry *geom)
417 {
418   int i;
419   const int vstep = geom->font_size * 2;
420   const int xpad = 10;
421   const int ypad = 10;
422   const int swatch = 20;
423   const int legend_top = geom->axis[SCALE_ORDINATE].data_max;
424   const int legend_bottom = legend_top -
425     (vstep * geom->n_datasets + 2 * ypad );
426
427   cairo_save (cr);
428
429   cairo_rectangle (cr, geom->legend_left, legend_top,
430                    geom->legend_right - xpad - geom->legend_left,
431                    legend_bottom - legend_top);
432   cairo_stroke (cr);
433
434   for (i = 0 ; i < geom->n_datasets ; ++i )
435     {
436       const int ypos = legend_top - vstep * (i + 1);
437       const int xpos = geom->legend_left + xpad;
438       const struct xrchart_colour *colour;
439
440       cairo_move_to (cr, xpos, ypos);
441
442       cairo_save (cr);
443       colour = &data_colour [ i % XRCHART_N_COLOURS];
444       cairo_set_source_rgb (cr,
445                             colour->red / 255.0,
446                             colour->green / 255.0,
447                             colour->blue / 255.0);
448       cairo_rectangle (cr, xpos, ypos, swatch, swatch);
449       cairo_fill_preserve (cr);
450       cairo_stroke (cr);
451       cairo_restore (cr);
452
453       cairo_move_to (cr, xpos + swatch * 1.5, ypos);
454       xrchart_label (cr, 'l', 'x', geom->font_size, geom->dataset[i]);
455     }
456
457   cairo_restore (cr);
458 }
459
460 /* Start a new vector called NAME */
461 void
462 xrchart_vector_start (cairo_t *cr, struct xrchart_geometry *geom, const char *name)
463 {
464   const struct xrchart_colour *colour;
465
466   cairo_save (cr);
467
468   colour = &data_colour[geom->n_datasets % XRCHART_N_COLOURS];
469   cairo_set_source_rgb (cr,
470                         colour->red / 255.0,
471                         colour->green / 255.0,
472                         colour->blue / 255.0);
473
474   geom->n_datasets++;
475   geom->dataset = xrealloc (geom->dataset,
476                             geom->n_datasets * sizeof (*geom->dataset));
477
478   geom->dataset[geom->n_datasets - 1] = strdup (name);
479 }
480
481 /* Plot a data point */
482 void
483 xrchart_datum (cairo_t *cr, const struct xrchart_geometry *geom,
484              int dataset UNUSED, double x, double y)
485 {
486   double x_pos = (x - geom->axis[SCALE_ABSCISSA].min) * geom->axis[SCALE_ABSCISSA].scale + geom->axis[SCALE_ABSCISSA].data_min;
487   double y_pos = (y - geom->axis[SCALE_ORDINATE].min) * geom->axis[SCALE_ORDINATE].scale + geom->axis[SCALE_ORDINATE].data_min;
488
489   xrchart_draw_marker (cr, x_pos, y_pos, XRMARKER_SQUARE, 15);
490 }
491
492 void
493 xrchart_vector_end (cairo_t *cr, struct xrchart_geometry *geom)
494 {
495   cairo_stroke (cr);
496   cairo_restore (cr);
497   geom->in_path = false;
498 }
499
500 /* Plot a data point */
501 void
502 xrchart_vector (cairo_t *cr, struct xrchart_geometry *geom, double x, double y)
503 {
504   const double x_pos =
505     (x - geom->axis[SCALE_ABSCISSA].min) * geom->axis[SCALE_ABSCISSA].scale + geom->axis[SCALE_ABSCISSA].data_min ;
506
507   const double y_pos =
508     (y - geom->axis[SCALE_ORDINATE].min) * geom->axis[SCALE_ORDINATE].scale + geom->axis[SCALE_ORDINATE].data_min ;
509
510   if (geom->in_path)
511     cairo_line_to (cr, x_pos, y_pos);
512   else
513     {
514       cairo_move_to (cr, x_pos, y_pos);
515       geom->in_path = true;
516     }
517 }
518
519
520
521 /* Draw a line with slope SLOPE and intercept INTERCEPT.
522    between the points limit1 and limit2.
523    If lim_dim is XRCHART_DIM_Y then the limit{1,2} are on the
524    y axis otherwise the x axis
525 */
526 void
527 xrchart_line(cairo_t *cr, const struct xrchart_geometry *geom,
528            double slope, double intercept,
529            double limit1, double limit2, enum xrchart_dim lim_dim)
530 {
531   double x1, y1;
532   double x2, y2;
533
534   if ( lim_dim == XRCHART_DIM_Y )
535     {
536       x1 = ( limit1 - intercept ) / slope;
537       x2 = ( limit2 - intercept ) / slope;
538       y1 = limit1;
539       y2 = limit2;
540     }
541   else
542     {
543       x1 = limit1;
544       x2 = limit2;
545       y1 = slope * x1 + intercept;
546       y2 = slope * x2 + intercept;
547     }
548
549   y1 = (y1 - geom->axis[SCALE_ORDINATE].min) * geom->axis[SCALE_ORDINATE].scale + geom->axis[SCALE_ORDINATE].data_min;
550   y2 = (y2 - geom->axis[SCALE_ORDINATE].min) * geom->axis[SCALE_ORDINATE].scale + geom->axis[SCALE_ORDINATE].data_min;
551   x1 = (x1 - geom->axis[SCALE_ABSCISSA].min) * geom->axis[SCALE_ABSCISSA].scale + geom->axis[SCALE_ABSCISSA].data_min;
552   x2 = (x2 - geom->axis[SCALE_ABSCISSA].min) * geom->axis[SCALE_ABSCISSA].scale + geom->axis[SCALE_ABSCISSA].data_min;
553
554   cairo_move_to (cr, x1, y1);
555   cairo_line_to (cr, x2, y2);
556   cairo_stroke (cr);
557 }