From 4a5a4c8bad866e53298a1cdf47104c2711c139df Mon Sep 17 00:00:00 2001 From: Carl Worth Date: Tue, 27 Jan 2009 08:12:39 -0800 Subject: [PATCH] Add minor ticks 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 | 46 ++++++++++++++++++++++++++++++---------------- 1 file changed, 30 insertions(+), 16 deletions(-) diff --git a/acre.c b/acre.c index a01dbe5..6cd3a0b 100644 --- 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); -- 2.43.0