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 const struct chart_colour data_colour[N_CHART_COLOURS] =
44 { 165, 42, 42 }, /* brown */
45 { 255, 0, 0 }, /* red */
46 { 255, 165, 0 }, /* orange */
47 { 255, 255, 0 }, /* yellow */
48 { 0, 255, 0 }, /* green */
49 { 0, 0, 255 }, /* blue */
50 { 238, 130, 238 }, /* violet */
51 { 190, 190, 190 }, /* grey */
52 { 255, 192, 203 }, /* pink */
56 chart_draw_marker (cairo_t *cr, double x, double y, enum marker_type marker,
60 cairo_translate (cr, x, y);
61 cairo_scale (cr, size / 2.0, size / 2.0);
62 cairo_set_line_width (cr, cairo_get_line_width (cr) / (size / 2.0));
66 cairo_arc (cr, 0, 0, 1.0, 0, 2 * M_PI);
71 cairo_move_to (cr, 0, -1.0); /* | */
72 cairo_line_to (cr, 0, 1.0);
73 cairo_move_to (cr, -M_SQRT1_2, -M_SQRT1_2); /* / */
74 cairo_line_to (cr, M_SQRT1_2, M_SQRT1_2);
75 cairo_move_to (cr, -M_SQRT1_2, M_SQRT1_2); /* \ */
76 cairo_line_to (cr, M_SQRT1_2, -M_SQRT1_2);
81 cairo_rectangle (cr, -1.0, -1.0, 2.0, 2.0);
89 chart_label (cairo_t *cr, int horz_justify, int vert_justify, double font_size,
92 PangoFontDescription *desc;
96 desc = pango_font_description_from_string ("sans serif");
102 pango_font_description_set_absolute_size (desc, font_size * PANGO_SCALE);
105 cairo_get_current_point (cr, &x, &y);
106 cairo_translate (cr, x, y);
107 cairo_move_to (cr, 0, 0);
108 cairo_scale (cr, 1.0, -1.0);
110 layout = pango_cairo_create_layout (cr);
111 pango_layout_set_font_description (layout, desc);
112 pango_layout_set_text (layout, string, -1);
113 if (horz_justify != 'l')
118 pango_layout_get_size (layout, &width_pango, NULL);
119 width = (double) width_pango / PANGO_SCALE;
120 if (horz_justify == 'r')
121 cairo_rel_move_to (cr, -width, 0);
123 cairo_rel_move_to (cr, -width / 2.0, 0);
125 if (vert_justify == 'x')
127 int baseline_pango = pango_layout_get_baseline (layout);
128 double baseline = (double) baseline_pango / PANGO_SCALE;
129 cairo_rel_move_to (cr, 0, -baseline);
131 else if (vert_justify != 't')
136 pango_layout_get_size (layout, NULL, &height_pango);
137 height = (double) height_pango / PANGO_SCALE;
138 if (vert_justify == 'b')
139 cairo_rel_move_to (cr, 0, -height);
140 else if (vert_justify == 'c')
141 cairo_rel_move_to (cr, 0, -height / 2.0);
143 pango_cairo_show_layout (cr, layout);
144 g_object_unref (layout);
150 pango_font_description_free (desc);
153 /* Draw a tick mark at position
154 If label is non zero, then print it at the tick mark
157 draw_tick (cairo_t *cr, const struct chart_geometry *geom,
158 enum tick_orientation orientation,
160 const char *label, ...)
162 const int tickSize = 10;
165 cairo_move_to (cr, geom->data_left, geom->data_bottom);
167 if (orientation == TICK_ABSCISSA)
169 cairo_rel_move_to (cr, position, 0);
170 cairo_rel_line_to (cr, 0, -tickSize);
172 else if (orientation == TICK_ORDINATE)
174 cairo_rel_move_to (cr, 0, position);
175 cairo_rel_line_to (cr, -tickSize, 0);
179 cairo_get_current_point (cr, &x, &y);
188 cairo_move_to (cr, x, y);
190 va_start (ap, label);
191 s = xvasprintf (label, ap);
192 if (orientation == TICK_ABSCISSA)
193 chart_label (cr, 'c', 't', geom->font_size, s);
194 else if (orientation == TICK_ORDINATE)
196 if (fabs (position) < DBL_EPSILON)
197 cairo_rel_move_to (cr, 0, 10);
198 chart_label (cr, 'r', 'c', geom->font_size, s);
206 /* Write the title on a chart*/
208 chart_write_title (cairo_t *cr, const struct chart_geometry *geom,
209 const char *title, ...)
215 cairo_move_to (cr, geom->data_left, geom->title_bottom);
218 s = xvasprintf (title, ap);
219 chart_label (cr, 'l', 'x', geom->font_size * 1.5, s);
227 /* Set the scale for the abscissa */
229 chart_write_xscale (cairo_t *cr, struct chart_geometry *geom,
230 double min, double max, int ticks)
234 const double tick_interval =
235 chart_rounded_tick ((max - min) / (double) ticks);
237 geom->x_max = ceil (max / tick_interval) * tick_interval;
238 geom->x_min = floor (min / tick_interval) * tick_interval;
239 geom->abscissa_scale = fabs(geom->data_right - geom->data_left) /
240 fabs(geom->x_max - geom->x_min);
242 for (x = geom->x_min; x <= geom->x_max; x += tick_interval)
243 draw_tick (cr, geom, TICK_ABSCISSA,
244 (x - geom->x_min) * geom->abscissa_scale, "%g", x);
248 /* Set the scale for the ordinate */
250 chart_write_yscale (cairo_t *cr, struct chart_geometry *geom,
251 double smin, double smax, int ticks)
255 const double tick_interval =
256 chart_rounded_tick ((smax - smin) / (double) ticks);
258 geom->y_max = ceil (smax / tick_interval) * tick_interval;
259 geom->y_min = floor (smin / tick_interval) * tick_interval;
261 geom->ordinate_scale =
262 (fabs (geom->data_top - geom->data_bottom)
263 / fabs (geom->y_max - geom->y_min));
265 for (y = geom->y_min; y <= geom->y_max; y += tick_interval)
266 draw_tick (cr, geom, TICK_ORDINATE,
267 (y - geom->y_min) * geom->ordinate_scale, "%g", y);
270 /* Write the abscissa label */
272 chart_write_xlabel (cairo_t *cr, const struct chart_geometry *geom,
275 cairo_move_to (cr, geom->data_left, geom->abscissa_top);
276 chart_label (cr, 'l', 't', geom->font_size, label);
279 /* Write the ordinate label */
281 chart_write_ylabel (cairo_t *cr, const struct chart_geometry *geom,
285 cairo_translate (cr, -geom->data_bottom, -geom->ordinate_right);
286 cairo_move_to (cr, 0, 0);
287 cairo_rotate (cr, M_PI / 2.0);
288 chart_label (cr, 'l', 'x', geom->font_size, label);
294 chart_write_legend (cairo_t *cr, const struct chart_geometry *geom)
297 const int vstep = geom->font_size * 2;
300 const int swatch = 20;
301 const int legend_top = geom->data_top;
302 const int legend_bottom = legend_top -
303 (vstep * geom->n_datasets + 2 * ypad );
307 cairo_rectangle (cr, geom->legend_left, legend_top,
308 geom->legend_right - xpad - geom->legend_left,
309 legend_bottom - legend_top);
312 for (i = 0 ; i < geom->n_datasets ; ++i )
314 const int ypos = legend_top - vstep * (i + 1);
315 const int xpos = geom->legend_left + xpad;
316 const struct chart_colour *colour;
318 cairo_move_to (cr, xpos, ypos);
321 colour = &data_colour [ i % N_CHART_COLOURS];
322 cairo_set_source_rgb (cr,
324 colour->green / 255.0,
325 colour->blue / 255.0);
326 cairo_rectangle (cr, xpos, ypos, swatch, swatch);
327 cairo_fill_preserve (cr);
331 cairo_move_to (cr, xpos + swatch * 1.5, ypos);
332 chart_label (cr, 'l', 'x', geom->font_size, geom->dataset[i]);