]> git.cworth.org Git - acre/commitdiff
Add a new BARS_OR_LINE style for drawing data sets.
authorCarl Worth <cworth@cworth.org>
Sat, 9 Nov 2013 00:16:44 +0000 (16:16 -0800)
committerCarl Worth <cworth@cworth.org>
Sat, 9 Nov 2013 00:16:44 +0000 (16:16 -0800)
If there is sufficient room, this will draw the dataset as a bar
chart. But if the bars would run into each other, it will draw as a
line chart instead.

acre.c
acre.h

diff --git a/acre.c b/acre.c
index 4e00beeee9557e9111d8a21467e1827e93ebc6e7..9a9851c568a684d3623e6b29b8d9a0b77c32301c 100644 (file)
--- a/acre.c
+++ b/acre.c
@@ -846,6 +846,48 @@ _draw_data_line (acre_t *acre, acre_data_t *data)
     cairo_restore (cr);
 }
 
+#define BARS_WIDTH 0.8
+
+/* Draw the given dataset as bars if there is room for that.
+ *
+ * Or, if the bars would run into each other, use a line instead.
+ */
+static void
+_draw_data_bars_or_line (acre_t *acre, acre_data_t *data)
+{
+    unsigned i;
+    cairo_t *cr = acre->cr;
+    double ignored, width;
+
+    /* Check device-space width available for inter-bad padding. */
+    width = 1.0 - BARS_WIDTH;
+    ignored = 0.0;
+    cairo_user_to_device_distance (cr, &width, &ignored);
+
+    /* If padding is less than two pixels, draw with a line instead. */
+    if (width < 2.0) {
+       _draw_data_line (acre, data);
+
+       return;
+    }
+
+    cairo_save (cr);
+
+    cairo_new_path (cr);
+
+    for (i = 0; i < data->num_points; i++) {
+       cairo_rectangle (cr,
+                        data->points[i].x - BARS_WIDTH / 2.0, 0.0,
+                        BARS_WIDTH, data->points[i].y);
+    }
+
+    cairo_identity_matrix (cr);
+    cairo_set_line_width (cr, 1.0);
+    cairo_stroke (cr);
+
+    cairo_restore (cr);
+}
+
 #define TIMELINE_BAR_HEIGHT 0.6
 
 /* Draw the given dataset as a timeline. Each (X,Y) point (potentially
@@ -942,6 +984,9 @@ _draw_data (acre_t *acre)
        case ACRE_STYLE_LINE:
            _draw_data_line (acre, data);
            break;
+       case ACRE_STYLE_BARS_OR_LINE:
+           _draw_data_bars_or_line (acre, data);
+           break;
        case ACRE_STYLE_TIMELINE:
            /* Position the timeline bars top-down */
            _draw_data_timeline (acre, data, acre->num_data - 1 - i);
diff --git a/acre.h b/acre.h
index 9611da3d8d15a8b7c4cf00d4bfd07f564b5e574a..4cd58d4f4c4b179bd94e675d27de27d1ccc76d09 100644 (file)
--- a/acre.h
+++ b/acre.h
@@ -118,6 +118,9 @@ typedef enum
        /* A simple line graph connection each (X,Y) pair in order. */
        ACRE_STYLE_LINE,
 
+       /* Bars if there is room for them, a line otherwise. */
+       ACRE_STYLE_BARS_OR_LINE,
+
        /* A timeline.
         *
         * Each (X,Y) pair specifies the (start,stop) time of a