Delete tab_raw function and tab_alloc macro.
[pspp] / src / output / charts / plot-chart.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2004, 2009 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>. */
16
17 #include <config.h>
18
19 #include <output/charts/plot-chart.h>
20
21 #include <assert.h>
22 #include <float.h>
23 #include <math.h>
24 #include <pango/pango-font.h>
25 #include <pango/pango-layout.h>
26 #include <pango/pango.h>
27 #include <pango/pangocairo.h>
28 #include <stdarg.h>
29 #include <stdio.h>
30 #include <stdio.h>
31 #include <string.h>
32
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>
39
40 #include "xalloc.h"
41
42 const struct chart_colour data_colour[N_CHART_COLOURS] =
43   {
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 */
53   };
54
55 void
56 chart_draw_marker (cairo_t *cr, double x, double y, enum marker_type marker,
57                    double size)
58 {
59   cairo_save (cr);
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));
63   switch (marker)
64     {
65     case MARKER_CIRCLE:
66       cairo_arc (cr, 0, 0, 1.0, 0, 2 * M_PI);
67       cairo_stroke (cr);
68       break;
69
70     case MARKER_ASTERISK:
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);
77       cairo_stroke (cr);
78       break;
79
80     case MARKER_SQUARE:
81       cairo_rectangle (cr, -1.0, -1.0, 2.0, 2.0);
82       cairo_stroke (cr);
83       break;
84     }
85   cairo_restore (cr);
86 }
87
88 void
89 chart_label (cairo_t *cr, int horz_justify, int vert_justify, double font_size,
90              const char *string)
91 {
92   PangoFontDescription *desc;
93   PangoLayout *layout;
94   double x, y;
95
96   desc = pango_font_description_from_string ("sans serif");
97   if (desc == NULL)
98     {
99       cairo_new_path (cr);
100       return;
101     }
102   pango_font_description_set_absolute_size (desc, font_size * PANGO_SCALE);
103
104   cairo_save (cr);
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);
109
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')
114     {
115       int width_pango;
116       double width;
117
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);
122       else
123         cairo_rel_move_to (cr, -width / 2.0, 0);
124     }
125   if (vert_justify == 'x')
126     {
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);
130     }
131   else if (vert_justify != 't')
132     {
133       int height_pango;
134       double height;
135
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);
142     }
143   pango_cairo_show_layout (cr, layout);
144   g_object_unref (layout);
145
146   cairo_restore (cr);
147
148   cairo_new_path (cr);
149
150   pango_font_description_free (desc);
151 }
152
153 /* Draw a tick mark at position
154    If label is non zero, then print it at the tick mark
155 */
156 void
157 draw_tick (cairo_t *cr, const struct chart_geometry *geom,
158            enum tick_orientation orientation,
159            double position,
160            const char *label, ...)
161 {
162   const int tickSize = 10;
163   double x, y;
164
165   cairo_move_to (cr, geom->data_left, geom->data_bottom);
166
167   if (orientation == TICK_ABSCISSA)
168     {
169       cairo_rel_move_to (cr, position, 0);
170       cairo_rel_line_to (cr, 0, -tickSize);
171     }
172   else if (orientation == TICK_ORDINATE)
173     {
174       cairo_rel_move_to (cr, 0, position);
175       cairo_rel_line_to (cr, -tickSize, 0);
176     }
177   else
178     NOT_REACHED ();
179   cairo_get_current_point (cr, &x, &y);
180
181   cairo_stroke (cr);
182
183   if (label != NULL)
184     {
185       va_list ap;
186       char *s;
187
188       cairo_move_to (cr, x, y);
189
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)
195         {
196           if (fabs (position) < DBL_EPSILON)
197             cairo_rel_move_to (cr, 0, 10);
198           chart_label (cr, 'r', 'c', geom->font_size, s);
199         }
200       free (s);
201       va_end (ap);
202     }
203 }
204
205
206 /* Write the title on a chart*/
207 void
208 chart_write_title (cairo_t *cr, const struct chart_geometry *geom,
209                    const char *title, ...)
210 {
211   va_list ap;
212   char *s;
213
214   cairo_save (cr);
215   cairo_move_to (cr, geom->data_left, geom->title_bottom);
216
217   va_start(ap, title);
218   s = xvasprintf (title, ap);
219   chart_label (cr, 'l', 'x', geom->font_size * 1.5, s);
220   free (s);
221   va_end (ap);
222
223   cairo_restore (cr);
224 }
225
226
227 /* Set the scale for the abscissa */
228 void
229 chart_write_xscale (cairo_t *cr, struct chart_geometry *geom,
230                     double min, double max, int ticks)
231 {
232   double x;
233
234   const double tick_interval =
235     chart_rounded_tick ((max - min) / (double) ticks);
236
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);
241
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);
245 }
246
247
248 /* Set the scale for the ordinate */
249 void
250 chart_write_yscale (cairo_t *cr, struct chart_geometry *geom,
251                     double smin, double smax, int ticks)
252 {
253   double y;
254
255   const double tick_interval =
256     chart_rounded_tick ((smax - smin) / (double) ticks);
257
258   geom->y_max = ceil (smax / tick_interval) * tick_interval;
259   geom->y_min = floor (smin / tick_interval) * tick_interval;
260
261   geom->ordinate_scale =
262     (fabs (geom->data_top - geom->data_bottom)
263      / fabs (geom->y_max - geom->y_min));
264
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);
268 }
269
270 /* Write the abscissa label */
271 void
272 chart_write_xlabel (cairo_t *cr, const struct chart_geometry *geom,
273                     const char *label)
274 {
275   cairo_move_to (cr, geom->data_left, geom->abscissa_top);
276   chart_label (cr, 'l', 't', geom->font_size, label);
277 }
278
279 /* Write the ordinate label */
280 void
281 chart_write_ylabel (cairo_t *cr, const struct chart_geometry *geom,
282                     const char *label)
283 {
284   cairo_save (cr);
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);
289   cairo_restore (cr);
290 }