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