]> git.cworth.org Git - acre/commitdiff
Add minor ticks
authorCarl Worth <cworth@cworth.org>
Tue, 27 Jan 2009 16:12:39 +0000 (08:12 -0800)
committerCarl Worth <cworth@cworth.org>
Thu, 16 Apr 2009 19:05:23 +0000 (12:05 -0700)
For now, do 5 divisions of each major tick, regardless of whether the
major tick spacing is 1, 2.5, 5, or 10. Also, we don't yet fix the
axes to guarantee integer spacing of the minor ticks.

acre.c

diff --git a/acre.c b/acre.c
index a01dbe5c2f1275c0c4f77b049844c8220b2c69e8..6cd3a0b3c309fef4d49d5bffc05601e76f520e99 100644 (file)
--- a/acre.c
+++ b/acre.c
@@ -162,7 +162,8 @@ 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 6
+#define ACRE_TICK_MAJOR_SIZE 6
+#define ACRE_TICK_MINOR_SIZE 3
 #define ACRE_X_TICK_VALUE_PAD 2
 #define ACRE_Y_TICK_VALUE_PAD 4
 
@@ -510,49 +511,63 @@ _draw_ticks (acre_t *acre,
             acre_ticks_t ticks)
 {
     cairo_t *cr = acre->cr;
-    double i, step;
+    double t, step, sub_step;
+    int minor_divisions = 5;
 
     cairo_save (cr);
 
     _set_transform_to_data_space (acre);
 
     step = _step_for_range (axis_max - axis_min);
-    i = (floor (axis_min / step) + 1) * step;
+    sub_step = step / minor_divisions;
 
-    while (i <= axis_max) {
-       if (ticks == ACRE_TICKS_X)
-           cairo_move_to (cr, i, acre->y_axis.min);
+    for (t = (floor (axis_min / sub_step) + 1) * sub_step;
+        t <= axis_max;
+        t += sub_step)
+    {
+       int tick_size;
+       if (fabs((t / step) - (int) (t / step)) < 0.5 * (sub_step / step))
+           tick_size = ACRE_TICK_MAJOR_SIZE;
        else
-           cairo_move_to (cr, acre->x_axis.min, i);
+           tick_size = ACRE_TICK_MINOR_SIZE;
 
        /* tick */
        cairo_save (cr);
        {
+           if (ticks == ACRE_TICKS_X)
+               cairo_move_to (cr, t, acre->y_axis.min);
+           else
+               cairo_move_to (cr, acre->x_axis.min, t);
+
            cairo_identity_matrix (cr);
+
            if (ticks == ACRE_TICKS_X) {
                cairo_rel_line_to (cr, 0, 0.5);
-               cairo_rel_line_to (cr, 0, -ACRE_TICK_SIZE-0.5);
+               cairo_rel_line_to (cr, 0, -tick_size - 0.5);
            } else {
                cairo_rel_line_to (cr, -0.5, 0);
-               cairo_rel_line_to (cr, ACRE_TICK_SIZE+0.5, 0);
+               cairo_rel_line_to (cr, tick_size + 0.5, 0);
            }
+
            cairo_set_line_width (cr, 1.0);
            cairo_stroke (cr);
        }
        cairo_restore (cr);
 
        /* label */
-       cairo_save (cr);
+       if (tick_size == ACRE_TICK_MAJOR_SIZE)
        {
            PangoLayout *layout;
            int width, height;
 
-           layout = _create_layout_printf (acre, "%g", i);
+           cairo_save (cr);
+
+           layout = _create_layout_printf (acre, "%g", t);
 
            if (ticks == ACRE_TICKS_X)
-               cairo_move_to (cr, i, acre->y_axis.min);
+               cairo_move_to (cr, t, acre->y_axis.min);
            else
-               cairo_move_to (cr, acre->x_axis.min, i);
+               cairo_move_to (cr, acre->x_axis.min, t);
 
            cairo_identity_matrix (cr);
            pango_layout_get_pixel_size (layout, &width, &height);
@@ -564,10 +579,9 @@ _draw_ticks (acre_t *acre,
                                   -height/2);
 
            _show_layout (cr, layout);
-       }
-       cairo_restore (cr);
 
-       i += step;
+           cairo_restore (cr);
+       }
     }
 
     cairo_restore (cr);