]> git.cworth.org Git - acre/blobdiff - acre.c
Add another dataset style: ACRE_STYLE_BARS
[acre] / acre.c
diff --git a/acre.c b/acre.c
index 92ee39beea6aa92e8e623d3c7ab0d9c36e4db63c..9f6f26ebaab1f854cd8cd392c92d19fa64a92e6c 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>
@@ -36,22 +37,46 @@ typedef struct _acre_data_point_2d {
 } acre_data_point_2d_t;
 
 struct _acre_data {
+    /* The name of this data set. */
     char *name;
 
+    /* The style for rendering (line, bar, etc.) */
+    acre_style_t style;
+
+    /* Minimum and mximum extents of data. */
     acre_data_point_2d_t min;
     acre_data_point_2d_t max;
 
+    /* The data itself. */
     acre_data_point_2d_t *points;
     unsigned int points_size;
     unsigned int num_points;
+
+    /* The names of data points (if any).
+     *
+     * This array is indexed with the same index as the 'points' array
+     * to provide names for points. It is legal for this array to be
+     * NULL or smaller than 'points', (in which case, the
+     * corresponding points simply have no names).
+     */
+    char **names;
+    unsigned int names_size;
+    unsigned int num_names;
 };
 
 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 +109,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 +134,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;
@@ -114,6 +151,7 @@ acre_create (void)
     acre->font = NULL;
     acre->colors = NULL;
     acre->num_colors = 0;
+    acre->colors_size = 0;
 
     acre->width = 0;
     acre->height = 0;
@@ -170,6 +208,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
@@ -186,6 +328,20 @@ acre_add_data (acre_t *acre, acre_data_t *data)
 
     acre->data[acre->num_data] = data;
 
+    /* For timeline datasets, the X and Y ranges need to be
+     * adjusted. The desired X range is the min/max of the X and Y
+     * ranges, and the desired Y range has a size of 1.0 (centered
+     * around the dataset's index) */
+    if (data->style == ACRE_STYLE_TIMELINE) {
+       if (data->min.y < data->min.x)
+           data->min.x = data->min.y;
+       if (data->max.y > data->max.x)
+           data->max.x = data->max.y;
+
+       data->min.y = acre->num_data -0.5;
+       data->max.y = acre->num_data + 0.5;
+    }
+
     if (acre->num_data == 0) {
        acre->x_axis.data_min = data->min.x;
        acre->y_axis.data_min = data->min.y;
@@ -223,11 +379,19 @@ _create_layout (acre_t *acre, const char *text)
 {
     PangoLayout *layout;
 
+    if (text == NULL)
+           text = "";
+
+    cairo_save (acre->cr);
+    cairo_identity_matrix (acre->cr);
+
     layout = pango_cairo_create_layout (acre->cr);
     pango_layout_set_font_description (layout, acre->font);
     pango_layout_set_text (layout, text, -1);
     pango_layout_set_alignment (layout, PANGO_ALIGN_CENTER);
 
+    cairo_restore (acre->cr);
+
     return layout;
 }
 
@@ -308,6 +472,7 @@ _draw_title_and_labels (acre_t *acre)
 
     title_layout = _create_layout (acre, acre->title);
     pango_layout_set_font_description (title_layout, title_font);
+    pango_font_description_free (title_font);
 
     x_axis_layout = _create_layout (acre, acre->x_axis.label);
     y_axis_layout = _create_layout (acre, acre->y_axis.label);
@@ -468,34 +633,117 @@ _set_transform_to_data_space (acre_t *acre)
 }
 
 static void
