]> git.cworth.org Git - acre/blob - acre.c
Add another dataset style: ACRE_STYLE_BARS
[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 <stdbool.h>
30 #include <math.h>
31
32 #include <lcms.h>
33
34 typedef struct _acre_data_point_2d {
35     double x;
36     double y;
37 } acre_data_point_2d_t;
38
39 struct _acre_data {
40     /* The name of this data set. */
41     char *name;
42
43     /* The style for rendering (line, bar, etc.) */
44     acre_style_t style;
45
46     /* Minimum and mximum extents of data. */
47     acre_data_point_2d_t min;
48     acre_data_point_2d_t max;
49
50     /* The data itself. */
51     acre_data_point_2d_t *points;
52     unsigned int points_size;
53     unsigned int num_points;
54
55     /* The names of data points (if any).
56      *
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).
61      */
62     char **names;
63     unsigned int names_size;
64     unsigned int num_names;
65 };
66
67 typedef struct _acre_axis {
68     char *label;
69
70     /* Range of data */
71     double data_min;
72     double data_max;
73
74     /* Range of data to be viewed. */
75     double view_min;
76     double view_max;
77
78     /* Has the view range been set? */
79     bool view_range_set;
80 } acre_axis_t;
81
82 typedef struct _acre_color {
83     double red;
84     double green;
85     double blue;
86 } acre_color_t;
87
88 struct _acre {
89     char *title;
90     acre_axis_t x_axis;
91     acre_axis_t y_axis;
92
93     acre_data_t **data;
94     unsigned int data_size;
95     unsigned int num_data;
96
97     /* Data for drawing. */
98     cairo_t *cr;
99     PangoFontDescription *font;
100     acre_color_t *colors;
101     int colors_size;
102     int num_colors;
103
104     /* Total size including labels. */
105     int width;
106     int height;
107
108     /* Position and size of chart alone. */
109     PangoRectangle chart;
110 };
111
112 static void
113 _find_x_range_given_y_range (acre_t *acre,
114                              double *x_min, double *x_max,
115                              double y_min, double y_max);
116
117 static void
118 _find_y_range_given_x_range (acre_t *acre,
119                              double *y_min, double *y_max,
120                              double x_min, double x_max);
121
122 /* Create a new, empty plot. */
123 acre_t *
124 acre_create (void)
125 {
126     acre_t *acre;
127
128     acre = xmalloc (sizeof (acre_t));
129
130     acre->title = NULL;
131
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;
138
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;
145
146     acre->data = NULL;
147     acre->data_size = 0;
148     acre->num_data = 0;
149
150     acre->cr = NULL;
151     acre->font = NULL;
152     acre->colors = NULL;
153     acre->num_colors = 0;
154     acre->colors_size = 0;
155
156     acre->width = 0;
157     acre->height = 0;
158
159     acre->chart.x = 0;
160     acre->chart.y = 0;
161     acre->chart.width = 0;
162     acre->chart.height = 0;
163
164     return acre;
165 }
166
167 /* Destroy a plot. */
168 void
169 acre_destroy (acre_t *acre)
170 {
171     unsigned int i;
172
173     free (acre->title);
174     free (acre->x_axis.label);
175     free (acre->y_axis.label);
176
177     for (i = 0; i < acre->num_data; i++)
178         acre_data_destroy (acre->data[i]);
179
180     free (acre->data);
181
182     free (acre->colors);
183
184     free (acre);
185 }
186
187 void
188 acre_set_title (acre_t *acre, const char *title)
189 {
190     free (acre->title);
191
192     acre->title = strdup (title);
193 }
194
195 void
196 acre_set_x_axis_label (acre_t *acre, const char *label)
197 {
198     free (acre->x_axis.label);
199
200     acre->x_axis.label = strdup (label);
201 }
202
203 void
204 acre_set_y_axis_label (acre_t *acre, const char *label)
205 {
206     free (acre->y_axis.label);
207
208     acre->y_axis.label = strdup (label);
209 }
210
211 void
212 acre_get_x_axis_data_range (acre_t *acre, double *x_min, double *x_max)
213 {
214     if (x_min)
215         *x_min = acre->x_axis.data_min;
216
217     if (x_max)
218         *x_max = acre->x_axis.data_max;
219 }
220
221 void
222 acre_get_x_axis_range (acre_t *acre, double *x_min, double *x_max)
223 {
224     /* If an X range has been set, return that. */
225     if (acre->x_axis.view_range_set) {
226         if (x_min)
227             *x_min = acre->x_axis.view_min;
228
229         if (x_max)
230             *x_max = acre->x_axis.view_max;
231
232         return;
233     }
234
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);
240
241         return;
242     }
243
244     /* Neither view range set. Return full, data-based X range. */
245     acre_get_x_axis_data_range (acre, x_min, x_max);
246 }
247
248 void
249 acre_set_x_axis_range (acre_t *acre, double x_min, double x_max)
250 {
251     acre->x_axis.view_min = x_min;
252     acre->x_axis.view_max = x_max;
253
254     acre->x_axis.view_range_set = true;
255 }
256
257 void
258 acre_set_x_axis_range_auto (acre_t *acre)
259 {
260     acre->x_axis.view_range_set = false;
261 }
262
263 void
264 acre_get_y_axis_data_range (acre_t *acre, double *y_min, double *y_max)
265 {
266     if (y_min)
267         *y_min = acre->y_axis.data_min;
268
269     if (y_max)
270         *y_max = acre->y_axis.data_max;
271 }
272
273 void
274 acre_get_y_axis_range (acre_t *acre, double *y_min, double *y_max)
275 {
276     /* If a Y range has been set, return that. */
277     if (acre->y_axis.view_range_set) {
278         if (y_min)
279             *y_min = acre->y_axis.view_min;
280
281         if (y_max)
282             *y_max = acre->y_axis.view_max;
283
284         return;
285     }
286
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);
292
293         return;
294     }
295
296     /* Neither view range set. Return full data-based Y range. */
297     acre_get_y_axis_data_range (acre, y_min, y_max);
298 }
299
300 void
301 acre_set_y_axis_range (acre_t *acre, double y_min, double y_max)
302 {
303     acre->y_axis.view_min = y_min;
304     acre->y_axis.view_max = y_max;
305
306     acre->y_axis.view_range_set = true;
307 }
308
309 void
310 acre_set_y_axis_range_auto (acre_t *acre)
311 {
312     acre->y_axis.view_range_set = false;
313 }
314
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. */
317 void
318 acre_add_data (acre_t *acre, acre_data_t *data)
319 {
320     if (acre->num_data >= acre->data_size) {
321         acre->data_size *= 2;
322         if (acre->data_size == 0)
323             acre->data_size = 1;
324         acre->data = xrealloc_ab (acre->data,
325                                   acre->data_size,
326                                   sizeof (acre_data_t *));
327     }
328
329     acre->data[acre->num_data] = data;
330
331     /* For timeline datasets, the X and Y ranges need to be
332      * adjusted. The desired X range is the min/max of the X and Y
333      * ranges, and the desired Y range has a size of 1.0 (centered
334      * around the dataset's index) */
335     if (data->style == ACRE_STYLE_TIMELINE) {
336         if (data->min.y < data->min.x)
337             data->min.x = data->min.y;
338         if (data->max.y > data->max.x)
339             data->max.x = data->max.y;
340
341         data->min.y = acre->num_data -0.5;
342         data->max.y = acre->num_data + 0.5;
343     }
344
345     if (acre->num_data == 0) {
346         acre->x_axis.data_min = data->min.x;
347         acre->y_axis.data_min = data->min.y;
348
349         acre->x_axis.data_max = data->max.x;
350         acre->y_axis.data_max = data->max.y;
351     } else {
352         if (data->min.x < acre->x_axis.data_min)
353             acre->x_axis.data_min = data->min.x;
354         if (data->min.y < acre->y_axis.data_min)
355             acre->y_axis.data_min = data->min.y;
356
357         if (data->max.x > acre->x_axis.data_max)
358             acre->x_axis.data_max = data->max.x;
359         if (data->max.y > acre->y_axis.data_max)
360             acre->y_axis.data_max = data->max.y;
361     }
362
363     acre->num_data++;
364 }
365
366 #define ACRE_FONT_FAMILY "sans"
367 #define ACRE_FONT_SIZE 12
368 #define ACRE_TITLE_FONT_SIZE 20
369 #define ACRE_PAD (ACRE_FONT_SIZE)
370 #define ACRE_TICK_MAJOR_SIZE 6
371 #define ACRE_TICK_MINOR_SIZE 3
372 #define ACRE_X_TICK_VALUE_PAD 2
373 #define ACRE_Y_TICK_VALUE_PAD 4
374 #define ACRE_LEGEND_PAD 4
375 #define ACRE_LEGEND_LINE_SIZE 10
376
377 static PangoLayout *
378 _create_layout (acre_t *acre, const char *text)
379 {
380     PangoLayout *layout;
381
382     if (text == NULL)
383             text = "";
384
385     cairo_save (acre->cr);
386     cairo_identity_matrix (acre->cr);
387
388     layout = pango_cairo_create_layout (acre->cr);
389     pango_layout_set_font_description (layout, acre->font);
390     pango_layout_set_text (layout, text, -1);
391     pango_layout_set_alignment (layout, PANGO_ALIGN_CENTER);
392
393     cairo_restore (acre->cr);
394
395     return layout;
396 }
397
398 #define PRINTF_FORMAT(fmt_index, va_index) __attribute__ ((__format__(__printf__, fmt_index, va_index)))
399
400 static PangoLayout *
401 _create_layout_vprintf (acre_t *acre, const char *fmt, va_list ap)
402 {
403     PangoLayout *layout;
404     char *text;
405
406     vasprintf (&text, fmt, ap);
407
408     layout = _create_layout (acre, text);
409
410     free (text);
411
412     return layout;
413 }
414
415 static PangoLayout *
416 _create_layout_printf (acre_t *acre, const char *fmt, ...)
417     PRINTF_FORMAT (2, 3);
418
419 static PangoLayout *
420 _create_layout_printf (acre_t *acre, const char *fmt, ...)
421 {
422     va_list ap;
423     PangoLayout *layout;
424
425     va_start (ap, fmt);
426
427     layout = _create_layout_vprintf (acre, fmt, ap);
428
429     va_end (ap);
430
431     return layout;
432 }
433
434 static void
435 _destroy_layout (PangoLayout *layout)
436 {
437     g_object_unref (layout);
438 }
439
440 static void
441 _show_layout (cairo_t *cr, PangoLayout *layout)
442 {
443     pango_cairo_show_layout (cr, layout);
444
445     _destroy_layout (layout);
446 }
447
448 static void
449 _draw_title_and_labels (acre_t *acre)
450 {
451     cairo_t *cr = acre->cr;
452     PangoFontDescription *title_font;
453     PangoLayout *title_layout, *x_axis_layout, *y_axis_layout;
454     PangoLayout *min_y, *max_y;
455     int min_y_width, max_y_width, y_axis_value_width;
456     int title_width, title_height;
457     int x_axis_width, x_axis_height;
458     int y_axis_width, y_axis_height;
459     PangoRectangle new_chart;
460
461     cairo_save (cr);
462
463     acre->font = pango_font_description_new ();
464     pango_font_description_set_family (acre->font, ACRE_FONT_FAMILY);
465     pango_font_description_set_absolute_size (acre->font,
466                                               ACRE_FONT_SIZE * PANGO_SCALE);
467
468     title_font = pango_font_description_new ();
469     pango_font_description_set_family (title_font, ACRE_FONT_FAMILY);
470     pango_font_description_set_absolute_size (title_font,
471                                               ACRE_TITLE_FONT_SIZE * PANGO_SCALE);
472
473     title_layout = _create_layout (acre, acre->title);
474     pango_layout_set_font_description (title_layout, title_font);
475     pango_font_description_free (title_font);
476
477     x_axis_layout = _create_layout (acre, acre->x_axis.label);
478     y_axis_layout = _create_layout (acre, acre->y_axis.label);
479
480     min_y = _create_layout_printf (acre, "%g",
481                                    round (acre->y_axis.view_min));
482     max_y = _create_layout_printf (acre, "%g",
483                                    round (acre->y_axis.view_max));
484
485     pango_layout_get_pixel_size (min_y, &min_y_width, NULL);
486     pango_layout_get_pixel_size (max_y, &max_y_width, NULL);
487     y_axis_value_width = MAX (min_y_width, max_y_width);
488
489     _destroy_layout (min_y);
490     _destroy_layout (max_y);
491
492     /* Iterate with the layout of the title and axis labels until they
493      * are stable, (this requires iteration since we don't know what
494      * to set their widths to in advance due to the wrapping of the
495      * other elements). */
496     while (1) {
497         pango_layout_set_width (title_layout, acre->chart.width * PANGO_SCALE);
498         pango_layout_set_width (x_axis_layout, acre->chart.width * PANGO_SCALE);
499         pango_layout_set_width (y_axis_layout, acre->chart.height * PANGO_SCALE);
500
501         pango_layout_get_pixel_size (title_layout, &title_width, &title_height);
502         pango_layout_get_pixel_size (x_axis_layout, &x_axis_width, &x_axis_height);
503         pango_layout_get_pixel_size (y_axis_layout, &y_axis_width, &y_axis_height);
504
505         new_chart.x = ACRE_PAD + y_axis_height +
506             ACRE_PAD + y_axis_value_width + ACRE_Y_TICK_VALUE_PAD;
507         new_chart.width = acre->width - acre->chart.x - ACRE_PAD;
508
509         new_chart.y = ACRE_PAD + title_height + ACRE_PAD;
510         new_chart.height = acre->height - acre->chart.y - 
511             (ACRE_X_TICK_VALUE_PAD + ACRE_FONT_SIZE +
512              ACRE_PAD + x_axis_height + ACRE_PAD);
513
514         if (new_chart.x == acre->chart.x &&
515             new_chart.y == acre->chart.y &&
516             new_chart.width == acre->chart.width &&
517             new_chart.height == acre->chart.height)
518         {
519             break;
520         }
521
522         acre->chart.x = new_chart.x;
523         acre->chart.y = new_chart.y;
524         acre->chart.width = new_chart.width;
525         acre->chart.height = new_chart.height;
526     }
527
528     cairo_set_source_rgb (cr, 0, 0, 0);
529
530     cairo_move_to (cr, acre->chart.x, ACRE_PAD);
531     _show_layout (cr, title_layout);
532
533     cairo_save (cr);
534     {
535         cairo_translate (cr, ACRE_PAD, acre->chart.y + acre->chart.height);
536         cairo_rotate (cr, - M_PI / 2.0);
537         cairo_move_to (cr, 0, 0);
538         _show_layout (cr, y_axis_layout);
539     }
540     cairo_restore (cr);
541
542     cairo_move_to (cr, acre->chart.x,
543                    acre->chart.y + acre->chart.height +
544                    ACRE_FONT_SIZE + ACRE_PAD);
545     _show_layout (cr, x_axis_layout);
546
547     cairo_restore (cr);
548 }
549
550 /* For a given axis range, compute a step size (in data space) to
551  * generate a suitable number of ticks (5 or so). */
552 static double
553 _step_for_range (double range, int *minor_divisions)
554 {
555     double step, scale_factor;
556
557     /* We want roughly 5 major ticks for the chart. */
558     step = range / 5;
559
560     /* Normalize the step so we can easily snap it to a desirable
561      * value. */
562     scale_factor = pow (10.0, floor (log10 (step)));
563     step /= scale_factor;
564
565     /* We want increments of 1, 2.5, 5, or 10 (times some power of
566      * 10). The threshold values between these are computed
567      * logarithmically. */
568     if (step < 3.535533905932738) {
569         if (step < 1.58113883008419) {
570             step = 1.0;
571             *minor_divisions = 4;
572         } else {
573             step = 2.5;
574             *minor_divisions = 5;
575         }
576     } else {
577         if (step < 7.071067811865475) {
578             step = 5.0;
579             *minor_divisions = 5;
580         } else {
581             step = 10.0;
582             *minor_divisions = 4;
583         }
584     }
585
586     /* Un-normalize and we now have the data value that we want to
587      * step at. */
588     return step * scale_factor;
589 }
590
591 /* Given an axis range, we can compute a desired data-space step
592  * amount for the major ticks (see _step_for_range). To get
593  * nice-looking pixel-snapped ticks we want to expand the range
594  * slightly. */
595 static void
596 _expand_range_for_width (double *axis_min, double *axis_max, int pixel_range)
597 {
598     double range, new_range, step, step_minor, pixel_step;
599     int minor_divisions;
600
601     range = *axis_max - *axis_min;
602
603     step = _step_for_range (range, &minor_divisions);
604     step_minor = step / minor_divisions;
605
606     pixel_step = step_minor * (pixel_range / range);
607
608     /* We expand the range by the ratio of the pixel step to the floor
609      * of the pixel_step.
610      */
611     new_range = range * pixel_step / floor (pixel_step);
612
613     /* And spread the increase out on either side of the range. */
614     *axis_min -= (new_range - range) / 2.0;
615     *axis_max += (new_range - range) / 2.0;
616 }
617
618 /* Setup a transformation in acre->cr such that data values plotted
619  * will appear where they should within the chart.
620  */
621 static void
622 _set_transform_to_data_space (acre_t *acre)
623 {
624     cairo_t *cr = acre->cr;
625
626     cairo_translate (cr,
627                      acre->chart.x,
628                      acre->chart.y + acre->chart.height);
629     cairo_scale (cr,
630                  acre->chart.width / (acre->x_axis.view_max - acre->x_axis.view_min),
631                  - acre->chart.height /(acre->y_axis.view_max - acre->y_axis.view_min));
632     cairo_translate (cr, -acre->x_axis.view_min, -acre->y_axis.view_min);
633 }
634
635 static void
636 _find_x_range_given_y_range (acre_t *acre,
637                              double *x_min, double *x_max,
638                              double y_min, double y_max)
639 {
640     acre_data_t *data;
641     unsigned d, i;
642     bool first;
643
644     first = true;
645
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].y >= y_min &&
650                 data->points[i].y <= y_max)
651             {
652                 if (first) {
653                     *x_min = data->points[i].x;
654                     *x_max = data->points[i].x;
655                     first = false;
656                 } else {
657                     if (data->points[i].x < *x_min)
658                         *x_min = data->points[i].x;
659                     if (data->points[i].x > *x_max)
660                         *x_max = data->points[i].x;
661                 }
662             }
663         }
664     }
665
666     /* If nothing is visible, punt to full X data range. */
667     if (first) {
668         *x_min = acre->x_axis.data_min;
669         *x_max = acre->x_axis.data_max;
670     }
671 }
672
673 static void
674 _find_y_range_given_x_range (acre_t *acre,
675                              double *y_min, double *y_max,
676                              double x_min, double x_max)
677 {
678     acre_data_t *data;
679     unsigned d, i;
680     bool first;
681
682     first = true;
683
684     for (d = 0; d < acre->num_data; d++) {
685         data = acre->data[d];
686
687         /* Never mess with the Y range for timeline data. */
688         if (data->style == ACRE_STYLE_TIMELINE)
689             continue;
690
691         for (i = 0; i < data->num_points; i++) {
692             if (data->points[i].x >= x_min &&
693                 data->points[i].x <= x_max)
694             {
695                 if (first) {
696                     *y_min = data->points[i].y;
697                     *y_max = data->points[i].y;
698                     first = false;
699                 } else {
700                     if (data->points[i].y < *y_min)
701                         *y_min = data->points[i].y;
702                     if (data->points[i].y > *y_max)
703                         *y_max = data->points[i].y;
704                 }
705             }
706         }
707     }
708
709     /* If nothing is visible, punt to full Y data range. */
710     if (first) {
711         *y_min = acre->y_axis.data_min;
712         *y_max = acre->y_axis.data_max;
713     }
714 }
715
716 static void
717 _compute_axis_ranges (acre_t *acre)
718 {
719     double x_adjust, y_adjust;
720     cairo_t *cr = acre->cr;
721
722     /* If neither view range is set, set both to data ranges. */
723     if (! acre->x_axis.view_range_set && ! acre->y_axis.view_range_set)
724     {
725         acre->x_axis.view_min = acre->x_axis.data_min;
726         acre->x_axis.view_max = acre->x_axis.data_max;
727
728         acre->y_axis.view_min = acre->y_axis.data_min;
729         acre->y_axis.view_max = acre->y_axis.data_max;
730     } else {
731         /* Otherwise, auto-fit unset range based on data. */
732         if (acre->x_axis.view_range_set && ! acre->y_axis.view_range_set) {
733             _find_y_range_given_x_range (acre,
734                                          &acre->y_axis.view_min,
735                                          &acre->y_axis.view_max,
736                                          acre->x_axis.view_min,
737                                          acre->x_axis.view_max);
738         }
739         else if (acre->y_axis.view_range_set && ! acre->x_axis.view_range_set) {
740             _find_x_range_given_y_range (acre,
741                                          &acre->x_axis.view_min,
742                                          &acre->x_axis.view_max,
743                                          acre->y_axis.view_min,
744                                          acre->y_axis.view_max);
745         }
746     }
747
748     /* Then, increase the axis ranges just enough so that the step
749      * sizes for the ticks will be integers.
750      */
751     _expand_range_for_width (&acre->x_axis.view_min,
752                              &acre->x_axis.view_max,
753                              acre->chart.width);
754
755     _expand_range_for_width (&acre->y_axis.view_min,
756                              &acre->y_axis.view_max,
757                              acre->chart.height);
758
759     /* Finally, we also translate the axis ranges slightly so that the
760      * ticks land on half-integer device-pixel positions.
761      */
762     cairo_save (cr);
763     {
764         _set_transform_to_data_space (acre);
765
766         x_adjust = 0.0;
767         y_adjust = 0.0;
768         cairo_user_to_device (cr, &x_adjust, &y_adjust);
769         x_adjust = (round (x_adjust + 0.5) - 0.5) - x_adjust;
770         y_adjust = (round (y_adjust + 0.5) - 0.5) - y_adjust;
771         cairo_device_to_user_distance (cr, &x_adjust, &y_adjust);
772
773         acre->x_axis.view_min -= x_adjust;
774         acre->x_axis.view_max -= x_adjust;
775
776         acre->y_axis.view_min -= y_adjust;
777         acre->y_axis.view_max -= y_adjust;
778     }
779     cairo_restore (cr);
780 }
781
782 static void
783 _choose_colors (acre_t *acre)
784 {
785     cmsHPROFILE lab_profile, srgb_profile;
786     cmsHTRANSFORM lab_to_srgb;
787     int i;
788     double theta, radius, srgb[3];
789     cmsCIELab lab;
790
791     lab_profile = cmsCreateLabProfile (NULL); /* D50 */
792     srgb_profile = cmsCreate_sRGBProfile ();
793
794     lab_to_srgb = cmsCreateTransform (lab_profile, TYPE_Lab_DBL,
795                                       srgb_profile, TYPE_RGB_DBL,
796                                       INTENT_PERCEPTUAL, 0);
797
798     acre->num_colors = acre->num_data;
799
800     if (acre->num_colors > acre->colors_size) {
801         acre->colors_size = acre->num_colors;
802         acre->colors = xrealloc (acre->colors,
803                                  acre->colors_size * sizeof (acre_color_t));
804     }
805
806     lab.L = 36;
807     radius = 130;
808     for (i = 0; i < acre->num_colors; i++) {
809         theta = 0.713 + 2 * M_PI * (double) i / acre->num_colors;
810         lab.a = radius * cos (theta);
811         lab.b = radius * sin (theta);
812
813         cmsDoTransform (lab_to_srgb, &lab, srgb, 1);
814
815         acre->colors[i].red = srgb[0];
816         acre->colors[i].green = srgb[1];
817         acre->colors[i].blue = srgb[2];
818     }
819
820     cmsDeleteTransform (lab_to_srgb);
821     cmsCloseProfile (lab_profile);
822     cmsCloseProfile (srgb_profile);
823 }
824
825 /* Draw the given dataset as a line. */
826 static void
827 _draw_data_line (acre_t *acre, acre_data_t *data)
828 {
829     unsigned i;
830     cairo_t *cr = acre->cr;
831
832     cairo_save (cr);
833
834     cairo_new_path (cr);
835
836     for (i = 0; i < data->num_points; i++) {
837         cairo_line_to (cr,
838                        data->points[i].x,
839                        data->points[i].y);
840     }
841
842     cairo_identity_matrix (cr);
843     cairo_set_line_width (cr, 1.0);
844     cairo_stroke (cr);
845
846     cairo_restore (cr);
847 }
848
849 #define BARS_WIDTH 0.8
850
851 /* Draw the given dataset as bars. */
852 static void
853 _draw_data_bars (acre_t *acre, acre_data_t *data)
854 {
855     unsigned i;
856     cairo_t *cr = acre->cr;
857
858     cairo_save (cr);
859
860     cairo_new_path (cr);
861
862     for (i = 0; i < data->num_points; i++) {
863         cairo_rectangle (cr,
864                          data->points[i].x - BARS_WIDTH / 2.0, 0.0,
865                          BARS_WIDTH, data->points[i].y);
866     }
867
868     cairo_identity_matrix (cr);
869     cairo_set_line_width (cr, 1.0);
870     cairo_stroke (cr);
871
872     cairo_restore (cr);
873 }
874
875 /* Draw the given dataset as bars if there is room for that.
876  *
877  * Or, if the bars would run into each other, use a line instead.
878  */
879 static void
880 _draw_data_bars_or_line (acre_t *acre, acre_data_t *data)
881 {
882     cairo_t *cr = acre->cr;
883     double ignored, width;
884
885     /* Check device-space width available for inter-bar padding. */
886     width = 1.0 - BARS_WIDTH;
887     ignored = 0.0;
888     cairo_user_to_device_distance (cr, &width, &ignored);
889
890     /* If padding is less than two pixels, draw with a line instead. */
891     if (width < 2.0)
892         _draw_data_line (acre, data);
893     else
894         _draw_data_bars (acre, data);
895 }
896
897 #define TIMELINE_BAR_HEIGHT 0.6
898
899 /* Draw the given dataset as a timeline. Each (X,Y) point (potentially
900  * with a name) specifies the (start,stop) of a single timeline bar.
901  *
902  * Each independent timeline dataset in the chart is given its own
903  * vertical position, as specified by 'y_position'.
904  */
905 static void
906 _draw_data_timeline (acre_t *acre, acre_data_t *data, int y_position)
907 {
908     unsigned i;
909     cairo_t *cr = acre->cr;
910     PangoLayout *timeline_label_layout;
911     double ignored, label_offset;
912     int labels_within_bar;
913
914     cairo_save (cr);
915
916     timeline_label_layout = _create_layout (acre, "Timeline");
917     pango_layout_set_font_description (timeline_label_layout, acre->font);
918
919     ignored = 0.0;
920     label_offset = ACRE_FONT_SIZE;
921     cairo_device_to_user_distance (cr, &ignored, &label_offset);
922
923     labels_within_bar = TIMELINE_BAR_HEIGHT / fabs (label_offset);
924
925     for (i = 0; i < data->num_points; i++) {
926         cairo_rectangle (cr,
927                          data->points[i].x,
928                          y_position - TIMELINE_BAR_HEIGHT / 2.0,
929                          data->points[i].y - data->points[i].x,
930                          TIMELINE_BAR_HEIGHT);
931
932         cairo_save (cr);
933         cairo_identity_matrix (cr);
934         cairo_set_line_width (cr, 1.0);
935         cairo_stroke_preserve (cr);
936         cairo_restore (cr);
937
938         cairo_new_path (cr);
939
940         if (i <= data->num_names && data->names[i]) {
941             cairo_save (cr);
942
943             cairo_move_to (cr, data->points[i].x,
944                            y_position + TIMELINE_BAR_HEIGHT / 2.0 +
945                            (i % labels_within_bar) * label_offset);
946             pango_layout_set_text (timeline_label_layout, data->names[i], -1);
947             cairo_identity_matrix (cr);
948             pango_cairo_show_layout (cr, timeline_label_layout);
949
950             cairo_restore (cr);
951         } else {
952             cairo_new_path (cr);
953         }
954
955     }
956
957     _destroy_layout (timeline_label_layout);
958
959     cairo_restore (cr);
960 }
961
962 /* Draw all the datasets of the chart. */
963 static void
964 _draw_data (acre_t *acre)
965 {
966     cairo_t *cr = acre->cr;
967     unsigned int i;
968     acre_data_t *data;
969
970     cairo_save (cr);
971
972     cairo_rectangle (cr,
973                      acre->chart.x, acre->chart.y,
974                      acre->chart.width, acre->chart.height);
975     cairo_clip (cr);
976
977     cairo_set_source_rgb (cr, 0, 0, 0);
978
979     _set_transform_to_data_space (acre);
980
981     for (i = 0; i < acre->num_data; i++) {
982         int color = i % acre->num_colors;
983         cairo_set_source_rgb (cr,
984                               acre->colors[color].red,
985                               acre->colors[color].green,
986                               acre->colors[color].blue);
987         data = acre->data[i];
988
989         switch (data->style) {
990         case ACRE_STYLE_LINE:
991             _draw_data_line (acre, data);
992             break;
993         case ACRE_STYLE_BARS:
994             _draw_data_bars (acre, data);
995             break;
996         case ACRE_STYLE_BARS_OR_LINE:
997             _draw_data_bars_or_line (acre, data);
998             break;
999         case ACRE_STYLE_TIMELINE:
1000             /* Position the timeline bars top-down */
1001             _draw_data_timeline (acre, data, acre->num_data - 1 - i);
1002             break;
1003         }
1004     }
1005
1006     cairo_restore (cr);
1007 }
1008
1009 typedef enum _ticks { ACRE_TICKS_X, ACRE_TICKS_Y } acre_ticks_t;
1010
1011 static void
1012 _draw_ticks (acre_t *acre,
1013              double axis_min, double axis_max,
1014              acre_ticks_t ticks)
1015 {
1016     cairo_t *cr = acre->cr;
1017     double t, step, sub_step;
1018     int minor_divisions;
1019
1020     cairo_save (cr);
1021
1022     _set_transform_to_data_space (acre);
1023
1024     step = _step_for_range (axis_max - axis_min, &minor_divisions);
1025     sub_step = step / minor_divisions;
1026
1027     for (t = (floor (axis_min / sub_step) + 1) * sub_step;
1028          t <= axis_max;
1029          t += sub_step)
1030     {
1031         int tick_size;
1032         if (fabs((t / step) - round (t / step)) < 0.5 * (sub_step / step))
1033             tick_size = ACRE_TICK_MAJOR_SIZE;
1034         else
1035             tick_size = ACRE_TICK_MINOR_SIZE;
1036
1037         /* tick */
1038         cairo_save (cr);
1039         {
1040             if (ticks == ACRE_TICKS_X)
1041                 cairo_move_to (cr, t, acre->y_axis.view_min);
1042             else
1043                 cairo_move_to (cr, acre->x_axis.view_min, t);
1044
1045             cairo_identity_matrix (cr);
1046
1047             if (ticks == ACRE_TICKS_X) {
1048                 cairo_rel_line_to (cr, 0, 0.5);
1049                 cairo_rel_line_to (cr, 0, -tick_size - 0.5);
1050             } else {
1051                 cairo_rel_line_to (cr, -0.5, 0);
1052                 cairo_rel_line_to (cr, tick_size + 0.5, 0);
1053             }
1054
1055             cairo_set_line_width (cr, 1.0);
1056             cairo_stroke (cr);
1057         }
1058         cairo_restore (cr);
1059
1060         /* label */
1061         if (tick_size == ACRE_TICK_MAJOR_SIZE)
1062         {
1063             PangoLayout *layout;
1064             int width, height;
1065             double label_value;
1066
1067             cairo_save (cr);
1068
1069             label_value = t;
1070             if (fabs (label_value) < (sub_step / 1000.))
1071                 label_value = 0.0;
1072             layout = _create_layout_printf (acre, "%g", label_value);
1073
1074             if (ticks == ACRE_TICKS_X)
1075                 cairo_move_to (cr, t, acre->y_axis.view_min);
1076             else
1077                 cairo_move_to (cr, acre->x_axis.view_min, t);
1078
1079             cairo_identity_matrix (cr);
1080             pango_layout_get_pixel_size (layout, &width, &height);
1081
1082             if (ticks == ACRE_TICKS_X)
1083                 cairo_rel_move_to (cr, -width / 2, ACRE_X_TICK_VALUE_PAD);
1084             else
1085                 cairo_rel_move_to (cr, -width - ACRE_Y_TICK_VALUE_PAD,
1086                                    -height/2);
1087
1088             _show_layout (cr, layout);
1089
1090             cairo_restore (cr);
1091         }
1092     }
1093
1094     cairo_restore (cr);
1095 }
1096
1097 static void
1098 _draw_legend (acre_t *acre)
1099 {
1100     PangoLayout *layout;
1101     int label_width, max_label_width = 0;
1102     int width, height;
1103     unsigned int i;
1104     cairo_t *cr = acre->cr;
1105
1106     cairo_save (cr);
1107
1108     for (i = 0; i < acre->num_data; i++) {
1109         layout = _create_layout (acre, acre->data[i]->name);
1110         pango_layout_get_pixel_size (layout, &label_width, NULL);
1111         _destroy_layout (layout);
1112         if (label_width > max_label_width)
1113             max_label_width = label_width;
1114     }
1115
1116     width = ACRE_LEGEND_PAD + ACRE_LEGEND_LINE_SIZE + ACRE_LEGEND_PAD + 
1117         max_label_width + ACRE_LEGEND_PAD;
1118     height = ACRE_LEGEND_PAD +
1119         acre->num_data * (ACRE_FONT_SIZE + ACRE_LEGEND_PAD);
1120
1121     cairo_translate (cr, acre->chart.x, acre->chart.y);
1122
1123     cairo_translate (cr,
1124                      acre->chart.width - ACRE_LEGEND_PAD - width,
1125                      ACRE_LEGEND_PAD);
1126
1127     cairo_rectangle (cr, -0.5, -0.5, width + 1.0, height + 1.0);
1128     cairo_set_source_rgb (cr, 0, 0, 0);
1129     cairo_set_line_width (cr, 1.0);
1130     cairo_stroke (cr);
1131
1132     cairo_translate (cr, ACRE_LEGEND_PAD, ACRE_LEGEND_PAD);
1133
1134     for (i = 0; i < acre->num_data; i++) {
1135         cairo_rectangle (cr,
1136                          0, ACRE_LEGEND_LINE_SIZE / 2,
1137                          ACRE_LEGEND_LINE_SIZE, ACRE_LEGEND_LINE_SIZE / 2);
1138         cairo_set_source_rgb (cr,
1139                               acre->colors[i % acre->num_colors].red,
1140                               acre->colors[i % acre->num_colors].green,
1141                               acre->colors[i % acre->num_colors].blue);
1142         cairo_fill (cr);
1143
1144         layout = _create_layout (acre, acre->data[i]->name);
1145         cairo_move_to (cr, ACRE_LEGEND_LINE_SIZE + ACRE_LEGEND_PAD, 0);
1146         cairo_set_source_rgb (cr, 0, 0, 0);
1147         _show_layout (cr, layout);
1148
1149         cairo_translate (cr, 0, ACRE_LEGEND_PAD + ACRE_FONT_SIZE);
1150     }
1151
1152     cairo_restore (cr);
1153 }
1154
1155 static void
1156 _draw_frame_and_ticks (acre_t *acre)
1157 {
1158     cairo_t *cr = acre->cr;
1159
1160     cairo_save (cr);
1161
1162     cairo_set_source_rgb (cr, 0, 0, 0); /* black */
1163
1164     /* ticks */
1165     _draw_ticks (acre, acre->x_axis.view_min, acre->x_axis.view_max, ACRE_TICKS_X);
1166     _draw_ticks (acre, acre->y_axis.view_min, acre->y_axis.view_max, ACRE_TICKS_Y);
1167
1168     /* frame */
1169     cairo_rectangle (cr,
1170                      acre->chart.x - 0.5, acre->chart.y - 0.5,
1171                      acre->chart.width + 1.0, acre->chart.height + 1.0);
1172     cairo_set_line_width (cr, 1.0);
1173     cairo_stroke (cr);
1174
1175     cairo_restore (cr);
1176 }
1177
1178 /* Draw the plot to the given cairo context within a user-space
1179  * rectangle from (0, 0) to (width, height). This size includes all
1180  * space for extra-plot elements (such as the title, the axis labels,
1181  * etc.)
1182  */
1183 void
1184 acre_draw (acre_t *acre, cairo_t *cr, int width, int height)
1185 {
1186     acre->cr = cr;
1187
1188     acre->width = width;
1189     acre->height = height;
1190
1191     acre->chart.width = width;
1192     acre->chart.height = height;
1193
1194     cairo_save (cr);
1195
1196     cairo_set_source_rgb (cr, 1, 1, 1);
1197
1198     _choose_colors (acre);
1199
1200     /* We compute the axis ranges before doing label layout so that we
1201      * can account for the width of the y-axis value labels. */
1202     _compute_axis_ranges (acre);
1203
1204     _draw_title_and_labels (acre);
1205
1206     /* And we recompute the axis ranges now that the title and axis
1207      * label space is all measured and accounted for. */
1208     _compute_axis_ranges (acre);
1209
1210     _draw_data (acre);
1211
1212     if (acre->num_data > 1)
1213         _draw_legend (acre);
1214
1215     _draw_frame_and_ticks (acre);
1216 }
1217
1218 /* Create a new dataset---a collection of (x, y) datapoints. A single
1219  * plot can contain multiple datasets, (see acre_add_data). */
1220 acre_data_t *
1221 acre_data_create (void)
1222 {
1223     acre_data_t *data;
1224
1225     data = xmalloc (sizeof (acre_data_t));
1226
1227     data->name = NULL;
1228
1229     data->style = ACRE_STYLE_LINE;
1230
1231     data->points = NULL;
1232     data->points_size = 0;
1233     data->num_points = 0;
1234
1235     return data;
1236 }
1237
1238 /* Destroy an acre dataset. Do not call this function if the dataset
1239  * has been added to an acre_t plot with acre_add_data. */
1240 void
1241 acre_data_destroy (acre_data_t *data)
1242 {
1243     unsigned i;
1244
1245     for (i = 0; i < data->num_names; i++) {
1246         if (data->names[i])
1247             free (data->names[i]);
1248     }
1249     free (data->names);
1250
1251     free (data->name);
1252
1253     free (data->points);
1254
1255     free (data);
1256 }
1257
1258 void
1259 acre_data_set_style (acre_data_t *data, acre_style_t style)
1260 {
1261     data->style = style;
1262 }
1263
1264 /* Set the label for this dataset (to appear in the plot's key). */
1265 void
1266 acre_data_set_name (acre_data_t *data, const char *name)
1267 {
1268     free (data->name);
1269
1270     data->name = strdup (name);
1271 }
1272
1273 /* Add a datapoint to the given dataset. */
1274 void
1275 acre_data_add_point_2d (acre_data_t *data, double x, double y)
1276 {
1277     if (data->num_points >= data->points_size) {
1278         data->points_size *= 2;
1279         if (data->points_size == 0)
1280             data->points_size = 16;
1281         data->points = xrealloc_ab (data->points,
1282                                     data->points_size,
1283                                     sizeof (acre_data_point_2d_t));
1284     }
1285
1286     data->points[data->num_points].x = x;
1287     data->points[data->num_points].y = y;
1288
1289     if (data->num_points == 0) {
1290         data->min.x = x;
1291         data->min.y = y;
1292
1293         data->max.x = x;
1294         data->max.y = y;
1295     } else {
1296         if (x < data->min.x)
1297             data->min.x = x;
1298         if (y < data->min.y)
1299             data->min.y = y;
1300
1301         if (x > data->max.x)
1302             data->max.x = x;
1303         if (y > data->max.y)
1304             data->max.y = y;
1305     }
1306
1307     data->num_points++;
1308 }
1309
1310 /* Add a datapoint with a name to the given dataset. */
1311 void
1312 acre_data_add_point_2d_named (acre_data_t *data, double x, double y, const char *name)
1313 {
1314     unsigned i;
1315
1316     acre_data_add_point_2d (data, x, y);
1317
1318     if (data->names_size < data->points_size) {
1319         data->names_size = data->points_size;
1320         data->names = xrealloc_ab (data->names,
1321                                    data->names_size,
1322                                    sizeof (char *));
1323     }
1324
1325     /* Initialize any newly-created holes in the array to NULL. */
1326     for (i = data->num_names; i < data->num_points - 1; i++)
1327         data->names[i] = NULL;
1328
1329     data->num_names = data->num_points;
1330
1331     data->names[data->num_names - 1] = xstrdup (name);
1332 }