]> git.cworth.org Git - acre/blob - acre.c
Add necessary -lm flag to link command
[acre] / acre.c
1 /* acre - A cairo-based library for creating plots and charts.
2  *
3  * Copyright © 2009 Carl Worth <cworth@cworth.org>
4  *
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.
9  *
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.
14  *
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.
18  */
19
20 #define _ISOC99_SOURCE /* for round() */
21 #define _XOPEN_SOURCE 500
22 #define _GNU_SOURCE /* for asprintf() */
23
24 #include "acre.h"
25 #include "xmalloc.h"
26
27 #include <string.h>
28 #include <stdarg.h>
29 #include <math.h>
30
31 #include <lcms.h>
32
33 typedef struct _acre_data_point_2d {
34     double x;
35     double y;
36 } acre_data_point_2d_t;
37
38 struct _acre_data {
39     char *name;
40
41     acre_data_point_2d_t *points;
42     unsigned int points_size;
43     unsigned int num_points;
44 };
45
46 typedef struct _acre_axis {
47     char *label;
48     double min;
49     double max;
50 } acre_axis_t;
51
52 typedef struct _acre_color {
53     double red;
54     double green;
55     double blue;
56 } acre_color_t;
57
58 struct _acre {
59     char *title;
60     acre_axis_t x_axis;
61     acre_axis_t y_axis;
62
63     acre_data_t **data;
64     unsigned int data_size;
65     unsigned int num_data;
66
67     /* Data for drawing. */
68     cairo_t *cr;
69     PangoFontDescription *font;
70     acre_color_t *colors;
71     int colors_size;
72     int num_colors;
73
74     /* Total size including labels. */
75     int width;
76     int height;
77
78     /* Position and size of chart alone. */
79     PangoRectangle chart;
80 };
81
82 /* Create a new, empty plot. */
83 acre_t *
84 acre_create (void)
85 {
86     acre_t *acre;
87
88     acre = xmalloc (sizeof (acre_t));
89
90     acre->title = NULL;
91
92     acre->x_axis.label = NULL;
93     acre->x_axis.min = 0.0;
94     acre->x_axis.max = 0.0;
95
96     acre->y_axis.label = NULL;
97     acre->y_axis.min = 0.0;
98     acre->y_axis.max = 0.0;
99
100     acre->data = NULL;
101     acre->data_size = 0;
102     acre->num_data = 0;
103
104     acre->cr = NULL;
105     acre->font = NULL;
106     acre->colors = NULL;
107     acre->num_colors = 0;
108
109     acre->width = 0;
110     acre->height = 0;
111
112     acre->chart.x = 0;
113     acre->chart.y = 0;
114     acre->chart.width = 0;
115     acre->chart.height = 0;
116
117     return acre;
118 }
119
120 /* Destroy a plot. */
121 void
122 acre_destroy (acre_t *acre)
123 {
124     unsigned int i;
125
126     free (acre->title);
127     free (acre->x_axis.label);
128     free (acre->y_axis.label);
129
130     for (i = 0; i < acre->num_data; i++)
131         acre_data_destroy (acre->data[i]);
132
133     free (acre->data);
134
135     free (acre->colors);
136
137     free (acre);
138 }
139
140 void
141 acre_set_title (acre_t *acre, const char *title)
142 {
143     free (acre->title);
144
145     acre->title = strdup (title);
146 }
147
148 void
149 acre_set_x_axis_label (acre_t *acre, const char *label)
150 {
151     free (acre->x_axis.label);
152
153     acre->x_axis.label = strdup (label);
154 }
155
156 void
157 acre_set_y_axis_label (acre_t *acre, const char *label)
158 {
159     free (acre->y_axis.label);
160
161     acre->y_axis.label = strdup (label);
162 }
163
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. */
166 void
167 acre_add_data (acre_t *acre, acre_data_t *data)
168 {
169     if (acre->num_data >= acre->data_size) {
170         acre->data_size *= 2;
171         if (acre->data_size == 0)
172             acre->data_size = 1;
173         acre->data = xrealloc_ab (acre->data,
174                                   acre->data_size,
175                                   sizeof (acre_data_t *));
176     }
177
178     acre->data[acre->num_data] = data;
179     acre->num_data++;
180 }
181
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
192
193 static PangoLayout *
194 _create_layout (acre_t *acre, const char *text)
195 {
196     PangoLayout *layout;
197
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);
202
203     return layout;
204 }
205
206 #define PRINTF_FORMAT(fmt_index, va_index) __attribute__ ((__format__(__printf__, fmt_index, va_index)))
207
208 static PangoLayout *
209 _create_layout_vprintf (acre_t *acre, const char *fmt, va_list ap)
210 {
211     PangoLayout *layout;
212     char *text;
213
214     vasprintf (&text, fmt, ap);
215
216     layout = _create_layout (acre, text);
217
218     free (text);
219
220     return layout;
221 }
222
223 static PangoLayout *
224 _create_layout_printf (acre_t *acre, const char *fmt, ...)
225     PRINTF_FORMAT (2, 3);
226
227 static PangoLayout *
228 _create_layout_printf (acre_t *acre, const char *fmt, ...)
229 {
230     va_list ap;
231     PangoLayout *layout;
232
233     va_start (ap, fmt);
234
235     layout = _create_layout_vprintf (acre, fmt, ap);
236
237     va_end (ap);
238
239     return layout;
240 }
241
242 static void
243 _destroy_layout (PangoLayout *layout)
244 {
245     g_object_unref (layout);
246 }
247
248 static void
249 _show_layout (cairo_t *cr, PangoLayout *layout)
250 {
251     pango_cairo_show_layout (cr, layout);
252
253     _destroy_layout (layout);
254 }
255
256 static void
257 _draw_title_and_labels (acre_t *acre)
258 {
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;
268
269     cairo_save (cr);
270
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);
275
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);
280
281     title_layout = _create_layout (acre, acre->title);
282     pango_layout_set_font_description (title_layout, title_font);
283
284     x_axis_layout = _create_layout (acre, acre->x_axis.label);
285     y_axis_layout = _create_layout (acre, acre->y_axis.label);
286
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));
291
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);
295
296     _destroy_layout (min_y);
297     _destroy_layout (max_y);
298
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). */
303     while (1) {
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);
307
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);
311
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;
315
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);
320
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)
325         {
326             break;
327         }
328
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;
333     }
334
335     cairo_set_source_rgb (cr, 0, 0, 0);
336
337     cairo_move_to (cr, acre->chart.x, ACRE_PAD);
338     _show_layout (cr, title_layout);
339
340     cairo_save (cr);
341     {
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);
346     }
347     cairo_restore (cr);
348
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);
353
354     cairo_restore (cr);
355 }
356
357 /* For a given axis range, compute a step size (in data space) to
358  * generate a suitable number of ticks (5 or so). */
359 static double
360 _step_for_range (double range, int *minor_divisions)
361 {
362     double step, scale_factor;
363
364     /* We want roughly 5 major ticks for the chart. */
365     step = range / 5;
366
367     /* Normalize the step so we can easily snap it to a desirable
368      * value. */
369     scale_factor = pow (10.0, floor (log10 (step)));
370     step /= scale_factor;
371
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) {
377             step = 1.0;
378             *minor_divisions = 4;
379         } else {
380             step = 2.5;
381             *minor_divisions = 5;
382         }
383     } else {
384         if (step < 7.071067811865475) {
385             step = 5.0;
386             *minor_divisions = 5;
387         } else {
388             step = 10.0;
389             *minor_divisions = 4;
390         }
391     }
392
393     /* Un-normalize and we now have the data value that we want to
394      * step at. */
395     return step * scale_factor;
396 }
397
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
401  * slightly. */
402 static void
403 _expand_range_for_width (double *axis_min, double *axis_max, int pixel_range)
404 {
405     double range, new_range, step, step_minor, pixel_step;
406     int minor_divisions;
407
408     range = *axis_max - *axis_min;
409
410     step = _step_for_range (range, &minor_divisions);
411     step_minor = step / minor_divisions;
412
413     pixel_step = step_minor * (pixel_range / range);
414
415     /* We expand the range by the ratio of the pixel step to the floor
416      * of the pixel_step.
417      */
418     new_range = range * pixel_step / floor (pixel_step);
419
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;
423 }
424
425 /* Setup a transformation in acre->cr such that data values plotted
426  * will appear where they should within the chart.
427  */
428 static void
429 _set_transform_to_data_space (acre_t *acre)
430 {
431     cairo_t *cr = acre->cr;
432
433     cairo_translate (cr,
434                      acre->chart.x,
435                      acre->chart.y + acre->chart.height);
436     cairo_scale (cr,
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);
440 }
441
442 static void
443 _compute_axis_ranges (acre_t *acre)
444 {
445     unsigned int d, i;
446     acre_data_t *data;
447     double x_adjust, y_adjust;
448     cairo_t *cr = acre->cr;
449
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;
454
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;
463
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;
468         }
469     }
470
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. */
473     cairo_save (cr);
474     {
475         double x, y;
476
477         _set_transform_to_data_space (acre);
478
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);
482
483         acre->x_axis.min -= x;
484         acre->y_axis.min += y;
485     }
486     cairo_restore (cr);
487
488     /* Then, increase the axis ranges just enough so that the step
489      * sizes for the ticks will be integers.
490      */
491     _expand_range_for_width (&acre->x_axis.min,
492                              &acre->x_axis.max,
493                              acre->chart.width);
494
495     _expand_range_for_width (&acre->y_axis.min,
496                              &acre->y_axis.max,
497                              acre->chart.height);
498
499     /* Finally, we also translate the axis ranges slightly so that the
500      * ticks land on half-integer device-pixel positions.
501      */
502     cairo_save (cr);
503     {
504         _set_transform_to_data_space (acre);
505
506         x_adjust = 0.0;
507         y_adjust = 0.0;
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);
512
513         acre->x_axis.min -= x_adjust;
514         acre->x_axis.max -= x_adjust;
515
516         acre->y_axis.min -= y_adjust;
517         acre->y_axis.max -= y_adjust;
518     }
519     cairo_restore (cr);
520 }
521
522 static void
523 _acre_color_from_hsv (acre_color_t *color,
524                       double hue,
525                       double saturation,
526                       double value)
527 {
528     double f, p, q, t;
529     int hmod6;
530
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);
536     
537     switch (hmod6) {
538       case 0:
539         color->red = value;
540         color->green = t;
541         color->blue = p;
542         break;
543       case 1:
544         color->red = q;
545         color->green = value;
546         color->blue = p;
547         break;
548       case 2:
549         color->red = p;
550         color->green = value;
551         color->blue = t;
552         break;
553       case 3:
554         color->red = p;
555         color->green = q;
556         color->blue = value;
557         break;
558       case 4:
559         color->red = t;
560         color->green = p;
561         color->blue = value;
562         break;
563       case 5:
564         color->red = value;
565         color->green = p;
566         color->blue = q;
567         break;
568     }
569 }
570
571 static void
572 _choose_colors (acre_t *acre)
573 {
574     cmsHPROFILE lab_profile, srgb_profile;
575     cmsHTRANSFORM lab_to_srgb;
576     int i;
577     double theta, radius, srgb[3];
578     cmsCIELab lab;
579
580     lab_profile = cmsCreateLabProfile (NULL); /* D50 */
581     srgb_profile = cmsCreate_sRGBProfile ();
582
583     lab_to_srgb = cmsCreateTransform (lab_profile, TYPE_Lab_DBL,
584                                       srgb_profile, TYPE_RGB_DBL,
585                                       INTENT_PERCEPTUAL, 0);
586
587     acre->num_colors = acre->num_data;
588
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));
593     }
594
595     lab.L = 36;
596     radius = 130;
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);
601
602         cmsDoTransform (lab_to_srgb, &lab, srgb, 1);
603
604         acre->colors[i].red = srgb[0];
605         acre->colors[i].green = srgb[1];
606         acre->colors[i].blue = srgb[2];
607     }
608
609     cmsDeleteTransform (lab_to_srgb);
610     cmsCloseProfile (lab_profile);
611     cmsCloseProfile (srgb_profile);
612 }
613
614 static void
615 _draw_data (acre_t *acre)
616 {
617     cairo_t *cr = acre->cr;
618     unsigned int d, i;
619     acre_data_t *data;
620
621     cairo_save (cr);
622
623     cairo_set_source_rgb (cr, 0, 0, 0);
624
625     _set_transform_to_data_space (acre);
626
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];
634         cairo_new_path (cr);
635         for (i = 0; i < data->num_points; i++) {
636             cairo_line_to (cr,
637                            data->points[i].x,
638                            data->points[i].y);
639         }
640         cairo_save (cr);
641         {
642             cairo_identity_matrix (cr);
643             cairo_set_line_width (cr, 1.0);
644             cairo_stroke (cr);
645         }
646         cairo_restore (cr);
647     }
648
649     cairo_restore (cr);
650 }
651
652 typedef enum _ticks { ACRE_TICKS_X, ACRE_TICKS_Y } acre_ticks_t;
653
654 static void
655 _draw_ticks (acre_t *acre,
656              double axis_min, double axis_max,
657              acre_ticks_t ticks)
658 {
659     cairo_t *cr = acre->cr;
660     double t, step, sub_step;
661     int minor_divisions;
662
663     cairo_save (cr);
664
665     _set_transform_to_data_space (acre);
666
667     step = _step_for_range (axis_max - axis_min, &minor_divisions);
668     sub_step = step / minor_divisions;
669
670     for (t = (floor (axis_min / sub_step) + 1) * sub_step;
671          t <= axis_max;
672          t += sub_step)
673     {
674         int tick_size;
675         if (fabs((t / step) - round (t / step)) < 0.5 * (sub_step / step))
676             tick_size = ACRE_TICK_MAJOR_SIZE;
677         else
678             tick_size = ACRE_TICK_MINOR_SIZE;
679
680         /* tick */
681         cairo_save (cr);
682         {
683             if (ticks == ACRE_TICKS_X)
684                 cairo_move_to (cr, t, acre->y_axis.min);
685             else
686                 cairo_move_to (cr, acre->x_axis.min, t);
687
688             cairo_identity_matrix (cr);
689
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);
693             } else {
694                 cairo_rel_line_to (cr, -0.5, 0);
695                 cairo_rel_line_to (cr, tick_size + 0.5, 0);
696             }
697
698             cairo_set_line_width (cr, 1.0);
699             cairo_stroke (cr);
700         }
701         cairo_restore (cr);
702
703         /* label */
704         if (tick_size == ACRE_TICK_MAJOR_SIZE)
705         {
706             PangoLayout *layout;
707             int width, height;
708
709             cairo_save (cr);
710
711             layout = _create_layout_printf (acre, "%g", t);
712
713             if (ticks == ACRE_TICKS_X)
714                 cairo_move_to (cr, t, acre->y_axis.min);
715             else
716                 cairo_move_to (cr, acre->x_axis.min, t);
717
718             cairo_identity_matrix (cr);
719             pango_layout_get_pixel_size (layout, &width, &height);
720
721             if (ticks == ACRE_TICKS_X)
722                 cairo_rel_move_to (cr, -width / 2, ACRE_X_TICK_VALUE_PAD);
723             else
724                 cairo_rel_move_to (cr, -width - ACRE_Y_TICK_VALUE_PAD,
725                                    -height/2);
726
727             _show_layout (cr, layout);
728
729             cairo_restore (cr);
730         }
731     }
732
733     cairo_restore (cr);
734 }
735
736 static void
737 _draw_legend (acre_t *acre)
738 {
739     PangoLayout *layout;
740     int label_width, max_label_width = 0;
741     int width, height;
742     unsigned int i;
743     cairo_t *cr = acre->cr;
744
745     cairo_save (cr);
746
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;
753     }
754
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);
759
760     cairo_translate (cr, acre->chart.x, acre->chart.y);
761
762     cairo_translate (cr,
763                      acre->chart.width - ACRE_LEGEND_PAD - width,
764                      ACRE_LEGEND_PAD);
765
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);
769     cairo_stroke (cr);
770
771     cairo_translate (cr, ACRE_LEGEND_PAD, ACRE_LEGEND_PAD);
772
773     for (i = 0; i < acre->num_data; i++) {
774         cairo_rectangle (cr,
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);
781         cairo_fill (cr);
782
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);
787
788         cairo_translate (cr, 0, ACRE_LEGEND_PAD + ACRE_FONT_SIZE);
789     }
790
791     cairo_restore (cr);
792 }
793
794 static void
795 _draw_frame_and_ticks (acre_t *acre)
796 {
797     cairo_t *cr = acre->cr;
798
799     cairo_save (cr);
800
801     cairo_set_source_rgb (cr, 0, 0, 0); /* black */
802
803     /* ticks */
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);
806
807     /* frame */
808     cairo_rectangle (cr,
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);
812     cairo_stroke (cr);
813
814     cairo_restore (cr);
815 }
816
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,
820  * etc.)
821  */
822 void
823 acre_draw (acre_t *acre, cairo_t *cr, int width, int height)
824 {
825     acre->cr = cr;
826
827     acre->width = width;
828     acre->height = height;
829
830     acre->chart.width = width;
831     acre->chart.height = height;
832
833     cairo_save (cr);
834
835     cairo_set_source_rgb (cr, 1, 1, 1);
836
837     _choose_colors (acre);
838
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);
842
843     _draw_title_and_labels (acre);
844
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);
848
849     _draw_data (acre);
850
851     if (acre->num_data > 1)
852         _draw_legend (acre);
853
854     _draw_frame_and_ticks (acre);
855 }
856
857 /* Create a new dataset---a collection of (x, y) datapoints. A single
858  * plot can contain multiple datasets, (see acre_add_data). */
859 acre_data_t *
860 acre_data_create (void)
861 {
862     acre_data_t *data;
863
864     data = xmalloc (sizeof (acre_data_t));
865
866     data->name = NULL;
867
868     data->points = NULL;
869     data->points_size = 0;
870     data->num_points = 0;
871
872     return data;
873 }
874
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. */
877 void
878 acre_data_destroy (acre_data_t *data)
879 {
880     free (data->points);
881
882     free (data);
883 }
884
885 /* Set the label for this dataset (to appear in the plot's key). */
886 void
887 acre_data_set_name (acre_data_t *data, const char *name)
888 {
889     free (data->name);
890
891     data->name = strdup (name);
892 }
893
894 /* Add a datapoint to the given dataset. */
895 void
896 acre_data_add_point_2d (acre_data_t *data, double x, double y)
897 {
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,
903                                     data->points_size,
904                                     sizeof (acre_data_point_2d_t));
905     }
906
907     data->points[data->num_points].x = x;
908     data->points[data->num_points].y = y;
909     data->num_points++;
910 }