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() */
33 typedef struct _acre_data_point_2d {
36 } acre_data_point_2d_t;
41 acre_data_point_2d_t *points;
42 unsigned int points_size;
43 unsigned int num_points;
46 typedef struct _acre_axis {
52 typedef struct _acre_color {
64 unsigned int data_size;
65 unsigned int num_data;
67 /* Data for drawing. */
69 PangoFontDescription *font;
74 /* Total size including labels. */
78 /* Position and size of chart alone. */
82 /* Create a new, empty plot. */
88 acre = xmalloc (sizeof (acre_t));
92 acre->x_axis.label = NULL;
93 acre->x_axis.min = 0.0;
94 acre->x_axis.max = 0.0;
96 acre->y_axis.label = NULL;
97 acre->y_axis.min = 0.0;
98 acre->y_axis.max = 0.0;
107 acre->num_colors = 0;
114 acre->chart.width = 0;
115 acre->chart.height = 0;
120 /* Destroy a plot. */
122 acre_destroy (acre_t *acre)
127 free (acre->x_axis.label);
128 free (acre->y_axis.label);
130 for (i = 0; i < acre->num_data; i++)
131 acre_data_destroy (acre->data[i]);
141 acre_set_title (acre_t *acre, const char *title)
145 acre->title = strdup (title);
149 acre_set_x_axis_label (acre_t *acre, const char *label)
151 free (acre->x_axis.label);
153 acre->x_axis.label = strdup (label);
157 acre_set_y_axis_label (acre_t *acre, const char *label)
159 free (acre->y_axis.label);
161 acre->y_axis.label = strdup (label);
164 /* Add a dataset to the plot. The plot assumes ownership of the
165 * dataset so it is not necessary to call acre_data_destroy on it. */
167 acre_add_data (acre_t *acre, acre_data_t *data)
169 if (acre->num_data >= acre->data_size) {
170 acre->data_size *= 2;
171 if (acre->data_size == 0)
173 acre->data = xrealloc_ab (acre->data,
175 sizeof (acre_data_t *));
178 acre->data[acre->num_data] = data;
182 #define ACRE_FONT_FAMILY "sans"
183 #define ACRE_FONT_SIZE 12
184 #define ACRE_TITLE_FONT_SIZE 20
185 #define ACRE_PAD (ACRE_FONT_SIZE)
186 #define ACRE_TICK_MAJOR_SIZE 6
187 #define ACRE_TICK_MINOR_SIZE 3
188 #define ACRE_X_TICK_VALUE_PAD 2
189 #define ACRE_Y_TICK_VALUE_PAD 4
190 #define ACRE_LEGEND_PAD 4
191 #define ACRE_LEGEND_LINE_SIZE 10
194 _create_layout (acre_t *acre, const char *text)
198 layout = pango_cairo_create_layout (acre->cr);
199 pango_layout_set_font_description (layout, acre->font);
200 pango_layout_set_text (layout, text, -1);
201 pango_layout_set_alignment (layout, PANGO_ALIGN_CENTER);
206 #define PRINTF_FORMAT(fmt_index, va_index) __attribute__ ((__format__(__printf__, fmt_index, va_index)))
209 _create_layout_vprintf (acre_t *acre, const char *fmt, va_list ap)
214 vasprintf (&text, fmt, ap);
216 layout = _create_layout (acre, text);
224 _create_layout_printf (acre_t *acre, const char *fmt, ...)
225 PRINTF_FORMAT (2, 3);
228 _create_layout_printf (acre_t *acre, const char *fmt, ...)
235 layout = _create_layout_vprintf (acre, fmt, ap);
243 _destroy_layout (PangoLayout *layout)
245 g_object_unref (layout);
249 _show_layout (cairo_t *cr, PangoLayout *layout)
251 pango_cairo_show_layout (cr, layout);
253 _destroy_layout (layout);
257 _draw_title_and_labels (acre_t *acre)
259 cairo_t *cr = acre->cr;
260 PangoFontDescription *title_font;
261 PangoLayout *title_layout, *x_axis_layout, *y_axis_layout;
262 PangoLayout *min_y, *max_y;
263 int min_y_width, max_y_width, y_axis_value_width;
264 int title_width, title_height;
265 int x_axis_width, x_axis_height;
266 int y_axis_width, y_axis_height;
267 PangoRectangle new_chart;
271 acre->font = pango_font_description_new ();
272 pango_font_description_set_family (acre->font, ACRE_FONT_FAMILY);
273 pango_font_description_set_absolute_size (acre->font,
274 ACRE_FONT_SIZE * PANGO_SCALE);
276 title_font = pango_font_description_new ();
277 pango_font_description_set_family (title_font, ACRE_FONT_FAMILY);
278 pango_font_description_set_absolute_size (title_font,
279 ACRE_TITLE_FONT_SIZE * PANGO_SCALE);
281 title_layout = _create_layout (acre, acre->title);
282 pango_layout_set_font_description (title_layout, title_font);
284 x_axis_layout = _create_layout (acre, acre->x_axis.label);
285 y_axis_layout = _create_layout (acre, acre->y_axis.label);
287 min_y = _create_layout_printf (acre, "%g",
288 round (acre->y_axis.min));
289 max_y = _create_layout_printf (acre, "%g",
290 round (acre->y_axis.max));
292 pango_layout_get_pixel_size (min_y, &min_y_width, NULL);
293 pango_layout_get_pixel_size (max_y, &max_y_width, NULL);
294 y_axis_value_width = MAX (min_y_width, max_y_width);
296 _destroy_layout (min_y);
297 _destroy_layout (max_y);
299 /* Iterate with the layout of the title and axis labels until they
300 * are stable, (this requires iteration since we don't know what
301 * to set their widths to in advance due to the wrapping of the
302 * other elements). */
304 pango_layout_set_width (title_layout, acre->chart.width * PANGO_SCALE);
305 pango_layout_set_width (x_axis_layout, acre->chart.width * PANGO_SCALE);
306 pango_layout_set_width (y_axis_layout, acre->chart.height * PANGO_SCALE);
308 pango_layout_get_pixel_size (title_layout, &title_width, &title_height);
309 pango_layout_get_pixel_size (x_axis_layout, &x_axis_width, &x_axis_height);
310 pango_layout_get_pixel_size (y_axis_layout, &y_axis_width, &y_axis_height);
312 new_chart.x = ACRE_PAD + y_axis_height +
313 ACRE_PAD + y_axis_value_width + ACRE_Y_TICK_VALUE_PAD;
314 new_chart.width = acre->width - acre->chart.x - ACRE_PAD;
316 new_chart.y = ACRE_PAD + title_height + ACRE_PAD;
317 new_chart.height = acre->height - acre->chart.y -
318 (ACRE_X_TICK_VALUE_PAD + ACRE_FONT_SIZE +
319 ACRE_PAD + x_axis_height + ACRE_PAD);
321 if (new_chart.x == acre->chart.x &&
322 new_chart.y == acre->chart.y &&
323 new_chart.width == acre->chart.width &&
324 new_chart.height == acre->chart.height)
329 acre->chart.x = new_chart.x;
330 acre->chart.y = new_chart.y;
331 acre->chart.width = new_chart.width;
332 acre->chart.height = new_chart.height;
335 cairo_set_source_rgb (cr, 0, 0, 0);
337 cairo_move_to (cr, acre->chart.x, ACRE_PAD);
338 _show_layout (cr, title_layout);
342 cairo_translate (cr, ACRE_PAD, acre->chart.y + acre->chart.height);
343 cairo_rotate (cr, - M_PI / 2.0);
344 cairo_move_to (cr, 0, 0);
345 _show_layout (cr, y_axis_layout);
349 cairo_move_to (cr, acre->chart.x,
350 acre->chart.y + acre->chart.height +
351 ACRE_FONT_SIZE + ACRE_PAD);
352 _show_layout (cr, x_axis_layout);
357 /* For a given axis range, compute a step size (in data space) to
358 * generate a suitable number of ticks (5 or so). */
360 _step_for_range (double range, int *minor_divisions)
362 double step, scale_factor;
364 /* We want roughly 5 major ticks for the chart. */
367 /* Normalize the step so we can easily snap it to a desirable
369 scale_factor = pow (10.0, floor (log10 (step)));
370 step /= scale_factor;
372 /* We want increments of 1, 2.5, 5, or 10 (times some power of
373 * 10). The threshold values between these are computed
374 * logarithmically. */
375 if (step < 3.535533905932738) {
376 if (step < 1.58113883008419) {
378 *minor_divisions = 4;
381 *minor_divisions = 5;
384 if (step < 7.071067811865475) {
386 *minor_divisions = 5;
389 *minor_divisions = 4;
393 /* Un-normalize and we now have the data value that we want to
395 return step * scale_factor;
398 /* Given an axis range, we can compute a desired data-space step
399 * amount for the major ticks (see _step_for_range). To get
400 * nice-looking pixel-snapped ticks we want to expand the range
403 _expand_range_for_width (double *axis_min, double *axis_max, int pixel_range)
405 double range, new_range, step, step_minor, pixel_step;
408 range = *axis_max - *axis_min;
410 step = _step_for_range (range, &minor_divisions);
411 step_minor = step / minor_divisions;
413 pixel_step = step_minor * (pixel_range / range);
415 /* We expand the range by the ratio of the pixel step to the floor
418 new_range = range * pixel_step / floor (pixel_step);
420 /* And spread the increase out on either side of the range. */
421 *axis_min -= (new_range - range) / 2.0;
422 *axis_max += (new_range - range) / 2.0;
425 /* Setup a transformation in acre->cr such that data values plotted
426 * will appear where they should within the chart.
429 _set_transform_to_data_space (acre_t *acre)
431 cairo_t *cr = acre->cr;
435 acre->chart.y + acre->chart.height);
437 acre->chart.width / (acre->x_axis.max - acre->x_axis.min),
438 - acre->chart.height /(acre->y_axis.max - acre->y_axis.min));
439 cairo_translate (cr, -acre->x_axis.min, -acre->y_axis.min);
443 _compute_axis_ranges (acre_t *acre)
447 double x_adjust, y_adjust;
448 cairo_t *cr = acre->cr;
450 acre->x_axis.min = acre->data[0]->points[0].x;
451 acre->x_axis.max = acre->data[0]->points[0].x;
452 acre->y_axis.min = acre->data[0]->points[0].y;
453 acre->y_axis.min = acre->data[0]->points[0].y;
455 /* First, simply find the extrema of the data. */
456 for (d = 0; d < acre->num_data; d++) {
457 data = acre->data[d];
458 for (i = 0; i < data->num_points; i++) {
459 if (data->points[i].x < acre->x_axis.min)
460 acre->x_axis.min = data->points[i].x;
461 if (data->points[i].x > acre->x_axis.max)
462 acre->x_axis.max = data->points[i].x;
464 if (data->points[i].y < acre->y_axis.min)
465 acre->y_axis.min = data->points[i].y;
466 if (data->points[i].y > acre->y_axis.max)
467 acre->y_axis.max = data->points[i].y;
471 /* Next, we want to ensure that the data never collides with the
472 * ticks. So we expand each axis on its minimum side as needed. */
477 _set_transform_to_data_space (acre);
479 x = ACRE_TICK_MAJOR_SIZE + 2.0;
480 y = ACRE_TICK_MAJOR_SIZE + 2.0;
481 cairo_device_to_user_distance (cr, &x, &y);
483 acre->x_axis.min -= x;
484 acre->y_axis.min += y;
488 /* Then, increase the axis ranges just enough so that the step
489 * sizes for the ticks will be integers.
491 _expand_range_for_width (&acre->x_axis.min,
495 _expand_range_for_width (&acre->y_axis.min,
499 /* Finally, we also translate the axis ranges slightly so that the
500 * ticks land on half-integer device-pixel positions.
504 _set_transform_to_data_space (acre);
508 cairo_user_to_device (cr, &x_adjust, &y_adjust);
509 x_adjust = (round (x_adjust + 0.5) - 0.5) - x_adjust;
510 y_adjust = (round (y_adjust + 0.5) - 0.5) - y_adjust;
511 cairo_device_to_user_distance (cr, &x_adjust, &y_adjust);
513 acre->x_axis.min -= x_adjust;
514 acre->x_axis.max -= x_adjust;
516 acre->y_axis.min -= y_adjust;
517 acre->y_axis.max -= y_adjust;
523 _acre_color_from_hsv (acre_color_t *color,
531 hmod6 = (int) floor (hue / 60) % 6;
532 f = hue / 60 - floor (hue / 60);
533 p = value * (1 - saturation);
534 q = value * (1 - f * saturation);
535 t = value * (1 - (1 - f) * saturation);
545 color->green = value;
550 color->green = value;
572 _choose_colors (acre_t *acre)
574 cmsHPROFILE lab_profile, srgb_profile;
575 cmsHTRANSFORM lab_to_srgb;
577 double theta, radius, srgb[3];
580 lab_profile = cmsCreateLabProfile (NULL); /* D50 */
581 srgb_profile = cmsCreate_sRGBProfile ();
583 lab_to_srgb = cmsCreateTransform (lab_profile, TYPE_Lab_DBL,
584 srgb_profile, TYPE_RGB_DBL,
585 INTENT_PERCEPTUAL, 0);
587 acre->num_colors = acre->num_data;
589 if (acre->num_colors > acre->colors_size) {
590 acre->colors_size = acre->num_colors;
591 acre->colors = xrealloc (acre->colors,
592 acre->colors_size * sizeof (acre_color_t));
597 for (i = 0; i < acre->num_colors; i++) {
598 theta = 0.713 + 2 * M_PI * (double) i / acre->num_colors;
599 lab.a = radius * cos (theta);
600 lab.b = radius * sin (theta);
602 cmsDoTransform (lab_to_srgb, &lab, srgb, 1);
604 acre->colors[i].red = srgb[0];
605 acre->colors[i].green = srgb[1];
606 acre->colors[i].blue = srgb[2];
609 cmsDeleteTransform (lab_to_srgb);
610 cmsCloseProfile (lab_profile);
611 cmsCloseProfile (srgb_profile);
615 _draw_data (acre_t *acre)
617 cairo_t *cr = acre->cr;
623 cairo_set_source_rgb (cr, 0, 0, 0);
625 _set_transform_to_data_space (acre);
627 for (d = 0; d < acre->num_data; d++) {
628 int color = d % acre->num_colors;
629 cairo_set_source_rgb (cr,
630 acre->colors[color].red,
631 acre->colors[color].green,
632 acre->colors[color].blue);
633 data = acre->data[d];
635 for (i = 0; i < data->num_points; i++) {
642 cairo_identity_matrix (cr);
643 cairo_set_line_width (cr, 1.0);
652 typedef enum _ticks { ACRE_TICKS_X, ACRE_TICKS_Y } acre_ticks_t;
655 _draw_ticks (acre_t *acre,
656 double axis_min, double axis_max,
659 cairo_t *cr = acre->cr;
660 double t, step, sub_step;
665 _set_transform_to_data_space (acre);
667 step = _step_for_range (axis_max - axis_min, &minor_divisions);
668 sub_step = step / minor_divisions;
670 for (t = (floor (axis_min / sub_step) + 1) * sub_step;
675 if (fabs((t / step) - round (t / step)) < 0.5 * (sub_step / step))
676 tick_size = ACRE_TICK_MAJOR_SIZE;
678 tick_size = ACRE_TICK_MINOR_SIZE;
683 if (ticks == ACRE_TICKS_X)
684 cairo_move_to (cr, t, acre->y_axis.min);
686 cairo_move_to (cr, acre->x_axis.min, t);
688 cairo_identity_matrix (cr);
690 if (ticks == ACRE_TICKS_X) {
691 cairo_rel_line_to (cr, 0, 0.5);
692 cairo_rel_line_to (cr, 0, -tick_size - 0.5);
694 cairo_rel_line_to (cr, -0.5, 0);
695 cairo_rel_line_to (cr, tick_size + 0.5, 0);
698 cairo_set_line_width (cr, 1.0);
704 if (tick_size == ACRE_TICK_MAJOR_SIZE)
711 layout = _create_layout_printf (acre, "%g", t);
713 if (ticks == ACRE_TICKS_X)
714 cairo_move_to (cr, t, acre->y_axis.min);
716 cairo_move_to (cr, acre->x_axis.min, t);
718 cairo_identity_matrix (cr);
719 pango_layout_get_pixel_size (layout, &width, &height);
721 if (ticks == ACRE_TICKS_X)
722 cairo_rel_move_to (cr, -width / 2, ACRE_X_TICK_VALUE_PAD);
724 cairo_rel_move_to (cr, -width - ACRE_Y_TICK_VALUE_PAD,
727 _show_layout (cr, layout);
737 _draw_legend (acre_t *acre)
740 int label_width, max_label_width = 0;
743 cairo_t *cr = acre->cr;
747 for (i = 0; i < acre->num_data; i++) {
748 layout = _create_layout (acre, acre->data[i]->name);
749 pango_layout_get_pixel_size (layout, &label_width, NULL);
750 _destroy_layout (layout);
751 if (label_width > max_label_width)
752 max_label_width = label_width;
755 width = ACRE_LEGEND_PAD + ACRE_LEGEND_LINE_SIZE + ACRE_LEGEND_PAD +
756 max_label_width + ACRE_LEGEND_PAD;
757 height = ACRE_LEGEND_PAD +
758 acre->num_data * (ACRE_FONT_SIZE + ACRE_LEGEND_PAD);
760 cairo_translate (cr, acre->chart.x, acre->chart.y);
763 acre->chart.width - ACRE_LEGEND_PAD - width,
766 cairo_rectangle (cr, -0.5, -0.5, width + 1.0, height + 1.0);
767 cairo_set_source_rgb (cr, 0, 0, 0);
768 cairo_set_line_width (cr, 1.0);
771 cairo_translate (cr, ACRE_LEGEND_PAD, ACRE_LEGEND_PAD);
773 for (i = 0; i < acre->num_data; i++) {
775 0, ACRE_LEGEND_LINE_SIZE / 2,
776 ACRE_LEGEND_LINE_SIZE, ACRE_LEGEND_LINE_SIZE / 2);
777 cairo_set_source_rgb (cr,
778 acre->colors[i % acre->num_colors].red,
779 acre->colors[i % acre->num_colors].green,
780 acre->colors[i % acre->num_colors].blue);
783 layout = _create_layout (acre, acre->data[i]->name);
784 cairo_move_to (cr, ACRE_LEGEND_LINE_SIZE + ACRE_LEGEND_PAD, 0);
785 cairo_set_source_rgb (cr, 0, 0, 0);
786 _show_layout (cr, layout);
788 cairo_translate (cr, 0, ACRE_LEGEND_PAD + ACRE_FONT_SIZE);
795 _draw_frame_and_ticks (acre_t *acre)
797 cairo_t *cr = acre->cr;
801 cairo_set_source_rgb (cr, 0, 0, 0); /* black */
804 _draw_ticks (acre, acre->x_axis.min, acre->x_axis.max, ACRE_TICKS_X);
805 _draw_ticks (acre, acre->y_axis.min, acre->y_axis.max, ACRE_TICKS_Y);
809 acre->chart.x - 0.5, acre->chart.y - 0.5,
810 acre->chart.width + 1.0, acre->chart.height + 1.0);
811 cairo_set_line_width (cr, 1.0);
817 /* Draw the plot to the given cairo context within a user-space
818 * rectangle from (0, 0) to (width, height). This size includes all
819 * space for extra-plot elements (such as the title, the axis labels,
823 acre_draw (acre_t *acre, cairo_t *cr, int width, int height)
828 acre->height = height;
830 acre->chart.width = width;
831 acre->chart.height = height;
835 cairo_set_source_rgb (cr, 1, 1, 1);
837 _choose_colors (acre);
839 /* We compute the axis ranges before doing label layout so that we
840 * can account for the width of the y-axis value labels. */
841 _compute_axis_ranges (acre);
843 _draw_title_and_labels (acre);
845 /* And we recompute the axis ranges now that the title and axis
846 * label space is all measured and accounted for. */
847 _compute_axis_ranges (acre);
851 if (acre->num_data > 1)
854 _draw_frame_and_ticks (acre);
857 /* Create a new dataset---a collection of (x, y) datapoints. A single
858 * plot can contain multiple datasets, (see acre_add_data). */
860 acre_data_create (void)
864 data = xmalloc (sizeof (acre_data_t));
869 data->points_size = 0;
870 data->num_points = 0;
875 /* Destroy an acre dataset. Do not call this function if the dataset
876 * has been added to an acre_t plot with acre_add_data. */
878 acre_data_destroy (acre_data_t *data)
885 /* Set the label for this dataset (to appear in the plot's key). */
887 acre_data_set_name (acre_data_t *data, const char *name)
891 data->name = strdup (name);
894 /* Add a datapoint to the given dataset. */
896 acre_data_add_point_2d (acre_data_t *data, double x, double y)
898 if (data->num_points >= data->points_size) {
899 data->points_size *= 2;
900 if (data->points_size == 0)
901 data->points_size = 16;
902 data->points = xrealloc_ab (data->points,
904 sizeof (acre_data_point_2d_t));
907 data->points[data->num_points].x = x;
908 data->points[data->num_points].y = y;