#include <string.h>
#include <stdarg.h>
+#include <stdbool.h>
#include <math.h>
#include <lcms.h>
typedef struct _acre_axis {
char *label;
+
+ /* 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->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.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->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
cairo_translate (cr, -acre->x_axis.view_min, -acre->y_axis.view_min);
}
+static void
+_find_x_range_given_y_range (acre_t *acre,
+ double *x_min, double *x_max,
+ double y_min, double y_max)
+{
+ acre_data_t *data;
+ unsigned d, i;
+ bool first;
+
+ 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].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;
+ }
+ }
+ }
+ }
+
+ /* 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;
+ }
+}
+
+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;
+
+ 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;
+ }
+ }
+ }
+ }
+
+ /* 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;
- /* First, simply set view ranges to data ranges. */
- acre->x_axis.view_min = acre->x_axis.data_min;
- acre->x_axis.view_max = acre->x_axis.data_max;
+ /* 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;
+ 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);
+ }
+ }
/* 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);
+ 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,