1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2004, 2009 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/charts/plot-chart.h>
24 #include <pango/pango-font.h>
25 #include <pango/pango-layout.h>
26 #include <pango/pango.h>
27 #include <pango/pangocairo.h>
33 #include <libpspp/assertion.h>
34 #include <libpspp/str.h>
35 #include <math/chart-geometry.h>
36 #include <output/chart-provider.h>
37 #include <output/manager.h>
38 #include <output/output.h>
42 #if ! PANGO_VERSION_CHECK (2, 22, 0)
43 int pango_layout_get_baseline (PangoLayout *layout);
45 /* Shamelessly copied from the pango source */
47 pango_layout_get_baseline (PangoLayout *layout)
51 /* XXX this is so inefficient */
52 PangoLayoutIter *iter = pango_layout_get_iter (layout);
53 baseline = pango_layout_iter_get_baseline (iter);
54 pango_layout_iter_free (iter);
62 const struct chart_colour data_colour[N_CHART_COLOURS] =
64 { 165, 42, 42 }, /* brown */
65 { 255, 0, 0 }, /* red */
66 { 255, 165, 0 }, /* orange */
67 { 255, 255, 0 }, /* yellow */
68 { 0, 255, 0 }, /* green */
69 { 0, 0, 255 }, /* blue */
70 { 238, 130, 238 }, /* violet */
71 { 190, 190, 190 }, /* grey */
72 { 255, 192, 203 }, /* pink */
76 chart_draw_marker (cairo_t *cr, double x, double y, enum marker_type marker,
80 cairo_translate (cr, x, y);
81 cairo_scale (cr, size / 2.0, size / 2.0);
82 cairo_set_line_width (cr, cairo_get_line_width (cr) / (size / 2.0));
86 cairo_arc (cr, 0, 0, 1.0, 0, 2 * M_PI);
91 cairo_move_to (cr, 0, -1.0); /* | */
92 cairo_line_to (cr, 0, 1.0);
93 cairo_move_to (cr, -M_SQRT1_2, -M_SQRT1_2); /* / */
94 cairo_line_to (cr, M_SQRT1_2, M_SQRT1_2);
95 cairo_move_to (cr, -M_SQRT1_2, M_SQRT1_2); /* \ */
96 cairo_line_to (cr, M_SQRT1_2, -M_SQRT1_2);
101 cairo_rectangle (cr, -1.0, -1.0, 2.0, 2.0);
109 chart_label (cairo_t *cr, int horz_justify, int vert_justify, double font_size,
112 PangoFontDescription *desc;
116 desc = pango_font_description_from_string ("sans serif");
122 pango_font_description_set_absolute_size (desc, font_size * PANGO_SCALE);
125 cairo_get_current_point (cr, &x, &y);
126 cairo_translate (cr, x, y);
127 cairo_move_to (cr, 0, 0);
128 cairo_scale (cr, 1.0, -1.0);
130 layout = pango_cairo_create_layout (cr);
131 pango_layout_set_font_description (layout, desc);
132 pango_layout_set_text (layout, string, -1);
133 if (horz_justify != 'l')
138 pango_layout_get_size (layout, &width_pango, NULL);
139 width = (double) width_pango / PANGO_SCALE;
140 if (horz_justify == 'r')
141 cairo_rel_move_to (cr, -width, 0);
143 cairo_rel_move_to (cr, -width / 2.0, 0);
145 if (vert_justify == 'x')
147 int baseline_pango = pango_layout_get_baseline (layout);
148 double baseline = (double) baseline_pango / PANGO_SCALE;
149 cairo_rel_move_to (cr, 0, -baseline);
151 else if (vert_justify != 't')
156 pango_layout_get_size (layout, NULL, &height_pango);
157 height = (double) height_pango / PANGO_SCALE;
158 if (vert_justify == 'b')
159 cairo_rel_move_to (cr, 0, -height);
160 else if (vert_justify == 'c')
161 cairo_rel_move_to (cr, 0, -height / 2.0);
163 pango_cairo_show_layout (cr, layout);
164 g_object_unref (layout);
170 pango_font_description_free (desc);
173 /* Draw a tick mark at position
174 If label is non zero, then print it at the tick mark
177 draw_tick (cairo_t *cr, const struct chart_geometry *geom,
178 enum tick_orientation orientation,
180 const char *label, ...)
182 const int tickSize = 10;
185 cairo_move_to (cr, geom->data_left, geom->data_bottom);
187 if (orientation == TICK_ABSCISSA)
189 cairo_rel_move_to (cr, position, 0);
190 cairo_rel_line_to (cr, 0, -tickSize);
192 else if (orientation == TICK_ORDINATE)
194 cairo_rel_move_to (cr, 0, position);
195 cairo_rel_line_to (cr, -tickSize, 0);
199 cairo_get_current_point (cr, &x, &y);
208 cairo_move_to (cr, x, y);
210 va_start (ap, label);
211 s = xvasprintf (label, ap);
212 if (orientation == TICK_ABSCISSA)
213 chart_label (cr, 'c', 't', geom->font_size, s);
214 else if (orientation == TICK_ORDINATE)
216 if (fabs (position) < DBL_EPSILON)
217 cairo_rel_move_to (cr, 0, 10);
218 chart_label (cr, 'r', 'c', geom->font_size, s);
226 /* Write the title on a chart*/
228 chart_write_title (cairo_t *cr, const struct chart_geometry *geom,
229 const char *title, ...)
235 cairo_move_to (cr, geom->data_left, geom->title_bottom);
238 s = xvasprintf (title, ap);
239 chart_label (cr, 'l', 'x', geom->font_size * 1.5, s);
247 /* Set the scale for the abscissa */
249 chart_write_xscale (cairo_t *cr, struct chart_geometry *geom,
250 double min, double max, int ticks)
254 const double tick_interval =
255 chart_rounded_tick ((max - min) / (double) ticks);
257 geom->x_max = ceil (max / tick_interval) * tick_interval;
258 geom->x_min = floor (min / tick_interval) * tick_interval;
259 geom->abscissa_scale = fabs(geom->data_right - geom->data_left) /
260 fabs(geom->x_max - geom->x_min);
262 for (x = geom->x_min; x <= geom->x_max; x += tick_interval)
263 draw_tick (cr, geom, TICK_ABSCISSA,
264 (x - geom->x_min) * geom->abscissa_scale, "%g", x);
268 /* Set the scale for the ordinate */
270 chart_write_yscale (cairo_t *cr, struct chart_geometry *geom,
271 double smin, double smax, int ticks)
275 const double tick_interval =
276 chart_rounded_tick ((smax - smin) / (double) ticks);
278 geom->y_max = ceil (smax / tick_interval) * tick_interval;
279 geom->y_min = floor (smin / tick_interval) * tick_interval;
281 geom->ordinate_scale =
282 (fabs (geom->data_top - geom->data_bottom)
283 / fabs (geom->y_max - geom->y_min));
285 for (y = geom->y_min; y <= geom->y_max; y += tick_interval)
286 draw_tick (cr, geom, TICK_ORDINATE,
287 (y - geom->y_min) * geom->ordinate_scale, "%g", y);
290 /* Write the abscissa label */
292 chart_write_xlabel (cairo_t *cr, const struct chart_geometry *geom,
295 cairo_move_to (cr, geom->data_left, geom->abscissa_top);
296 chart_label (cr, 'l', 't', geom->font_size, label);
299 /* Write the ordinate label */
301 chart_write_ylabel (cairo_t *cr, const struct chart_geometry *geom,
305 cairo_translate (cr, -geom->data_bottom, -geom->ordinate_right);
306 cairo_move_to (cr, 0, 0);
307 cairo_rotate (cr, M_PI / 2.0);
308 chart_label (cr, 'l', 'x', geom->font_size, label);
314 chart_write_legend (cairo_t *cr, const struct chart_geometry *geom)
317 const int vstep = geom->font_size * 2;
320 const int swatch = 20;
321 const int legend_top = geom->data_top;
322 const int legend_bottom = legend_top -
323 (vstep * geom->n_datasets + 2 * ypad );
327 cairo_rectangle (cr, geom->legend_left, legend_top,
328 geom->legend_right - xpad - geom->legend_left,
329 legend_bottom - legend_top);
332 for (i = 0 ; i < geom->n_datasets ; ++i )
334 const int ypos = legend_top - vstep * (i + 1);
335 const int xpos = geom->legend_left + xpad;
336 const struct chart_colour *colour;
338 cairo_move_to (cr, xpos, ypos);
341 colour = &data_colour [ i % N_CHART_COLOURS];
342 cairo_set_source_rgb (cr,
344 colour->green / 255.0,
345 colour->blue / 255.0);
346 cairo_rectangle (cr, xpos, ypos, swatch, swatch);
347 cairo_fill_preserve (cr);
351 cairo_move_to (cr, xpos + swatch * 1.5, ypos);
352 chart_label (cr, 'l', 'x', geom->font_size, geom->dataset[i]);