1 /* acre - A cairo-based library for creating plots and charts.
3 * Copyright © 2009 Carl Worth <cworth@cworth.org>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20 #define _ISOC99_SOURCE /* for round() */
21 #define _XOPEN_SOURCE 500
29 typedef struct _acre_data_point_2d {
32 } acre_data_point_2d_t;
37 acre_data_point_2d_t *points;
38 unsigned int points_size;
39 unsigned int num_points;
42 typedef struct _acre_axis {
54 unsigned int data_size;
55 unsigned int num_data;
57 /* Data for drawing. */
60 /* Total size including labels. */
64 /* Position and size of chart alone. */
68 /* Create a new, empty plot. */
74 acre = xmalloc (sizeof (acre_t));
78 acre->x_axis.label = NULL;
79 acre->x_axis.min = 0.0;
80 acre->x_axis.max = 0.0;
82 acre->y_axis.label = NULL;
83 acre->y_axis.min = 0.0;
84 acre->y_axis.max = 0.0;
95 acre_destroy (acre_t *acre)
100 free (acre->x_axis.label);
101 free (acre->y_axis.label);
103 for (i = 0; i < acre->num_data; i++)
104 acre_data_destroy (acre->data[i]);
112 acre_set_title (acre_t *acre, const char *title)
116 acre->title = strdup (title);
120 acre_set_x_axis_label (acre_t *acre, const char *label)
122 free (acre->x_axis.label);
124 acre->x_axis.label = strdup (label);
128 acre_set_y_axis_label (acre_t *acre, const char *label)
130 free (acre->y_axis.label);
132 acre->y_axis.label = strdup (label);
135 /* Add a dataset to the plot. The plot assumes ownership of the
136 * dataset so it is not necessary to call acre_data_destroy on it. */
138 acre_add_data (acre_t *acre, acre_data_t *data)
140 if (acre->num_data >= acre->data_size) {
141 acre->data_size *= 2;
142 if (acre->data_size == 0)
144 acre->data = xrealloc_ab (acre->data,
146 sizeof (acre_data_t *));
149 acre->data[acre->num_data] = data;
153 #define ACRE_FONT_FAMILY "sans"
154 #define ACRE_FONT_SIZE 12
155 #define ACRE_TITLE_FONT_SIZE 32
156 #define ACRE_PAD (ACRE_FONT_SIZE)
157 #define ACRE_TICK_SIZE 6
160 _draw_title_and_labels (acre_t *acre)
162 cairo_t *cr = acre->cr;
163 PangoFontDescription *acre_font, *title_font;
164 PangoLayout *title_layout, *x_axis_layout, *y_axis_layout;
165 int title_width, title_height;
166 int x_axis_width, x_axis_height;
167 int y_axis_width, y_axis_height;
168 PangoRectangle new_chart;
172 acre_font = pango_font_description_new ();
173 pango_font_description_set_family (acre_font, ACRE_FONT_FAMILY);
174 pango_font_description_set_absolute_size (acre_font,
175 ACRE_FONT_SIZE * PANGO_SCALE);
177 title_font = pango_font_description_new ();
178 pango_font_description_set_family (title_font, ACRE_FONT_FAMILY);
179 pango_font_description_set_absolute_size (title_font,
180 ACRE_TITLE_FONT_SIZE * PANGO_SCALE);
182 title_layout = pango_cairo_create_layout (cr);
183 pango_layout_set_font_description (title_layout, title_font);
184 pango_layout_set_text (title_layout, acre->title, -1);
185 pango_layout_set_alignment (title_layout, PANGO_ALIGN_CENTER);
187 x_axis_layout = pango_cairo_create_layout (cr);
188 pango_layout_set_font_description (x_axis_layout, acre_font);
189 pango_layout_set_text (x_axis_layout, acre->x_axis.label, -1);
190 pango_layout_set_alignment (x_axis_layout, PANGO_ALIGN_CENTER);
192 y_axis_layout = pango_cairo_create_layout (cr);
193 pango_layout_set_font_description (y_axis_layout, acre_font);
194 pango_layout_set_text (y_axis_layout, acre->y_axis.label, -1);
195 pango_layout_set_alignment (y_axis_layout, PANGO_ALIGN_CENTER);
197 /* Iterate with the layout of the title and axis labels until they
198 * are stable, (this requires iteration since we don't know what
199 * to set their widths to in advance due to the wrapping of the
200 * other elements). */
203 acre->chart.width = acre->width;
204 acre->chart.height = acre->height;
206 pango_layout_set_width (title_layout, acre->chart.width * PANGO_SCALE);
207 pango_layout_set_width (x_axis_layout, acre->chart.width * PANGO_SCALE);
208 pango_layout_set_width (y_axis_layout, acre->chart.height * PANGO_SCALE);
210 pango_layout_get_pixel_size (title_layout, &title_width, &title_height);
211 pango_layout_get_pixel_size (x_axis_layout, &x_axis_width, &x_axis_height);
212 pango_layout_get_pixel_size (y_axis_layout, &y_axis_width, &y_axis_height);
214 new_chart.x = ACRE_PAD + y_axis_height +
215 ACRE_PAD + ACRE_FONT_SIZE;
216 new_chart.width = acre->width - acre->chart.x - ACRE_PAD;
218 new_chart.y = ACRE_PAD + title_height + ACRE_PAD;
219 new_chart.height = acre->height - acre->chart.y - (ACRE_FONT_SIZE + ACRE_PAD + x_axis_height + ACRE_PAD);
221 if (new_chart.x == acre->chart.x &&
222 new_chart.y == acre->chart.y &&
223 new_chart.width == acre->chart.width &&
224 new_chart.height == acre->chart.height)
229 acre->chart.x = new_chart.x;
230 acre->chart.y = new_chart.y;
231 acre->chart.width = new_chart.width;
232 acre->chart.height = new_chart.height;
235 cairo_set_source_rgb (cr, 0, 0, 0);
237 cairo_move_to (cr, acre->chart.x, ACRE_PAD);
238 pango_cairo_show_layout (cr, title_layout);
242 cairo_translate (cr, ACRE_PAD, acre->chart.y + acre->chart.height);
243 cairo_rotate (cr, - M_PI / 2.0);
244 cairo_move_to (cr, 0, 0);
245 pango_cairo_show_layout (cr, y_axis_layout);
249 cairo_move_to (cr, acre->chart.x,
250 acre->chart.y + acre->chart.height +
251 ACRE_FONT_SIZE + ACRE_PAD);
252 pango_cairo_show_layout (cr, x_axis_layout);
257 /* For a given axis range, compute a step size (in data space) to
258 * generate a suitable number of ticks (5 or so). */
260 _step_for_range (double range)
262 double step, scale_factor;
264 /* We want roughly 5 major ticks for the chart. */
267 /* Normalize the step so we can easily snap it to a desirable
269 scale_factor = pow (10.0, floor (log10 (step)));
270 step /= scale_factor;
272 /* We want increments of 1, 2.5, 5, or 10 (times some power of
273 * 10). The threshold values between these are computed
274 * logarithmically. */
275 if (step < 3.535533905932738) {
276 if (step < 1.58113883008419)
281 if (step < 7.071067811865475)
287 /* Un-normalize and we now have the data value that we want to
289 return step * scale_factor;
292 /* Given an axis range, we can compute a desired data-space step
293 * amount for the major ticks (see _step_for_range). To get
294 * nice-looking pixel-snapped ticks we want to expand the range
297 _expand_range_for_width (double *axis_min, double *axis_max, int pixel_size)
299 double range, new_range, step, pixel_step;
301 range = *axis_max - *axis_min;
303 step = _step_for_range (range);
304 pixel_step = step * pixel_size / range;
306 /* We expand the range by the ratio of the pixel step to the floor
309 new_range = range * pixel_step / floor (pixel_step);
311 /* And spread the increase out on either side of the range. */
312 *axis_min -= (new_range - range) / 2.0;
313 *axis_max += (new_range - range) / 2.0;
316 /* Setup a transformation in acre->cr such that data values plotted
317 * will appear where they should within the chart.
320 _set_transform_to_data_space (acre_t *acre)
322 cairo_t *cr = acre->cr;
326 acre->chart.y + acre->chart.height);
328 acre->chart.width / (acre->x_axis.max - acre->x_axis.min),
329 - acre->chart.height /(acre->y_axis.max - acre->y_axis.min));
330 cairo_translate (cr, -acre->x_axis.min, -acre->y_axis.min);
334 _compute_axis_ranges (acre_t *acre)
338 double x_adjust, y_adjust;
339 cairo_t *cr = acre->cr;
341 /* First, simply find the extrema of the data. */
342 for (d = 0; d < acre->num_data; d++) {
343 data = acre->data[d];
344 for (i = 0; i < data->num_points; i++) {
345 if (data->points[i].x < acre->x_axis.min)
346 acre->x_axis.min = data->points[i].x;
347 if (data->points[i].x > acre->x_axis.max)
348 acre->x_axis.max = data->points[i].x;
350 if (data->points[i].y < acre->y_axis.min)
351 acre->y_axis.min = data->points[i].y;
352 if (data->points[i].y > acre->y_axis.max)
353 acre->y_axis.max = data->points[i].y;
357 /* Next, increase the axis ranges just enough so that the step
358 * sizes for the ticks will be integers.
360 _expand_range_for_width (&acre->x_axis.min,
364 _expand_range_for_width (&acre->y_axis.min,
368 /* Finally, we also translate the axis ranges slightly so that the
369 * ticks land on half-integer device-pixel positions.
373 _set_transform_to_data_space (acre);
377 cairo_user_to_device (cr, &x_adjust, &y_adjust);
378 x_adjust = (round (x_adjust + 0.5) - 0.5) - x_adjust;
379 y_adjust = (round (y_adjust + 0.5) - 0.5) - y_adjust;
380 cairo_device_to_user_distance (cr, &x_adjust, &y_adjust);
382 acre->x_axis.min -= x_adjust;
383 acre->x_axis.max -= x_adjust;
385 acre->y_axis.min -= y_adjust;
386 acre->y_axis.max -= y_adjust;
392 _draw_data (acre_t *acre)
394 cairo_t *cr = acre->cr;
400 cairo_set_source_rgb (cr, 0, 0, 0);
402 _set_transform_to_data_space (acre);
404 for (d = 0; d < acre->num_data; d++) {
405 data = acre->data[d];
407 for (i = 0; i < data->num_points; i++) {
414 cairo_identity_matrix (cr);
415 cairo_set_line_width (cr, 1.0);
425 _draw_frame_and_ticks (acre_t *acre)
427 cairo_t *cr = acre->cr;
432 cairo_set_source_rgb (cr, 0, 0, 0); /* black */
434 /* First the ticks within data space. */
437 _set_transform_to_data_space (acre);
439 step = _step_for_range (acre->x_axis.max -acre->x_axis.min);
440 x = (floor (acre->x_axis.min / step) + 1) * step;
441 while (x <= acre->x_axis.max) {
442 cairo_move_to (cr, x, acre->y_axis.min);
445 cairo_identity_matrix (cr);
446 cairo_rel_line_to (cr, 0, 0.5);
447 cairo_rel_line_to (cr, 0, -ACRE_TICK_SIZE-0.5);
448 cairo_set_line_width (cr, 1.0);
455 step = _step_for_range (acre->y_axis.max -acre->y_axis.min);
456 y = (floor (acre->y_axis.min / step) + 1) * step;
457 while (y <= acre->y_axis.max) {
458 cairo_move_to (cr, acre->x_axis.min, y);
461 cairo_identity_matrix (cr);
462 cairo_rel_line_to (cr, -0.5, 0);
463 cairo_rel_line_to (cr, ACRE_TICK_SIZE+0.5, 0);
464 cairo_set_line_width (cr, 1.0);
473 /* Then the frame drawn in pixel space. */
475 acre->chart.x - 0.5, acre->chart.y - 0.5,
476 acre->chart.width + 1.0, acre->chart.height + 1.0);
477 cairo_set_line_width (cr, 1.0);
483 /* Draw the plot to the given cairo context within a user-space
484 * rectangle from (0, 0) to (width, height). This size includes all
485 * space for extra-plot elements (such as the title, the axis labels,
489 acre_draw (acre_t *acre, cairo_t *cr, int width, int height)
493 acre->height = height;
497 cairo_set_source_rgb (cr, 1, 1, 1);
501 _draw_title_and_labels (acre);
503 _compute_axis_ranges (acre);
507 _draw_frame_and_ticks (acre);
510 /* Create a new dataset---a collection of (x, y) datapoints. A single
511 * plot can contain multiple datasets, (see acre_add_data). */
513 acre_data_create (void)
517 data = xmalloc (sizeof (acre_data_t));
522 data->points_size = 0;
523 data->num_points = 0;
528 /* Destroy an acre dataset. Do not call this function if the dataset
529 * has been added to an acre_t plot with acre_add_data. */
531 acre_data_destroy (acre_data_t *data)
538 /* Set the label for this dataset (to appear in the plot's key). */
540 acre_data_set_name (acre_data_t *data, const char *name)
544 data->name = strdup (name);
547 /* Add a datapoint to the given dataset. */
549 acre_data_add_point_2d (acre_data_t *data, double x, double y)
551 if (data->num_points >= data->points_size) {
552 data->points_size *= 2;
553 if (data->points_size == 0)
554 data->points_size = 16;
555 data->points = xrealloc_ab (data->points,
557 sizeof (acre_data_point_2d_t));
560 data->points[data->num_points].x = x;
561 data->points[data->num_points].y = y;