]> git.cworth.org Git - acre/blobdiff - acre.c
Eliminate some code duplication in the tick adjustment phase.
[acre] / acre.c
diff --git a/acre.c b/acre.c
index 6e75ab7ee8403cb537c682e069bdd0c26749850e..6149cd38515b849786643811547351970393bb5a 100644 (file)
--- a/acre.c
+++ b/acre.c
@@ -17,6 +17,9 @@
  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  */
 
+#define _ISOC99_SOURCE /* for round() */
+#define _XOPEN_SOURCE 500
+
 #include "acre.h"
 #include "xmalloc.h"
 
@@ -36,10 +39,16 @@ struct _acre_data {
     unsigned int num_points;
 };
 
+typedef struct _acre_axis {
+    char *label;
+    double min;
+    double max;
+} acre_axis_t;
+
 struct _acre {
     char *title;
-    char *x_axis_label;
-    char *y_axis_label;
+    acre_axis_t x_axis;
+    acre_axis_t y_axis;
 
     acre_data_t **data;
     unsigned int data_size;
@@ -65,8 +74,14 @@ acre_create (void)
     acre = xmalloc (sizeof (acre_t));
 
     acre->title = NULL;
-    acre->x_axis_label = NULL;
-    acre->y_axis_label = NULL;
+
+    acre->x_axis.label = NULL;
+    acre->x_axis.min = 0.0;
+    acre->x_axis.max = 0.0;
+
+    acre->y_axis.label = NULL;
+    acre->y_axis.min = 0.0;
+    acre->y_axis.max = 0.0;
 
     acre->data = NULL;
     acre->data_size = 0;
@@ -82,8 +97,8 @@ acre_destroy (acre_t *acre)
     unsigned int i;
 
     free (acre->title);
-    free (acre->x_axis_label);
-    free (acre->y_axis_label);
+    free (acre->x_axis.label);
+    free (acre->y_axis.label);
 
     for (i = 0; i < acre->num_data; i++)
        acre_data_destroy (acre->data[i]);
@@ -104,17 +119,17 @@ acre_set_title (acre_t *acre, const char *title)
 void
 acre_set_x_axis_label (acre_t *acre, const char *label)
 {
-    free (acre->x_axis_label);
+    free (acre->x_axis.label);
 
-    acre->x_axis_label = strdup (label);
+    acre->x_axis.label = strdup (label);
 }
 
 void
 acre_set_y_axis_label (acre_t *acre, const char *label)
 {
-    free (acre->y_axis_label);
+    free (acre->y_axis.label);
 
-    acre->y_axis_label = strdup (label);
+    acre->y_axis.label = strdup (label);
 }
 
 /* Add a dataset to the plot. The plot assumes ownership of the
@@ -139,7 +154,7 @@ acre_add_data (acre_t *acre, acre_data_t *data)
 #define ACRE_FONT_SIZE 12
 #define ACRE_TITLE_FONT_SIZE 32
 #define ACRE_PAD (ACRE_FONT_SIZE)
-#define ACRE_TICK_SIZE 4
+#define ACRE_TICK_SIZE 6
 
 static void
 _draw_title_and_labels (acre_t *acre)
@@ -152,6 +167,8 @@ _draw_title_and_labels (acre_t *acre)
     int y_axis_width, y_axis_height;
     PangoRectangle new_chart;
 
+    cairo_save (cr);
+
     acre_font = pango_font_description_new ();
     pango_font_description_set_family (acre_font, ACRE_FONT_FAMILY);
     pango_font_description_set_absolute_size (acre_font,
@@ -169,12 +186,12 @@ _draw_title_and_labels (acre_t *acre)
 
     x_axis_layout = pango_cairo_create_layout (cr);
     pango_layout_set_font_description (x_axis_layout, acre_font);
-    pango_layout_set_text (x_axis_layout, acre->x_axis_label, -1);
+    pango_layout_set_text (x_axis_layout, acre->x_axis.label, -1);
     pango_layout_set_alignment (x_axis_layout, PANGO_ALIGN_CENTER);
 
     y_axis_layout = pango_cairo_create_layout (cr);
     pango_layout_set_font_description (y_axis_layout, acre_font);
-    pango_layout_set_text (y_axis_layout, acre->y_axis_label, -1);
+    pango_layout_set_text (y_axis_layout, acre->y_axis.label, -1);
     pango_layout_set_alignment (y_axis_layout, PANGO_ALIGN_CENTER);
 
     /* Iterate with the layout of the title and axis labels until they
@@ -233,6 +250,234 @@ _draw_title_and_labels (acre_t *acre)
                   acre->chart.y + acre->chart.height +
                   ACRE_FONT_SIZE + ACRE_PAD);
     pango_cairo_show_layout (cr, x_axis_layout);
+
+    cairo_restore (cr);
+}
+
+/* For a given axis range, compute a step size (in data space) to
+ * generate a suitable number of ticks (5 or so). */
+static double
+_step_for_range (double range)
+{
+    double step, scale_factor;
+
+    /* We want roughly 5 major ticks for the chart. */
+    step = range / 5;
+
+    /* Normalize the step so we can easily snap it to a desirable
+     * value. */
+    scale_factor = pow (10.0, floor (log10 (step)));
+    step /= scale_factor;
+
+    /* We want increments of 1, 2.5, 5, or 10 (times some power of
+     * 10). The threshold values between these are computed
+     * logarithmically. */
+    if (step < 3.535533905932738) {
+       if (step < 1.58113883008419)
+           step = 1.0;
+       else
+           step = 2.5;
+    } else {
+       if (step < 7.071067811865475)
+           step = 5.0;
+       else
+           step = 10.0;
+    }
+
+    /* Un-normalize and we now have the data value that we want to
+     * step at. */
+    return step * scale_factor;
+}
+
+/* Given an axis range, we can compute a desired data-space step
+ * amount for the major ticks (see _step_for_range). To get
+ * nice-looking pixel-snapped ticks we want to expand the range
+ * slightly. */
+static void
+_expand_range_for_width (double *axis_min, double *axis_max, int pixel_size)
+{
+    double range, new_range, step, pixel_step;
+
+    range = *axis_max - *axis_min;
+
+    step = _step_for_range (range);
+    pixel_step = step * pixel_size / range;
+
+    /* We expand the range by the ratio of the pixel step to the floor
+     * of the pixel_step.
+     */
+    new_range = range * pixel_step / floor (pixel_step);
+
+    /* And spread the increase out on either side of the range. */
+    *axis_min -= (new_range - range) / 2.0;
+    *axis_max += (new_range - range) / 2.0;
+}
+
+/* Setup a transformation in acre->cr such that data values plotted
+ * will appear where they should within the chart.
+ */
+static void
+_set_transform_to_data_space (acre_t *acre)
+{
+    cairo_t *cr = acre->cr;
+
+    cairo_translate (cr,
+                    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);
+}
+
+static void
+_compute_axis_ranges (acre_t *acre)
+{
+    unsigned int d, i;
+    acre_data_t *data;
+    double x_adjust, y_adjust;
+    cairo_t *cr = acre->cr;
+
+    /* 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;
+       }
+    }
+
+    /* Next, 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,
+                            acre->chart.width);
+
+    _expand_range_for_width (&acre->y_axis.min,
+                            &acre->y_axis.max,
+                            acre->chart.height);
+
+    /* Finally, we also translate the axis ranges slightly so that the
+     * ticks land on half-integer device-pixel positions.
+     */
+    cairo_save (cr);
+    {
+       _set_transform_to_data_space (acre);
+
+       x_adjust = 0.0;
+       y_adjust = 0.0;
+       cairo_user_to_device (cr, &x_adjust, &y_adjust);
+       x_adjust = (round (x_adjust + 0.5) - 0.5) - x_adjust;
+       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->y_axis.min -= y_adjust;
+       acre->y_axis.max -= y_adjust;
+    }
+    cairo_restore (cr);
+}
+
+static void
+_draw_data (acre_t *acre)
+{
+    cairo_t *cr = acre->cr;
+    unsigned int d, i;
+    acre_data_t *data;
+
+    cairo_save (cr);
+
+    cairo_set_source_rgb (cr, 0, 0, 0);
+
+    _set_transform_to_data_space (acre);
+
+    for (d = 0; d < acre->num_data; d++) {
+       data = acre->data[d];
+       cairo_new_path (cr);
+       for (i = 0; i < data->num_points; i++) {
+           cairo_line_to (cr,
+                          data->points[i].x,
+                          data->points[i].y);
+       }
+       cairo_save (cr);
+       {
+           cairo_identity_matrix (cr);
+           cairo_set_line_width (cr, 1.0);
+           cairo_stroke (cr);
+       }
+       cairo_restore (cr);
+    }
+
+    cairo_restore (cr);
+}
+
+static void
+_draw_frame_and_ticks (acre_t *acre)
+{
+    cairo_t *cr = acre->cr;
+    double step, x, y;
+
+    cairo_save (cr);
+
+    cairo_set_source_rgb (cr, 0, 0, 0); /* black */
+
+    /* First the ticks within data space. */
+    cairo_save (cr);
+    {
+       _set_transform_to_data_space (acre);
+
+       step = _step_for_range (acre->x_axis.max -acre->x_axis.min);
+       x = (floor (acre->x_axis.min / step) + 1) * step;
+       while (x <= acre->x_axis.max) {
+           cairo_move_to (cr, x, acre->y_axis.min);
+           cairo_save (cr);
+           {
+               cairo_identity_matrix (cr);
+               cairo_rel_line_to (cr, 0, 0.5);
+               cairo_rel_line_to (cr, 0, -ACRE_TICK_SIZE-0.5);
+               cairo_set_line_width (cr, 1.0);
+               cairo_stroke (cr);
+           }
+           cairo_restore (cr);
+           x += step;
+       }
+
+       step = _step_for_range (acre->y_axis.max -acre->y_axis.min);
+       y = (floor (acre->y_axis.min / step) + 1) * step;
+       while (y <= acre->y_axis.max) {
+           cairo_move_to (cr, acre->x_axis.min, y);
+           cairo_save (cr);
+           {
+               cairo_identity_matrix (cr);
+               cairo_rel_line_to (cr, -0.5, 0);
+               cairo_rel_line_to (cr, ACRE_TICK_SIZE+0.5, 0);
+               cairo_set_line_width (cr, 1.0);
+               cairo_stroke (cr);
+           }
+           cairo_restore (cr);
+           y += step;
+       }
+    }
+    cairo_restore (cr);
+
+    /* Then the frame drawn in pixel space. */
+    cairo_rectangle (cr,
+                    acre->chart.x - 0.5, acre->chart.y - 0.5,
+                    acre->chart.width + 1.0, acre->chart.height + 1.0);
+    cairo_set_line_width (cr, 1.0);
+    cairo_stroke (cr);
+
+    cairo_restore (cr);
 }
 
 /* Draw the plot to the given cairo context within a user-space
@@ -255,11 +500,11 @@ acre_draw (acre_t *acre, cairo_t *cr, int width, int height)
 
     _draw_title_and_labels (acre);
 
-    cairo_rectangle (cr,
-                    acre->chart.x - 0.5, acre->chart.y - 0.5,
-                    acre->chart.width + 1.0, acre->chart.height + 1.0);
-    cairo_set_line_width (cr, 1.0);
-    cairo_stroke (cr);
+    _compute_axis_ranges (acre);
+
+    _draw_data (acre);
+
+    _draw_frame_and_ticks (acre);
 }
 
 /* Create a new dataset---a collection of (x, y) datapoints. A single