1 /* acre - A cairo-based library for creating plots and charts.
3 * Copyright © 2009 Carl Worth <cworth@cworth.org>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20 #define _ISOC99_SOURCE /* for round() */
21 #define _XOPEN_SOURCE 500
22 #define _GNU_SOURCE /* for asprintf() */
30 typedef struct _acre_data_point_2d {
33 } acre_data_point_2d_t;
38 acre_data_point_2d_t *points;
39 unsigned int points_size;
40 unsigned int num_points;
43 typedef struct _acre_axis {
55 unsigned int data_size;
56 unsigned int num_data;
58 /* Data for drawing. */
60 PangoFontDescription *font;
62 /* Total size including labels. */
66 /* Position and size of chart alone. */
70 /* Create a new, empty plot. */
76 acre = xmalloc (sizeof (acre_t));
80 acre->x_axis.label = NULL;
81 acre->x_axis.min = 0.0;
82 acre->x_axis.max = 0.0;
84 acre->y_axis.label = NULL;
85 acre->y_axis.min = 0.0;
86 acre->y_axis.max = 0.0;
97 acre_destroy (acre_t *acre)
102 free (acre->x_axis.label);
103 free (acre->y_axis.label);
105 for (i = 0; i < acre->num_data; i++)
106 acre_data_destroy (acre->data[i]);
114 acre_set_title (acre_t *acre, const char *title)
118 acre->title = strdup (title);
122 acre_set_x_axis_label (acre_t *acre, const char *label)
124 free (acre->x_axis.label);
126 acre->x_axis.label = strdup (label);
130 acre_set_y_axis_label (acre_t *acre, const char *label)
132 free (acre->y_axis.label);
134 acre->y_axis.label = strdup (label);
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. */
140 acre_add_data (acre_t *acre, acre_data_t *data)
142 if (acre->num_data >= acre->data_size) {
143 acre->data_size *= 2;
144 if (acre->data_size == 0)
146 acre->data = xrealloc_ab (acre->data,
148 sizeof (acre_data_t *));
151 acre->data[acre->num_data] = data;
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
162 _create_layout (acre_t *acre, const char *text)
166 layout = pango_cairo_create_layout (acre->cr);
167 pango_layout_set_font_description (layout, acre->font);
168 pango_layout_set_text (layout, text, -1);
169 pango_layout_set_alignment (layout, PANGO_ALIGN_CENTER);
175 _destroy_layout (PangoLayout *layout)
177 g_object_unref (layout);
181 _show_layout (cairo_t *cr, PangoLayout *layout)
183 pango_cairo_show_layout (cr, layout);
185 _destroy_layout (layout);
189 _draw_title_and_labels (acre_t *acre)
191 cairo_t *cr = acre->cr;
192 PangoFontDescription *title_font;
193 PangoLayout *title_layout, *x_axis_layout, *y_axis_layout;
194 int title_width, title_height;
195 int x_axis_width, x_axis_height;
196 int y_axis_width, y_axis_height;
197 PangoRectangle new_chart;
201 acre->font = pango_font_description_new ();
202 pango_font_description_set_family (acre->font, ACRE_FONT_FAMILY);
203 pango_font_description_set_absolute_size (acre->font,
204 ACRE_FONT_SIZE * PANGO_SCALE);
206 title_font = pango_font_description_new ();
207 pango_font_description_set_family (title_font, ACRE_FONT_FAMILY);
208 pango_font_description_set_absolute_size (title_font,
209 ACRE_TITLE_FONT_SIZE * PANGO_SCALE);
211 title_layout = _create_layout (acre, acre->title);
212 pango_layout_set_font_description (title_layout, title_font);
214 x_axis_layout = _create_layout (acre, acre->x_axis.label);
215 y_axis_layout = _create_layout (acre, acre->y_axis.label);
217 /* Iterate with the layout of the title and axis labels until they
218 * are stable, (this requires iteration since we don't know what
219 * to set their widths to in advance due to the wrapping of the
220 * other elements). */
223 acre->chart.width = acre->width;
224 acre->chart.height = acre->height;
226 pango_layout_set_width (title_layout, acre->chart.width * PANGO_SCALE);
227 pango_layout_set_width (x_axis_layout, acre->chart.width * PANGO_SCALE);
228 pango_layout_set_width (y_axis_layout, acre->chart.height * PANGO_SCALE);
230 pango_layout_get_pixel_size (title_layout, &title_width, &title_height);
231 pango_layout_get_pixel_size (x_axis_layout, &x_axis_width, &x_axis_height);
232 pango_layout_get_pixel_size (y_axis_layout, &y_axis_width, &y_axis_height);
234 new_chart.x = ACRE_PAD + y_axis_height +
235 ACRE_PAD + ACRE_FONT_SIZE;
236 new_chart.width = acre->width - acre->chart.x - ACRE_PAD;
238 new_chart.y = ACRE_PAD + title_height + ACRE_PAD;
239 new_chart.height = acre->height - acre->chart.y - (ACRE_FONT_SIZE + ACRE_PAD + x_axis_height + ACRE_PAD);
241 if (new_chart.x == acre->chart.x &&
242 new_chart.y == acre->chart.y &&
243 new_chart.width == acre->chart.width &&
244 new_chart.height == acre->chart.height)
249 acre->chart.x = new_chart.x;
250 acre->chart.y = new_chart.y;
251 acre->chart.width = new_chart.width;
252 acre->chart.height = new_chart.height;
255 cairo_set_source_rgb (cr, 0, 0, 0);
257 cairo_move_to (cr, acre->chart.x, ACRE_PAD);
258 _show_layout (cr, title_layout);
262 cairo_translate (cr, ACRE_PAD, acre->chart.y + acre->chart.height);
263 cairo_rotate (cr, - M_PI / 2.0);
264 cairo_move_to (cr, 0, 0);
265 _show_layout (cr, y_axis_layout);
269 cairo_move_to (cr, acre->chart.x,
270 acre->chart.y + acre->chart.height +
271 ACRE_FONT_SIZE + ACRE_PAD);
272 _show_layout (cr, x_axis_layout);
277 /* For a given axis range, compute a step size (in data space) to
278 * generate a suitable number of ticks (5 or so). */
280 _step_for_range (double range)
282 double step, scale_factor;
284 /* We want roughly 5 major ticks for the chart. */
287 /* Normalize the step so we can easily snap it to a desirable
289 scale_factor = pow (10.0, floor (log10 (step)));
290 step /= scale_factor;
292 /* We want increments of 1, 2.5, 5, or 10 (times some power of
293 * 10). The threshold values between these are computed
294 * logarithmically. */
295 if (step < 3.535533905932738) {
296 if (step < 1.58113883008419)
301 if (step < 7.071067811865475)
307 /* Un-normalize and we now have the data value that we want to
309 return step * scale_factor;
312 /* Given an axis range, we can compute a desired data-space step
313 * amount for the major ticks (see _step_for_range). To get
314 * nice-looking pixel-snapped ticks we want to expand the range
317 _expand_range_for_width (double *axis_min, double *axis_max, int pixel_size)
319 double range, new_range, step, pixel_step;
321 range = *axis_max - *axis_min;
323 step = _step_for_range (range);
324 pixel_step = step * pixel_size / range;
326 /* We expand the range by the ratio of the pixel step to the floor
329 new_range = range * pixel_step / floor (pixel_step);
331 /* And spread the increase out on either side of the range. */
332 *axis_min -= (new_range - range) / 2.0;
333 *axis_max += (new_range - range) / 2.0;
336 /* Setup a transformation in acre->cr such that data values plotted
337 * will appear where they should within the chart.
340 _set_transform_to_data_space (acre_t *acre)
342 cairo_t *cr = acre->cr;
346 acre->chart.y + acre->chart.height);
348 acre->chart.width / (acre->x_axis.max - acre->x_axis.min),
349 - acre->chart.height /(acre->y_axis.max - acre->y_axis.min));
350 cairo_translate (cr, -acre->x_axis.min, -acre->y_axis.min);
354 _compute_axis_ranges (acre_t *acre)
358 double x_adjust, y_adjust;
359 cairo_t *cr = acre->cr;
361 /* First, simply find the extrema of the data. */
362 for (d = 0; d < acre->num_data; d++) {
363 data = acre->data[d];
364 for (i = 0; i < data->num_points; i++) {
365 if (data->points[i].x < acre->x_axis.min)
366 acre->x_axis.min = data->points[i].x;
367 if (data->points[i].x > acre->x_axis.max)
368 acre->x_axis.max = data->points[i].x;
370 if (data->points[i].y < acre->y_axis.min)
371 acre->y_axis.min = data->points[i].y;
372 if (data->points[i].y > acre->y_axis.max)
373 acre->y_axis.max = data->points[i].y;
377 /* Next, increase the axis ranges just enough so that the step
378 * sizes for the ticks will be integers.
380 _expand_range_for_width (&acre->x_axis.min,
384 _expand_range_for_width (&acre->y_axis.min,
388 /* Finally, we also translate the axis ranges slightly so that the
389 * ticks land on half-integer device-pixel positions.
393 _set_transform_to_data_space (acre);
397 cairo_user_to_device (cr, &x_adjust, &y_adjust);
398 x_adjust = (round (x_adjust + 0.5) - 0.5) - x_adjust;
399 y_adjust = (round (y_adjust + 0.5) - 0.5) - y_adjust;
400 cairo_device_to_user_distance (cr, &x_adjust, &y_adjust);
402 acre->x_axis.min -= x_adjust;
403 acre->x_axis.max -= x_adjust;
405 acre->y_axis.min -= y_adjust;
406 acre->y_axis.max -= y_adjust;
412 _draw_data (acre_t *acre)
414 cairo_t *cr = acre->cr;
420 cairo_set_source_rgb (cr, 0, 0, 0);
422 _set_transform_to_data_space (acre);
424 for (d = 0; d < acre->num_data; d++) {
425 data = acre->data[d];
427 for (i = 0; i < data->num_points; i++) {
434 cairo_identity_matrix (cr);
435 cairo_set_line_width (cr, 1.0);
445 _draw_frame_and_ticks (acre_t *acre)
447 cairo_t *cr = acre->cr;
452 cairo_set_source_rgb (cr, 0, 0, 0); /* black */
454 /* First the ticks within data space. */
457 _set_transform_to_data_space (acre);
459 step = _step_for_range (acre->x_axis.max -acre->x_axis.min);
460 x = (floor (acre->x_axis.min / step) + 1) * step;
461 while (x <= acre->x_axis.max) {
462 cairo_move_to (cr, x, acre->y_axis.min);
468 cairo_identity_matrix (cr);
469 cairo_rel_line_to (cr, 0, 0.5);
470 cairo_rel_line_to (cr, 0, -ACRE_TICK_SIZE-0.5);
471 cairo_set_line_width (cr, 1.0);
482 asprintf (&label, "%g", x);
483 layout = _create_layout (acre, label);
485 cairo_move_to (cr, x, acre->y_axis.min);
486 cairo_identity_matrix (cr);
487 pango_layout_get_pixel_size (layout, &width, &height);
488 cairo_rel_move_to (cr, -width / 2, 2);
489 _show_layout (cr, layout);
497 step = _step_for_range (acre->y_axis.max -acre->y_axis.min);
498 y = (floor (acre->y_axis.min / step) + 1) * step;
499 while (y <= acre->y_axis.max) {
500 cairo_move_to (cr, acre->x_axis.min, y);
506 cairo_identity_matrix (cr);
507 cairo_rel_line_to (cr, -0.5, 0);
508 cairo_rel_line_to (cr, ACRE_TICK_SIZE+0.5, 0);
509 cairo_set_line_width (cr, 1.0);
520 asprintf (&label, "%g", y);
521 layout = _create_layout (acre, label);
523 cairo_move_to (cr, acre->x_axis.min, y);
524 cairo_identity_matrix (cr);
525 pango_layout_get_pixel_size (layout, &width, &height);
526 cairo_rel_move_to (cr, -width-2, -height/2);
527 _show_layout (cr, layout);
537 /* Then the frame drawn in pixel space. */
539 acre->chart.x - 0.5, acre->chart.y - 0.5,
540 acre->chart.width + 1.0, acre->chart.height + 1.0);
541 cairo_set_line_width (cr, 1.0);
547 /* Draw the plot to the given cairo context within a user-space
548 * rectangle from (0, 0) to (width, height). This size includes all
549 * space for extra-plot elements (such as the title, the axis labels,
553 acre_draw (acre_t *acre, cairo_t *cr, int width, int height)
557 acre->height = height;
561 cairo_set_source_rgb (cr, 1, 1, 1);
565 _draw_title_and_labels (acre);
567 _compute_axis_ranges (acre);
571 _draw_frame_and_ticks (acre);
574 /* Create a new dataset---a collection of (x, y) datapoints. A single
575 * plot can contain multiple datasets, (see acre_add_data). */
577 acre_data_create (void)
581 data = xmalloc (sizeof (acre_data_t));
586 data->points_size = 0;
587 data->num_points = 0;
592 /* Destroy an acre dataset. Do not call this function if the dataset
593 * has been added to an acre_t plot with acre_add_data. */
595 acre_data_destroy (acre_data_t *data)
602 /* Set the label for this dataset (to appear in the plot's key). */
604 acre_data_set_name (acre_data_t *data, const char *name)
608 data->name = strdup (name);
611 /* Add a datapoint to the given dataset. */
613 acre_data_add_point_2d (acre_data_t *data, double x, double y)
615 if (data->num_points >= data->points_size) {
616 data->points_size *= 2;
617 if (data->points_size == 0)
618 data->points_size = 16;
619 data->points = xrealloc_ab (data->points,
621 sizeof (acre_data_point_2d_t));
624 data->points[data->num_points].x = x;
625 data->points[data->num_points].y = y;