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,
92 PangoFontDescription *desc;
96 desc = pango_font_description_from_string ("sans serif");
102 pango_font_description_set_absolute_size (desc, 15 * 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 != 't')
130 pango_layout_get_size (layout, NULL, &height_pango);
131 height = (double) height_pango / PANGO_SCALE;
132 if (vert_justify == 'b' || vert_justify == 'x')
133 cairo_rel_move_to (cr, 0, -height);
134 else if (vert_justify == 'c')
135 cairo_rel_move_to (cr, 0, -height / 2.0);
137 pango_cairo_show_layout (cr, layout);
138 g_object_unref (layout);
144 pango_font_description_free (desc);
147 /* Draw a tick mark at position
148 If label is non zero, then print it at the tick mark
151 draw_tick (cairo_t *cr, const struct chart_geometry *geom,
152 enum tick_orientation orientation,
154 const char *label, ...)
156 const int tickSize = 10;
159 cairo_move_to (cr, geom->data_left, geom->data_bottom);
161 if (orientation == TICK_ABSCISSA)
163 cairo_rel_move_to (cr, position, 0);
164 cairo_rel_line_to (cr, 0, -tickSize);
166 else if (orientation == TICK_ORDINATE)
168 cairo_rel_move_to (cr, 0, position);
169 cairo_rel_line_to (cr, -tickSize, 0);
173 cairo_get_current_point (cr, &x, &y);
182 cairo_move_to (cr, x, y);
184 va_start (ap, label);
185 s = xvasprintf (label, ap);
186 if (orientation == TICK_ABSCISSA)
187 chart_label (cr, 'c', 't', s);
188 else if (orientation == TICK_ORDINATE)
190 if (fabs (position) < DBL_EPSILON)
191 cairo_rel_move_to (cr, 0, 10);
192 chart_label (cr, 'r', 'c', s);
200 /* Write the title on a chart*/
202 chart_write_title (cairo_t *cr, const struct chart_geometry *geom,
203 const char *title, ...)
209 // pl_ffontsize_r (cr, geom->font_size * 1.5); /* XXX */
210 cairo_move_to (cr, geom->data_left, geom->title_bottom);
213 s = xvasprintf (title, ap);
214 chart_label (cr, 'l', 'x', s);
222 /* Set the scale for the abscissa */
224 chart_write_xscale (cairo_t *cr, struct chart_geometry *geom,
225 double min, double max, int ticks)
229 const double tick_interval =
230 chart_rounded_tick ((max - min) / (double) ticks);
232 geom->x_max = ceil (max / tick_interval) * tick_interval;
233 geom->x_min = floor (min / tick_interval) * tick_interval;
234 geom->abscissa_scale = fabs(geom->data_right - geom->data_left) /
235 fabs(geom->x_max - geom->x_min);
237 for (x = geom->x_min; x <= geom->x_max; x += tick_interval)
238 draw_tick (cr, geom, TICK_ABSCISSA,
239 (x - geom->x_min) * geom->abscissa_scale, "%g", x);
243 /* Set the scale for the ordinate */
245 chart_write_yscale (cairo_t *cr, struct chart_geometry *geom,
246 double smin, double smax, int ticks)
250 const double tick_interval =
251 chart_rounded_tick ((smax - smin) / (double) ticks);
253 geom->y_max = ceil (smax / tick_interval) * tick_interval;
254 geom->y_min = floor (smin / tick_interval) * tick_interval;
256 geom->ordinate_scale =
257 (fabs (geom->data_top - geom->data_bottom)
258 / fabs (geom->y_max - geom->y_min));
260 for (y = geom->y_min; y <= geom->y_max; y += tick_interval)
261 draw_tick (cr, geom, TICK_ORDINATE,
262 (y - geom->y_min) * geom->ordinate_scale, "%g", y);
265 /* Write the abscissa label */
267 chart_write_xlabel (cairo_t *cr, const struct chart_geometry *geom,
270 cairo_move_to (cr, geom->data_left, geom->abscissa_top);
271 chart_label (cr, 'l', 't', label);
274 /* Write the ordinate label */
276 chart_write_ylabel (cairo_t *cr, const struct chart_geometry *geom,
280 cairo_translate (cr, -geom->data_bottom, -geom->ordinate_right);
281 cairo_move_to (cr, 0, 0);
282 cairo_rotate (cr, M_PI / 2.0);
283 chart_label (cr, 'l', 'x', label);