1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2004, 2009, 2010, 2011 Free Software Foundation, Inc.
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.
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.
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/>. */
19 #include "output/cairo-chart.h"
22 #include <cairo/cairo.h>
23 #include <pango/pango.h>
24 #include <pango/pangocairo.h>
33 #include "libpspp/assertion.h"
34 #include "math/chart-geometry.h"
35 #include "output/cairo.h"
36 #include "output/chart-item.h"
39 #include "gl/xalloc.h"
40 #include "gl/xvasprintf.h"
43 #define _(msgid) gettext (msgid)
46 xrchart_geometry_init (cairo_t *cr, struct xrchart_geometry *geom,
47 double width, double length)
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;
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;
65 geom->fill_colour.red = 255;
66 geom->fill_colour.green = 0;
67 geom->fill_colour.blue = 0;
69 cairo_set_line_width (cr, 1.0);
71 cairo_rectangle (cr, geom->axis[SCALE_ABSCISSA].data_min, geom->axis[SCALE_ORDINATE].data_min,
72 geom->axis[SCALE_ABSCISSA].data_max - geom->axis[SCALE_ABSCISSA].data_min,
73 geom->axis[SCALE_ORDINATE].data_max - geom->axis[SCALE_ORDINATE].data_min);
78 xrchart_geometry_free (cairo_t *cr UNUSED, struct xrchart_geometry *geom)
82 for (i = 0 ; i < geom->n_datasets; ++i)
83 free (geom->dataset[i]);
87 #if ! PANGO_VERSION_CHECK (1, 22, 0)
88 int pango_layout_get_baseline (PangoLayout *layout);
90 /* Shamelessly copied from the pango source */
92 pango_layout_get_baseline (PangoLayout *layout)
96 /* XXX this is so inefficient */
97 PangoLayoutIter *iter = pango_layout_get_iter (layout);
98 baseline = pango_layout_iter_get_baseline (iter);
99 pango_layout_iter_free (iter);
107 const struct xrchart_colour data_colour[XRCHART_N_COLOURS] =
109 { 165, 42, 42 }, /* brown */
110 { 255, 0, 0 }, /* red */
111 { 255, 165, 0 }, /* orange */
112 { 255, 255, 0 }, /* yellow */
113 { 0, 255, 0 }, /* green */
114 { 0, 0, 255 }, /* blue */
115 { 238, 130, 238 }, /* violet */
116 { 190, 190, 190 }, /* grey */
117 { 255, 192, 203 }, /* pink */
121 xrchart_draw_marker (cairo_t *cr, double x, double y,
122 enum xrmarker_type marker, double size)
125 cairo_translate (cr, x, y);
126 cairo_scale (cr, size / 2.0, size / 2.0);
127 cairo_set_line_width (cr, cairo_get_line_width (cr) / (size / 2.0));
130 case XRMARKER_CIRCLE:
131 cairo_arc (cr, 0, 0, 1.0, 0, 2 * M_PI);
135 case XRMARKER_ASTERISK:
136 cairo_move_to (cr, 0, -1.0); /* | */
137 cairo_line_to (cr, 0, 1.0);
138 cairo_move_to (cr, -M_SQRT1_2, -M_SQRT1_2); /* / */
139 cairo_line_to (cr, M_SQRT1_2, M_SQRT1_2);
140 cairo_move_to (cr, -M_SQRT1_2, M_SQRT1_2); /* \ */
141 cairo_line_to (cr, M_SQRT1_2, -M_SQRT1_2);
145 case XRMARKER_SQUARE:
146 cairo_rectangle (cr, -1.0, -1.0, 2.0, 2.0);
154 xrchart_label (cairo_t *cr, int horz_justify, int vert_justify,
155 double font_size, const char *string)
157 PangoFontDescription *desc;
161 desc = pango_font_description_from_string ("sans serif");
167 pango_font_description_set_absolute_size (desc, font_size * PANGO_SCALE);
170 cairo_get_current_point (cr, &x, &y);
171 cairo_translate (cr, x, y);
172 cairo_move_to (cr, 0, 0);
173 cairo_scale (cr, 1.0, -1.0);
175 layout = pango_cairo_create_layout (cr);
176 pango_layout_set_font_description (layout, desc);
177 pango_layout_set_text (layout, string, -1);
178 if (horz_justify != 'l')
183 pango_layout_get_size (layout, &width_pango, NULL);
184 width = (double) width_pango / PANGO_SCALE;
185 if (horz_justify == 'r')
186 cairo_rel_move_to (cr, -width, 0);
188 cairo_rel_move_to (cr, -width / 2.0, 0);
190 if (vert_justify == 'x')
192 int baseline_pango = pango_layout_get_baseline (layout);
193 double baseline = (double) baseline_pango / PANGO_SCALE;
194 cairo_rel_move_to (cr, 0, -baseline);
196 else if (vert_justify != 't')
201 pango_layout_get_size (layout, NULL, &height_pango);
202 height = (double) height_pango / PANGO_SCALE;
203 if (vert_justify == 'b')
204 cairo_rel_move_to (cr, 0, -height);
205 else if (vert_justify == 'c')
206 cairo_rel_move_to (cr, 0, -height / 2.0);
208 pango_cairo_show_layout (cr, layout);
209 g_object_unref (layout);
215 pango_font_description_free (desc);
218 /* Draw a tick mark at position
219 If label is non null, then print it at the tick mark
222 draw_tick_internal (cairo_t *cr, const struct xrchart_geometry *geom,
223 enum tick_orientation orientation,
228 draw_tick (cairo_t *cr, const struct xrchart_geometry *geom,
229 enum tick_orientation orientation,
231 const char *label, ...)
235 va_start (ap, label);
236 s = xvasprintf (label, ap);
238 if (fabs (position) < DBL_EPSILON)
241 draw_tick_internal (cr, geom, orientation, position, s);
248 draw_tick_internal (cairo_t *cr, const struct xrchart_geometry *geom,
249 enum tick_orientation orientation,
253 const int tickSize = 10;
256 cairo_move_to (cr, geom->axis[SCALE_ABSCISSA].data_min, geom->axis[SCALE_ORDINATE].data_min);
258 if (orientation == SCALE_ABSCISSA)
260 cairo_rel_move_to (cr, position, 0);
261 cairo_rel_line_to (cr, 0, -tickSize);
263 else if (orientation == SCALE_ORDINATE)
265 cairo_rel_move_to (cr, 0, position);
266 cairo_rel_line_to (cr, -tickSize, 0);
270 cairo_get_current_point (cr, &x, &y);
276 cairo_move_to (cr, x, y);
278 if (orientation == SCALE_ABSCISSA)
279 xrchart_label (cr, 'c', 't', geom->font_size, s);
280 else if (orientation == SCALE_ORDINATE)
282 if (fabs (position) < DBL_EPSILON)
283 cairo_rel_move_to (cr, 0, 10);
284 xrchart_label (cr, 'r', 'c', geom->font_size, s);
290 /* Write the title on a chart*/
292 xrchart_write_title (cairo_t *cr, const struct xrchart_geometry *geom,
293 const char *title, ...)
299 cairo_move_to (cr, geom->axis[SCALE_ABSCISSA].data_min, geom->title_bottom);
302 s = xvasprintf (title, ap);
303 xrchart_label (cr, 'l', 'x', geom->font_size * 1.5, s);
311 /* Set the scale for the ordinate */
313 xrchart_write_scale (cairo_t *cr, struct xrchart_geometry *geom,
314 double smin, double smax, int ticks, enum tick_orientation orient)
318 const double tick_interval =
319 chart_rounded_tick ((smax - smin) / (double) ticks);
321 int upper = ceil (smax / tick_interval);
322 int lower = floor (smin / tick_interval);
324 geom->axis[orient].max = tick_interval * upper;
325 geom->axis[orient].min = tick_interval * lower;
327 geom->axis[orient].scale = (fabs (geom->axis[orient].data_max - geom->axis[orient].data_min)
328 / fabs (geom->axis[orient].max - geom->axis[orient].min));
330 for (s = 0 ; s < upper - lower; ++s)
332 double pos = (s + lower) * tick_interval;
333 draw_tick (cr, geom, orient,
334 s * tick_interval * geom->axis[orient].scale, "%g", pos);
339 xrchart_write_yscale (cairo_t *cr, struct xrchart_geometry *geom,
340 double smin, double smax, int ticks)
342 xrchart_write_scale (cr, geom, smin, smax, ticks, SCALE_ORDINATE);
345 /* Set the scale for the abscissa */
347 xrchart_write_xscale (cairo_t *cr, struct xrchart_geometry *geom,
348 double smin, double smax, int ticks)
350 xrchart_write_scale (cr, geom, smin, smax, ticks, SCALE_ABSCISSA);
354 /* Write the abscissa label */
356 xrchart_write_xlabel (cairo_t *cr, const struct xrchart_geometry *geom,
359 cairo_move_to (cr, geom->axis[SCALE_ABSCISSA].data_min, geom->abscissa_bottom);
360 xrchart_label (cr, 'l', 't', geom->font_size, label);
363 /* Write the ordinate label */
365 xrchart_write_ylabel (cairo_t *cr, const struct xrchart_geometry *geom,
369 cairo_translate (cr, geom->ordinate_left, geom->axis[SCALE_ORDINATE].data_min);
370 cairo_rotate (cr, M_PI / 2.0);
372 xrchart_label (cr, 'l', 'x', geom->font_size, label);
378 xrchart_write_legend (cairo_t *cr, const struct xrchart_geometry *geom)
381 const int vstep = geom->font_size * 2;
384 const int swatch = 20;
385 const int legend_top = geom->axis[SCALE_ORDINATE].data_max;
386 const int legend_bottom = legend_top -
387 (vstep * geom->n_datasets + 2 * ypad );
391 cairo_rectangle (cr, geom->legend_left, legend_top,
392 geom->legend_right - xpad - geom->legend_left,
393 legend_bottom - legend_top);
396 for (i = 0 ; i < geom->n_datasets ; ++i )
398 const int ypos = legend_top - vstep * (i + 1);
399 const int xpos = geom->legend_left + xpad;
400 const struct xrchart_colour *colour;
402 cairo_move_to (cr, xpos, ypos);
405 colour = &data_colour [ i % XRCHART_N_COLOURS];
406 cairo_set_source_rgb (cr,
408 colour->green / 255.0,
409 colour->blue / 255.0);
410 cairo_rectangle (cr, xpos, ypos, swatch, swatch);
411 cairo_fill_preserve (cr);
415 cairo_move_to (cr, xpos + swatch * 1.5, ypos);
416 xrchart_label (cr, 'l', 'x', geom->font_size, geom->dataset[i]);
422 /* Start a new vector called NAME */
424 xrchart_vector_start (cairo_t *cr, struct xrchart_geometry *geom, const char *name)
426 const struct xrchart_colour *colour;
430 colour = &data_colour[geom->n_datasets % XRCHART_N_COLOURS];
431 cairo_set_source_rgb (cr,
433 colour->green / 255.0,
434 colour->blue / 255.0);
437 geom->dataset = xrealloc (geom->dataset,
438 geom->n_datasets * sizeof (*geom->dataset));
440 geom->dataset[geom->n_datasets - 1] = strdup (name);
443 /* Plot a data point */
445 xrchart_datum (cairo_t *cr, const struct xrchart_geometry *geom,
446 int dataset UNUSED, double x, double y)
448 double x_pos = (x - geom->axis[SCALE_ABSCISSA].min) * geom->axis[SCALE_ABSCISSA].scale + geom->axis[SCALE_ABSCISSA].data_min;
449 double y_pos = (y - geom->axis[SCALE_ORDINATE].min) * geom->axis[SCALE_ORDINATE].scale + geom->axis[SCALE_ORDINATE].data_min;
451 xrchart_draw_marker (cr, x_pos, y_pos, XRMARKER_SQUARE, 15);
455 xrchart_vector_end (cairo_t *cr, struct xrchart_geometry *geom)
459 geom->in_path = false;
462 /* Plot a data point */
464 xrchart_vector (cairo_t *cr, struct xrchart_geometry *geom, double x, double y)
467 (x - geom->axis[SCALE_ABSCISSA].min) * geom->axis[SCALE_ABSCISSA].scale + geom->axis[SCALE_ABSCISSA].data_min ;
470 (y - geom->axis[SCALE_ORDINATE].min) * geom->axis[SCALE_ORDINATE].scale + geom->axis[SCALE_ORDINATE].data_min ;
473 cairo_line_to (cr, x_pos, y_pos);
476 cairo_move_to (cr, x_pos, y_pos);
477 geom->in_path = true;
483 /* Draw a line with slope SLOPE and intercept INTERCEPT.
484 between the points limit1 and limit2.
485 If lim_dim is XRCHART_DIM_Y then the limit{1,2} are on the
486 y axis otherwise the x axis
489 xrchart_line(cairo_t *cr, const struct xrchart_geometry *geom,
490 double slope, double intercept,
491 double limit1, double limit2, enum xrchart_dim lim_dim)
496 if ( lim_dim == XRCHART_DIM_Y )
498 x1 = ( limit1 - intercept ) / slope;
499 x2 = ( limit2 - intercept ) / slope;
507 y1 = slope * x1 + intercept;
508 y2 = slope * x2 + intercept;
511 y1 = (y1 - geom->axis[SCALE_ORDINATE].min) * geom->axis[SCALE_ORDINATE].scale + geom->axis[SCALE_ORDINATE].data_min;
512 y2 = (y2 - geom->axis[SCALE_ORDINATE].min) * geom->axis[SCALE_ORDINATE].scale + geom->axis[SCALE_ORDINATE].data_min;
513 x1 = (x1 - geom->axis[SCALE_ABSCISSA].min) * geom->axis[SCALE_ABSCISSA].scale + geom->axis[SCALE_ABSCISSA].data_min;
514 x2 = (x2 - geom->axis[SCALE_ABSCISSA].min) * geom->axis[SCALE_ABSCISSA].scale + geom->axis[SCALE_ABSCISSA].data_min;
516 cairo_move_to (cr, x1, y1);
517 cairo_line_to (cr, x2, y2);