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;
143 acre->chart.width = 0;
144 acre->chart.height = 0;
149 /* Destroy a plot. */
151 acre_destroy (acre_t *acre)
156 free (acre->x_axis.label);
157 free (acre->y_axis.label);
159 for (i = 0; i < acre->num_data; i++)
160 acre_data_destroy (acre->data[i]);
170 acre_set_title (acre_t *acre, const char *title)
174 acre->title = strdup (title);
178 acre_set_x_axis_label (acre_t *acre, const char *label)
180 free (acre->x_axis.label);
182 acre->x_axis.label = strdup (label);
186 acre_set_y_axis_label (acre_t *acre, const char *label)
188 free (acre->y_axis.label);
190 acre->y_axis.label = strdup (label);
194 acre_get_x_axis_data_range (acre_t *acre, double *x_min, double *x_max)
197 *x_min = acre->x_axis.data_min;
200 *x_max = acre->x_axis.data_max;
204 acre_get_x_axis_range (acre_t *acre, double *x_min, double *x_max)
206 /* If an X range has been set, return that. */
207 if (acre->x_axis.view_range_set) {
209 *x_min = acre->x_axis.view_min;
212 *x_max = acre->x_axis.view_max;
217 /* Otherwise, if a Y range has been set, use that to compute X. */
218 if (acre->y_axis.view_range_set) {
219 _find_x_range_given_y_range (acre, x_min, x_max,
220 acre->y_axis.view_min,
221 acre->y_axis.view_max);
226 /* Neither view range set. Return full, data-based X range. */
227 acre_get_x_axis_data_range (acre, x_min, x_max);
231 acre_set_x_axis_range (acre_t *acre, double x_min, double x_max)
233 acre->x_axis.view_min = x_min;
234 acre->x_axis.view_max = x_max;
236 acre->x_axis.view_range_set = true;
240 acre_set_x_axis_range_auto (acre_t *acre)
242 acre->x_axis.view_range_set = false;
246 acre_get_y_axis_data_range (acre_t *acre, double *y_min, double *y_max)
249 *y_min = acre->y_axis.data_min;
252 *y_max = acre->y_axis.data_max;
256 acre_get_y_axis_range (acre_t *acre, double *y_min, double *y_max)
258 /* If a Y range has been set, return that. */
259 if (acre->y_axis.view_range_set) {
261 *y_min = acre->y_axis.view_min;
264 *y_max = acre->y_axis.view_max;
269 /* Otherwise, if an X range has been set, use that to compute Y. */
270 if (acre->x_axis.view_range_set) {
271 _find_y_range_given_x_range (acre, y_min, y_max,
272 acre->x_axis.view_min,
273 acre->x_axis.view_max);
278 /* Neither view range set. Return full data-based Y range. */
279 acre_get_y_axis_data_range (acre, y_min, y_max);
283 acre_set_y_axis_range (acre_t *acre, double y_min, double y_max)
285 acre->y_axis.view_min = y_min;
286 acre->y_axis.view_max = y_max;
288 acre->y_axis.view_range_set = true;
292 acre_set_y_axis_range_auto (acre_t *acre)
294 acre->y_axis.view_range_set = false;
297 /* Add a dataset to the plot. The plot assumes ownership of the
298 * dataset so it is not necessary to call acre_data_destroy on it. */
300 acre_add_data (acre_t *acre, acre_data_t *data)
302 if (acre->num_data >= acre->data_size) {
303 acre->data_size *= 2;
304 if (acre->data_size == 0)
306 acre->data = xrealloc_ab (acre->data,
308 sizeof (acre_data_t *));
311 acre->data[acre->num_data] = data;
313 if (acre->num_data == 0) {
314 acre->x_axis.data_min = data->min.x;
315 acre->y_axis.data_min = data->min.y;
317 acre->x_axis.data_max = data->max.x;
318 acre->y_axis.data_max = data->max.y;
320 if (data->min.x < acre->x_axis.data_min)
321 acre->x_axis.data_min = data->min.x;
322 if (data->min.y < acre->y_axis.data_min)
323 acre->y_axis.data_min = data->min.y;
325 if (data->max.x > acre->x_axis.data_max)
326 acre->x_axis.data_max = data->max.x;
327 if (data->max.y > acre->y_axis.data_max)
328 acre->y_axis.data_max = data->max.y;
334 #define ACRE_FONT_FAMILY "sans"
335 #define ACRE_FONT_SIZE 12
336 #define ACRE_TITLE_FONT_SIZE 20
337 #define ACRE_PAD (ACRE_FONT_SIZE)
338 #define ACRE_TICK_MAJOR_SIZE 6
339 #define ACRE_TICK_MINOR_SIZE 3
340 #define ACRE_X_TICK_VALUE_PAD 2
341 #define ACRE_Y_TICK_VALUE_PAD 4
342 #define ACRE_LEGEND_PAD 4
343 #define ACRE_LEGEND_LINE_SIZE 10
346 _create_layout (acre_t *acre, const char *text)
353 layout = pango_cairo_create_layout (acre->cr);
354 pango_layout_set_font_description (layout, acre->font);
355 pango_layout_set_text (layout, text, -1);
356 pango_layout_set_alignment (layout, PANGO_ALIGN_CENTER);
361 #define PRINTF_FORMAT(fmt_index, va_index) __attribute__ ((__format__(__printf__, fmt_index, va_index)))
364 _create_layout_vprintf (acre_t *acre, const char *fmt, va_list ap)
369 vasprintf (&text, fmt, ap);
371 layout = _create_layout (acre, text);
379 _create_layout_printf (acre_t *acre, const char *fmt, ...)
380 PRINTF_FORMAT (2, 3);
383 _create_layout_printf (acre_t *acre, const char *fmt, ...)
390 layout = _create_layout_vprintf (acre, fmt, ap);
398 _destroy_layout (PangoLayout *layout)
400 g_object_unref (layout);
404 _show_layout (cairo_t *cr, PangoLayout *layout)
406 pango_cairo_show_layout (cr, layout);
408 _destroy_layout (layout);
412 _draw_title_and_labels (acre_t *acre)
414 cairo_t *cr = acre->cr;
415 PangoFontDescription *title_font;
416 PangoLayout *title_layout, *x_axis_layout, *y_axis_layout;
417 PangoLayout *min_y, *max_y;
418 int min_y_width, max_y_width, y_axis_value_width;
419 int title_width, title_height;
420 int x_axis_width, x_axis_height;
421 int y_axis_width, y_axis_height;
422 PangoRectangle new_chart;
426 acre->font = pango_font_description_new ();
427 pango_font_description_set_family (acre->font, ACRE_FONT_FAMILY);
428 pango_font_description_set_absolute_size (acre->font,
429 ACRE_FONT_SIZE * PANGO_SCALE);
431 title_font = pango_font_description_new ();
432 pango_font_description_set_family (title_font, ACRE_FONT_FAMILY);
433 pango_font_description_set_absolute_size (title_font,
434 ACRE_TITLE_FONT_SIZE * PANGO_SCALE);
436 title_layout = _create_layout (acre, acre->title);
437 pango_layout_set_font_description (title_layout, title_font);
439 x_axis_layout = _create_layout (acre, acre->x_axis.label);
440 y_axis_layout = _create_layout (acre, acre->y_axis.label);
442 min_y = _create_layout_printf (acre, "%g",
443 round (acre->y_axis.view_min));
444 max_y = _create_layout_printf (acre, "%g",
445 round (acre->y_axis.view_max));
447 pango_layout_get_pixel_size (min_y, &min_y_width, NULL);
448 pango_layout_get_pixel_size (max_y, &max_y_width, NULL);
449 y_axis_value_width = MAX (min_y_width, max_y_width);
451 _destroy_layout (min_y);
452 _destroy_layout (max_y);
454 /* Iterate with the layout of the title and axis labels until they
455 * are stable, (this requires iteration since we don't know what
456 * to set their widths to in advance due to the wrapping of the
457 * other elements). */
459 pango_layout_set_width (title_layout, acre->chart.width * PANGO_SCALE);
460 pango_layout_set_width (x_axis_layout, acre->chart.width * PANGO_SCALE);
461 pango_layout_set_width (y_axis_layout, acre->chart.height * PANGO_SCALE);
463 pango_layout_get_pixel_size (title_layout, &title_width, &title_height);
464 pango_layout_get_pixel_size (x_axis_layout, &x_axis_width, &x_axis_height);
465 pango_layout_get_pixel_size (y_axis_layout, &y_axis_width, &y_axis_height);
467 new_chart.x = ACRE_PAD + y_axis_height +
468 ACRE_PAD + y_axis_value_width + ACRE_Y_TICK_VALUE_PAD;
469 new_chart.width = acre->width - acre->chart.x - ACRE_PAD;
471 new_chart.y = ACRE_PAD + title_height + ACRE_PAD;
472 new_chart.height = acre->height - acre->chart.y -
473 (ACRE_X_TICK_VALUE_PAD + ACRE_FONT_SIZE +
474 ACRE_PAD + x_axis_height + ACRE_PAD);
476 if (new_chart.x == acre->chart.x &&
477 new_chart.y == acre->chart.y &&
478 new_chart.width == acre->chart.width &&
479 new_chart.height == acre->chart.height)
484 acre->chart.x = new_chart.x;
485 acre->chart.y = new_chart.y;
486 acre->chart.width = new_chart.width;
487 acre->chart.height = new_chart.height;
490 cairo_set_source_rgb (cr, 0, 0, 0);
492 cairo_move_to (cr, acre->chart.x, ACRE_PAD);
493 _show_layout (cr, title_layout);
497 cairo_translate (cr, ACRE_PAD, acre->chart.y + acre->chart.height);
498 cairo_rotate (cr, - M_PI / 2.0);
499 cairo_move_to (cr, 0, 0);
500 _show_layout (cr, y_axis_layout);
504 cairo_move_to (cr, acre->chart.x,
505 acre->chart.y + acre->chart.height +
506 ACRE_FONT_SIZE + ACRE_PAD);
507 _show_layout (cr, x_axis_layout);
512 /* For a given axis range, compute a step size (in data space) to
513 * generate a suitable number of ticks (5 or so). */
515 _step_for_range (double range, int *minor_divisions)
517 double step, scale_factor;
519 /* We want roughly 5 major ticks for the chart. */
522 /* Normalize the step so we can easily snap it to a desirable
524 scale_factor = pow (10.0, floor (log10 (step)));
525 step /= scale_factor;
527 /* We want increments of 1, 2.5, 5, or 10 (times some power of
528 * 10). The threshold values between these are computed
529 * logarithmically. */
530 if (step < 3.535533905932738) {
531 if (step < 1.58113883008419) {
533 *minor_divisions = 4;
536 *minor_divisions = 5;
539 if (step < 7.071067811865475) {
541 *minor_divisions = 5;
544 *minor_divisions = 4;
548 /* Un-normalize and we now have the data value that we want to
550 return step * scale_factor;
553 /* Given an axis range, we can compute a desired data-space step
554 * amount for the major ticks (see _step_for_range). To get
555 * nice-looking pixel-snapped ticks we want to expand the range
558 _expand_range_for_width (double *axis_min, double *axis_max, int pixel_range)
560 double range, new_range, step, step_minor, pixel_step;
563 range = *axis_max - *axis_min;
565 step = _step_for_range (range, &minor_divisions);
566 step_minor = step / minor_divisions;
568 pixel_step = step_minor * (pixel_range / range);
570 /* We expand the range by the ratio of the pixel step to the floor
573 new_range = range * pixel_step / floor (pixel_step);
575 /* And spread the increase out on either side of the range. */
576 *axis_min -= (new_range - range) / 2.0;
577 *axis_max += (new_range - range) / 2.0;
580 /* Setup a transformation in acre->cr such that data values plotted
581 * will appear where they should within the chart.
584 _set_transform_to_data_space (acre_t *acre)
586 cairo_t *cr = acre->cr;
590 acre->chart.y + acre->chart.height);
592 acre->chart.width / (acre->x_axis.view_max - acre->x_axis.view_min),
593 - acre->chart.height /(acre->y_axis.view_max - acre->y_axis.view_min));
594 cairo_translate (cr, -acre->x_axis.view_min, -acre->y_axis.view_min);
598 _find_x_range_given_y_range (acre_t *acre,
599 double *x_min, double *x_max,
600 double y_min, double y_max)
608 for (d = 0; d < acre->num_data; d++) {
609 data = acre->data[d];
610 for (i = 0; i < data->num_points; i++) {
611 if (data->points[i].y >= y_min &&
612 data->points[i].y <= y_max)
615 *x_min = data->points[i].x;
616 *x_max = data->points[i].x;
619 if (data->points[i].x < *x_min)
620 *x_min = data->points[i].x;
621 if (data->points[i].x > *x_max)
622 *x_max = data->points[i].x;
628 /* If nothing is visible, punt to full X data range. */
630 *x_min = acre->x_axis.data_min;
631 *x_max = acre->x_axis.data_max;
636 _find_y_range_given_x_range (acre_t *acre,
637 double *y_min, double *y_max,
638 double x_min, double x_max)
646 for (d = 0; d < acre->num_data; d++) {
647 data = acre->data[d];
648 for (i = 0; i < data->num_points; i++) {
649 if (data->points[i].x >= x_min &&
650 data->points[i].x <= x_max)
653 *y_min = data->points[i].y;
654 *y_max = data->points[i].y;
657 if (data->points[i].y < *y_min)
658 *y_min = data->points[i].y;
659 if (data->points[i].y > *y_max)
660 *y_max = data->points[i].y;
666 /* If nothing is visible, punt to full Y data range. */
668 *y_min = acre->y_axis.data_min;
669 *y_max = acre->y_axis.data_max;
674 _compute_axis_ranges (acre_t *acre)
676 double x_adjust, y_adjust;
677 cairo_t *cr = acre->cr;
679 /* If neither view range is set, set both to data ranges. */
680 if (! acre->x_axis.view_range_set && ! acre->y_axis.view_range_set)
682 acre->x_axis.view_min = acre->x_axis.data_min;
683 acre->x_axis.view_max = acre->x_axis.data_max;
685 acre->y_axis.view_min = acre->y_axis.data_min;
686 acre->y_axis.view_max = acre->y_axis.data_max;
688 /* Otherwise, auto-fit unset range based on data. */
689 if (acre->x_axis.view_range_set && ! acre->y_axis.view_range_set) {
690 _find_y_range_given_x_range (acre,
691 &acre->y_axis.view_min,
692 &acre->y_axis.view_max,
693 acre->x_axis.view_min,
694 acre->x_axis.view_max);
696 else if (acre->y_axis.view_range_set && ! acre->x_axis.view_range_set) {
697 _find_x_range_given_y_range (acre,
698 &acre->x_axis.view_min,
699 &acre->x_axis.view_max,
700 acre->y_axis.view_min,
701 acre->y_axis.view_max);
705 /* Then, increase the axis ranges just enough so that the step
706 * sizes for the ticks will be integers.
708 _expand_range_for_width (&acre->x_axis.view_min,
709 &acre->x_axis.view_max,
712 _expand_range_for_width (&acre->y_axis.view_min,
713 &acre->y_axis.view_max,
716 /* Finally, we also translate the axis ranges slightly so that the
717 * ticks land on half-integer device-pixel positions.
721 _set_transform_to_data_space (acre);
725 cairo_user_to_device (cr, &x_adjust, &y_adjust);
726 x_adjust = (round (x_adjust + 0.5) - 0.5) - x_adjust;
727 y_adjust = (round (y_adjust + 0.5) - 0.5) - y_adjust;
728 cairo_device_to_user_distance (cr, &x_adjust, &y_adjust);
730 acre->x_axis.view_min -= x_adjust;
731 acre->x_axis.view_max -= x_adjust;
733 acre->y_axis.view_min -= y_adjust;
734 acre->y_axis.view_max -= y_adjust;
740 _choose_colors (acre_t *acre)
742 cmsHPROFILE lab_profile, srgb_profile;
743 cmsHTRANSFORM lab_to_srgb;
745 double theta, radius, srgb[3];
748 lab_profile = cmsCreateLabProfile (NULL); /* D50 */
749 srgb_profile = cmsCreate_sRGBProfile ();
751 lab_to_srgb = cmsCreateTransform (lab_profile, TYPE_Lab_DBL,
752 srgb_profile, TYPE_RGB_DBL,
753 INTENT_PERCEPTUAL, 0);
755 acre->num_colors = acre->num_data;
757 if (acre->num_colors > acre->colors_size) {
758 acre->colors_size = acre->num_colors;
759 acre->colors = xrealloc (acre->colors,
760 acre->colors_size * sizeof (acre_color_t));
765 for (i = 0; i < acre->num_colors; i++) {
766 theta = 0.713 + 2 * M_PI * (double) i / acre->num_colors;
767 lab.a = radius * cos (theta);
768 lab.b = radius * sin (theta);
770 cmsDoTransform (lab_to_srgb, &lab, srgb, 1);
772 acre->colors[i].red = srgb[0];
773 acre->colors[i].green = srgb[1];
774 acre->colors[i].blue = srgb[2];
777 cmsDeleteTransform (lab_to_srgb);
778 cmsCloseProfile (lab_profile);
779 cmsCloseProfile (srgb_profile);
783 _draw_data (acre_t *acre)
785 cairo_t *cr = acre->cr;
792 acre->chart.x, acre->chart.y,
793 acre->chart.width, acre->chart.height);
796 cairo_set_source_rgb (cr, 0, 0, 0);
798 _set_transform_to_data_space (acre);
800 for (d = 0; d < acre->num_data; d++) {
801 int color = d % acre->num_colors;
802 cairo_set_source_rgb (cr,
803 acre->colors[color].red,
804 acre->colors[color].green,
805 acre->colors[color].blue);
806 data = acre->data[d];
810 for (i = 0; i < data->num_points; i++) {
817 cairo_identity_matrix (cr);
818 cairo_set_line_width (cr, 1.0);
827 typedef enum _ticks { ACRE_TICKS_X, ACRE_TICKS_Y } acre_ticks_t;
830 _draw_ticks (acre_t *acre,
831 double axis_min, double axis_max,
834 cairo_t *cr = acre->cr;
835 double t, step, sub_step;
840 _set_transform_to_data_space (acre);
842 step = _step_for_range (axis_max - axis_min, &minor_divisions);
843 sub_step = step / minor_divisions;
845 for (t = (floor (axis_min / sub_step) + 1) * sub_step;
850 if (fabs((t / step) - round (t / step)) < 0.5 * (sub_step / step))
851 tick_size = ACRE_TICK_MAJOR_SIZE;
853 tick_size = ACRE_TICK_MINOR_SIZE;
858 if (ticks == ACRE_TICKS_X)
859 cairo_move_to (cr, t, acre->y_axis.view_min);
861 cairo_move_to (cr, acre->x_axis.view_min, t);
863 cairo_identity_matrix (cr);
865 if (ticks == ACRE_TICKS_X) {
866 cairo_rel_line_to (cr, 0, 0.5);
867 cairo_rel_line_to (cr, 0, -tick_size - 0.5);
869 cairo_rel_line_to (cr, -0.5, 0);
870 cairo_rel_line_to (cr, tick_size + 0.5, 0);
873 cairo_set_line_width (cr, 1.0);
879 if (tick_size == ACRE_TICK_MAJOR_SIZE)
886 layout = _create_layout_printf (acre, "%g", t);
888 if (ticks == ACRE_TICKS_X)
889 cairo_move_to (cr, t, acre->y_axis.view_min);
891 cairo_move_to (cr, acre->x_axis.view_min, t);
893 cairo_identity_matrix (cr);
894 pango_layout_get_pixel_size (layout, &width, &height);
896 if (ticks == ACRE_TICKS_X)
897 cairo_rel_move_to (cr, -width / 2, ACRE_X_TICK_VALUE_PAD);
899 cairo_rel_move_to (cr, -width - ACRE_Y_TICK_VALUE_PAD,
902 _show_layout (cr, layout);
912 _draw_legend (acre_t *acre)
915 int label_width, max_label_width = 0;
918 cairo_t *cr = acre->cr;
922 for (i = 0; i < acre->num_data; i++) {
923 layout = _create_layout (acre, acre->data[i]->name);
924 pango_layout_get_pixel_size (layout, &label_width, NULL);
925 _destroy_layout (layout);
926 if (label_width > max_label_width)
927 max_label_width = label_width;
930 width = ACRE_LEGEND_PAD + ACRE_LEGEND_LINE_SIZE + ACRE_LEGEND_PAD +
931 max_label_width + ACRE_LEGEND_PAD;
932 height = ACRE_LEGEND_PAD +
933 acre->num_data * (ACRE_FONT_SIZE + ACRE_LEGEND_PAD);
935 cairo_translate (cr, acre->chart.x, acre->chart.y);
938 acre->chart.width - ACRE_LEGEND_PAD - width,
941 cairo_rectangle (cr, -0.5, -0.5, width + 1.0, height + 1.0);
942 cairo_set_source_rgb (cr, 0, 0, 0);
943 cairo_set_line_width (cr, 1.0);
946 cairo_translate (cr, ACRE_LEGEND_PAD, ACRE_LEGEND_PAD);
948 for (i = 0; i < acre->num_data; i++) {
950 0, ACRE_LEGEND_LINE_SIZE / 2,
951 ACRE_LEGEND_LINE_SIZE, ACRE_LEGEND_LINE_SIZE / 2);
952 cairo_set_source_rgb (cr,
953 acre->colors[i % acre->num_colors].red,
954 acre->colors[i % acre->num_colors].green,
955 acre->colors[i % acre->num_colors].blue);
958 layout = _create_layout (acre, acre->data[i]->name);
959 cairo_move_to (cr, ACRE_LEGEND_LINE_SIZE + ACRE_LEGEND_PAD, 0);
960 cairo_set_source_rgb (cr, 0, 0, 0);
961 _show_layout (cr, layout);
963 cairo_translate (cr, 0, ACRE_LEGEND_PAD + ACRE_FONT_SIZE);
970 _draw_frame_and_ticks (acre_t *acre)
972 cairo_t *cr = acre->cr;
976 cairo_set_source_rgb (cr, 0, 0, 0); /* black */
979 _draw_ticks (acre, acre->x_axis.view_min, acre->x_axis.view_max, ACRE_TICKS_X);
980 _draw_ticks (acre, acre->y_axis.view_min, acre->y_axis.view_max, ACRE_TICKS_Y);
984 acre->chart.x - 0.5, acre->chart.y - 0.5,
985 acre->chart.width + 1.0, acre->chart.height + 1.0);
986 cairo_set_line_width (cr, 1.0);
992 /* Draw the plot to the given cairo context within a user-space
993 * rectangle from (0, 0) to (width, height). This size includes all
994 * space for extra-plot elements (such as the title, the axis labels,
998 acre_draw (acre_t *acre, cairo_t *cr, int width, int height)
1002 acre->width = width;
1003 acre->height = height;
1005 acre->chart.width = width;
1006 acre->chart.height = height;
1010 cairo_set_source_rgb (cr, 1, 1, 1);
1012 _choose_colors (acre);
1014 /* We compute the axis ranges before doing label layout so that we
1015 * can account for the width of the y-axis value labels. */
1016 _compute_axis_ranges (acre);
1018 _draw_title_and_labels (acre);
1020 /* And we recompute the axis ranges now that the title and axis
1021 * label space is all measured and accounted for. */
1022 _compute_axis_ranges (acre);
1026 if (acre->num_data > 1)
1027 _draw_legend (acre);
1029 _draw_frame_and_ticks (acre);
1032 /* Create a new dataset---a collection of (x, y) datapoints. A single
1033 * plot can contain multiple datasets, (see acre_add_data). */
1035 acre_data_create (void)
1039 data = xmalloc (sizeof (acre_data_t));
1043 data->points = NULL;
1044 data->points_size = 0;
1045 data->num_points = 0;
1050 /* Destroy an acre dataset. Do not call this function if the dataset
1051 * has been added to an acre_t plot with acre_add_data. */
1053 acre_data_destroy (acre_data_t *data)
1055 free (data->points);
1060 /* Set the label for this dataset (to appear in the plot's key). */
1062 acre_data_set_name (acre_data_t *data, const char *name)
1066 data->name = strdup (name);
1069 /* Add a datapoint to the given dataset. */
1071 acre_data_add_point_2d (acre_data_t *data, double x, double y)
1073 if (data->num_points >= data->points_size) {
1074 data->points_size *= 2;
1075 if (data->points_size == 0)
1076 data->points_size = 16;
1077 data->points = xrealloc_ab (data->points,
1079 sizeof (acre_data_point_2d_t));
1082 data->points[data->num_points].x = x;
1083 data->points[data->num_points].y = y;
1085 if (data->num_points == 0) {
1092 if (x < data->min.x)
1094 if (y < data->min.y)
1097 if (x > data->max.x)
1099 if (y > data->max.y)