#include <string.h>
#include <stdarg.h>
+#include <stdbool.h>
#include <math.h>
#include <lcms.h>
} acre_data_point_2d_t;
struct _acre_data {
+ /* The name of this data set. */
char *name;
+ /* Minimum and mximum extents of data. */
+ acre_data_point_2d_t min;
+ acre_data_point_2d_t max;
+
+ /* The data itself. */
acre_data_point_2d_t *points;
unsigned int points_size;
unsigned int num_points;
+
+ /* The names of data points (if any).
+ *
+ * This array is indexed with the same index as the 'points' array
+ * to provide names for points. It is legal for this array to be
+ * NULL or smaller than 'points', (in which case, the
+ * corresponding points simply have no names).
+ */
+ char **names;
+ unsigned int names_size;
+ unsigned int num_names;
};
typedef struct _acre_axis {
char *label;
- double min;
- double max;
+
+ /* Range of data */
+ double data_min;
+ double data_max;
+
+ /* Range of data to be viewed. */
+ double view_min;
+ double view_max;
+
+ /* Has the view range been set? */
+ bool view_range_set;
} acre_axis_t;
typedef struct _acre_color {
PangoRectangle chart;
};
+static void
+_find_x_range_given_y_range (acre_t *acre,
+ double *x_min, double *x_max,
+ double y_min, double y_max);
+
+static void
+_find_y_range_given_x_range (acre_t *acre,
+ double *y_min, double *y_max,
+ double x_min, double x_max);
+
/* Create a new, empty plot. */
acre_t *
acre_create (void)
acre->title = NULL;
acre->x_axis.label = NULL;
- acre->x_axis.min = 0.0;
- acre->x_axis.max = 0.0;
+ acre->x_axis.data_min = 0.0;
+ acre->x_axis.data_max = 0.0;
+ acre->x_axis.view_min = 0.0;
+ acre->x_axis.view_max = 0.0;
+ acre->x_axis.view_range_set = false;
acre->y_axis.label = NULL;
- acre->y_axis.min = 0.0;
- acre->y_axis.max = 0.0;
+ acre->y_axis.data_min = 0.0;
+ acre->y_axis.data_max = 0.0;
+ acre->y_axis.view_min = 0.0;
+ acre->y_axis.view_max = 0.0;
+ acre->y_axis.view_range_set = false;
acre->data = NULL;
acre->data_size = 0;
acre->font = NULL;
acre->colors = NULL;
acre->num_colors = 0;
+ acre->colors_size = 0;
acre->width = 0;
acre->height = 0;
acre->y_axis.label = strdup (label);
}
+void
+acre_get_x_axis_data_range (acre_t *acre, double *x_min, double *x_max)
+{
+ if (x_min)
+ *x_min = acre->x_axis.data_min;
+
+ if (x_max)
+ *x_max = acre->x_axis.data_max;
+}
+
+void
+acre_get_x_axis_range (acre_t *acre, double *x_min, double *x_max)
+{
+ /* If an X range has been set, return that. */
+ if (acre->x_axis.view_range_set) {
+ if (x_min)
+ *x_min = acre->x_axis.view_min;
+
+ if (x_max)
+ *x_max = acre->x_axis.view_max;
+
+ return;
+ }
+
+ /* Otherwise, if a Y range has been set, use that to compute X. */
+ if (acre->y_axis.view_range_set) {
+ _find_x_range_given_y_range (acre, x_min, x_max,
+ acre->y_axis.view_min,
+ acre->y_axis.view_max);
+
+ return;
+ }
+
+ /* Neither view range set. Return full, data-based X range. */
+ acre_get_x_axis_data_range (acre, x_min, x_max);
+}
+
+void
+acre_set_x_axis_range (acre_t *acre, double x_min, double x_max)
+{
+ acre->x_axis.view_min = x_min;
+ acre->x_axis.view_max = x_max;
+
+ acre->x_axis.view_range_set = true;
+}
+
+void
+acre_set_x_axis_range_auto (acre_t *acre)
+{
+ acre->x_axis.view_range_set = false;
+}
+
+void
+acre_get_y_axis_data_range (acre_t *acre, double *y_min, double *y_max)
+{
+ if (y_min)
+ *y_min = acre->y_axis.data_min;
+
+ if (y_max)
+ *y_max = acre->y_axis.data_max;
+}
+
+void
+acre_get_y_axis_range (acre_t *acre, double *y_min, double *y_max)
+{
+ /* If a Y range has been set, return that. */
+ if (acre->y_axis.view_range_set) {
+ if (y_min)
+ *y_min = acre->y_axis.view_min;
+
+ if (y_max)
+ *y_max = acre->y_axis.view_max;
+
+ return;
+ }
+
+ /* Otherwise, if an X range has been set, use that to compute Y. */
+ if (acre->x_axis.view_range_set) {
+ _find_y_range_given_x_range (acre, y_min, y_max,
+ acre->x_axis.view_min,
+ acre->x_axis.view_max);
+
+ return;
+ }
+
+ /* Neither view range set. Return full data-based Y range. */
+ acre_get_y_axis_data_range (acre, y_min, y_max);
+}
+
+void
+acre_set_y_axis_range (acre_t *acre, double y_min, double y_max)
+{
+ acre->y_axis.view_min = y_min;
+ acre->y_axis.view_max = y_max;
+
+ acre->y_axis.view_range_set = true;
+}
+
+void
+acre_set_y_axis_range_auto (acre_t *acre)
+{
+ acre->y_axis.view_range_set = false;
+}
+
/* Add a dataset to the plot. The plot assumes ownership of the
* dataset so it is not necessary to call acre_data_destroy on it. */
void
}
acre->data[acre->num_data] = data;
+
+ if (acre->num_data == 0) {
+ acre->x_axis.data_min = data->min.x;
+ acre->y_axis.data_min = data->min.y;
+
+ acre->x_axis.data_max = data->max.x;
+ acre->y_axis.data_max = data->max.y;
+ } else {
+ if (data->min.x < acre->x_axis.data_min)
+ acre->x_axis.data_min = data->min.x;
+ if (data->min.y < acre->y_axis.data_min)
+ acre->y_axis.data_min = data->min.y;
+
+ if (data->max.x > acre->x_axis.data_max)
+ acre->x_axis.data_max = data->max.x;
+ if (data->max.y > acre->y_axis.data_max)
+ acre->y_axis.data_max = data->max.y;
+ }
+
acre->num_data++;
}
{
PangoLayout *layout;
+ if (text == NULL)
+ text = "";
+
layout = pango_cairo_create_layout (acre->cr);
pango_layout_set_font_description (layout, acre->font);
pango_layout_set_text (layout, text, -1);
title_layout = _create_layout (acre, acre->title);
pango_layout_set_font_description (title_layout, title_font);
+ pango_font_description_free (title_font);
x_axis_layout = _create_layout (acre, acre->x_axis.label);
y_axis_layout = _create_layout (acre, acre->y_axis.label);
min_y = _create_layout_printf (acre, "%g",
- round (acre->y_axis.min));
+ round (acre->y_axis.view_min));
max_y = _create_layout_printf (acre, "%g",
- round (acre->y_axis.max));
+ round (acre->y_axis.view_max));
pango_layout_get_pixel_size (min_y, &min_y_width, NULL);
pango_layout_get_pixel_size (max_y, &max_y_width, NULL);
acre->chart.x,
acre->chart.y + acre->chart.height);
cairo_scale (cr,
- acre->chart.width / (acre->x_axis.max - acre->x_axis.min),
- - acre->chart.height /(acre->y_axis.max - acre->y_axis.min));
- cairo_translate (cr, -acre->x_axis.min, -acre->y_axis.min);
+ acre->chart.width / (acre->x_axis.view_max - acre->x_axis.view_min),
+ - acre->chart.height /(acre->y_axis.view_max - acre->y_axis.view_min));
+ cairo_translate (cr, -acre->x_axis.view_min, -acre->y_axis.view_min);
}
static void
-_compute_axis_ranges (acre_t *acre)
+_find_x_range_given_y_range (acre_t *acre,
+ double *x_min, double *x_max,
+ double y_min, double y_max)
{
- unsigned int d, i;
acre_data_t *data;
- double x_adjust, y_adjust;
- cairo_t *cr = acre->cr;
+ unsigned d, i;
+ bool first;
- acre->x_axis.min = acre->data[0]->points[0].x;
- acre->x_axis.max = acre->data[0]->points[0].x;
- acre->y_axis.min = acre->data[0]->points[0].y;
- acre->y_axis.min = acre->data[0]->points[0].y;
+ first = true;
- /* First, simply find the extrema of the data. */
for (d = 0; d < acre->num_data; d++) {
data = acre->data[d];
for (i = 0; i < data->num_points; i++) {
- if (data->points[i].x < acre->x_axis.min)
- acre->x_axis.min = data->points[i].x;
- if (data->points[i].x > acre->x_axis.max)
- acre->x_axis.max = data->points[i].x;
-
- if (data->points[i].y < acre->y_axis.min)
- acre->y_axis.min = data->points[i].y;
- if (data->points[i].y > acre->y_axis.max)
- acre->y_axis.max = data->points[i].y;
+ if (data->points[i].y >= y_min &&
+ data->points[i].y <= y_max)
+ {
+ if (first) {
+ *x_min = data->points[i].x;
+ *x_max = data->points[i].x;
+ first = false;
+ } else {
+ if (data->points[i].x < *x_min)
+ *x_min = data->points[i].x;
+ if (data->points[i].x > *x_max)
+ *x_max = data->points[i].x;
+ }
+ }
}
}
- /* Next, we want to ensure that the data never collides with the
- * ticks. So we expand each axis on its minimum side as needed. */
- cairo_save (cr);
- {
- double x, y;
+ /* If nothing is visible, punt to full X data range. */
+ if (first) {
+ *x_min = acre->x_axis.data_min;
+ *x_max = acre->x_axis.data_max;
+ }
+}
- _set_transform_to_data_space (acre);
+static void
+_find_y_range_given_x_range (acre_t *acre,
+ double *y_min, double *y_max,
+ double x_min, double x_max)
+{
+ acre_data_t *data;
+ unsigned d, i;
+ bool first;
- x = ACRE_TICK_MAJOR_SIZE + 2.0;
- y = ACRE_TICK_MAJOR_SIZE + 2.0;
- cairo_device_to_user_distance (cr, &x, &y);
+ first = true;
+
+ for (d = 0; d < acre->num_data; d++) {
+ data = acre->data[d];
+ for (i = 0; i < data->num_points; i++) {
+ if (data->points[i].x >= x_min &&
+ data->points[i].x <= x_max)
+ {
+ if (first) {
+ *y_min = data->points[i].y;
+ *y_max = data->points[i].y;
+ first = false;
+ } else {
+ if (data->points[i].y < *y_min)
+ *y_min = data->points[i].y;
+ if (data->points[i].y > *y_max)
+ *y_max = data->points[i].y;
+ }
+ }
+ }
+ }
- acre->x_axis.min -= x;
- acre->y_axis.min += y;
+ /* If nothing is visible, punt to full Y data range. */
+ if (first) {
+ *y_min = acre->y_axis.data_min;
+ *y_max = acre->y_axis.data_max;
+ }
+}
+
+static void
+_compute_axis_ranges (acre_t *acre)
+{
+ double x_adjust, y_adjust;
+ cairo_t *cr = acre->cr;
+
+ /* If neither view range is set, set both to data ranges. */
+ if (! acre->x_axis.view_range_set && ! acre->y_axis.view_range_set)
+ {
+ acre->x_axis.view_min = acre->x_axis.data_min;
+ acre->x_axis.view_max = acre->x_axis.data_max;
+
+ acre->y_axis.view_min = acre->y_axis.data_min;
+ acre->y_axis.view_max = acre->y_axis.data_max;
+ } else {
+ /* Otherwise, auto-fit unset range based on data. */
+ if (acre->x_axis.view_range_set && ! acre->y_axis.view_range_set) {
+ _find_y_range_given_x_range (acre,
+ &acre->y_axis.view_min,
+ &acre->y_axis.view_max,
+ acre->x_axis.view_min,
+ acre->x_axis.view_max);
+ }
+ else if (acre->y_axis.view_range_set && ! acre->x_axis.view_range_set) {
+ _find_x_range_given_y_range (acre,
+ &acre->x_axis.view_min,
+ &acre->x_axis.view_max,
+ acre->y_axis.view_min,
+ acre->y_axis.view_max);
+ }
}
- cairo_restore (cr);
/* Then, increase the axis ranges just enough so that the step
* sizes for the ticks will be integers.
*/
- _expand_range_for_width (&acre->x_axis.min,
- &acre->x_axis.max,
+ _expand_range_for_width (&acre->x_axis.view_min,
+ &acre->x_axis.view_max,
acre->chart.width);
- _expand_range_for_width (&acre->y_axis.min,
- &acre->y_axis.max,
+ _expand_range_for_width (&acre->y_axis.view_min,
+ &acre->y_axis.view_max,
acre->chart.height);
/* Finally, we also translate the axis ranges slightly so that the
y_adjust = (round (y_adjust + 0.5) - 0.5) - y_adjust;
cairo_device_to_user_distance (cr, &x_adjust, &y_adjust);
- acre->x_axis.min -= x_adjust;
- acre->x_axis.max -= x_adjust;
+ acre->x_axis.view_min -= x_adjust;
+ acre->x_axis.view_max -= x_adjust;
- acre->y_axis.min -= y_adjust;
- acre->y_axis.max -= y_adjust;
+ acre->y_axis.view_min -= y_adjust;
+ acre->y_axis.view_max -= y_adjust;
}
cairo_restore (cr);
}
-static void
-_acre_color_from_hsv (acre_color_t *color,
- double hue,
- double saturation,
- double value)
-{
- double f, p, q, t;
- int hmod6;
-
- hmod6 = (int) floor (hue / 60) % 6;
- f = hue / 60 - floor (hue / 60);
- p = value * (1 - saturation);
- q = value * (1 - f * saturation);
- t = value * (1 - (1 - f) * saturation);
-
- switch (hmod6) {
- case 0:
- color->red = value;
- color->green = t;
- color->blue = p;
- break;
- case 1:
- color->red = q;
- color->green = value;
- color->blue = p;
- break;
- case 2:
- color->red = p;
- color->green = value;
- color->blue = t;
- break;
- case 3:
- color->red = p;
- color->green = q;
- color->blue = value;
- break;
- case 4:
- color->red = t;
- color->green = p;
- color->blue = value;
- break;
- case 5:
- color->red = value;
- color->green = p;
- color->blue = q;
- break;
- }
-}
-
static void
_choose_colors (acre_t *acre)
{
cairo_save (cr);
+ cairo_rectangle (cr,
+ acre->chart.x, acre->chart.y,
+ acre->chart.width, acre->chart.height);
+ cairo_clip (cr);
+
cairo_set_source_rgb (cr, 0, 0, 0);
_set_transform_to_data_space (acre);
acre->colors[color].green,
acre->colors[color].blue);
data = acre->data[d];
+
cairo_new_path (cr);
+
for (i = 0; i < data->num_points; i++) {
cairo_line_to (cr,
data->points[i].x,
cairo_save (cr);
{
if (ticks == ACRE_TICKS_X)
- cairo_move_to (cr, t, acre->y_axis.min);
+ cairo_move_to (cr, t, acre->y_axis.view_min);
else
- cairo_move_to (cr, acre->x_axis.min, t);
+ cairo_move_to (cr, acre->x_axis.view_min, t);
cairo_identity_matrix (cr);
layout = _create_layout_printf (acre, "%g", t);
if (ticks == ACRE_TICKS_X)
- cairo_move_to (cr, t, acre->y_axis.min);
+ cairo_move_to (cr, t, acre->y_axis.view_min);
else
- cairo_move_to (cr, acre->x_axis.min, t);
+ cairo_move_to (cr, acre->x_axis.view_min, t);
cairo_identity_matrix (cr);
pango_layout_get_pixel_size (layout, &width, &height);
cairo_set_source_rgb (cr, 0, 0, 0); /* black */
/* ticks */
- _draw_ticks (acre, acre->x_axis.min, acre->x_axis.max, ACRE_TICKS_X);
- _draw_ticks (acre, acre->y_axis.min, acre->y_axis.max, ACRE_TICKS_Y);
+ _draw_ticks (acre, acre->x_axis.view_min, acre->x_axis.view_max, ACRE_TICKS_X);
+ _draw_ticks (acre, acre->y_axis.view_min, acre->y_axis.view_max, ACRE_TICKS_Y);
/* frame */
cairo_rectangle (cr,
void
acre_data_destroy (acre_data_t *data)
{
+ unsigned i;
+
+ for (i = 0; i < data->num_names; i++) {
+ if (data->names[i])
+ free (data->names[i]);
+ }
+ free (data->names);
+
+ free (data->name);
+
free (data->points);
free (data);
data->points[data->num_points].x = x;
data->points[data->num_points].y = y;
+
+ if (data->num_points == 0) {
+ data->min.x = x;
+ data->min.y = y;
+
+ data->max.x = x;
+ data->max.y = y;
+ } else {
+ if (x < data->min.x)
+ data->min.x = x;
+ if (y < data->min.y)
+ data->min.y = y;
+
+ if (x > data->max.x)
+ data->max.x = x;
+ if (y > data->max.y)
+ data->max.y = y;
+ }
+
data->num_points++;
}
+
+/* Add a datapoint with a name to the given dataset. */
+void
+acre_data_add_point_2d_named (acre_data_t *data, double x, double y, const char *name)
+{
+ unsigned i;
+
+ acre_data_add_point_2d (data, x, y);
+
+ if (data->names_size < data->points_size) {
+ data->names_size = data->points_size;
+ data->names = xrealloc_ab (data->names,
+ data->names_size,
+ sizeof (char *));
+ }
+
+ /* Initialize any newly-created holes in the array to NULL. */
+ for (i = data->num_names; i < data->num_points - 1; i++)
+ data->names[i] = NULL;
+
+ data->num_names = data->num_points;
+
+ data->names[data->num_names - 1] = xstrdup (name);
+}