]> git.cworth.org Git - acre/commitdiff
Allow zooming in or out by setting ranges of data to be shown on either axis.
authorCarl Worth <cworth@cworth.org>
Wed, 6 Nov 2013 23:33:54 +0000 (15:33 -0800)
committerCarl Worth <cworth@cworth.org>
Thu, 7 Nov 2013 01:35:06 +0000 (17:35 -0800)
The second axis can also be explicitly set to a desired range, or it
can be allowed to be automatically set to fit the data.

acre-test.c
acre.c
acre.h

index 472ea98a37c9ac4aa0812b37ecc3e6675520a2d2..c0cc8836f83d03697b038df7918464fdb45fb4a8 100644 (file)
@@ -1,4 +1,5 @@
 #include "acre.h"
+#include <math.h>
 
 int
 main (void)
@@ -11,11 +12,10 @@ main (void)
     acre_t *acre;
     acre_data_t *data0, *data1, *data2;
 
-    surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 800, 600);
+    surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 1600, 1200);
     cr = cairo_create (surface);
 
     acre = acre_create ();
-    acre_set_title (acre, "Acre Test Plot");
     acre_set_x_axis_label (acre, "X axis");
     acre_set_y_axis_label (acre, "Y axis");
 
@@ -28,15 +28,58 @@ main (void)
     acre_data_set_name (data2, "Data 2");
 
     for (i = 0; i <= 100; i++) {
-       acre_data_add_point_2d (data0, i, 100 - (i/4.0)*(i/4.0));
-       acre_data_add_point_2d (data1, i,  50 - (i/3.5)*(i/3.5));
-       acre_data_add_point_2d (data2, i,   0 - (i/3.0)*(i/3.0));
+       acre_data_add_point_2d (data0, i,   0 - (i/3.0)*(i/3.0));
+    }
+
+    for (i = 0; i < 100; i++) {
+       double t = 1.0 - (i / 100.0);
+       acre_data_add_point_2d (data1, i, -1000 * (1.0 - t*t*t));
+    }
+
+    for (i = 0; i <= 1000; i++) {
+       double t, x, y;
+       t = i/10.0 - 50;
+       x = t + 50;
+       if (t == 0.0)
+           y = -200;
+       else
+           y = -1200 + 1000 * sin(t) / t;
+       acre_data_add_point_2d (data2, x, y);
     }
 
     acre_add_data (acre, data0);
     acre_add_data (acre, data1);
     acre_add_data (acre, data2);
 
+    /* Draw a full-data-range graph in upper-left. */
+    acre_set_title (acre, "All the data");
+
+    acre_draw (acre, cr, 800, 600);
+
+    /* Zoom in on X and draw at lower-left. */
+    cairo_translate (cr, 0, 600);
+
+    acre_set_title (acre, "Zoom X, auto-size Y");
+    acre_set_x_axis_range (acre, 40, 60);
+
+    acre_draw (acre, cr, 800, 600);
+
+    /* Zoom in on Y and draw at upper-right. */
+    cairo_translate (cr, 800, -600);
+
+    acre_set_title (acre, "Zoom Y, auto-size X");
+    acre_set_x_axis_range_auto (acre);
+    acre_set_y_axis_range (acre, -250, 0);
+
+    acre_draw (acre, cr, 800, 600);
+
+    /* Zoom out on X and Y lower-right. */
+    cairo_translate (cr, 0, 600);
+
+    acre_set_title (acre, "Zoom out X and Y");
+    acre_set_x_axis_range (acre, -50, 150);
+    acre_set_y_axis_range (acre, -1500, 500);
+
     acre_draw (acre, cr, 800, 600);
 
     acre_destroy (acre);
diff --git a/acre.c b/acre.c
index 92ee39beea6aa92e8e623d3c7ab0d9c36e4db63c..e3604dcff6a8de23ed264edc96bbeeaf374ee0fa 100644 (file)
--- a/acre.c
+++ b/acre.c
@@ -26,6 +26,7 @@
 
 #include <string.h>
 #include <stdarg.h>
