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
22 #define _GNU_SOURCE /* for asprintf() */
31 typedef struct _acre_data_point_2d {
34 } acre_data_point_2d_t;
39 acre_data_point_2d_t *points;
40 unsigned int points_size;
41 unsigned int num_points;
44 typedef struct _acre_axis {
56 unsigned int data_size;
57 unsigned int num_data;
59 /* Data for drawing. */
61 PangoFontDescription *font;
63 /* Total size including labels. */
67 /* Position and size of chart alone. */
71 /* Create a new, empty plot. */
77 acre = xmalloc (sizeof (acre_t));
81 acre->x_axis.label = NULL;
82 acre->x_axis.min = 0.0;
83 acre->x_axis.max = 0.0;
85 acre->y_axis.label = NULL;
86 acre->y_axis.min = 0.0;
87 acre->y_axis.max = 0.0;
95 acre->chart.width = 0;
96 acre->chart.height = 0;
101 /* Destroy a plot. */
103 acre_destroy (acre_t *acre)
108 free (acre->x_axis.label);
109 free (acre->y_axis.label);
111 for (i = 0; i < acre->num_data; i++)
112 acre_data_destroy (acre->data[i]);
120 acre_set_title (acre_t *acre, const char *title)
124 acre->title = strdup (title);
128 acre_set_x_axis_label (acre_t *acre, const char *label)
130 free (acre->x_axis.label);
132 acre->x_axis.label = strdup (label);
136 acre_set_y_axis_label (acre_t *acre, const char *label)
138 free (acre->y_axis.label);
140 acre->y_axis.label = strdup (label);
143 /* Add a dataset to the plot. The plot assumes ownership of the
144 * dataset so it is not necessary to call acre_data_destroy on it. */
146 acre_add_data (acre_t *acre, acre_data_t *data)
148 if (acre->num_data >= acre->data_size) {
149 acre->data_size *= 2;
150 if (acre->data_size == 0)
152 acre->data = xrealloc_ab (acre->data,
154 sizeof (acre_data_t *));
157 acre->data[acre->num_data] = data;
161 #define ACRE_FONT_FAMILY "sans"
162 #define ACRE_FONT_SIZE 12
163 #define ACRE_TITLE_FONT_SIZE 32
164 #define ACRE_PAD (ACRE_FONT_SIZE)
165 #define ACRE_TICK_SIZE 6
168 _create_layout (acre_t *acre, const char *text)
172 layout = pango_cairo_create_layout (acre->cr);
173 pango_layout_set_font_description (layout, acre->font);
174 pango_layout_set_text (layout, text, -1);
175 pango_layout_set_alignment (layout, PANGO_ALIGN_CENTER);
180 #define PRINTF_FORMAT(fmt_index, va_index) __attribute__ ((__format__(__printf__, fmt_index, va_index)))
183 _create_layout_vprintf (acre_t *acre, const char *fmt, va_list ap)
188 vasprintf (&text, fmt, ap);
190 layout = _create_layout (acre, text);
198 _create_layout_printf (acre_t *acre, const char *fmt, ...)
199 PRINTF_FORMAT (2, 3);
202 _create_layout_printf (acre_t *acre, const char *fmt, ...)
209 layout = _create_layout_vprintf (acre, fmt, ap);
217 _destroy_layout (PangoLayout *layout)
219 g_object_unref (layout);
223 _show_layout (cairo_t *cr, PangoLayout *layout)
225 pango_cairo_show_layout (cr, layout);
227 _destroy_layout (layout);
231 _draw_title_and_labels (acre_t *acre)
233 cairo_t *cr = acre->cr;
234 PangoFontDescription *title_font;
235 PangoLayout *title_layout, *x_axis_layout, *y_axis_layout;
236 PangoLayout *min_y, *max_y;
237 int min_y_width, max_y_width, y_axis_value_width;
238 int title_width, title_height;
239 int x_axis_width, x_axis_height;
240 int y_axis_width, y_axis_height;
241 PangoRectangle new_chart;
245 acre->font = pango_font_description_new ();
246 pango_font_description_set_family (acre->font, ACRE_FONT_FAMILY);
247 pango_font_description_set_absolute_size (acre->font,
248 ACRE_FONT_SIZE * PANGO_SCALE);
250 title_font = pango_font_description_new ();
251 pango_font_description_set_family (title_font, ACRE_FONT_FAMILY);
252 pango_font_description_set_absolute_size (title_font,
253 ACRE_TITLE_FONT_SIZE * PANGO_SCALE);
255 title_layout = _create_layout (acre, acre->title);
256 pango_layout_set_font_description (title_layout, title_font);
258 x_axis_layout = _create_layout (acre, acre->x_axis.label);
259 y_axis_layout = _create_layout (acre, acre->y_axis.label);
261 min_y = _create_layout_printf (acre, "%g",
262 round (acre->y_axis.min));
263 max_y = _create_layout_printf (acre, "%g",
264 round (acre->y_axis.max));
266 pango_layout_get_pixel_size (min_y, &min_y_width, NULL);
267 pango_layout_get_pixel_size (max_y, &max_y_width, NULL);
268 y_axis_value_width = MAX (min_y_width, max_y_width);
270 _destroy_layout (min_y);
271 _destroy_layout (max_y);
273 /* Iterate with the layout of the title and axis labels until they
274 * are stable, (this requires iteration since we don't know what
275 * to set their widths to in advance due to the wrapping of the
276 * other elements). */
278 pango_layout_set_width (title_layout, acre->chart.width * PANGO_SCALE);
279 pango_layout_set_width (x_axis_layout, acre->chart.width * PANGO_SCALE);
280 pango_layout_set_width (y_axis_layout, acre->chart.height * PANGO_SCALE);
282 pango_layout_get_pixel_size (title_layout, &title_width, &title_height);
283 pango_layout_get_pixel_size (x_axis_layout, &x_axis_width, &x_axis_height);
284 pango_layout_get_pixel_size (y_axis_layout, &y_axis_width, &y_axis_height);
286 new_chart.x = ACRE_PAD + y_axis_height +
287 ACRE_PAD + y_axis_value_width;
288 new_chart.width = acre->width - acre->chart.x - ACRE_PAD;
290 new_chart.y = ACRE_PAD + title_height + ACRE_PAD;
291 new_chart.height = acre->height - acre->chart.y - (ACRE_FONT_SIZE + ACRE_PAD + x_axis_height + ACRE_PAD);
293 if (new_chart.x == acre->chart.x &&
294 new_chart.y == acre->chart.y &&
295 new_chart.width == acre->chart.width &&
296 new_chart.height == acre->chart.height)
301 acre->chart.x = new_chart.x;
302 acre->chart.y = new_chart.y;
303 acre->chart.width = new_chart.width;
304 acre->chart.height = new_chart.height;
307 cairo_set_source_rgb (cr, 0, 0, 0);
309 cairo_move_to (cr, acre->chart.x, ACRE_PAD);
310 _show_layout (cr, title_layout);
314 cairo_translate (cr, ACRE_PAD, acre->chart.y + acre->chart.height);
315 cairo_rotate (cr, - M_PI / 2.0);
316 cairo_move_to (cr, 0, 0);
317 _show_layout (cr, y_axis_layout);
321 cairo_move_to (cr, acre->chart.x,
322 acre->chart.y + acre->chart.height +
323 ACRE_FONT_SIZE + ACRE_PAD);
324 _show_layout (cr, x_axis_layout);
329 /* For a given axis range, compute a step size (in data space) to
330 * generate a suitable number of ticks (5 or so). */
332 _step_for_range (double range)
334 double step, scale_factor;
336 /* We want roughly 5 major ticks for the chart. */
339 /* Normalize the step so we can easily snap it to a desirable
341 scale_factor = pow (10.0, floor (log10 (step)));
342 step /= scale_factor;
344 /* We want increments of 1, 2.5, 5, or 10 (times some power of
345 * 10). The threshold values between these are computed
346 * logarithmically. */
347 if (step < 3.535533905932738) {
348 if (step < 1.58113883008419)
353 if (step < 7.071067811865475)
359 /* Un-normalize and we now have the data value that we want to
361 return step * scale_factor;
364 /* Given an axis range, we can compute a desired data-space step
365 * amount for the major ticks (see _step_for_range). To get
366 * nice-looking pixel-snapped ticks we want to expand the range
369 _expand_range_for_width (double *axis_min, double *axis_max, int pixel_size)
371 double range, new_range, step, pixel_step;
373 range = *axis_max - *axis_min;
375 step = _step_for_range (range);
376 pixel_step = step * pixel_size / range;
378 /* We expand the range by the ratio of the pixel step to the floor
381 new_range = range * pixel_step / floor (pixel_step);
383 /* And spread the increase out on either side of the range. */
384 *axis_min -= (new_range - range) / 2.0;
385 *axis_max += (new_range - range) / 2.0;
388 /* Setup a transformation in acre->cr such that data values plotted
389 * will appear where they should within the chart.
392 _set_transform_to_data_space (acre_t *acre)
394 cairo_t *cr = acre->cr;
398 acre->chart.y + acre->chart.height);
400 acre->chart.width / (acre->x_axis.max - acre->x_axis.min),
401 - acre->chart.height /(acre->y_axis.max - acre->y_axis.min));
402 cairo_translate (cr, -acre->x_axis.min, -acre->y_axis.min);
406 _compute_axis_ranges (acre_t *acre)
410 double x_adjust, y_adjust;
411 cairo_t *cr = acre->cr;
413 acre->x_axis.min = acre->data[0]->points[0].x;
414 acre->x_axis.max = acre->data[0]->points[0].x;
415 acre->y_axis.min = acre->data[0]->points[0].y;
416 acre->y_axis.min = acre->data[0]->points[0].y;
418 /* First, simply find the extrema of the data. */
419 for (d = 0; d < acre->num_data; d++) {
420 data = acre->data[d];
421 for (i = 0; i < data->num_points; i++) {
422 if (data->points[i].x < acre->x_axis.min)
423 acre->x_axis.min = data->points[i].x;
424 if (data->points[i].x > acre->x_axis.max)
425 acre->x_axis.max = data->points[i].x;
427 if (data->points[i].y < acre->y_axis.min)
428 acre->y_axis.min = data->points[i].y;
429 if (data->points[i].y > acre->y_axis.max)
430 acre->y_axis.max = data->points[i].y;
434 /* Next, increase the axis ranges just enough so that the step
435 * sizes for the ticks will be integers.
437 _expand_range_for_width (&acre->x_axis.min,
441 _expand_range_for_width (&acre->y_axis.min,
445 /* Finally, we also translate the axis ranges slightly so that the
446 * ticks land on half-integer device-pixel positions.
450 _set_transform_to_data_space (acre);
454 cairo_user_to_device (cr, &x_adjust, &y_adjust);
455 x_adjust = (round (x_adjust + 0.5) - 0.5) - x_adjust;
456 y_adjust = (round (y_adjust + 0.5) - 0.5) - y_adjust;
457 cairo_device_to_user_distance (cr, &x_adjust, &y_adjust);
459 acre->x_axis.min -= x_adjust;
460 acre->x_axis.max -= x_adjust;
462 acre->y_axis.min -= y_adjust;
463 acre->y_axis.max -= y_adjust;
469 _draw_data (acre_t *acre)
471 cairo_t *cr = acre->cr;
477 cairo_set_source_rgb (cr, 0, 0, 0);
479 _set_transform_to_data_space (acre);
481 for (d = 0; d < acre->num_data; d++) {
482 data = acre->data[d];
484 for (i = 0; i < data->num_points; i++) {
491 cairo_identity_matrix (cr);
492 cairo_set_line_width (cr, 1.0);
502 _draw_frame_and_ticks (acre_t *acre)
504 cairo_t *cr = acre->cr;
509 cairo_set_source_rgb (cr, 0, 0, 0); /* black */
511 /* First the ticks within data space. */
514 _set_transform_to_data_space (acre);
516 step = _step_for_range (acre->x_axis.max -acre->x_axis.min);
517 x = (floor (acre->x_axis.min / step) + 1) * step;
518 while (x <= acre->x_axis.max) {
519 cairo_move_to (cr, x, acre->y_axis.min);
525 cairo_identity_matrix (cr);
526 cairo_rel_line_to (cr, 0, 0.5);
527 cairo_rel_line_to (cr, 0, -ACRE_TICK_SIZE-0.5);
528 cairo_set_line_width (cr, 1.0);
538 layout = _create_layout_printf (acre, "%g", x);
539 cairo_move_to (cr, x, acre->y_axis.min);
540 cairo_identity_matrix (cr);
541 pango_layout_get_pixel_size (layout, &width, &height);
542 cairo_rel_move_to (cr, -width / 2, 2);
543 _show_layout (cr, layout);
551 step = _step_for_range (acre->y_axis.max -acre->y_axis.min);
552 y = (floor (acre->y_axis.min / step) + 1) * step;
553 while (y <= acre->y_axis.max) {
554 cairo_move_to (cr, acre->x_axis.min, y);
560 cairo_identity_matrix (cr);
561 cairo_rel_line_to (cr, -0.5, 0);
562 cairo_rel_line_to (cr, ACRE_TICK_SIZE+0.5, 0);
563 cairo_set_line_width (cr, 1.0);
573 layout = _create_layout_printf (acre, "%g", y);
574 cairo_move_to (cr, acre->x_axis.min, y);
575 cairo_identity_matrix (cr);
576 pango_layout_get_pixel_size (layout, &width, &height);
577 cairo_rel_move_to (cr, -width-4, -height/2);
578 _show_layout (cr, layout);
588 /* Then the frame drawn in pixel space. */
590 acre->chart.x - 0.5, acre->chart.y - 0.5,
591 acre->chart.width + 1.0, acre->chart.height + 1.0);
592 cairo_set_line_width (cr, 1.0);
598 /* Draw the plot to the given cairo context within a user-space
599 * rectangle from (0, 0) to (width, height). This size includes all
600 * space for extra-plot elements (such as the title, the axis labels,
604 acre_draw (acre_t *acre, cairo_t *cr, int width, int height)
609 acre->height = height;
611 acre->chart.width = width;
612 acre->chart.height = height;
616 cairo_set_source_rgb (cr, 1, 1, 1);
618 /* We compute the axis ranges before doing label layout so that we
619 * can account for the width of the y-axis value labels. */
620 _compute_axis_ranges (acre);
622 _draw_title_and_labels (acre);
624 /* And we recompute the axis ranges now that the title and axis
625 * label space is all measured and accounted for. */
626 _compute_axis_ranges (acre);
630 _draw_frame_and_ticks (acre);
633 /* Create a new dataset---a collection of (x, y) datapoints. A single
634 * plot can contain multiple datasets, (see acre_add_data). */
636 acre_data_create (void)
640 data = xmalloc (sizeof (acre_data_t));
645 data->points_size = 0;
646 data->num_points = 0;
651 /* Destroy an acre dataset. Do not call this function if the dataset
652 * has been added to an acre_t plot with acre_add_data. */
654 acre_data_destroy (acre_data_t *data)
661 /* Set the label for this dataset (to appear in the plot's key). */
663 acre_data_set_name (acre_data_t *data, const char *name)
667 data->name = strdup (name);
670 /* Add a datapoint to the given dataset. */
672 acre_data_add_point_2d (acre_data_t *data, double x, double y)
674 if (data->num_points >= data->points_size) {
675 data->points_size *= 2;
676 if (data->points_size == 0)
677 data->points_size = 16;
678 data->points = xrealloc_ab (data->points,
680 sizeof (acre_data_point_2d_t));
683 data->points[data->num_points].x = x;
684 data->points[data->num_points].y = y;