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 _draw_title_and_labels (acre_t *acre)
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;
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);
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);
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);
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);
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);
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). */
205 acre->chart.width = acre->width;
206 acre->chart.height = acre->height;
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);
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);
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;
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);
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)
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;
237 cairo_set_source_rgb (cr, 0, 0, 0);
239 cairo_move_to (cr, acre->chart.x, ACRE_PAD);
240 pango_cairo_show_layout (cr, title_layout);
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);
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);
259 /* For a given axis range, compute a step size (in data space) to
260 * generate a suitable number of ticks (5 or so). */
262 _step_for_range (double range)
264 double step, scale_factor;
266 /* We want roughly 5 major ticks for the chart. */
269 /* Normalize the step so we can easily snap it to a desirable
271 scale_factor = pow (10.0, floor (log10 (step)));
272 step /= scale_factor;
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)
283 if (step < 7.071067811865475)
289 /* Un-normalize and we now have the data value that we want to
291 return step * scale_factor;
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
299 _expand_range_for_width (double *axis_min, double *axis_max, int pixel_size)
301 double range, new_range, step, pixel_step;
303 range = *axis_max - *axis_min;
305 step = _step_for_range (range);
306 pixel_step = step * pixel_size / range;
308 /* We expand the range by the ratio of the pixel step to the floor
311 new_range = range * pixel_step / floor (pixel_step);
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;
318 /* Setup a transformation in acre->cr such that data values plotted
319 * will appear where they should within the chart.
322 _set_transform_to_data_space (acre_t *acre)
324 cairo_t *cr = acre->cr;
328 acre->chart.y + acre->chart.height);
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);
336 _compute_axis_ranges (acre_t *acre)
340 double x_adjust, y_adjust;
341 cairo_t *cr = acre->cr;
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;
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;
359 /* Next, increase the axis ranges just enough so that the step
360 * sizes for the ticks will be integers.
362 _expand_range_for_width (&acre->x_axis.min,
366 _expand_range_for_width (&acre->y_axis.min,
370 /* Finally, we also translate the axis ranges slightly so that the
371 * ticks land on half-integer device-pixel positions.
375 _set_transform_to_data_space (acre);
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);
384 acre->x_axis.min -= x_adjust;
385 acre->x_axis.max -= x_adjust;
387 acre->y_axis.min -= y_adjust;
388 acre->y_axis.max -= y_adjust;
394 _draw_data (acre_t *acre)
396 cairo_t *cr = acre->cr;
402 cairo_set_source_rgb (cr, 0, 0, 0);
404 _set_transform_to_data_space (acre);
406 for (d = 0; d < acre->num_data; d++) {
407 data = acre->data[d];
409 for (i = 0; i < data->num_points; i++) {
416 cairo_identity_matrix (cr);
417 cairo_set_line_width (cr, 1.0);
427 _draw_frame_and_ticks (acre_t *acre)
429 cairo_t *cr = acre->cr;
434 cairo_set_source_rgb (cr, 0, 0, 0); /* black */
436 /* First the ticks within data space. */
439 _set_transform_to_data_space (acre);
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);
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);
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);
469 pango_layout_set_alignment (layout,
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);
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);
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);
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);
511 pango_layout_set_alignment (layout,
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);
527 /* Then the frame drawn in pixel space. */
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);
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,
543 acre_draw (acre_t *acre, cairo_t *cr, int width, int height)
547 acre->height = height;
551 cairo_set_source_rgb (cr, 1, 1, 1);
555 _draw_title_and_labels (acre);
557 _compute_axis_ranges (acre);
561 _draw_frame_and_ticks (acre);
564 /* Create a new dataset---a collection of (x, y) datapoints. A single
565 * plot can contain multiple datasets, (see acre_add_data). */
567 acre_data_create (void)
571 data = xmalloc (sizeof (acre_data_t));
576 data->points_size = 0;
577 data->num_points = 0;
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. */
585 acre_data_destroy (acre_data_t *data)
592 /* Set the label for this dataset (to appear in the plot's key). */
594 acre_data_set_name (acre_data_t *data, const char *name)
598 data->name = strdup (name);
601 /* Add a datapoint to the given dataset. */
603 acre_data_add_point_2d (acre_data_t *data, double x, double y)
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,
611 sizeof (acre_data_point_2d_t));
614 data->points[data->num_points].x = x;
615 data->points[data->num_points].y = y;