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