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