+#include <stdbool.h>
 #include <math.h>
 
 #include <lcms.h>
@@ -48,10 +49,17 @@ struct _acre_data {
 
 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 {
@@ -84,6 +92,16 @@ struct _acre {
     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)
@@ -99,12 +117,14 @@ 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;
@@ -170,6 +190,110 @@ acre_set_y_axis_label (acre_t *acre, const char *label)
     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
@@ -467,18 +591,113 @@ _set_transform_to_data_space (acre_t *acre)
     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. */
@@ -629,6 +848,7 @@ _draw_data (acre_t *acre)
     cairo_t *cr = acre->cr;
     unsigned int d, i;
     acre_data_t *data;
+    bool pen_up;
 
     cairo_save (cr);
 
@@ -643,11 +863,29 @@ _draw_data (acre_t *acre)
                              acre->colors[color].green,
                              acre->colors[color].blue);
        data = acre->data[d];
+
+       pen_up = true;
        cairo_new_path (cr);
+
        for (i = 0; i < data->num_points; i++) {
-           cairo_line_to (cr,
-                          data->points[i].x,
-                          data->points[i].y);
+           if (data->points[i].x >= acre->x_axis.view_min &&
+               data->points[i].x <= acre->x_axis.view_max &&
+               data->points[i].y >= acre->y_axis.view_min &&
+               data->points[i].y <= acre->y_axis.view_max)
+           {
+               if (pen_up)
+                   cairo_move_to (cr,
+                                  data->points[i].x,
+                                  data->points[i].y);
+               else
+                   cairo_line_to (cr,
+                                  data->points[i].x,
+                                  data->points[i].y);
+
+               pen_up = false;
+           } else {
+               pen_up = true;
+           }
        }
        cairo_save (cr);
        {
diff --git a/acre.h b/acre.h
index 0c602c18547cfe919234a81d45b1b2f7a739937d..df23199cbc0ea6a4a7230cadc1b48fbf818a0d85 100644 (file)
--- a/acre.h
+++ b/acre.h
@@ -42,6 +42,52 @@ acre_set_x_axis_label (acre_t *acre, const char *label);
 void
 acre_set_y_axis_label (acre_t *acre, const char *label);
 
+/* Get the range of data values in the X dimension. */
+void
+acre_get_x_axis_data_range (acre_t *acre, double *x_min, double *x_max);
+
+/* Get the range of the currently viewable X axis. */
+void
+acre_get_x_axis_range (acre_t *acre, double *x_min, double *x_max);
+
+/* Set the range of the currently viewable X axis. */
+void
+acre_set_x_axis_range (acre_t *acre, double x_min, double x_max);
+
+/* Set the X axis viewable range to be sized automatically.
+ *
+ * If both X and Y axes are sized automatically, then the X range will
+ * be set to the full range of data in the X dimension.
+ *
+ * But if the Y axis has been set to a limited viewable range, (with
+ * acre_set_y_axis_range), then the X axis will be set to the
+ * corresponding X range of data that lies within that set Y range. */
+void
+acre_set_x_axis_range_auto (acre_t *acre);
+
+/* Get the range of data values in the Y dimnension. */
+void
+acre_get_y_axis_data_range (acre_t *acre, double *y_min, double *y_max);
+
+/* Get the range of the currently viewable Y axis. */
+void
+acre_get_y_axis_range (acre_t *acre, double *y_min, double *y_max);
+
+/* Set the range of the currently viewable Y axis. */
+void
+acre_set_y_axis_range (acre_t *acre, double y_min, double y_max);
+
+/* Set the Y axis viewable range to be sized automatically.
+ *
+ * If both X and Y axes are sized automatically, then the Y range will
+ * be set to the full range of data in the Y dimension.
+ *
+ * But if the X axis has been set to a limited viewable range, (with
+ * acre_set_x_axis_range), then the Y axis will be set to the
+ * corresponding Y range of data that lies within that set X range. */
+void
+acre_set_y_axis_range_auto (acre_t *acre);
+
 /* 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