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;
40 /* The name of this data set. */
43 /* The style for rendering (line, bar, etc.) */
46 /* Minimum and mximum extents of data. */
47 acre_data_point_2d_t min;
48 acre_data_point_2d_t max;
50 /* The data itself. */
51 acre_data_point_2d_t *points;
52 unsigned int points_size;
53 unsigned int num_points;
55 /* The names of data points (if any).
57 * This array is indexed with the same index as the 'points' array
58 * to provide names for points. It is legal for this array to be
59 * NULL or smaller than 'points', (in which case, the
60 * corresponding points simply have no names).
63 unsigned int names_size;
64 unsigned int num_names;
67 typedef struct _acre_axis {
74 /* Range of data to be viewed. */
78 /* Has the view range been set? */
82 typedef struct _acre_color {
94 unsigned int data_size;
95 unsigned int num_data;
97 /* Data for drawing. */
99 PangoFontDescription *font;
100 acre_color_t *colors;
104 /* Total size including labels. */
108 /* Position and size of chart alone. */
109 PangoRectangle chart;
113 _find_x_range_given_y_range (acre_t *acre,
114 double *x_min, double *x_max,
115 double y_min, double y_max);
118 _find_y_range_given_x_range (acre_t *acre,
119 double *y_min, double *y_max,
120 double x_min, double x_max);
122 /* Create a new, empty plot. */
128 acre = xmalloc (sizeof (acre_t));
132 acre->x_axis.label = NULL;
133 acre->x_axis.data_min = 0.0;
134 acre->x_axis.data_max = 0.0;
135 acre->x_axis.view_min = 0.0;
136 acre->x_axis.view_max = 0.0;
137 acre->x_axis.view_range_set = false;
139 acre->y_axis.label = NULL;
140 acre->y_axis.data_min = 0.0;
141 acre->y_axis.data_max = 0.0;
142 acre->y_axis.view_min = 0.0;
143 acre->y_axis.view_max = 0.0;
144 acre->y_axis.view_range_set = false;
153 acre->num_colors = 0;
154 acre->colors_size = 0;
161 acre->chart.width = 0;
162 acre->chart.height = 0;
167 /* Destroy a plot. */
169 acre_destroy (acre_t *acre)
174 free (acre->x_axis.label);
175 free (acre->y_axis.label);
177 for (i = 0; i < acre->num_data; i++)
178 acre_data_destroy (acre->data[i]);
188 acre_set_title (acre_t *acre, const char *title)
192 acre->title = strdup (title);
196 acre_set_x_axis_label (acre_t *acre, const char *label)
198 free (acre->x_axis.label);
200 acre->x_axis.label = strdup (label);
204 acre_set_y_axis_label (acre_t *acre, const char *label)
206 free (acre->y_axis.label);
208 acre->y_axis.label = strdup (label);
212 acre_get_x_axis_data_range (acre_t *acre, double *x_min, double *x_max)
215 *x_min = acre->x_axis.data_min;
218 *x_max = acre->x_axis.data_max;
222 acre_get_x_axis_range (acre_t *acre, double *x_min, double *x_max)
224 /* If an X range has been set, return that. */
225 if (acre->x_axis.view_range_set) {
227 *x_min = acre->x_axis.view_min;
230 *x_max = acre->x_axis.view_max;
235 /* Otherwise, if a Y range has been set, use that to compute X. */
236 if (acre->y_axis.view_range_set) {
237 _find_x_range_given_y_range (acre, x_min, x_max,
238 acre->y_axis.view_min,
239 acre->y_axis.view_max);
244 /* Neither view range set. Return full, data-based X range. */
245 acre_get_x_axis_data_range (acre, x_min, x_max);
249 acre_set_x_axis_range (acre_t *acre, double x_min, double x_max)
251 acre->x_axis.view_min = x_min;
252 acre->x_axis.view_max = x_max;
254 acre->x_axis.view_range_set = true;
258 acre_set_x_axis_range_auto (acre_t *acre)
260 acre->x_axis.view_range_set = false;
264 acre_get_y_axis_data_range (acre_t *acre, double *y_min, double *y_max)
267 *y_min = acre->y_axis.data_min;
270 *y_max = acre->y_axis.data_max;
274 acre_get_y_axis_range (acre_t *acre, double *y_min, double *y_max)
276 /* If a Y range has been set, return that. */
277 if (acre->y_axis.view_range_set) {
279 *y_min = acre->y_axis.view_min;
282 *y_max = acre->y_axis.view_max;
287 /* Otherwise, if an X range has been set, use that to compute Y. */
288 if (acre->x_axis.view_range_set) {
289 _find_y_range_given_x_range (acre, y_min, y_max,
290 acre->x_axis.view_min,
291 acre->x_axis.view_max);
296 /* Neither view range set. Return full data-based Y range. */
297 acre_get_y_axis_data_range (acre, y_min, y_max);
301 acre_set_y_axis_range (acre_t *acre, double y_min, double y_max)
303 acre->y_axis.view_min = y_min;
304 acre->y_axis.view_max = y_max;
306 acre->y_axis.view_range_set = true;
310 acre_set_y_axis_range_auto (acre_t *acre)
312 acre->y_axis.view_range_set = false;
315 /* Add a dataset to the plot. The plot assumes ownership of the
316 * dataset so it is not necessary to call acre_data_destroy on it. */
318 acre_add_data (acre_t *acre, acre_data_t *data)
320 if (acre->num_data >= acre->data_size) {
321 acre->data_size *= 2;
322 if (acre->data_size == 0)
324 acre->data = xrealloc_ab (acre->data,
326 sizeof (acre_data_t *));
329 acre->data[acre->num_data] = data;
331 if (acre->num_data == 0) {
332 acre->x_axis.data_min = data->min.x;
333 acre->y_axis.data_min = data->min.y;
335 acre->x_axis.data_max = data->max.x;
336 acre->y_axis.data_max = data->max.y;
338 if (data->min.x < acre->x_axis.data_min)
339 acre->x_axis.data_min = data->min.x;
340 if (data->min.y < acre->y_axis.data_min)
341 acre->y_axis.data_min = data->min.y;
343 if (data->max.x > acre->x_axis.data_max)
344 acre->x_axis.data_max = data->max.x;
345 if (data->max.y > acre->y_axis.data_max)
346 acre->y_axis.data_max = data->max.y;
352 #define ACRE_FONT_FAMILY "sans"
353 #define ACRE_FONT_SIZE 12
354 #define ACRE_TITLE_FONT_SIZE 20
355 #define ACRE_PAD (ACRE_FONT_SIZE)
356 #define ACRE_TICK_MAJOR_SIZE 6
357 #define ACRE_TICK_MINOR_SIZE 3
358 #define ACRE_X_TICK_VALUE_PAD 2
359 #define ACRE_Y_TICK_VALUE_PAD 4
360 #define ACRE_LEGEND_PAD 4
361 #define ACRE_LEGEND_LINE_SIZE 10
364 _create_layout (acre_t *acre, const char *text)
371 layout = pango_cairo_create_layout (acre->cr);
372 pango_layout_set_font_description (layout, acre->font);
373 pango_layout_set_text (layout, text, -1);
374 pango_layout_set_alignment (layout, PANGO_ALIGN_CENTER);
379 #define PRINTF_FORMAT(fmt_index, va_index) __attribute__ ((__format__(__printf__, fmt_index, va_index)))
382 _create_layout_vprintf (acre_t *acre, const char *fmt, va_list ap)
387 vasprintf (&text, fmt, ap);
389 layout = _create_layout (acre, text);
397 _create_layout_printf (acre_t *acre, const char *fmt, ...)
398 PRINTF_FORMAT (2, 3);
401 _create_layout_printf (acre_t *acre, const char *fmt, ...)
408 layout = _create_layout_vprintf (acre, fmt, ap);
416 _destroy_layout (PangoLayout *layout)
418 g_object_unref (layout);
422 _show_layout (cairo_t *cr, PangoLayout *layout)
424 pango_cairo_show_layout (cr, layout);
426 _destroy_layout (layout);
430 _draw_title_and_labels (acre_t *acre)
432 cairo_t *cr = acre->cr;
433 PangoFontDescription *title_font;
434 PangoLayout *title_layout, *x_axis_layout, *y_axis_layout;
435 PangoLayout *min_y, *max_y;
436 int min_y_width, max_y_width, y_axis_value_width;
437 int title_width, title_height;
438 int x_axis_width, x_axis_height;
439 int y_axis_width, y_axis_height;
440 PangoRectangle new_chart;
444 acre->font = pango_font_description_new ();
445 pango_font_description_set_family (acre->font, ACRE_FONT_FAMILY);
446 pango_font_description_set_absolute_size (acre->font,
447 ACRE_FONT_SIZE * PANGO_SCALE);
449 title_font = pango_font_description_new ();
450 pango_font_description_set_family (title_font, ACRE_FONT_FAMILY);
451 pango_font_description_set_absolute_size (title_font,
452 ACRE_TITLE_FONT_SIZE * PANGO_SCALE);
454 title_layout = _create_layout (acre, acre->title);
455 pango_layout_set_font_description (title_layout, title_font);
456 pango_font_description_free (title_font);
458 x_axis_layout = _create_layout (acre, acre->x_axis.label);
459 y_axis_layout = _create_layout (acre, acre->y_axis.label);
461 min_y = _create_layout_printf (acre, "%g",
462 round (acre->y_axis.view_min));
463 max_y = _create_layout_printf (acre, "%g",
464 round (acre->y_axis.view_max));
466 pango_layout_get_pixel_size (min_y, &min_y_width, NULL);
467 pango_layout_get_pixel_size (max_y, &max_y_width, NULL);
468 y_axis_value_width = MAX (min_y_width, max_y_width);
470 _destroy_layout (min_y);
471 _destroy_layout (max_y);
473 /* Iterate with the layout of the title and axis labels until they
474 * are stable, (this requires iteration since we don't know what
475 * to set their widths to in advance due to the wrapping of the
476 * other elements). */
478 pango_layout_set_width (title_layout, acre->chart.width * PANGO_SCALE);
479 pango_layout_set_width (x_axis_layout, acre->chart.width * PANGO_SCALE);
480 pango_layout_set_width (y_axis_layout, acre->chart.height * PANGO_SCALE);
482 pango_layout_get_pixel_size (title_layout, &title_width, &title_height);
483 pango_layout_get_pixel_size (x_axis_layout, &x_axis_width, &x_axis_height);
484 pango_layout_get_pixel_size (y_axis_layout, &y_axis_width, &y_axis_height);
486 new_chart.x = ACRE_PAD + y_axis_height +
487 ACRE_PAD + y_axis_value_width + ACRE_Y_TICK_VALUE_PAD;
488 new_chart.width = acre->width - acre->chart.x - ACRE_PAD;
490 new_chart.y = ACRE_PAD + title_height + ACRE_PAD;
491 new_chart.height = acre->height - acre->chart.y -
492 (ACRE_X_TICK_VALUE_PAD + ACRE_FONT_SIZE +
493 ACRE_PAD + x_axis_height + ACRE_PAD);
495 if (new_chart.x == acre->chart.x &&
496 new_chart.y == acre->chart.y &&
497 new_chart.width == acre->chart.width &&
498 new_chart.height == acre->chart.height)
503 acre->chart.x = new_chart.x;
504 acre->chart.y = new_chart.y;
505 acre->chart.width = new_chart.width;
506 acre->chart.height = new_chart.height;
509 cairo_set_source_rgb (cr, 0, 0, 0);
511 cairo_move_to (cr, acre->chart.x, ACRE_PAD);
512 _show_layout (cr, title_layout);
516 cairo_translate (cr, ACRE_PAD, acre->chart.y + acre->chart.height);
517 cairo_rotate (cr, - M_PI / 2.0);
518 cairo_move_to (cr, 0, 0);
519 _show_layout (cr, y_axis_layout);
523 cairo_move_to (cr, acre->chart.x,
524 acre->chart.y + acre->chart.height +
525 ACRE_FONT_SIZE + ACRE_PAD);
526 _show_layout (cr, x_axis_layout);
531 /* For a given axis range, compute a step size (in data space) to
532 * generate a suitable number of ticks (5 or so). */
534 _step_for_range (double range, int *minor_divisions)
536 double step, scale_factor;
538 /* We want roughly 5 major ticks for the chart. */
541 /* Normalize the step so we can easily snap it to a desirable
543 scale_factor = pow (10.0, floor (log10 (step)));
544 step /= scale_factor;
546 /* We want increments of 1, 2.5, 5, or 10 (times some power of
547 * 10). The threshold values between these are computed
548 * logarithmically. */
549 if (step < 3.535533905932738) {
550 if (step < 1.58113883008419) {
552 *minor_divisions = 4;
555 *minor_divisions = 5;
558 if (step < 7.071067811865475) {
560 *minor_divisions = 5;
563 *minor_divisions = 4;
567 /* Un-normalize and we now have the data value that we want to
569 return step * scale_factor;
572 /* Given an axis range, we can compute a desired data-space step
573 * amount for the major ticks (see _step_for_range). To get
574 * nice-looking pixel-snapped ticks we want to expand the range
577 _expand_range_for_width (double *axis_min, double *axis_max, int pixel_range)
579 double range, new_range, step, step_minor, pixel_step;
582 range = *axis_max - *axis_min;
584 step = _step_for_range (range, &minor_divisions);
585 step_minor = step / minor_divisions;
587 pixel_step = step_minor * (pixel_range / range);
589 /* We expand the range by the ratio of the pixel step to the floor
592 new_range = range * pixel_step / floor (pixel_step);
594 /* And spread the increase out on either side of the range. */
595 *axis_min -= (new_range - range) / 2.0;
596 *axis_max += (new_range - range) / 2.0;
599 /* Setup a transformation in acre->cr such that data values plotted
600 * will appear where they should within the chart.
603 _set_transform_to_data_space (acre_t *acre)
605 cairo_t *cr = acre->cr;
609 acre->chart.y + acre->chart.height);
611 acre->chart.width / (acre->x_axis.view_max - acre->x_axis.view_min),
612 - acre->chart.height /(acre->y_axis.view_max - acre->y_axis.view_min));
613 cairo_translate (cr, -acre->x_axis.view_min, -acre->y_axis.view_min);
617 _find_x_range_given_y_range (acre_t *acre,
618 double *x_min, double *x_max,
619 double y_min, double y_max)
627 for (d = 0; d < acre->num_data; d++) {
628 data = acre->data[d];
629 for (i = 0; i < data->num_points; i++) {
630 if (data->points[i].y >= y_min &&
631 data->points[i].y <= y_max)
634 *x_min = data->points[i].x;
635 *x_max = data->points[i].x;
638 if (data->points[i].x < *x_min)
639 *x_min = data->points[i].x;
640 if (data->points[i].x > *x_max)
641 *x_max = data->points[i].x;
647 /* If nothing is visible, punt to full X data range. */
649 *x_min = acre->x_axis.data_min;
650 *x_max = acre->x_axis.data_max;
655 _find_y_range_given_x_range (acre_t *acre,
656 double *y_min, double *y_max,
657 double x_min, double x_max)
665 for (d = 0; d < acre->num_data; d++) {
666 data = acre->data[d];
667 for (i = 0; i < data->num_points; i++) {
668 if (data->points[i].x >= x_min &&
669 data->points[i].x <= x_max)
672 *y_min = data->points[i].y;
673 *y_max = data->points[i].y;
676 if (data->points[i].y < *y_min)
677 *y_min = data->points[i].y;
678 if (data->points[i].y > *y_max)
679 *y_max = data->points[i].y;
685 /* If nothing is visible, punt to full Y data range. */
687 *y_min = acre->y_axis.data_min;
688 *y_max = acre->y_axis.data_max;
693 _compute_axis_ranges (acre_t *acre)
695 double x_adjust, y_adjust;
696 cairo_t *cr = acre->cr;
698 /* If neither view range is set, set both to data ranges. */
699 if (! acre->x_axis.view_range_set && ! acre->y_axis.view_range_set)
701 acre->x_axis.view_min = acre->x_axis.data_min;
702 acre->x_axis.view_max = acre->x_axis.data_max;
704 acre->y_axis.view_min = acre->y_axis.data_min;
705 acre->y_axis.view_max = acre->y_axis.data_max;
707 /* Otherwise, auto-fit unset range based on data. */
708 if (acre->x_axis.view_range_set && ! acre->y_axis.view_range_set) {
709 _find_y_range_given_x_range (acre,
710 &acre->y_axis.view_min,
711 &acre->y_axis.view_max,
712 acre->x_axis.view_min,
713 acre->x_axis.view_max);
715 else if (acre->y_axis.view_range_set && ! acre->x_axis.view_range_set) {
716 _find_x_range_given_y_range (acre,
717 &acre->x_axis.view_min,
718 &acre->x_axis.view_max,
719 acre->y_axis.view_min,
720 acre->y_axis.view_max);
724 /* Then, increase the axis ranges just enough so that the step
725 * sizes for the ticks will be integers.
727 _expand_range_for_width (&acre->x_axis.view_min,
728 &acre->x_axis.view_max,
731 _expand_range_for_width (&acre->y_axis.view_min,
732 &acre->y_axis.view_max,
735 /* Finally, we also translate the axis ranges slightly so that the
736 * ticks land on half-integer device-pixel positions.
740 _set_transform_to_data_space (acre);
744 cairo_user_to_device (cr, &x_adjust, &y_adjust);
745 x_adjust = (round (x_adjust + 0.5) - 0.5) - x_adjust;
746 y_adjust = (round (y_adjust + 0.5) - 0.5) - y_adjust;
747 cairo_device_to_user_distance (cr, &x_adjust, &y_adjust);
749 acre->x_axis.view_min -= x_adjust;
750 acre->x_axis.view_max -= x_adjust;
752 acre->y_axis.view_min -= y_adjust;
753 acre->y_axis.view_max -= y_adjust;
759 _choose_colors (acre_t *acre)
761 cmsHPROFILE lab_profile, srgb_profile;
762 cmsHTRANSFORM lab_to_srgb;
764 double theta, radius, srgb[3];
767 lab_profile = cmsCreateLabProfile (NULL); /* D50 */
768 srgb_profile = cmsCreate_sRGBProfile ();
770 lab_to_srgb = cmsCreateTransform (lab_profile, TYPE_Lab_DBL,
771 srgb_profile, TYPE_RGB_DBL,
772 INTENT_PERCEPTUAL, 0);
774 acre->num_colors = acre->num_data;
776 if (acre->num_colors > acre->colors_size) {
777 acre->colors_size = acre->num_colors;
778 acre->colors = xrealloc (acre->colors,
779 acre->colors_size * sizeof (acre_color_t));
784 for (i = 0; i < acre->num_colors; i++) {
785 theta = 0.713 + 2 * M_PI * (double) i / acre->num_colors;
786 lab.a = radius * cos (theta);
787 lab.b = radius * sin (theta);
789 cmsDoTransform (lab_to_srgb, &lab, srgb, 1);
791 acre->colors[i].red = srgb[0];
792 acre->colors[i].green = srgb[1];
793 acre->colors[i].blue = srgb[2];
796 cmsDeleteTransform (lab_to_srgb);
797 cmsCloseProfile (lab_profile);
798 cmsCloseProfile (srgb_profile);
801 /* Draw the given dataset as a line. */
803 _draw_data_line (acre_t *acre, acre_data_t *data)
806 cairo_t *cr = acre->cr;
812 for (i = 0; i < data->num_points; i++) {
818 cairo_identity_matrix (cr);
819 cairo_set_line_width (cr, 1.0);
825 /* Draw all the datasets of the chart. */
827 _draw_data (acre_t *acre)
829 cairo_t *cr = acre->cr;
836 acre->chart.x, acre->chart.y,
837 acre->chart.width, acre->chart.height);
840 cairo_set_source_rgb (cr, 0, 0, 0);
842 _set_transform_to_data_space (acre);
844 for (i = 0; i < acre->num_data; i++) {
845 int color = i % acre->num_colors;
846 cairo_set_source_rgb (cr,
847 acre->colors[color].red,
848 acre->colors[color].green,
849 acre->colors[color].blue);
850 data = acre->data[i];
852 switch (data->style) {
853 case ACRE_STYLE_LINE:
854 _draw_data_line (acre, data);
862 typedef enum _ticks { ACRE_TICKS_X, ACRE_TICKS_Y } acre_ticks_t;
865 _draw_ticks (acre_t *acre,
866 double axis_min, double axis_max,
869 cairo_t *cr = acre->cr;
870 double t, step, sub_step;
875 _set_transform_to_data_space (acre);
877 step = _step_for_range (axis_max - axis_min, &minor_divisions);
878 sub_step = step / minor_divisions;
880 for (t = (floor (axis_min / sub_step) + 1) * sub_step;
885 if (fabs((t / step) - round (t / step)) < 0.5 * (sub_step / step))
886 tick_size = ACRE_TICK_MAJOR_SIZE;
888 tick_size = ACRE_TICK_MINOR_SIZE;
893 if (ticks == ACRE_TICKS_X)
894 cairo_move_to (cr, t, acre->y_axis.view_min);
896 cairo_move_to (cr, acre->x_axis.view_min, t);
898 cairo_identity_matrix (cr);
900 if (ticks == ACRE_TICKS_X) {
901 cairo_rel_line_to (cr, 0, 0.5);
902 cairo_rel_line_to (cr, 0, -tick_size - 0.5);
904 cairo_rel_line_to (cr, -0.5, 0);
905 cairo_rel_line_to (cr, tick_size + 0.5, 0);
908 cairo_set_line_width (cr, 1.0);
914 if (tick_size == ACRE_TICK_MAJOR_SIZE)
921 layout = _create_layout_printf (acre, "%g", t);
923 if (ticks == ACRE_TICKS_X)
924 cairo_move_to (cr, t, acre->y_axis.view_min);
926 cairo_move_to (cr, acre->x_axis.view_min, t);
928 cairo_identity_matrix (cr);
929 pango_layout_get_pixel_size (layout, &width, &height);
931 if (ticks == ACRE_TICKS_X)
932 cairo_rel_move_to (cr, -width / 2, ACRE_X_TICK_VALUE_PAD);
934 cairo_rel_move_to (cr, -width - ACRE_Y_TICK_VALUE_PAD,
937 _show_layout (cr, layout);
947 _draw_legend (acre_t *acre)
950 int label_width, max_label_width = 0;
953 cairo_t *cr = acre->cr;
957 for (i = 0; i < acre->num_data; i++) {
958 layout = _create_layout (acre, acre->data[i]->name);
959 pango_layout_get_pixel_size (layout, &label_width, NULL);
960 _destroy_layout (layout);
961 if (label_width > max_label_width)
962 max_label_width = label_width;
965 width = ACRE_LEGEND_PAD + ACRE_LEGEND_LINE_SIZE + ACRE_LEGEND_PAD +
966 max_label_width + ACRE_LEGEND_PAD;
967 height = ACRE_LEGEND_PAD +
968 acre->num_data * (ACRE_FONT_SIZE + ACRE_LEGEND_PAD);
970 cairo_translate (cr, acre->chart.x, acre->chart.y);
973 acre->chart.width - ACRE_LEGEND_PAD - width,
976 cairo_rectangle (cr, -0.5, -0.5, width + 1.0, height + 1.0);
977 cairo_set_source_rgb (cr, 0, 0, 0);
978 cairo_set_line_width (cr, 1.0);
981 cairo_translate (cr, ACRE_LEGEND_PAD, ACRE_LEGEND_PAD);
983 for (i = 0; i < acre->num_data; i++) {
985 0, ACRE_LEGEND_LINE_SIZE / 2,
986 ACRE_LEGEND_LINE_SIZE, ACRE_LEGEND_LINE_SIZE / 2);
987 cairo_set_source_rgb (cr,
988 acre->colors[i % acre->num_colors].red,
989 acre->colors[i % acre->num_colors].green,
990 acre->colors[i % acre->num_colors].blue);
993 layout = _create_layout (acre, acre->data[i]->name);
994 cairo_move_to (cr, ACRE_LEGEND_LINE_SIZE + ACRE_LEGEND_PAD, 0);
995 cairo_set_source_rgb (cr, 0, 0, 0);
996 _show_layout (cr, layout);
998 cairo_translate (cr, 0, ACRE_LEGEND_PAD + ACRE_FONT_SIZE);
1005 _draw_frame_and_ticks (acre_t *acre)
1007 cairo_t *cr = acre->cr;
1011 cairo_set_source_rgb (cr, 0, 0, 0); /* black */
1014 _draw_ticks (acre, acre->x_axis.view_min, acre->x_axis.view_max, ACRE_TICKS_X);
1015 _draw_ticks (acre, acre->y_axis.view_min, acre->y_axis.view_max, ACRE_TICKS_Y);
1018 cairo_rectangle (cr,
1019 acre->chart.x - 0.5, acre->chart.y - 0.5,
1020 acre->chart.width + 1.0, acre->chart.height + 1.0);
1021 cairo_set_line_width (cr, 1.0);
1027 /* Draw the plot to the given cairo context within a user-space
1028 * rectangle from (0, 0) to (width, height). This size includes all
1029 * space for extra-plot elements (such as the title, the axis labels,
1033 acre_draw (acre_t *acre, cairo_t *cr, int width, int height)
1037 acre->width = width;
1038 acre->height = height;
1040 acre->chart.width = width;
1041 acre->chart.height = height;
1045 cairo_set_source_rgb (cr, 1, 1, 1);
1047 _choose_colors (acre);
1049 /* We compute the axis ranges before doing label layout so that we
1050 * can account for the width of the y-axis value labels. */
1051 _compute_axis_ranges (acre);
1053 _draw_title_and_labels (acre);
1055 /* And we recompute the axis ranges now that the title and axis
1056 * label space is all measured and accounted for. */
1057 _compute_axis_ranges (acre);
1061 if (acre->num_data > 1)
1062 _draw_legend (acre);
1064 _draw_frame_and_ticks (acre);
1067 /* Create a new dataset---a collection of (x, y) datapoints. A single
1068 * plot can contain multiple datasets, (see acre_add_data). */
1070 acre_data_create (void)
1074 data = xmalloc (sizeof (acre_data_t));
1078 data->style = ACRE_STYLE_LINE;
1080 data->points = NULL;
1081 data->points_size = 0;
1082 data->num_points = 0;
1087 /* Destroy an acre dataset. Do not call this function if the dataset
1088 * has been added to an acre_t plot with acre_add_data. */
1090 acre_data_destroy (acre_data_t *data)
1094 for (i = 0; i < data->num_names; i++) {
1096 free (data->names[i]);
1102 free (data->points);
1107 /* Set the label for this dataset (to appear in the plot's key). */
1109 acre_data_set_name (acre_data_t *data, const char *name)
1113 data->name = strdup (name);
1116 /* Add a datapoint to the given dataset. */
1118 acre_data_add_point_2d (acre_data_t *data, double x, double y)
1120 if (data->num_points >= data->points_size) {
1121 data->points_size *= 2;
1122 if (data->points_size == 0)
1123 data->points_size = 16;
1124 data->points = xrealloc_ab (data->points,
1126 sizeof (acre_data_point_2d_t));
1129 data->points[data->num_points].x = x;
1130 data->points[data->num_points].y = y;
1132 if (data->num_points == 0) {
1139 if (x < data->min.x)
1141 if (y < data->min.y)
1144 if (x > data->max.x)
1146 if (y > data->max.y)
1153 /* Add a datapoint with a name to the given dataset. */
1155 acre_data_add_point_2d_named (acre_data_t *data, double x, double y, const char *name)
1159 acre_data_add_point_2d (data, x, y);
1161 if (data->names_size < data->points_size) {
1162 data->names_size = data->points_size;
1163 data->names = xrealloc_ab (data->names,
1168 /* Initialize any newly-created holes in the array to NULL. */
1169 for (i = data->num_names; i < data->num_points - 1; i++)
1170 data->names[i] = NULL;
1172 data->num_names = data->num_points;
1174 data->names[data->num_names - 1] = xstrdup (name);