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_MAJOR_SIZE 6
166 #define ACRE_TICK_MINOR_SIZE 3
167 #define ACRE_X_TICK_VALUE_PAD 2
168 #define ACRE_Y_TICK_VALUE_PAD 4
171 _create_layout (acre_t *acre, const char *text)
175 layout = pango_cairo_create_layout (acre->cr);
176 pango_layout_set_font_description (layout, acre->font);
177 pango_layout_set_text (layout, text, -1);
178 pango_layout_set_alignment (layout, PANGO_ALIGN_CENTER);
183 #define PRINTF_FORMAT(fmt_index, va_index) __attribute__ ((__format__(__printf__, fmt_index, va_index)))
186 _create_layout_vprintf (acre_t *acre, const char *fmt, va_list ap)
191 vasprintf (&text, fmt, ap);
193 layout = _create_layout (acre, text);
201 _create_layout_printf (acre_t *acre, const char *fmt, ...)
202 PRINTF_FORMAT (2, 3);
205 _create_layout_printf (acre_t *acre, const char *fmt, ...)
212 layout = _create_layout_vprintf (acre, fmt, ap);
220 _destroy_layout (PangoLayout *layout)
222 g_object_unref (layout);
226 _show_layout (cairo_t *cr, PangoLayout *layout)
228 pango_cairo_show_layout (cr, layout);
230 _destroy_layout (layout);
234 _draw_title_and_labels (acre_t *acre)
236 cairo_t *cr = acre->cr;
237 PangoFontDescription *title_font;
238 PangoLayout *title_layout, *x_axis_layout, *y_axis_layout;
239 PangoLayout *min_y, *max_y;
240 int min_y_width, max_y_width, y_axis_value_width;
241 int title_width, title_height;
242 int x_axis_width, x_axis_height;
243 int y_axis_width, y_axis_height;
244 PangoRectangle new_chart;
248 acre->font = pango_font_description_new ();
249 pango_font_description_set_family (acre->font, ACRE_FONT_FAMILY);
250 pango_font_description_set_absolute_size (acre->font,
251 ACRE_FONT_SIZE * PANGO_SCALE);
253 title_font = pango_font_description_new ();
254 pango_font_description_set_family (title_font, ACRE_FONT_FAMILY);
255 pango_font_description_set_absolute_size (title_font,
256 ACRE_TITLE_FONT_SIZE * PANGO_SCALE);
258 title_layout = _create_layout (acre, acre->title);
259 pango_layout_set_font_description (title_layout, title_font);
261 x_axis_layout = _create_layout (acre, acre->x_axis.label);
262 y_axis_layout = _create_layout (acre, acre->y_axis.label);
264 min_y = _create_layout_printf (acre, "%g",
265 round (acre->y_axis.min));
266 max_y = _create_layout_printf (acre, "%g",
267 round (acre->y_axis.max));
269 pango_layout_get_pixel_size (min_y, &min_y_width, NULL);
270 pango_layout_get_pixel_size (max_y, &max_y_width, NULL);
271 y_axis_value_width = MAX (min_y_width, max_y_width);
273 _destroy_layout (min_y);
274 _destroy_layout (max_y);
276 /* Iterate with the layout of the title and axis labels until they
277 * are stable, (this requires iteration since we don't know what
278 * to set their widths to in advance due to the wrapping of the
279 * other elements). */
281 pango_layout_set_width (title_layout, acre->chart.width * PANGO_SCALE);
282 pango_layout_set_width (x_axis_layout, acre->chart.width * PANGO_SCALE);
283 pango_layout_set_width (y_axis_layout, acre->chart.height * PANGO_SCALE);
285 pango_layout_get_pixel_size (title_layout, &title_width, &title_height);
286 pango_layout_get_pixel_size (x_axis_layout, &x_axis_width, &x_axis_height);
287 pango_layout_get_pixel_size (y_axis_layout, &y_axis_width, &y_axis_height);
289 new_chart.x = ACRE_PAD + y_axis_height +
290 ACRE_PAD + y_axis_value_width + ACRE_Y_TICK_VALUE_PAD;
291 new_chart.width = acre->width - acre->chart.x - ACRE_PAD;
293 new_chart.y = ACRE_PAD + title_height + ACRE_PAD;
294 new_chart.height = acre->height - acre->chart.y -
295 (ACRE_X_TICK_VALUE_PAD + ACRE_FONT_SIZE +
296 ACRE_PAD + x_axis_height + ACRE_PAD);
298 if (new_chart.x == acre->chart.x &&
299 new_chart.y == acre->chart.y &&
300 new_chart.width == acre->chart.width &&
301 new_chart.height == acre->chart.height)
306 acre->chart.x = new_chart.x;
307 acre->chart.y = new_chart.y;
308 acre->chart.width = new_chart.width;
309 acre->chart.height = new_chart.height;
312 cairo_set_source_rgb (cr, 0, 0, 0);
314 cairo_move_to (cr, acre->chart.x, ACRE_PAD);
315 _show_layout (cr, title_layout);
319 cairo_translate (cr, ACRE_PAD, acre->chart.y + acre->chart.height);
320 cairo_rotate (cr, - M_PI / 2.0);
321 cairo_move_to (cr, 0, 0);
322 _show_layout (cr, y_axis_layout);
326 cairo_move_to (cr, acre->chart.x,
327 acre->chart.y + acre->chart.height +
328 ACRE_FONT_SIZE + ACRE_PAD);
329 _show_layout (cr, x_axis_layout);
334 /* For a given axis range, compute a step size (in data space) to
335 * generate a suitable number of ticks (5 or so). */
337 _step_for_range (double range, int *minor_divisions)
339 double step, scale_factor;
341 /* We want roughly 5 major ticks for the chart. */
344 /* Normalize the step so we can easily snap it to a desirable
346 scale_factor = pow (10.0, floor (log10 (step)));
347 step /= scale_factor;
349 /* We want increments of 1, 2.5, 5, or 10 (times some power of
350 * 10). The threshold values between these are computed
351 * logarithmically. */
352 if (step < 3.535533905932738) {
353 if (step < 1.58113883008419) {
355 *minor_divisions = 4;
358 *minor_divisions = 5;
361 if (step < 7.071067811865475) {
363 *minor_divisions = 5;
366 *minor_divisions = 4;
370 /* Un-normalize and we now have the data value that we want to
372 return step * scale_factor;
375 /* Given an axis range, we can compute a desired data-space step
376 * amount for the major ticks (see _step_for_range). To get
377 * nice-looking pixel-snapped ticks we want to expand the range
380 _expand_range_for_width (double *axis_min, double *axis_max, int pixel_size)
382 double range, new_range, step, pixel_step;
385 range = *axis_max - *axis_min;
387 step = _step_for_range (range, &minor_divisions);
388 pixel_step = step * pixel_size / range / minor_divisions;
390 /* We expand the range by the ratio of the pixel step to the floor
393 new_range = range * pixel_step / floor (pixel_step);
395 /* And spread the increase out on either side of the range. */
396 *axis_min -= (new_range - range) / 2.0;
397 *axis_max += (new_range - range) / 2.0;
400 /* Setup a transformation in acre->cr such that data values plotted
401 * will appear where they should within the chart.
404 _set_transform_to_data_space (acre_t *acre)
406 cairo_t *cr = acre->cr;
410 acre->chart.y + acre->chart.height);
412 acre->chart.width / (acre->x_axis.max - acre->x_axis.min),
413 - acre->chart.height /(acre->y_axis.max - acre->y_axis.min));
414 cairo_translate (cr, -acre->x_axis.min, -acre->y_axis.min);
418 _compute_axis_ranges (acre_t *acre)
422 double x_adjust, y_adjust;
423 cairo_t *cr = acre->cr;
425 acre->x_axis.min = acre->data[0]->points[0].x;
426 acre->x_axis.max = acre->data[0]->points[0].x;
427 acre->y_axis.min = acre->data[0]->points[0].y;
428 acre->y_axis.min = acre->data[0]->points[0].y;
430 /* First, simply find the extrema of the data. */
431 for (d = 0; d < acre->num_data; d++) {
432 data = acre->data[d];
433 for (i = 0; i < data->num_points; i++) {
434 if (data->points[i].x < acre->x_axis.min)
435 acre->x_axis.min = data->points[i].x;
436 if (data->points[i].x > acre->x_axis.max)
437 acre->x_axis.max = data->points[i].x;
439 if (data->points[i].y < acre->y_axis.min)
440 acre->y_axis.min = data->points[i].y;
441 if (data->points[i].y > acre->y_axis.max)
442 acre->y_axis.max = data->points[i].y;
446 /* Next, increase the axis ranges just enough so that the step
447 * sizes for the ticks will be integers.
449 _expand_range_for_width (&acre->x_axis.min,
453 _expand_range_for_width (&acre->y_axis.min,
457 /* Finally, we also translate the axis ranges slightly so that the
458 * ticks land on half-integer device-pixel positions.
462 _set_transform_to_data_space (acre);
466 cairo_user_to_device (cr, &x_adjust, &y_adjust);
467 x_adjust = (round (x_adjust + 0.5) - 0.5) - x_adjust;
468 y_adjust = (round (y_adjust + 0.5) - 0.5) - y_adjust;
469 cairo_device_to_user_distance (cr, &x_adjust, &y_adjust);
471 acre->x_axis.min -= x_adjust;
472 acre->x_axis.max -= x_adjust;
474 acre->y_axis.min -= y_adjust;
475 acre->y_axis.max -= y_adjust;
481 _draw_data (acre_t *acre)
483 cairo_t *cr = acre->cr;
489 cairo_set_source_rgb (cr, 0, 0, 0);
491 _set_transform_to_data_space (acre);
493 for (d = 0; d < acre->num_data; d++) {
494 data = acre->data[d];
496 for (i = 0; i < data->num_points; i++) {
503 cairo_identity_matrix (cr);
504 cairo_set_line_width (cr, 1.0);
513 typedef enum _ticks { ACRE_TICKS_X, ACRE_TICKS_Y } acre_ticks_t;
516 _draw_ticks (acre_t *acre,
517 double axis_min, double axis_max,
520 cairo_t *cr = acre->cr;
521 double t, step, sub_step;
526 _set_transform_to_data_space (acre);
528 step = _step_for_range (axis_max - axis_min, &minor_divisions);
529 sub_step = step / minor_divisions;
531 for (t = (floor (axis_min / sub_step) + 1) * sub_step;
536 if (fabs((t / step) - (int) (t / step)) < 0.5 * (sub_step / step))
537 tick_size = ACRE_TICK_MAJOR_SIZE;
539 tick_size = ACRE_TICK_MINOR_SIZE;
544 if (ticks == ACRE_TICKS_X)
545 cairo_move_to (cr, t, acre->y_axis.min);
547 cairo_move_to (cr, acre->x_axis.min, t);
549 cairo_identity_matrix (cr);
551 if (ticks == ACRE_TICKS_X) {
552 cairo_rel_line_to (cr, 0, 0.5);
553 cairo_rel_line_to (cr, 0, -tick_size - 0.5);
555 cairo_rel_line_to (cr, -0.5, 0);
556 cairo_rel_line_to (cr, tick_size + 0.5, 0);
559 cairo_set_line_width (cr, 1.0);
565 if (tick_size == ACRE_TICK_MAJOR_SIZE)
572 layout = _create_layout_printf (acre, "%g", t);
574 if (ticks == ACRE_TICKS_X)
575 cairo_move_to (cr, t, acre->y_axis.min);
577 cairo_move_to (cr, acre->x_axis.min, t);
579 cairo_identity_matrix (cr);
580 pango_layout_get_pixel_size (layout, &width, &height);
582 if (ticks == ACRE_TICKS_X)
583 cairo_rel_move_to (cr, -width / 2, ACRE_X_TICK_VALUE_PAD);
585 cairo_rel_move_to (cr, -width - ACRE_Y_TICK_VALUE_PAD,
588 _show_layout (cr, layout);
598 _draw_frame_and_ticks (acre_t *acre)
600 cairo_t *cr = acre->cr;
604 cairo_set_source_rgb (cr, 0, 0, 0); /* black */
607 _draw_ticks (acre, acre->x_axis.min, acre->x_axis.max, ACRE_TICKS_X);
608 _draw_ticks (acre, acre->y_axis.min, acre->y_axis.max, ACRE_TICKS_Y);
612 acre->chart.x - 0.5, acre->chart.y - 0.5,
613 acre->chart.width + 1.0, acre->chart.height + 1.0);
614 cairo_set_line_width (cr, 1.0);
620 /* Draw the plot to the given cairo context within a user-space
621 * rectangle from (0, 0) to (width, height). This size includes all
622 * space for extra-plot elements (such as the title, the axis labels,
626 acre_draw (acre_t *acre, cairo_t *cr, int width, int height)
631 acre->height = height;
633 acre->chart.width = width;
634 acre->chart.height = height;
638 cairo_set_source_rgb (cr, 1, 1, 1);
640 /* We compute the axis ranges before doing label layout so that we
641 * can account for the width of the y-axis value labels. */
642 _compute_axis_ranges (acre);
644 _draw_title_and_labels (acre);
646 /* And we recompute the axis ranges now that the title and axis
647 * label space is all measured and accounted for. */
648 _compute_axis_ranges (acre);
652 _draw_frame_and_ticks (acre);
655 /* Create a new dataset---a collection of (x, y) datapoints. A single
656 * plot can contain multiple datasets, (see acre_add_data). */
658 acre_data_create (void)
662 data = xmalloc (sizeof (acre_data_t));
667 data->points_size = 0;
668 data->num_points = 0;
673 /* Destroy an acre dataset. Do not call this function if the dataset
674 * has been added to an acre_t plot with acre_add_data. */
676 acre_data_destroy (acre_data_t *data)
683 /* Set the label for this dataset (to appear in the plot's key). */
685 acre_data_set_name (acre_data_t *data, const char *name)
689 data->name = strdup (name);
692 /* Add a datapoint to the given dataset. */
694 acre_data_add_point_2d (acre_data_t *data, double x, double y)
696 if (data->num_points >= data->points_size) {
697 data->points_size *= 2;
698 if (data->points_size == 0)
699 data->points_size = 16;
700 data->points = xrealloc_ab (data->points,
702 sizeof (acre_data_point_2d_t));
705 data->points[data->num_points].x = x;
706 data->points[data->num_points].y = y;