]> git.cworth.org Git - acre/blob - acre.c
Leave sufficient space for Y-axis value labels
[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 typedef struct _acre_data_point_2d {
32     double x;
33     double y;
34 } acre_data_point_2d_t;
35
36 struct _acre_data {
37     char *name;
38
39     acre_data_point_2d_t *points;
40     unsigned int points_size;
41     unsigned int num_points;
42 };
43
44 typedef struct _acre_axis {
45     char *label;
46     double min;
47     double max;
48 } acre_axis_t;
49
50 struct _acre {
51     char *title;
52     acre_axis_t x_axis;
53     acre_axis_t y_axis;
54
55     acre_data_t **data;
56     unsigned int data_size;
57     unsigned int num_data;
58
59     /* Data for drawing. */
60     cairo_t *cr;
61     PangoFontDescription *font;
62
63     /* Total size including labels. */
64     int width;
65     int height;
66
67     /* Position and size of chart alone. */
68     PangoRectangle chart;
69 };
70
71 /* Create a new, empty plot. */
72 acre_t *
73 acre_create (void)
74 {
75     acre_t *acre;
76
77     acre = xmalloc (sizeof (acre_t));
78
79     acre->title = NULL;
80
81     acre->x_axis.label = NULL;
82     acre->x_axis.min = 0.0;
83     acre->x_axis.max = 0.0;
84
85     acre->y_axis.label = NULL;
86     acre->y_axis.min = 0.0;
87     acre->y_axis.max = 0.0;
88
89     acre->data = NULL;
90     acre->data_size = 0;
91     acre->num_data = 0;
92
93     acre->chart.x = 0;
94     acre->chart.y = 0;
95     acre->chart.width = 0;
96     acre->chart.height = 0;
97
98     return acre;
99 }
100
101 /* Destroy a plot. */
102 void
103 acre_destroy (acre_t *acre)
104 {
105     unsigned int i;
106
107     free (acre->title);
108     free (acre->x_axis.label);
109     free (acre->y_axis.label);
110
111     for (i = 0; i < acre->num_data; i++)
112         acre_data_destroy (acre->data[i]);
113
114     free (acre->data);
115
116     free (acre);
117 }
118
119 void
120 acre_set_title (acre_t *acre, const char *title)
121 {
122     free (acre->title);
123
124     acre->title = strdup (title);
125 }
126
127 void
128 acre_set_x_axis_label (acre_t *acre, const char *label)
129 {
130     free (acre->x_axis.label);
131
132     acre->x_axis.label = strdup (label);
133 }
134
135 void
136 acre_set_y_axis_label (acre_t *acre, const char *label)
137 {
138     free (acre->y_axis.label);
139
140     acre->y_axis.label = strdup (label);
141 }
142
143 /* Add a dataset to the plot. The plot assumes ownership of the
144  * dataset so it is not necessary to call acre_data_destroy on it. */
145 void
146 acre_add_data (acre_t *acre, acre_data_t *data)
147 {
148     if (acre->num_data >= acre->data_size) {
149         acre->data_size *= 2;
150         if (acre->data_size == 0)
151             acre->data_size = 1;
152         acre->data = xrealloc_ab (acre->data,
153                                   acre->data_size,
154                                   sizeof (acre_data_t *));
155     }
156
157     acre->data[acre->num_data] = data;
158     acre->num_data++;
159 }
160
161 #define ACRE_FONT_FAMILY "sans"
162 #define ACRE_FONT_SIZE 12
163 #define ACRE_TITLE_FONT_SIZE 32
164 #define ACRE_PAD (ACRE_FONT_SIZE)
165 #define ACRE_TICK_SIZE 6
166
167 static PangoLayout *
168 _create_layout (acre_t *acre, const char *text)
169 {
170     PangoLayout *layout;
171
172     layout = pango_cairo_create_layout (acre->cr);
173     pango_layout_set_font_description (layout, acre->font);
174     pango_layout_set_text (layout, text, -1);
175     pango_layout_set_alignment (layout, PANGO_ALIGN_CENTER);
176
177     return layout;
178 }
179
180 #define PRINTF_FORMAT(fmt_index, va_index) __attribute__ ((__format__(__printf__, fmt_index, va_index)))
181
182 static PangoLayout *
183 _create_layout_vprintf (acre_t *acre, const char *fmt, va_list ap)
184 {
185     PangoLayout *layout;
186     char *text;
187
188     vasprintf (&text, fmt, ap);
189
190     layout = _create_layout (acre, text);
191
192     free (text);
193
194     return layout;
195 }
196
197 static PangoLayout *
198 _create_layout_printf (acre_t *acre, const char *fmt, ...)
199     PRINTF_FORMAT (2, 3);
200
201 static PangoLayout *
202 _create_layout_printf (acre_t *acre, const char *fmt, ...)
203 {
204     va_list ap;
205     PangoLayout *layout;
206
207     va_start (ap, fmt);
208
209     layout = _create_layout_vprintf (acre, fmt, ap);
210
211     va_end (ap);
212
213     return layout;
214 }
215
216 static void
217 _destroy_layout (PangoLayout *layout)
218 {
219     g_object_unref (layout);
220 }
221
222 static void
223 _show_layout (cairo_t *cr, PangoLayout *layout)
224 {
225     pango_cairo_show_layout (cr, layout);
226
227     _destroy_layout (layout);
228 }
229
230 static void
231 _draw_title_and_labels (acre_t *acre)
232 {
233     cairo_t *cr = acre->cr;
234     PangoFontDescription *title_font;
235     PangoLayout *title_layout, *x_axis_layout, *y_axis_layout;
236     PangoLayout *min_y, *max_y;
237     int min_y_width, max_y_width, y_axis_value_width;
238     int title_width, title_height;
239     int x_axis_width, x_axis_height;
240     int y_axis_width, y_axis_height;
241     PangoRectangle new_chart;
242
243     cairo_save (cr);
244
245     acre->font = pango_font_description_new ();
246     pango_font_description_set_family (acre->font, ACRE_FONT_FAMILY);
247     pango_font_description_set_absolute_size (acre->font,
248                                               ACRE_FONT_SIZE * PANGO_SCALE);
249
250     title_font = pango_font_description_new ();
251     pango_font_description_set_family (title_font, ACRE_FONT_FAMILY);
252     pango_font_description_set_absolute_size (title_font,
253                                               ACRE_TITLE_FONT_SIZE * PANGO_SCALE);
254
255     title_layout = _create_layout (acre, acre->title);
256     pango_layout_set_font_description (title_layout, title_font);
257
258     x_axis_layout = _create_layout (acre, acre->x_axis.label);
259     y_axis_layout = _create_layout (acre, acre->y_axis.label);
260
261     min_y = _create_layout_printf (acre, "%g",
262                                    round (acre->y_axis.min));
263     max_y = _create_layout_printf (acre, "%g",
264                                    round (acre->y_axis.max));
265
266     pango_layout_get_pixel_size (min_y, &min_y_width, NULL);
267     pango_layout_get_pixel_size (max_y, &max_y_width, NULL);
268     y_axis_value_width = MAX (min_y_width, max_y_width);
269
270     _destroy_layout (min_y);
271     _destroy_layout (max_y);
272
273     /* Iterate with the layout of the title and axis labels until they
274      * are stable, (this requires iteration since we don't know what
275      * to set their widths to in advance due to the wrapping of the
276      * other elements). */
277     while (1) {
278         pango_layout_set_width (title_layout, acre->chart.width * PANGO_SCALE);
279         pango_layout_set_width (x_axis_layout, acre->chart.width * PANGO_SCALE);
280         pango_layout_set_width (y_axis_layout, acre->chart.height * PANGO_SCALE);
281
282         pango_layout_get_pixel_size (title_layout, &title_width, &title_height);
283         pango_layout_get_pixel_size (x_axis_layout, &x_axis_width, &x_axis_height);
284         pango_layout_get_pixel_size (y_axis_layout, &y_axis_width, &y_axis_height);
285
286         new_chart.x = ACRE_PAD + y_axis_height +
287             ACRE_PAD + y_axis_value_width;
288         new_chart.width = acre->width - acre->chart.x - ACRE_PAD;
289
290         new_chart.y = ACRE_PAD + title_height + ACRE_PAD;
291         new_chart.height = acre->height - acre->chart.y - (ACRE_FONT_SIZE + ACRE_PAD + x_axis_height + ACRE_PAD);
292
293         if (new_chart.x == acre->chart.x &&
294             new_chart.y == acre->chart.y &&
295             new_chart.width == acre->chart.width &&
296             new_chart.height == acre->chart.height)
297         {
298             break;
299         }
300
301         acre->chart.x = new_chart.x;
302         acre->chart.y = new_chart.y;
303         acre->chart.width = new_chart.width;
304         acre->chart.height = new_chart.height;
305     }
306
307     cairo_set_source_rgb (cr, 0, 0, 0);
308
309     cairo_move_to (cr, acre->chart.x, ACRE_PAD);
310     _show_layout (cr, title_layout);
311
312     cairo_save (cr);
313     {
314         cairo_translate (cr, ACRE_PAD, acre->chart.y + acre->chart.height);
315         cairo_rotate (cr, - M_PI / 2.0);
316         cairo_move_to (cr, 0, 0);
317         _show_layout (cr, y_axis_layout);
318     }
319     cairo_restore (cr);
320
321     cairo_move_to (cr, acre->chart.x,
322                    acre->chart.y + acre->chart.height +
323                    ACRE_FONT_SIZE + ACRE_PAD);
324     _show_layout (cr, x_axis_layout);
325
326     cairo_restore (cr);
327 }
328
329 /* For a given axis range, compute a step size (in data space) to
330  * generate a suitable number of ticks (5 or so). */
331 static double
332 _step_for_range (double range)
333 {
334     double step, scale_factor;
335
336     /* We want roughly 5 major ticks for the chart. */
337     step = range / 5;
338
339     /* Normalize the step so we can easily snap it to a desirable
340      * value. */
341     scale_factor = pow (10.0, floor (log10 (step)));
342     step /= scale_factor;
343
344     /* We want increments of 1, 2.5, 5, or 10 (times some power of
345      * 10). The threshold values between these are computed
346      * logarithmically. */
347     if (step < 3.535533905932738) {
348         if (step < 1.58113883008419)
349             step = 1.0;
350         else
351             step = 2.5;
352     } else {
353         if (step < 7.071067811865475)
354             step = 5.0;
355         else
356             step = 10.0;
357     }
358
359     /* Un-normalize and we now have the data value that we want to
360      * step at. */
361     return step * scale_factor;
362 }
363
364 /* Given an axis range, we can compute a desired data-space step
365  * amount for the major ticks (see _step_for_range). To get
366  * nice-looking pixel-snapped ticks we want to expand the range
367  * slightly. */
368 static void
369 _expand_range_for_width (double *axis_min, double *axis_max, int pixel_size)
370 {
371     double range, new_range, step, pixel_step;
372
373     range = *axis_max - *axis_min;
374
375     step = _step_for_range (range);
376     pixel_step = step * pixel_size / range;
377
378     /* We expand the range by the ratio of the pixel step to the floor
379      * of the pixel_step.
380      */
381     new_range = range * pixel_step / floor (pixel_step);
382
383     /* And spread the increase out on either side of the range. */
384     *axis_min -= (new_range - range) / 2.0;
385     *axis_max += (new_range - range) / 2.0;
386 }
387
388 /* Setup a transformation in acre->cr such that data values plotted
389  * will appear where they should within the chart.
390  */
391 static void
392 _set_transform_to_data_space (acre_t *acre)
393 {
394     cairo_t *cr = acre->cr;
395
396     cairo_translate (cr,
397                      acre->chart.x,
398                      acre->chart.y + acre->chart.height);
399     cairo_scale (cr,
400                  acre->chart.width / (acre->x_axis.max - acre->x_axis.min),
401                  - acre->chart.height /(acre->y_axis.max - acre->y_axis.min));
402     cairo_translate (cr, -acre->x_axis.min, -acre->y_axis.min);
403 }
404
405 static void
406 _compute_axis_ranges (acre_t *acre)
407 {
408     unsigned int d, i;
409     acre_data_t *data;
410     double x_adjust, y_adjust;
411     cairo_t *cr = acre->cr;
412
413     acre->x_axis.min = acre->data[0]->points[0].x;
414     acre->x_axis.max = acre->data[0]->points[0].x;
415     acre->y_axis.min = acre->data[0]->points[0].y;
416     acre->y_axis.min = acre->data[0]->points[0].y;
417
418     /* First, simply find the extrema of the data. */
419     for (d = 0; d < acre->num_data; d++) {
420         data = acre->data[d];
421         for (i = 0; i < data->num_points; i++) {
422             if (data->points[i].x < acre->x_axis.min)
423                 acre->x_axis.min = data->points[i].x;
424             if (data->points[i].x > acre->x_axis.max)
425                 acre->x_axis.max = data->points[i].x;
426
427             if (data->points[i].y < acre->y_axis.min)
428                 acre->y_axis.min = data->points[i].y;
429             if (data->points[i].y > acre->y_axis.max)
430                 acre->y_axis.max = data->points[i].y;
431         }
432     }
433
434     /* Next, increase the axis ranges just enough so that the step
435      * sizes for the ticks will be integers.
436      */
437     _expand_range_for_width (&acre->x_axis.min,
438                              &acre->x_axis.max,
439                              acre->chart.width);
440
441     _expand_range_for_width (&acre->y_axis.min,
442                              &acre->y_axis.max,
443                              acre->chart.height);
444
445     /* Finally, we also translate the axis ranges slightly so that the
446      * ticks land on half-integer device-pixel positions.
447      */
448     cairo_save (cr);
449     {
450         _set_transform_to_data_space (acre);
451
452         x_adjust = 0.0;
453         y_adjust = 0.0;
454         cairo_user_to_device (cr, &x_adjust, &y_adjust);
455         x_adjust = (round (x_adjust + 0.5) - 0.5) - x_adjust;
456         y_adjust = (round (y_adjust + 0.5) - 0.5) - y_adjust;
457         cairo_device_to_user_distance (cr, &x_adjust, &y_adjust);
458
459         acre->x_axis.min -= x_adjust;
460         acre->x_axis.max -= x_adjust;
461
462         acre->y_axis.min -= y_adjust;
463         acre->y_axis.max -= y_adjust;
464     }
465     cairo_restore (cr);
466 }
467
468 static void
469 _draw_data (acre_t *acre)
470 {
471     cairo_t *cr = acre->cr;
472     unsigned int d, i;
473     acre_data_t *data;
474
475     cairo_save (cr);
476
477     cairo_set_source_rgb (cr, 0, 0, 0);
478
479     _set_transform_to_data_space (acre);
480
481     for (d = 0; d < acre->num_data; d++) {
482         data = acre->data[d];
483         cairo_new_path (cr);
484         for (i = 0; i < data->num_points; i++) {
485             cairo_line_to (cr,
486                            data->points[i].x,
487                            data->points[i].y);
488         }
489         cairo_save (cr);
490         {
491             cairo_identity_matrix (cr);
492             cairo_set_line_width (cr, 1.0);
493             cairo_stroke (cr);
494         }
495         cairo_restore (cr);
496     }
497
498     cairo_restore (cr);
499 }
500
501 static void
502 _draw_frame_and_ticks (acre_t *acre)
503 {
504     cairo_t *cr = acre->cr;
505     double step, x, y;
506
507     cairo_save (cr);
508
509     cairo_set_source_rgb (cr, 0, 0, 0); /* black */
510
511     /* First the ticks within data space. */
512     cairo_save (cr);
513     {
514         _set_transform_to_data_space (acre);
515
516         step = _step_for_range (acre->x_axis.max -acre->x_axis.min);
517         x = (floor (acre->x_axis.min / step) + 1) * step;
518         while (x <= acre->x_axis.max) {
519             cairo_move_to (cr, x, acre->y_axis.min);
520             cairo_save (cr);
521             {
522                 /* tick */
523                 cairo_save (cr);
524                 {
525                     cairo_identity_matrix (cr);
526                     cairo_rel_line_to (cr, 0, 0.5);
527                     cairo_rel_line_to (cr, 0, -ACRE_TICK_SIZE-0.5);
528                     cairo_set_line_width (cr, 1.0);
529                     cairo_stroke (cr);
530                 }
531                 cairo_restore (cr);
532
533                 /* label */
534                 cairo_save (cr);
535                 {
536                     PangoLayout *layout;
537                     int width, height;
538                     layout = _create_layout_printf (acre, "%g", x);
539                     cairo_move_to (cr, x, acre->y_axis.min);
540                     cairo_identity_matrix (cr);
541                     pango_layout_get_pixel_size (layout, &width, &height);
542                     cairo_rel_move_to (cr, -width / 2, 2);
543                     _show_layout (cr, layout);
544                 }
545                 cairo_restore (cr);
546             }
547             cairo_restore (cr);
548             x += step;
549         }
550
551         step = _step_for_range (acre->y_axis.max -acre->y_axis.min);
552         y = (floor (acre->y_axis.min / step) + 1) * step;
553         while (y <= acre->y_axis.max) {
554             cairo_move_to (cr, acre->x_axis.min, y);
555             cairo_save (cr);
556             {
557                 /* tick */
558                 cairo_save (cr);
559                 {
560                     cairo_identity_matrix (cr);
561                     cairo_rel_line_to (cr, -0.5, 0);
562                     cairo_rel_line_to (cr, ACRE_TICK_SIZE+0.5, 0);
563                     cairo_set_line_width (cr, 1.0);
564                     cairo_stroke (cr);
565                 }
566                 cairo_restore (cr);
567
568                 /* label */
569                 cairo_save (cr);
570                 {
571                     PangoLayout *layout;
572                     int width, height;
573                     layout = _create_layout_printf (acre, "%g", y);
574                     cairo_move_to (cr, acre->x_axis.min, y);
575                     cairo_identity_matrix (cr);
576                     pango_layout_get_pixel_size (layout, &width, &height);
577                     cairo_rel_move_to (cr, -width-4, -height/2);
578                     _show_layout (cr, layout);
579                 }
580                 cairo_restore (cr);
581             }
582             cairo_restore (cr);
583             y += step;
584         }
585     }
586     cairo_restore (cr);
587
588     /* Then the frame drawn in pixel space. */
589     cairo_rectangle (cr,
590                      acre->chart.x - 0.5, acre->chart.y - 0.5,
591                      acre->chart.width + 1.0, acre->chart.height + 1.0);
592     cairo_set_line_width (cr, 1.0);
593     cairo_stroke (cr);
594
595     cairo_restore (cr);
596 }
597
598 /* Draw the plot to the given cairo context within a user-space
599  * rectangle from (0, 0) to (width, height). This size includes all
600  * space for extra-plot elements (such as the title, the axis labels,
601  * etc.)
602  */
603 void
604 acre_draw (acre_t *acre, cairo_t *cr, int width, int height)
605 {
606     acre->cr = cr;
607
608     acre->width = width;
609     acre->height = height;
610
611     acre->chart.width = width;
612     acre->chart.height = height;
613
614     cairo_save (cr);
615
616     cairo_set_source_rgb (cr, 1, 1, 1);
617
618     /* We compute the axis ranges before doing label layout so that we
619      * can account for the width of the y-axis value labels. */
620     _compute_axis_ranges (acre);
621
622     _draw_title_and_labels (acre);
623
624     /* And we recompute the axis ranges now that the title and axis
625      * label space is all measured and accounted for. */
626     _compute_axis_ranges (acre);
627
628     _draw_data (acre);
629
630     _draw_frame_and_ticks (acre);
631 }
632
633 /* Create a new dataset---a collection of (x, y) datapoints. A single
634  * plot can contain multiple datasets, (see acre_add_data). */
635 acre_data_t *
636 acre_data_create (void)
637 {
638     acre_data_t *data;
639
640     data = xmalloc (sizeof (acre_data_t));
641
642     data->name = NULL;
643
644     data->points = NULL;
645     data->points_size = 0;
646     data->num_points = 0;
647
648     return data;
649 }
650
651 /* Destroy an acre dataset. Do not call this function if the dataset
652  * has been added to an acre_t plot with acre_add_data. */
653 void
654 acre_data_destroy (acre_data_t *data)
655 {
656     free (data->points);
657
658     free (data);
659 }
660
661 /* Set the label for this dataset (to appear in the plot's key). */
662 void
663 acre_data_set_name (acre_data_t *data, const char *name)
664 {
665     free (data->name);
666
667     data->name = strdup (name);
668 }
669
670 /* Add a datapoint to the given dataset. */
671 void
672 acre_data_add_point_2d (acre_data_t *data, double x, double y)
673 {
674     if (data->num_points >= data->points_size) {
675         data->points_size *= 2;
676         if (data->points_size == 0)
677             data->points_size = 16;
678         data->points = xrealloc_ab (data->points,
679                                     data->points_size,
680                                     sizeof (acre_data_point_2d_t));
681     }
682
683     data->points[data->num_points].x = x;
684     data->points[data->num_points].y = y;
685     data->num_points++;
686 }