-_compute_axis_ranges (acre_t *acre)
+_find_x_range_given_y_range (acre_t *acre,
+                            double *x_min, double *x_max,
+                            double y_min, double y_max)
 {
-    double x_adjust, y_adjust;
-    cairo_t *cr = acre->cr;
+    acre_data_t *data;
+    unsigned d, i;
+    bool first;
+
+    first = true;
 
-    /* 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;
+    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;
+               }
+           }
+       }
+    }
 
-    acre->y_axis.view_min = acre->y_axis.data_min;
-    acre->y_axis.view_max = acre->y_axis.data_max;
+    /* 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;
+    }
+}
 
-    /* Next, we want to ensure that the data never collides with the
-     * ticks. So we expand each axis on its minimum side as needed. */
-    cairo_save (cr);
-    {
-       double x, y;
+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;
 
-       _set_transform_to_data_space (acre);
+    first = true;
 
-       x = ACRE_TICK_MAJOR_SIZE + 2.0;
-       y = ACRE_TICK_MAJOR_SIZE + 2.0;
-       cairo_device_to_user_distance (cr, &x, &y);
+    for (d = 0; d < acre->num_data; d++) {
+       data = acre->data[d];
 
-       acre->x_axis.view_min -= x;
-       acre->y_axis.view_min += y;
+       /* Never mess with the Y range for timeline data. */
+       if (data->style == ACRE_STYLE_TIMELINE)
+           continue;
+
+       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;
+
+    /* 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;
+    } 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);
+       }
     }
-    cairo_restore (cr);
 
     /* Then, increase the axis ranges just enough so that the step
      * sizes for the ticks will be integers.
@@ -531,55 +779,6 @@ _compute_axis_ranges (acre_t *acre)
     cairo_restore (cr);
 }
 
-static void
-_acre_color_from_hsv (acre_color_t *color,
-                     double hue,
-                     double saturation,
-                     double value)
-{
-    double f, p, q, t;
-    int hmod6;
-
-    hmod6 = (int) floor (hue / 60) % 6;
-    f = hue / 60 - floor (hue / 60);
-    p = value * (1 - saturation);
-    q = value * (1 - f * saturation);
-    t = value * (1 - (1 - f) * saturation);
-    
-    switch (hmod6) {
-      case 0:
-       color->red = value;
-       color->green = t;
-       color->blue = p;
-       break;
-      case 1:
-       color->red = q;
-       color->green = value;
-       color->blue = p;
-       break;
-      case 2:
-       color->red = p;
-       color->green = value;
-       color->blue = t;
-       break;
-      case 3:
-       color->red = p;
-       color->green = q;
-       color->blue = value;
-       break;
-      case 4:
-       color->red = t;
-       color->green = p;
-       color->blue = value;
-       break;
-      case 5:
-       color->red = value;
-       color->green = p;
-       color->blue = q;
-       break;
-    }
-}
-
 static void
 _choose_colors (acre_t *acre)
 {
@@ -623,39 +822,185 @@ _choose_colors (acre_t *acre)
     cmsCloseProfile (srgb_profile);
 }
 
+/* Draw the given dataset as a line. */
+static void
+_draw_data_line (acre_t *acre, acre_data_t *data)
+{
+    unsigned i;
+    cairo_t *cr = acre->cr;
+
+    cairo_save (cr);
+
+    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_identity_matrix (cr);
+    cairo_set_line_width (cr, 1.0);
+    cairo_stroke (cr);
+
+    cairo_restore (cr);
+}
+
+#define BARS_WIDTH 0.8
+
+/* Draw the given dataset as bars. */
+static void
+_draw_data_bars (acre_t *acre, acre_data_t *data)
+{
+    unsigned i;
+    cairo_t *cr = acre->cr;
+
+    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);
+}
+
+/* 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)
+{
+    cairo_t *cr = acre->cr;
+    double ignored, width;
+
+    /* Check device-space width available for inter-bar 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);
+    else
+       _draw_data_bars (acre, data);
+}
+
+#define TIMELINE_BAR_HEIGHT 0.6
+
+/* Draw the given dataset as a timeline. Each (X,Y) point (potentially
+ * with a name) specifies the (start,stop) of a single timeline bar.
+ *
+ * Each independent timeline dataset in the chart is given its own
+ * vertical position, as specified by 'y_position'.
+ */
+static void
+_draw_data_timeline (acre_t *acre, acre_data_t *data, int y_position)
+{
+    unsigned i;
+    cairo_t *cr = acre->cr;
+    PangoLayout *timeline_label_layout;
+    double ignored, label_offset;
+    int labels_within_bar;
+
+    cairo_save (cr);
+
+    timeline_label_layout = _create_layout (acre, "Timeline");
+    pango_layout_set_font_description (timeline_label_layout, acre->font);
+
+    ignored = 0.0;
+    label_offset = ACRE_FONT_SIZE;
+    cairo_device_to_user_distance (cr, &ignored, &label_offset);
+
+    labels_within_bar = TIMELINE_BAR_HEIGHT / fabs (label_offset);
+
+    for (i = 0; i < data->num_points; i++) {
+       cairo_rectangle (cr,
+                        data->points[i].x,
+                        y_position - TIMELINE_BAR_HEIGHT / 2.0,
+                        data->points[i].y - data->points[i].x,
+                        TIMELINE_BAR_HEIGHT);
+
+       cairo_save (cr);
+       cairo_identity_matrix (cr);
+       cairo_set_line_width (cr, 1.0);
+       cairo_stroke_preserve (cr);
+       cairo_restore (cr);
+
+       cairo_new_path (cr);
+
+       if (i <= data->num_names && data->names[i]) {
+           cairo_save (cr);
+
+           cairo_move_to (cr, data->points[i].x,
+                          y_position + TIMELINE_BAR_HEIGHT / 2.0 +
+                          (i % labels_within_bar) * label_offset);
+           pango_layout_set_text (timeline_label_layout, data->names[i], -1);
+           cairo_identity_matrix (cr);
+           pango_cairo_show_layout (cr, timeline_label_layout);
+
+           cairo_restore (cr);
+       } else {
+           cairo_new_path (cr);
+       }
+
+    }
+
+    _destroy_layout (timeline_label_layout);
+
+    cairo_restore (cr);
+}
+
+/* Draw all the datasets of the chart. */
 static void
 _draw_data (acre_t *acre)
 {
     cairo_t *cr = acre->cr;
-    unsigned int d, i;
+    unsigned int i;
     acre_data_t *data;
 
     cairo_save (cr);
 
+    cairo_rectangle (cr,
+                    acre->chart.x, acre->chart.y,
+                    acre->chart.width, acre->chart.height);
+    cairo_clip (cr);
+
     cairo_set_source_rgb (cr, 0, 0, 0);
 
     _set_transform_to_data_space (acre);
 
-    for (d = 0; d < acre->num_data; d++) {
-       int color = d % acre->num_colors;
+    for (i = 0; i < acre->num_data; i++) {
+       int color = i % acre->num_colors;
        cairo_set_source_rgb (cr,
                              acre->colors[color].red,
                              acre->colors[color].green,
                              acre->colors[color].blue);
-       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);
+       data = acre->data[i];
+
+       switch (data->style) {
+       case ACRE_STYLE_LINE:
+           _draw_data_line (acre, data);
+           break;
+       case ACRE_STYLE_BARS:
+           _draw_data_bars (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);
+           break;
        }
-       cairo_restore (cr);
     }
 
     cairo_restore (cr);
@@ -717,10 +1062,14 @@ _draw_ticks (acre_t *acre,
        {
            PangoLayout *layout;
            int width, height;
+           double label_value;
 
            cairo_save (cr);
 
-           layout = _create_layout_printf (acre, "%g", t);
+           label_value = t;
+           if (fabs (label_value) < (sub_step / 1000.))
+               label_value = 0.0;
+           layout = _create_layout_printf (acre, "%g", label_value);
 
            if (ticks == ACRE_TICKS_X)
                cairo_move_to (cr, t, acre->y_axis.view_min);
@@ -877,6 +1226,8 @@ acre_data_create (void)
 
     data->name = NULL;
 
+    data->style = ACRE_STYLE_LINE;
+
     data->points = NULL;
     data->points_size = 0;
     data->num_points = 0;
@@ -889,11 +1240,27 @@ acre_data_create (void)
 void
 acre_data_destroy (acre_data_t *data)
 {
+    unsigned i;
+
+    for (i = 0; i < data->num_names; i++) {
+       if (data->names[i])
+           free (data->names[i]);
+    }
+    free (data->names);
+
+    free (data->name);
+
     free (data->points);
 
     free (data);
 }
 
+void
+acre_data_set_style (acre_data_t *data, acre_style_t style)
+{
+    data->style = style;
+}
+
 /* Set the label for this dataset (to appear in the plot's key). */
 void
 acre_data_set_name (acre_data_t *data, const char *name)
@@ -939,3 +1306,27 @@ acre_data_add_point_2d (acre_data_t *data, double x, double y)
 
     data->num_points++;
 }
+
+/* Add a datapoint with a name to the given dataset. */
+void
+acre_data_add_point_2d_named (acre_data_t *data, double x, double y, const char *name)
+{
+    unsigned i;
+
+    acre_data_add_point_2d (data, x, y);
+
+    if (data->names_size < data->points_size) {
+       data->names_size = data->points_size;
+       data->names = xrealloc_ab (data->names,
+                                  data->names_size,
+                                  sizeof (char *));
+    }
+
+    /* Initialize any newly-created holes in the array to NULL. */
+    for (i = data->num_names; i < data->num_points - 1; i++)
+       data->names[i] = NULL;
+
+    data->num_names = data->num_points;
+
+    data->names[data->num_names - 1] = xstrdup (name);
+}