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 {
50 typedef struct _acre_color {
62 unsigned int data_size;
63 unsigned int num_data;
65 /* Data for drawing. */
67 PangoFontDescription *font;
72 /* Total size including labels. */
76 /* Position and size of chart alone. */
80 /* Create a new, empty plot. */
86 acre = xmalloc (sizeof (acre_t));
90 acre->x_axis.label = NULL;
91 acre->x_axis.min = 0.0;
92 acre->x_axis.max = 0.0;
94 acre->y_axis.label = NULL;
95 acre->y_axis.min = 0.0;
96 acre->y_axis.max = 0.0;
105 acre->num_colors = 0;
112 acre->chart.width = 0;
113 acre->chart.height = 0;
118 /* Destroy a plot. */
120 acre_destroy (acre_t *acre)
125 free (acre->x_axis.label);
126 free (acre->y_axis.label);
128 for (i = 0; i < acre->num_data; i++)
129 acre_data_destroy (acre->data[i]);
139 acre_set_title (acre_t *acre, const char *title)
143 acre->title = strdup (title);
147 acre_set_x_axis_label (acre_t *acre, const char *label)
149 free (acre->x_axis.label);
151 acre->x_axis.label = strdup (label);
155 acre_set_y_axis_label (acre_t *acre, const char *label)
157 free (acre->y_axis.label);
159 acre->y_axis.label = strdup (label);
162 /* Add a dataset to the plot. The plot assumes ownership of the
163 * dataset so it is not necessary to call acre_data_destroy on it. */
165 acre_add_data (acre_t *acre, acre_data_t *data)
167 if (acre->num_data >= acre->data_size) {
168 acre->data_size *= 2;
169 if (acre->data_size == 0)
171 acre->data = xrealloc_ab (acre->data,
173 sizeof (acre_data_t *));
176 acre->data[acre->num_data] = data;
180 #define ACRE_FONT_FAMILY "sans"
181 #define ACRE_FONT_SIZE 12
182 #define ACRE_TITLE_FONT_SIZE 20
183 #define ACRE_PAD (ACRE_FONT_SIZE)
184 #define ACRE_TICK_MAJOR_SIZE 6
185 #define ACRE_TICK_MINOR_SIZE 3
186 #define ACRE_X_TICK_VALUE_PAD 2
187 #define ACRE_Y_TICK_VALUE_PAD 4
188 #define ACRE_LEGEND_PAD 4
189 #define ACRE_LEGEND_LINE_SIZE 10
192 _create_layout (acre_t *acre, const char *text)
196 layout = pango_cairo_create_layout (acre->cr);
197 pango_layout_set_font_description (layout, acre->font);
198 pango_layout_set_text (layout, text, -1);
199 pango_layout_set_alignment (layout, PANGO_ALIGN_CENTER);
204 #define PRINTF_FORMAT(fmt_index, va_index) __attribute__ ((__format__(__printf__, fmt_index, va_index)))
207 _create_layout_vprintf (acre_t *acre, const char *fmt, va_list ap)
212 vasprintf (&text, fmt, ap);
214 layout = _create_layout (acre, text);
222 _create_layout_printf (acre_t *acre, const char *fmt, ...)
223 PRINTF_FORMAT (2, 3);
226 _create_layout_printf (acre_t *acre, const char *fmt, ...)
233 layout = _create_layout_vprintf (acre, fmt, ap);
241 _destroy_layout (PangoLayout *layout)
243 g_object_unref (layout);
247 _show_layout (cairo_t *cr, PangoLayout *layout)
249 pango_cairo_show_layout (cr, layout);
251 _destroy_layout (layout);
255 _draw_title_and_labels (acre_t *acre)
257 cairo_t *cr = acre->cr;
258 PangoFontDescription *title_font;
259 PangoLayout *title_layout, *x_axis_layout, *y_axis_layout;
260 PangoLayout *min_y, *max_y;
261 int min_y_width, max_y_width, y_axis_value_width;
262 int title_width, title_height;
263 int x_axis_width, x_axis_height;
264 int y_axis_width, y_axis_height;
265 PangoRectangle new_chart;
269 acre->font = pango_font_description_new ();
270 pango_font_description_set_family (acre->font, ACRE_FONT_FAMILY);
271 pango_font_description_set_absolute_size (acre->font,
272 ACRE_FONT_SIZE * PANGO_SCALE);
274 title_font = pango_font_description_new ();
275 pango_font_description_set_family (title_font, ACRE_FONT_FAMILY);
276 pango_font_description_set_absolute_size (title_font,
277 ACRE_TITLE_FONT_SIZE * PANGO_SCALE);
279 title_layout = _create_layout (acre, acre->title);
280 pango_layout_set_font_description (title_layout, title_font);
282 x_axis_layout = _create_layout (acre, acre->x_axis.label);
283 y_axis_layout = _create_layout (acre, acre->y_axis.label);
285 min_y = _create_layout_printf (acre, "%g",
286 round (acre->y_axis.min));
287 max_y = _create_layout_printf (acre, "%g",
288 round (acre->y_axis.max));
290 pango_layout_get_pixel_size (min_y, &min_y_width, NULL);
291 pango_layout_get_pixel_size (max_y, &max_y_width, NULL);
292 y_axis_value_width = MAX (min_y_width, max_y_width);
294 _destroy_layout (min_y);
295 _destroy_layout (max_y);
297 /* Iterate with the layout of the title and axis labels until they
298 * are stable, (this requires iteration since we don't know what
299 * to set their widths to in advance due to the wrapping of the
300 * other elements). */
302 pango_layout_set_width (title_layout, acre->chart.width * PANGO_SCALE);
303 pango_layout_set_width (x_axis_layout, acre->chart.width * PANGO_SCALE);
304 pango_layout_set_width (y_axis_layout, acre->chart.height * PANGO_SCALE);
306 pango_layout_get_pixel_size (title_layout, &title_width, &title_height);
307 pango_layout_get_pixel_size (x_axis_layout, &x_axis_width, &x_axis_height);
308 pango_layout_get_pixel_size (y_axis_layout, &y_axis_width, &y_axis_height);
310 new_chart.x = ACRE_PAD + y_axis_height +
311 ACRE_PAD + y_axis_value_width + ACRE_Y_TICK_VALUE_PAD;
312 new_chart.width = acre->width - acre->chart.x - ACRE_PAD;
314 new_chart.y = ACRE_PAD + title_height + ACRE_PAD;
315 new_chart.height = acre->height - acre->chart.y -
316 (ACRE_X_TICK_VALUE_PAD + ACRE_FONT_SIZE +
317 ACRE_PAD + x_axis_height + ACRE_PAD);
319 if (new_chart.x == acre->chart.x &&
320 new_chart.y == acre->chart.y &&
321 new_chart.width == acre->chart.width &&
322 new_chart.height == acre->chart.height)
327 acre->chart.x = new_chart.x;
328 acre->chart.y = new_chart.y;
329 acre->chart.width = new_chart.width;
330 acre->chart.height = new_chart.height;
333 cairo_set_source_rgb (cr, 0, 0, 0);
335 cairo_move_to (cr, acre->chart.x, ACRE_PAD);
336 _show_layout (cr, title_layout);
340 cairo_translate (cr, ACRE_PAD, acre->chart.y + acre->chart.height);
341 cairo_rotate (cr, - M_PI / 2.0);
342 cairo_move_to (cr, 0, 0);
343 _show_layout (cr, y_axis_layout);
347 cairo_move_to (cr, acre->chart.x,
348 acre->chart.y + acre->chart.height +
349 ACRE_FONT_SIZE + ACRE_PAD);
350 _show_layout (cr, x_axis_layout);
355 /* For a given axis range, compute a step size (in data space) to
356 * generate a suitable number of ticks (5 or so). */
358 _step_for_range (double range, int *minor_divisions)
360 double step, scale_factor;
362 /* We want roughly 5 major ticks for the chart. */
365 /* Normalize the step so we can easily snap it to a desirable
367 scale_factor = pow (10.0, floor (log10 (step)));
368 step /= scale_factor;
370 /* We want increments of 1, 2.5, 5, or 10 (times some power of
371 * 10). The threshold values between these are computed
372 * logarithmically. */
373 if (step < 3.535533905932738) {
374 if (step < 1.58113883008419) {
376 *minor_divisions = 4;
379 *minor_divisions = 5;
382 if (step < 7.071067811865475) {
384 *minor_divisions = 5;
387 *minor_divisions = 4;
391 /* Un-normalize and we now have the data value that we want to
393 return step * scale_factor;
396 /* Given an axis range, we can compute a desired data-space step
397 * amount for the major ticks (see _step_for_range). To get
398 * nice-looking pixel-snapped ticks we want to expand the range
401 _expand_range_for_width (double *axis_min, double *axis_max, int pixel_range)
403 double range, new_range, step, step_minor, pixel_step;
406 range = *axis_max - *axis_min;
408 step = _step_for_range (range, &minor_divisions);
409 step_minor = step / minor_divisions;
411 pixel_step = step_minor * (pixel_range / range);
413 /* We expand the range by the ratio of the pixel step to the floor
416 new_range = range * pixel_step / floor (pixel_step);
418 /* And spread the increase out on either side of the range. */
419 *axis_min -= (new_range - range) / 2.0;
420 *axis_max += (new_range - range) / 2.0;
423 /* Setup a transformation in acre->cr such that data values plotted
424 * will appear where they should within the chart.
427 _set_transform_to_data_space (acre_t *acre)
429 cairo_t *cr = acre->cr;
433 acre->chart.y + acre->chart.height);
435 acre->chart.width / (acre->x_axis.max - acre->x_axis.min),
436 - acre->chart.height /(acre->y_axis.max - acre->y_axis.min));
437 cairo_translate (cr, -acre->x_axis.min, -acre->y_axis.min);
441 _compute_axis_ranges (acre_t *acre)
445 double x_adjust, y_adjust;
446 cairo_t *cr = acre->cr;
448 acre->x_axis.min = acre->data[0]->points[0].x;
449 acre->x_axis.max = acre->data[0]->points[0].x;
450 acre->y_axis.min = acre->data[0]->points[0].y;
451 acre->y_axis.min = acre->data[0]->points[0].y;
453 /* First, simply find the extrema of the data. */
454 for (d = 0; d < acre->num_data; d++) {
455 data = acre->data[d];
456 for (i = 0; i < data->num_points; i++) {
457 if (data->points[i].x < acre->x_axis.min)
458 acre->x_axis.min = data->points[i].x;
459 if (data->points[i].x > acre->x_axis.max)
460 acre->x_axis.max = data->points[i].x;
462 if (data->points[i].y < acre->y_axis.min)
463 acre->y_axis.min = data->points[i].y;
464 if (data->points[i].y > acre->y_axis.max)
465 acre->y_axis.max = data->points[i].y;
469 /* Next, we want to ensure that the data never collides with the
470 * ticks. So we expand each axis on its minimum side as needed. */
475 _set_transform_to_data_space (acre);
477 x = ACRE_TICK_MAJOR_SIZE + 2.0;
478 y = ACRE_TICK_MAJOR_SIZE + 2.0;
479 cairo_device_to_user_distance (cr, &x, &y);
481 acre->x_axis.min -= x;
482 acre->y_axis.min += y;
486 /* Then, increase the axis ranges just enough so that the step
487 * sizes for the ticks will be integers.
489 _expand_range_for_width (&acre->x_axis.min,
493 _expand_range_for_width (&acre->y_axis.min,
497 /* Finally, we also translate the axis ranges slightly so that the
498 * ticks land on half-integer device-pixel positions.
502 _set_transform_to_data_space (acre);
506 cairo_user_to_device (cr, &x_adjust, &y_adjust);
507 x_adjust = (round (x_adjust + 0.5) - 0.5) - x_adjust;
508 y_adjust = (round (y_adjust + 0.5) - 0.5) - y_adjust;
509 cairo_device_to_user_distance (cr, &x_adjust, &y_adjust);
511 acre->x_axis.min -= x_adjust;
512 acre->x_axis.max -= x_adjust;
514 acre->y_axis.min -= y_adjust;
515 acre->y_axis.max -= y_adjust;
521 _acre_color_from_hsv (acre_color_t *color,
529 hmod6 = (int) floor (hue / 60) % 6;
530 f = hue / 60 - floor (hue / 60);
531 p = value * (1 - saturation);
532 q = value * (1 - f * saturation);
533 t = value * (1 - (1 - f) * saturation);
543 color->green = value;
548 color->green = value;
570 _choose_colors (acre_t *acre)
572 double hue, saturation, value;
575 acre->num_colors = acre->num_data;
577 if (acre->num_colors > acre->colors_size) {
578 acre->colors_size = acre->num_colors;
579 acre->colors = xrealloc (acre->colors,
580 acre->colors_size * sizeof (acre_color_t));
585 for (i = 0; i < acre->num_colors; i++) {
586 hue = 360 * (double) i / acre->num_colors;
587 _acre_color_from_hsv (&acre->colors[i],
588 hue, saturation, value);
593 _draw_data (acre_t *acre)
595 cairo_t *cr = acre->cr;
601 cairo_set_source_rgb (cr, 0, 0, 0);
603 _set_transform_to_data_space (acre);
605 for (d = 0; d < acre->num_data; d++) {
606 int color = d % acre->num_colors;
607 cairo_set_source_rgb (cr,
608 acre->colors[color].red,
609 acre->colors[color].green,
610 acre->colors[color].blue);
611 data = acre->data[d];
613 for (i = 0; i < data->num_points; i++) {
620 cairo_identity_matrix (cr);
621 cairo_set_line_width (cr, 1.0);
630 typedef enum _ticks { ACRE_TICKS_X, ACRE_TICKS_Y } acre_ticks_t;
633 _draw_ticks (acre_t *acre,
634 double axis_min, double axis_max,
637 cairo_t *cr = acre->cr;
638 double t, step, sub_step;
643 _set_transform_to_data_space (acre);
645 step = _step_for_range (axis_max - axis_min, &minor_divisions);
646 sub_step = step / minor_divisions;
648 for (t = (floor (axis_min / sub_step) + 1) * sub_step;
653 if (fabs((t / step) - round (t / step)) < 0.5 * (sub_step / step))
654 tick_size = ACRE_TICK_MAJOR_SIZE;
656 tick_size = ACRE_TICK_MINOR_SIZE;
661 if (ticks == ACRE_TICKS_X)
662 cairo_move_to (cr, t, acre->y_axis.min);
664 cairo_move_to (cr, acre->x_axis.min, t);
666 cairo_identity_matrix (cr);
668 if (ticks == ACRE_TICKS_X) {
669 cairo_rel_line_to (cr, 0, 0.5);
670 cairo_rel_line_to (cr, 0, -tick_size - 0.5);
672 cairo_rel_line_to (cr, -0.5, 0);
673 cairo_rel_line_to (cr, tick_size + 0.5, 0);
676 cairo_set_line_width (cr, 1.0);
682 if (tick_size == ACRE_TICK_MAJOR_SIZE)
689 layout = _create_layout_printf (acre, "%g", t);
691 if (ticks == ACRE_TICKS_X)
692 cairo_move_to (cr, t, acre->y_axis.min);
694 cairo_move_to (cr, acre->x_axis.min, t);
696 cairo_identity_matrix (cr);
697 pango_layout_get_pixel_size (layout, &width, &height);
699 if (ticks == ACRE_TICKS_X)
700 cairo_rel_move_to (cr, -width / 2, ACRE_X_TICK_VALUE_PAD);
702 cairo_rel_move_to (cr, -width - ACRE_Y_TICK_VALUE_PAD,
705 _show_layout (cr, layout);
715 _draw_legend (acre_t *acre)
718 int label_width, max_label_width = 0;
721 cairo_t *cr = acre->cr;
725 for (i = 0; i < acre->num_data; i++) {
726 layout = _create_layout (acre, acre->data[i]->name);
727 pango_layout_get_pixel_size (layout, &label_width, NULL);
728 _destroy_layout (layout);
729 if (label_width > max_label_width)
730 max_label_width = label_width;
733 width = ACRE_LEGEND_PAD + ACRE_LEGEND_LINE_SIZE + ACRE_LEGEND_PAD +
734 max_label_width + ACRE_LEGEND_PAD;
735 height = ACRE_LEGEND_PAD +
736 acre->num_data * (ACRE_FONT_SIZE + ACRE_LEGEND_PAD);
738 cairo_translate (cr, acre->chart.x, acre->chart.y);
741 acre->chart.width - ACRE_LEGEND_PAD - width,
744 cairo_rectangle (cr, -0.5, -0.5, width + 1.0, height + 1.0);
745 cairo_set_source_rgb (cr, 0, 0, 0);
746 cairo_set_line_width (cr, 1.0);
749 cairo_translate (cr, ACRE_LEGEND_PAD, ACRE_LEGEND_PAD);
751 for (i = 0; i < acre->num_data; i++) {
753 0, ACRE_LEGEND_LINE_SIZE / 2,
754 ACRE_LEGEND_LINE_SIZE, ACRE_LEGEND_LINE_SIZE / 2);
755 cairo_set_source_rgb (cr,
756 acre->colors[i % acre->num_colors].red,
757 acre->colors[i % acre->num_colors].green,
758 acre->colors[i % acre->num_colors].blue);
761 layout = _create_layout (acre, acre->data[i]->name);
762 cairo_move_to (cr, ACRE_LEGEND_LINE_SIZE + ACRE_LEGEND_PAD, 0);
763 cairo_set_source_rgb (cr, 0, 0, 0);
764 _show_layout (cr, layout);
766 cairo_translate (cr, 0, ACRE_LEGEND_PAD + ACRE_FONT_SIZE);
773 _draw_frame_and_ticks (acre_t *acre)
775 cairo_t *cr = acre->cr;
779 cairo_set_source_rgb (cr, 0, 0, 0); /* black */
782 _draw_ticks (acre, acre->x_axis.min, acre->x_axis.max, ACRE_TICKS_X);
783 _draw_ticks (acre, acre->y_axis.min, acre->y_axis.max, ACRE_TICKS_Y);
787 acre->chart.x - 0.5, acre->chart.y - 0.5,
788 acre->chart.width + 1.0, acre->chart.height + 1.0);
789 cairo_set_line_width (cr, 1.0);
795 /* Draw the plot to the given cairo context within a user-space
796 * rectangle from (0, 0) to (width, height). This size includes all
797 * space for extra-plot elements (such as the title, the axis labels,
801 acre_draw (acre_t *acre, cairo_t *cr, int width, int height)
806 acre->height = height;
808 acre->chart.width = width;
809 acre->chart.height = height;
813 cairo_set_source_rgb (cr, 1, 1, 1);
815 _choose_colors (acre);
817 /* We compute the axis ranges before doing label layout so that we
818 * can account for the width of the y-axis value labels. */
819 _compute_axis_ranges (acre);
821 _draw_title_and_labels (acre);
823 /* And we recompute the axis ranges now that the title and axis
824 * label space is all measured and accounted for. */
825 _compute_axis_ranges (acre);
829 if (acre->num_data > 1)
832 _draw_frame_and_ticks (acre);
835 /* Create a new dataset---a collection of (x, y) datapoints. A single
836 * plot can contain multiple datasets, (see acre_add_data). */
838 acre_data_create (void)
842 data = xmalloc (sizeof (acre_data_t));
847 data->points_size = 0;
848 data->num_points = 0;
853 /* Destroy an acre dataset. Do not call this function if the dataset
854 * has been added to an acre_t plot with acre_add_data. */
856 acre_data_destroy (acre_data_t *data)
863 /* Set the label for this dataset (to appear in the plot's key). */
865 acre_data_set_name (acre_data_t *data, const char *name)
869 data->name = strdup (name);
872 /* Add a datapoint to the given dataset. */
874 acre_data_add_point_2d (acre_data_t *data, double x, double y)
876 if (data->num_points >= data->points_size) {
877 data->points_size *= 2;
878 if (data->points_size == 0)
879 data->points_size = 16;
880 data->points = xrealloc_ab (data->points,
882 sizeof (acre_data_point_2d_t));
885 data->points[data->num_points].x = x;
886 data->points[data->num_points].y = y;