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() */
34 typedef struct _acre_data_point_2d {
37 } acre_data_point_2d_t;
42 acre_data_point_2d_t min;
43 acre_data_point_2d_t max;
45 acre_data_point_2d_t *points;
46 unsigned int points_size;
47 unsigned int num_points;
50 typedef struct _acre_axis {
57 /* Range of data to be viewed. */
61 /* Has the view range been set? */
65 typedef struct _acre_color {
77 unsigned int data_size;
78 unsigned int num_data;
80 /* Data for drawing. */
82 PangoFontDescription *font;
87 /* Total size including labels. */
91 /* Position and size of chart alone. */
96 _find_x_range_given_y_range (acre_t *acre,
97 double *x_min, double *x_max,
98 double y_min, double y_max);
101 _find_y_range_given_x_range (acre_t *acre,
102 double *y_min, double *y_max,
103 double x_min, double x_max);
105 /* Create a new, empty plot. */
111 acre = xmalloc (sizeof (acre_t));
115 acre->x_axis.label = NULL;
116 acre->x_axis.data_min = 0.0;
117 acre->x_axis.data_max = 0.0;
118 acre->x_axis.view_min = 0.0;
119 acre->x_axis.view_max = 0.0;
120 acre->x_axis.view_range_set = false;
122 acre->y_axis.label = NULL;
123 acre->y_axis.data_min = 0.0;
124 acre->y_axis.data_max = 0.0;
125 acre->y_axis.view_min = 0.0;
126 acre->y_axis.view_max = 0.0;
127 acre->y_axis.view_range_set = false;
136 acre->num_colors = 0;
137 acre->colors_size = 0;
144 acre->chart.width = 0;
145 acre->chart.height = 0;
150 /* Destroy a plot. */
152 acre_destroy (acre_t *acre)
157 free (acre->x_axis.label);
158 free (acre->y_axis.label);
160 for (i = 0; i < acre->num_data; i++)
161 acre_data_destroy (acre->data[i]);
171 acre_set_title (acre_t *acre, const char *title)
175 acre->title = strdup (title);
179 acre_set_x_axis_label (acre_t *acre, const char *label)
181 free (acre->x_axis.label);
183 acre->x_axis.label = strdup (label);
187 acre_set_y_axis_label (acre_t *acre, const char *label)
189 free (acre->y_axis.label);
191 acre->y_axis.label = strdup (label);
195 acre_get_x_axis_data_range (acre_t *acre, double *x_min, double *x_max)
198 *x_min = acre->x_axis.data_min;
201 *x_max = acre->x_axis.data_max;
205 acre_get_x_axis_range (acre_t *acre, double *x_min, double *x_max)
207 /* If an X range has been set, return that. */
208 if (acre->x_axis.view_range_set) {
210 *x_min = acre->x_axis.view_min;
213 *x_max = acre->x_axis.view_max;
218 /* Otherwise, if a Y range has been set, use that to compute X. */
219 if (acre->y_axis.view_range_set) {
220 _find_x_range_given_y_range (acre, x_min, x_max,
221 acre->y_axis.view_min,
222 acre->y_axis.view_max);
227 /* Neither view range set. Return full, data-based X range. */
228 acre_get_x_axis_data_range (acre, x_min, x_max);
232 acre_set_x_axis_range (acre_t *acre, double x_min, double x_max)
234 acre->x_axis.view_min = x_min;
235 acre->x_axis.view_max = x_max;
237 acre->x_axis.view_range_set = true;
241 acre_set_x_axis_range_auto (acre_t *acre)
243 acre->x_axis.view_range_set = false;
247 acre_get_y_axis_data_range (acre_t *acre, double *y_min, double *y_max)
250 *y_min = acre->y_axis.data_min;
253 *y_max = acre->y_axis.data_max;
257 acre_get_y_axis_range (acre_t *acre, double *y_min, double *y_max)
259 /* If a Y range has been set, return that. */
260 if (acre->y_axis.view_range_set) {
262 *y_min = acre->y_axis.view_min;
265 *y_max = acre->y_axis.view_max;
270 /* Otherwise, if an X range has been set, use that to compute Y. */
271 if (acre->x_axis.view_range_set) {
272 _find_y_range_given_x_range (acre, y_min, y_max,
273 acre->x_axis.view_min,
274 acre->x_axis.view_max);
279 /* Neither view range set. Return full data-based Y range. */
280 acre_get_y_axis_data_range (acre, y_min, y_max);
284 acre_set_y_axis_range (acre_t *acre, double y_min, double y_max)
286 acre->y_axis.view_min = y_min;
287 acre->y_axis.view_max = y_max;
289 acre->y_axis.view_range_set = true;
293 acre_set_y_axis_range_auto (acre_t *acre)
295 acre->y_axis.view_range_set = false;
298 /* Add a dataset to the plot. The plot assumes ownership of the
299 * dataset so it is not necessary to call acre_data_destroy on it. */
301 acre_add_data (acre_t *acre, acre_data_t *data)
303 if (acre->num_data >= acre->data_size) {
304 acre->data_size *= 2;
305 if (acre->data_size == 0)
307 acre->data = xrealloc_ab (acre->data,
309 sizeof (acre_data_t *));
312 acre->data[acre->num_data] = data;
314 if (acre->num_data == 0) {
315 acre->x_axis.data_min = data->min.x;
316 acre->y_axis.data_min = data->min.y;
318 acre->x_axis.data_max = data->max.x;
319 acre->y_axis.data_max = data->max.y;
321 if (data->min.x < acre->x_axis.data_min)
322 acre->x_axis.data_min = data->min.x;
323 if (data->min.y < acre->y_axis.data_min)
324 acre->y_axis.data_min = data->min.y;
326 if (data->max.x > acre->x_axis.data_max)
327 acre->x_axis.data_max = data->max.x;
328 if (data->max.y > acre->y_axis.data_max)
329 acre->y_axis.data_max = data->max.y;
335 #define ACRE_FONT_FAMILY "sans"
336 #define ACRE_FONT_SIZE 12
337 #define ACRE_TITLE_FONT_SIZE 20
338 #define ACRE_PAD (ACRE_FONT_SIZE)
339 #define ACRE_TICK_MAJOR_SIZE 6
340 #define ACRE_TICK_MINOR_SIZE 3
341 #define ACRE_X_TICK_VALUE_PAD 2
342 #define ACRE_Y_TICK_VALUE_PAD 4
343 #define ACRE_LEGEND_PAD 4
344 #define ACRE_LEGEND_LINE_SIZE 10
347 _create_layout (acre_t *acre, const char *text)
354 layout = pango_cairo_create_layout (acre->cr);
355 pango_layout_set_font_description (layout, acre->font);
356 pango_layout_set_text (layout, text, -1);
357 pango_layout_set_alignment (layout, PANGO_ALIGN_CENTER);
362 #define PRINTF_FORMAT(fmt_index, va_index) __attribute__ ((__format__(__printf__, fmt_index, va_index)))
365 _create_layout_vprintf (acre_t *acre, const char *fmt, va_list ap)
370 vasprintf (&text, fmt, ap);
372 layout = _create_layout (acre, text);
380 _create_layout_printf (acre_t *acre, const char *fmt, ...)
381 PRINTF_FORMAT (2, 3);
384 _create_layout_printf (acre_t *acre, const char *fmt, ...)
391 layout = _create_layout_vprintf (acre, fmt, ap);
399 _destroy_layout (PangoLayout *layout)
401 g_object_unref (layout);
405 _show_layout (cairo_t *cr, PangoLayout *layout)
407 pango_cairo_show_layout (cr, layout);
409 _destroy_layout (layout);
413 _draw_title_and_labels (acre_t *acre)
415 cairo_t *cr = acre->cr;
416 PangoFontDescription *title_font;
417 PangoLayout *title_layout, *x_axis_layout, *y_axis_layout;
418 PangoLayout *min_y, *max_y;
419 int min_y_width, max_y_width, y_axis_value_width;
420 int title_width, title_height;
421 int x_axis_width, x_axis_height;
422 int y_axis_width, y_axis_height;
423 PangoRectangle new_chart;
427 acre->font = pango_font_description_new ();
428 pango_font_description_set_family (acre->font, ACRE_FONT_FAMILY);
429 pango_font_description_set_absolute_size (acre->font,
430 ACRE_FONT_SIZE * PANGO_SCALE);
432 title_font = pango_font_description_new ();
433 pango_font_description_set_family (title_font, ACRE_FONT_FAMILY);
434 pango_font_description_set_absolute_size (title_font,
435 ACRE_TITLE_FONT_SIZE * PANGO_SCALE);
437 title_layout = _create_layout (acre, acre->title);
438 pango_layout_set_font_description (title_layout, title_font);
440 x_axis_layout = _create_layout (acre, acre->x_axis.label);
441 y_axis_layout = _create_layout (acre, acre->y_axis.label);
443 min_y = _create_layout_printf (acre, "%g",
444 round (acre->y_axis.view_min));
445 max_y = _create_layout_printf (acre, "%g",
446 round (acre->y_axis.view_max));
448 pango_layout_get_pixel_size (min_y, &min_y_width, NULL);
449 pango_layout_get_pixel_size (max_y, &max_y_width, NULL);
450 y_axis_value_width = MAX (min_y_width, max_y_width);
452 _destroy_layout (min_y);
453 _destroy_layout (max_y);
455 /* Iterate with the layout of the title and axis labels until they
456 * are stable, (this requires iteration since we don't know what
457 * to set their widths to in advance due to the wrapping of the
458 * other elements). */
460 pango_layout_set_width (title_layout, acre->chart.width * PANGO_SCALE);
461 pango_layout_set_width (x_axis_layout, acre->chart.width * PANGO_SCALE);
462 pango_layout_set_width (y_axis_layout, acre->chart.height * PANGO_SCALE);
464 pango_layout_get_pixel_size (title_layout, &title_width, &title_height);
465 pango_layout_get_pixel_size (x_axis_layout, &x_axis_width, &x_axis_height);
466 pango_layout_get_pixel_size (y_axis_layout, &y_axis_width, &y_axis_height);
468 new_chart.x = ACRE_PAD + y_axis_height +
469 ACRE_PAD + y_axis_value_width + ACRE_Y_TICK_VALUE_PAD;
470 new_chart.width = acre->width - acre->chart.x - ACRE_PAD;
472 new_chart.y = ACRE_PAD + title_height + ACRE_PAD;
473 new_chart.height = acre->height - acre->chart.y -
474 (ACRE_X_TICK_VALUE_PAD + ACRE_FONT_SIZE +
475 ACRE_PAD + x_axis_height + ACRE_PAD);
477 if (new_chart.x == acre->chart.x &&
478 new_chart.y == acre->chart.y &&
479 new_chart.width == acre->chart.width &&
480 new_chart.height == acre->chart.height)
485 acre->chart.x = new_chart.x;
486 acre->chart.y = new_chart.y;
487 acre->chart.width = new_chart.width;
488 acre->chart.height = new_chart.height;
491 cairo_set_source_rgb (cr, 0, 0, 0);
493 cairo_move_to (cr, acre->chart.x, ACRE_PAD);
494 _show_layout (cr, title_layout);
498 cairo_translate (cr, ACRE_PAD, acre->chart.y + acre->chart.height);
499 cairo_rotate (cr, - M_PI / 2.0);
500 cairo_move_to (cr, 0, 0);
501 _show_layout (cr, y_axis_layout);
505 cairo_move_to (cr, acre->chart.x,
506 acre->chart.y + acre->chart.height +
507 ACRE_FONT_SIZE + ACRE_PAD);
508 _show_layout (cr, x_axis_layout);
513 /* For a given axis range, compute a step size (in data space) to
514 * generate a suitable number of ticks (5 or so). */
516 _step_for_range (double range, int *minor_divisions)
518 double step, scale_factor;
520 /* We want roughly 5 major ticks for the chart. */
523 /* Normalize the step so we can easily snap it to a desirable
525 scale_factor = pow (10.0, floor (log10 (step)));
526 step /= scale_factor;
528 /* We want increments of 1, 2.5, 5, or 10 (times some power of
529 * 10). The threshold values between these are computed
530 * logarithmically. */
531 if (step < 3.535533905932738) {
532 if (step < 1.58113883008419) {
534 *minor_divisions = 4;
537 *minor_divisions = 5;
540 if (step < 7.071067811865475) {
542 *minor_divisions = 5;
545 *minor_divisions = 4;
549 /* Un-normalize and we now have the data value that we want to
551 return step * scale_factor;
554 /* Given an axis range, we can compute a desired data-space step
555 * amount for the major ticks (see _step_for_range). To get
556 * nice-looking pixel-snapped ticks we want to expand the range
559 _expand_range_for_width (double *axis_min, double *axis_max, int pixel_range)
561 double range, new_range, step, step_minor, pixel_step;
564 range = *axis_max - *axis_min;
566 step = _step_for_range (range, &minor_divisions);
567 step_minor = step / minor_divisions;
569 pixel_step = step_minor * (pixel_range / range);
571 /* We expand the range by the ratio of the pixel step to the floor
574 new_range = range * pixel_step / floor (pixel_step);
576 /* And spread the increase out on either side of the range. */
577 *axis_min -= (new_range - range) / 2.0;
578 *axis_max += (new_range - range) / 2.0;
581 /* Setup a transformation in acre->cr such that data values plotted
582 * will appear where they should within the chart.
585 _set_transform_to_data_space (acre_t *acre)
587 cairo_t *cr = acre->cr;
591 acre->chart.y + acre->chart.height);
593 acre->chart.width / (acre->x_axis.view_max - acre->x_axis.view_min),
594 - acre->chart.height /(acre->y_axis.view_max - acre->y_axis.view_min));
595 cairo_translate (cr, -acre->x_axis.view_min, -acre->y_axis.view_min);
599 _find_x_range_given_y_range (acre_t *acre,
600 double *x_min, double *x_max,
601 double y_min, double y_max)
609 for (d = 0; d < acre->num_data; d++) {
610 data = acre->data[d];
611 for (i = 0; i < data->num_points; i++) {
612 if (data->points[i].y >= y_min &&
613 data->points[i].y <= y_max)
616 *x_min = data->points[i].x;
617 *x_max = data->points[i].x;
620 if (data->points[i].x < *x_min)
621 *x_min = data->points[i].x;
622 if (data->points[i].x > *x_max)
623 *x_max = data->points[i].x;
629 /* If nothing is visible, punt to full X data range. */
631 *x_min = acre->x_axis.data_min;
632 *x_max = acre->x_axis.data_max;
637 _find_y_range_given_x_range (acre_t *acre,
638 double *y_min, double *y_max,
639 double x_min, double x_max)
647 for (d = 0; d < acre->num_data; d++) {
648 data = acre->data[d];
649 for (i = 0; i < data->num_points; i++) {
650 if (data->points[i].x >= x_min &&
651 data->points[i].x <= x_max)
654 *y_min = data->points[i].y;
655 *y_max = data->points[i].y;
658 if (data->points[i].y < *y_min)
659 *y_min = data->points[i].y;
660 if (data->points[i].y > *y_max)
661 *y_max = data->points[i].y;
667 /* If nothing is visible, punt to full Y data range. */
669 *y_min = acre->y_axis.data_min;
670 *y_max = acre->y_axis.data_max;
675 _compute_axis_ranges (acre_t *acre)
677 double x_adjust, y_adjust;
678 cairo_t *cr = acre->cr;
680 /* If neither view range is set, set both to data ranges. */
681 if (! acre->x_axis.view_range_set && ! acre->y_axis.view_range_set)
683 acre->x_axis.view_min = acre->x_axis.data_min;
684 acre->x_axis.view_max = acre->x_axis.data_max;
686 acre->y_axis.view_min = acre->y_axis.data_min;
687 acre->y_axis.view_max = acre->y_axis.data_max;
689 /* Otherwise, auto-fit unset range based on data. */
690 if (acre->x_axis.view_range_set && ! acre->y_axis.view_range_set) {
691 _find_y_range_given_x_range (acre,
692 &acre->y_axis.view_min,
693 &acre->y_axis.view_max,
694 acre->x_axis.view_min,
695 acre->x_axis.view_max);
697 else if (acre->y_axis.view_range_set && ! acre->x_axis.view_range_set) {
698 _find_x_range_given_y_range (acre,
699 &acre->x_axis.view_min,
700 &acre->x_axis.view_max,
701 acre->y_axis.view_min,
702 acre->y_axis.view_max);
706 /* Then, increase the axis ranges just enough so that the step
707 * sizes for the ticks will be integers.
709 _expand_range_for_width (&acre->x_axis.view_min,
710 &acre->x_axis.view_max,
713 _expand_range_for_width (&acre->y_axis.view_min,
714 &acre->y_axis.view_max,
717 /* Finally, we also translate the axis ranges slightly so that the
718 * ticks land on half-integer device-pixel positions.
722 _set_transform_to_data_space (acre);
726 cairo_user_to_device (cr, &x_adjust, &y_adjust);
727 x_adjust = (round (x_adjust + 0.5) - 0.5) - x_adjust;
728 y_adjust = (round (y_adjust + 0.5) - 0.5) - y_adjust;
729 cairo_device_to_user_distance (cr, &x_adjust, &y_adjust);
731 acre->x_axis.view_min -= x_adjust;
732 acre->x_axis.view_max -= x_adjust;
734 acre->y_axis.view_min -= y_adjust;
735 acre->y_axis.view_max -= y_adjust;
741 _choose_colors (acre_t *acre)
743 cmsHPROFILE lab_profile, srgb_profile;
744 cmsHTRANSFORM lab_to_srgb;
746 double theta, radius, srgb[3];
749 lab_profile = cmsCreateLabProfile (NULL); /* D50 */
750 srgb_profile = cmsCreate_sRGBProfile ();
752 lab_to_srgb = cmsCreateTransform (lab_profile, TYPE_Lab_DBL,
753 srgb_profile, TYPE_RGB_DBL,
754 INTENT_PERCEPTUAL, 0);
756 acre->num_colors = acre->num_data;
758 if (acre->num_colors > acre->colors_size) {
759 acre->colors_size = acre->num_colors;
760 acre->colors = xrealloc (acre->colors,
761 acre->colors_size * sizeof (acre_color_t));
766 for (i = 0; i < acre->num_colors; i++) {
767 theta = 0.713 + 2 * M_PI * (double) i / acre->num_colors;
768 lab.a = radius * cos (theta);
769 lab.b = radius * sin (theta);
771 cmsDoTransform (lab_to_srgb, &lab, srgb, 1);
773 acre->colors[i].red = srgb[0];
774 acre->colors[i].green = srgb[1];
775 acre->colors[i].blue = srgb[2];
778 cmsDeleteTransform (lab_to_srgb);
779 cmsCloseProfile (lab_profile);
780 cmsCloseProfile (srgb_profile);
784 _draw_data (acre_t *acre)
786 cairo_t *cr = acre->cr;
793 acre->chart.x, acre->chart.y,
794 acre->chart.width, acre->chart.height);
797 cairo_set_source_rgb (cr, 0, 0, 0);
799 _set_transform_to_data_space (acre);
801 for (d = 0; d < acre->num_data; d++) {
802 int color = d % acre->num_colors;
803 cairo_set_source_rgb (cr,
804 acre->colors[color].red,
805 acre->colors[color].green,
806 acre->colors[color].blue);
807 data = acre->data[d];
811 for (i = 0; i < data->num_points; i++) {
818 cairo_identity_matrix (cr);
819 cairo_set_line_width (cr, 1.0);
828 typedef enum _ticks { ACRE_TICKS_X, ACRE_TICKS_Y } acre_ticks_t;
831 _draw_ticks (acre_t *acre,
832 double axis_min, double axis_max,
835 cairo_t *cr = acre->cr;
836 double t, step, sub_step;
841 _set_transform_to_data_space (acre);
843 step = _step_for_range (axis_max - axis_min, &minor_divisions);
844 sub_step = step / minor_divisions;
846 for (t = (floor (axis_min / sub_step) + 1) * sub_step;
851 if (fabs((t / step) - round (t / step)) < 0.5 * (sub_step / step))
852 tick_size = ACRE_TICK_MAJOR_SIZE;
854 tick_size = ACRE_TICK_MINOR_SIZE;
859 if (ticks == ACRE_TICKS_X)
860 cairo_move_to (cr, t, acre->y_axis.view_min);
862 cairo_move_to (cr, acre->x_axis.view_min, t);
864 cairo_identity_matrix (cr);
866 if (ticks == ACRE_TICKS_X) {
867 cairo_rel_line_to (cr, 0, 0.5);
868 cairo_rel_line_to (cr, 0, -tick_size - 0.5);
870 cairo_rel_line_to (cr, -0.5, 0);
871 cairo_rel_line_to (cr, tick_size + 0.5, 0);
874 cairo_set_line_width (cr, 1.0);
880 if (tick_size == ACRE_TICK_MAJOR_SIZE)
887 layout = _create_layout_printf (acre, "%g", t);
889 if (ticks == ACRE_TICKS_X)
890 cairo_move_to (cr, t, acre->y_axis.view_min);
892 cairo_move_to (cr, acre->x_axis.view_min, t);
894 cairo_identity_matrix (cr);
895 pango_layout_get_pixel_size (layout, &width, &height);
897 if (ticks == ACRE_TICKS_X)
898 cairo_rel_move_to (cr, -width / 2, ACRE_X_TICK_VALUE_PAD);
900 cairo_rel_move_to (cr, -width - ACRE_Y_TICK_VALUE_PAD,
903 _show_layout (cr, layout);
913 _draw_legend (acre_t *acre)
916 int label_width, max_label_width = 0;
919 cairo_t *cr = acre->cr;
923 for (i = 0; i < acre->num_data; i++) {
924 layout = _create_layout (acre, acre->data[i]->name);
925 pango_layout_get_pixel_size (layout, &label_width, NULL);
926 _destroy_layout (layout);
927 if (label_width > max_label_width)
928 max_label_width = label_width;
931 width = ACRE_LEGEND_PAD + ACRE_LEGEND_LINE_SIZE + ACRE_LEGEND_PAD +
932 max_label_width + ACRE_LEGEND_PAD;
933 height = ACRE_LEGEND_PAD +
934 acre->num_data * (ACRE_FONT_SIZE + ACRE_LEGEND_PAD);
936 cairo_translate (cr, acre->chart.x, acre->chart.y);
939 acre->chart.width - ACRE_LEGEND_PAD - width,
942 cairo_rectangle (cr, -0.5, -0.5, width + 1.0, height + 1.0);
943 cairo_set_source_rgb (cr, 0, 0, 0);
944 cairo_set_line_width (cr, 1.0);
947 cairo_translate (cr, ACRE_LEGEND_PAD, ACRE_LEGEND_PAD);
949 for (i = 0; i < acre->num_data; i++) {
951 0, ACRE_LEGEND_LINE_SIZE / 2,
952 ACRE_LEGEND_LINE_SIZE, ACRE_LEGEND_LINE_SIZE / 2);
953 cairo_set_source_rgb (cr,
954 acre->colors[i % acre->num_colors].red,
955 acre->colors[i % acre->num_colors].green,
956 acre->colors[i % acre->num_colors].blue);
959 layout = _create_layout (acre, acre->data[i]->name);
960 cairo_move_to (cr, ACRE_LEGEND_LINE_SIZE + ACRE_LEGEND_PAD, 0);
961 cairo_set_source_rgb (cr, 0, 0, 0);
962 _show_layout (cr, layout);
964 cairo_translate (cr, 0, ACRE_LEGEND_PAD + ACRE_FONT_SIZE);
971 _draw_frame_and_ticks (acre_t *acre)
973 cairo_t *cr = acre->cr;
977 cairo_set_source_rgb (cr, 0, 0, 0); /* black */
980 _draw_ticks (acre, acre->x_axis.view_min, acre->x_axis.view_max, ACRE_TICKS_X);
981 _draw_ticks (acre, acre->y_axis.view_min, acre->y_axis.view_max, ACRE_TICKS_Y);
985 acre->chart.x - 0.5, acre->chart.y - 0.5,
986 acre->chart.width + 1.0, acre->chart.height + 1.0);
987 cairo_set_line_width (cr, 1.0);
993 /* Draw the plot to the given cairo context within a user-space
994 * rectangle from (0, 0) to (width, height). This size includes all
995 * space for extra-plot elements (such as the title, the axis labels,
999 acre_draw (acre_t *acre, cairo_t *cr, int width, int height)
1003 acre->width = width;
1004 acre->height = height;
1006 acre->chart.width = width;
1007 acre->chart.height = height;
1011 cairo_set_source_rgb (cr, 1, 1, 1);
1013 _choose_colors (acre);
1015 /* We compute the axis ranges before doing label layout so that we
1016 * can account for the width of the y-axis value labels. */
1017 _compute_axis_ranges (acre);
1019 _draw_title_and_labels (acre);
1021 /* And we recompute the axis ranges now that the title and axis
1022 * label space is all measured and accounted for. */
1023 _compute_axis_ranges (acre);
1027 if (acre->num_data > 1)
1028 _draw_legend (acre);
1030 _draw_frame_and_ticks (acre);
1033 /* Create a new dataset---a collection of (x, y) datapoints. A single
1034 * plot can contain multiple datasets, (see acre_add_data). */
1036 acre_data_create (void)
1040 data = xmalloc (sizeof (acre_data_t));
1044 data->points = NULL;
1045 data->points_size = 0;
1046 data->num_points = 0;
1051 /* Destroy an acre dataset. Do not call this function if the dataset
1052 * has been added to an acre_t plot with acre_add_data. */
1054 acre_data_destroy (acre_data_t *data)
1056 free (data->points);
1061 /* Set the label for this dataset (to appear in the plot's key). */
1063 acre_data_set_name (acre_data_t *data, const char *name)
1067 data->name = strdup (name);
1070 /* Add a datapoint to the given dataset. */
1072 acre_data_add_point_2d (acre_data_t *data, double x, double y)
1074 if (data->num_points >= data->points_size) {
1075 data->points_size *= 2;
1076 if (data->points_size == 0)
1077 data->points_size = 16;
1078 data->points = xrealloc_ab (data->points,
1080 sizeof (acre_data_point_2d_t));
1083 data->points[data->num_points].x = x;
1084 data->points[data->num_points].y = y;
1086 if (data->num_points == 0) {
1093 if (x < data->min.x)
1095 if (y < data->min.y)
1098 if (x > data->max.x)
1100 if (y > data->max.y)