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 chartetry. */
50 geom->data_top = 0.900 * length;
51 geom->data_right = 0.800 * width;
52 geom->data_bottom = 0.120 * length;
53 geom->data_left = 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;
64 geom->fill_colour.red = 255;
65 geom->fill_colour.green = 0;
66 geom->fill_colour.blue = 0;
68 cairo_set_line_width (cr, 1.0);
70 cairo_rectangle (cr, geom->data_left, geom->data_bottom,
71 geom->data_right - geom->data_left,
72 geom->data_top - geom->data_bottom);
77 xrchart_geometry_free (cairo_t *cr UNUSED, struct xrchart_geometry *geom)
81 for (i = 0 ; i < geom->n_datasets; ++i)
82 free (geom->dataset[i]);
86 #if ! PANGO_VERSION_CHECK (1, 22, 0)
87 int pango_layout_get_baseline (PangoLayout *layout);
89 /* Shamelessly copied from the pango source */
91 pango_layout_get_baseline (PangoLayout *layout)
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);
106 const struct xrchart_colour data_colour[XRCHART_N_COLOURS] =
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 */
120 xrchart_draw_marker (cairo_t *cr, double x, double y,
121 enum xrmarker_type marker, double size)
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));
129 case XRMARKER_CIRCLE:
130 cairo_arc (cr, 0, 0, 1.0, 0, 2 * M_PI);
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);
144 case XRMARKER_SQUARE:
145 cairo_rectangle (cr, -1.0, -1.0, 2.0, 2.0);
153 xrchart_label (cairo_t *cr, int horz_justify, int vert_justify,
154 double font_size, const char *string)
156 PangoFontDescription *desc;
160 desc = pango_font_description_from_string ("sans serif");
166 pango_font_description_set_absolute_size (desc, font_size * PANGO_SCALE);
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);
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')
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);
187 cairo_rel_move_to (cr, -width / 2.0, 0);
189 if (vert_justify == 'x')
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);
195 else if (vert_justify != 't')
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);
207 pango_cairo_show_layout (cr, layout);
208 g_object_unref (layout);
214 pango_font_description_free (desc);
217 /* Draw a tick mark at position
218 If label is non zero, then print it at the tick mark
221 draw_tick (cairo_t *cr, const struct xrchart_geometry *geom,
222 enum tick_orientation orientation,
224 const char *label, ...)
226 const int tickSize = 10;
229 cairo_move_to (cr, geom->data_left, geom->data_bottom);
231 if (orientation == TICK_ABSCISSA)
233 cairo_rel_move_to (cr, position, 0);
234 cairo_rel_line_to (cr, 0, -tickSize);
236 else if (orientation == TICK_ORDINATE)
238 cairo_rel_move_to (cr, 0, position);
239 cairo_rel_line_to (cr, -tickSize, 0);
243 cairo_get_current_point (cr, &x, &y);
252 cairo_move_to (cr, x, y);
254 va_start (ap, label);
255 s = xvasprintf (label, ap);
256 if (orientation == TICK_ABSCISSA)
257 xrchart_label (cr, 'c', 't', geom->font_size, s);
258 else if (orientation == TICK_ORDINATE)
260 if (fabs (position) < DBL_EPSILON)
261 cairo_rel_move_to (cr, 0, 10);
262 xrchart_label (cr, 'r', 'c', geom->font_size, s);
270 /* Write the title on a chart*/
272 xrchart_write_title (cairo_t *cr, const struct xrchart_geometry *geom,
273 const char *title, ...)
279 cairo_move_to (cr, geom->data_left, geom->title_bottom);
282 s = xvasprintf (title, ap);
283 xrchart_label (cr, 'l', 'x', geom->font_size * 1.5, s);
291 /* Set the scale for the abscissa */
293 xrchart_write_xscale (cairo_t *cr, struct xrchart_geometry *geom,
294 double min, double max, int ticks)
298 const double tick_interval =
299 chart_rounded_tick ((max - min) / (double) ticks);
301 geom->x_max = ceil (max / tick_interval) * tick_interval;
302 geom->x_min = floor (min / tick_interval) * tick_interval;
303 geom->abscissa_scale = fabs(geom->data_right - geom->data_left) /
304 fabs(geom->x_max - geom->x_min);
306 for (x = geom->x_min; x <= geom->x_max; x += tick_interval)
307 draw_tick (cr, geom, TICK_ABSCISSA,
308 (x - geom->x_min) * geom->abscissa_scale, "%g", x);
312 /* Set the scale for the ordinate */
314 xrchart_write_yscale (cairo_t *cr, struct xrchart_geometry *geom,
315 double smin, double smax, int ticks)
319 const double tick_interval =
320 chart_rounded_tick ((smax - smin) / (double) ticks);
322 geom->y_max = ceil (smax / tick_interval) * tick_interval;
323 geom->y_min = floor (smin / tick_interval) * tick_interval;
325 geom->ordinate_scale =
326 (fabs (geom->data_top - geom->data_bottom)
327 / fabs (geom->y_max - geom->y_min));
329 for (y = geom->y_min; y <= geom->y_max; y += tick_interval)
330 draw_tick (cr, geom, TICK_ORDINATE,
331 (y - geom->y_min) * geom->ordinate_scale, "%g", y);
334 /* Write the abscissa label */
336 xrchart_write_xlabel (cairo_t *cr, const struct xrchart_geometry *geom,
339 cairo_move_to (cr, geom->data_left, geom->abscissa_top);
340 xrchart_label (cr, 'l', 't', geom->font_size, label);
343 /* Write the ordinate label */
345 xrchart_write_ylabel (cairo_t *cr, const struct xrchart_geometry *geom,
349 cairo_translate (cr, -geom->data_bottom, -geom->ordinate_right);
350 cairo_move_to (cr, 0, 0);
351 cairo_rotate (cr, M_PI / 2.0);
352 xrchart_label (cr, 'l', 'x', geom->font_size, label);
358 xrchart_write_legend (cairo_t *cr, const struct xrchart_geometry *geom)
361 const int vstep = geom->font_size * 2;
364 const int swatch = 20;
365 const int legend_top = geom->data_top;
366 const int legend_bottom = legend_top -
367 (vstep * geom->n_datasets + 2 * ypad );
371 cairo_rectangle (cr, geom->legend_left, legend_top,
372 geom->legend_right - xpad - geom->legend_left,
373 legend_bottom - legend_top);
376 for (i = 0 ; i < geom->n_datasets ; ++i )
378 const int ypos = legend_top - vstep * (i + 1);
379 const int xpos = geom->legend_left + xpad;
380 const struct xrchart_colour *colour;
382 cairo_move_to (cr, xpos, ypos);
385 colour = &data_colour [ i % XRCHART_N_COLOURS];
386 cairo_set_source_rgb (cr,
388 colour->green / 255.0,
389 colour->blue / 255.0);
390 cairo_rectangle (cr, xpos, ypos, swatch, swatch);
391 cairo_fill_preserve (cr);
395 cairo_move_to (cr, xpos + swatch * 1.5, ypos);
396 xrchart_label (cr, 'l', 'x', geom->font_size, geom->dataset[i]);
402 /* Start a new vector called NAME */
404 xrchart_vector_start (cairo_t *cr, struct xrchart_geometry *geom, const char *name)
406 const struct xrchart_colour *colour;
410 colour = &data_colour[geom->n_datasets % XRCHART_N_COLOURS];
411 cairo_set_source_rgb (cr,
413 colour->green / 255.0,
414 colour->blue / 255.0);
417 geom->dataset = xrealloc (geom->dataset,
418 geom->n_datasets * sizeof (*geom->dataset));
420 geom->dataset[geom->n_datasets - 1] = strdup (name);
423 /* Plot a data point */
425 xrchart_datum (cairo_t *cr, const struct xrchart_geometry *geom,
426 int dataset UNUSED, double x, double y)
428 double x_pos = (x - geom->x_min) * geom->abscissa_scale + geom->data_left;
429 double y_pos = (y - geom->y_min) * geom->ordinate_scale + geom->data_bottom;
431 xrchart_draw_marker (cr, x_pos, y_pos, XRMARKER_SQUARE, 15);
435 xrchart_vector_end (cairo_t *cr, struct xrchart_geometry *geom)
439 geom->in_path = false;
442 /* Plot a data point */
444 xrchart_vector (cairo_t *cr, struct xrchart_geometry *geom, double x, double y)
447 (x - geom->x_min) * geom->abscissa_scale + geom->data_left ;
450 (y - geom->y_min) * geom->ordinate_scale + geom->data_bottom ;
453 cairo_line_to (cr, x_pos, y_pos);
456 cairo_move_to (cr, x_pos, y_pos);
457 geom->in_path = true;
463 /* Draw a line with slope SLOPE and intercept INTERCEPT.
464 between the points limit1 and limit2.
465 If lim_dim is XRCHART_DIM_Y then the limit{1,2} are on the
466 y axis otherwise the x axis
469 xrchart_line(cairo_t *cr, const struct xrchart_geometry *geom,
470 double slope, double intercept,
471 double limit1, double limit2, enum xrchart_dim lim_dim)
476 if ( lim_dim == XRCHART_DIM_Y )
478 x1 = ( limit1 - intercept ) / slope;
479 x2 = ( limit2 - intercept ) / slope;
487 y1 = slope * x1 + intercept;
488 y2 = slope * x2 + intercept;
491 y1 = (y1 - geom->y_min) * geom->ordinate_scale + geom->data_bottom;
492 y2 = (y2 - geom->y_min) * geom->ordinate_scale + geom->data_bottom;
493 x1 = (x1 - geom->x_min) * geom->abscissa_scale + geom->data_left;
494 x2 = (x2 - geom->x_min) * geom->abscissa_scale + geom->data_left;
496 cairo_move_to (cr, x1, y1);
497 cairo_line_to (cr, x2, y2);