X-Git-Url: https://git.cworth.org/git?a=blobdiff_plain;f=acre.c;h=fac0a2053c4796a102c14fc0d91e5129967e60a4;hb=2c0f51b31a3f32c091ae04632f7d8e424f734577;hp=e3604dcff6a8de23ed264edc96bbeeaf374ee0fa;hpb=803fba5eca929a4e0cb541223182eae86a96f9bb;p=acre diff --git a/acre.c b/acre.c index e3604dc..fac0a20 100644 --- a/acre.c +++ b/acre.c @@ -37,14 +37,31 @@ 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 { @@ -134,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; @@ -347,6 +365,9 @@ _create_layout (acre_t *acre, const char *text) { PangoLayout *layout; + if (text == NULL) + text = ""; + layout = pango_cairo_create_layout (acre->cr); pango_layout_set_font_description (layout, acre->font); pango_layout_set_text (layout, text, -1); @@ -432,6 +453,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); @@ -699,23 +721,6 @@ _compute_axis_ranges (acre_t *acre) } } - /* 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; - - _set_transform_to_data_space (acre); - - x = ACRE_TICK_MAJOR_SIZE + 2.0; - y = ACRE_TICK_MAJOR_SIZE + 2.0; - cairo_device_to_user_distance (cr, &x, &y); - - acre->x_axis.view_min -= x; - acre->y_axis.view_min += y; - } - cairo_restore (cr); - /* Then, increase the axis ranges just enough so that the step * sizes for the ticks will be integers. */ @@ -750,55 +755,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) { @@ -842,58 +798,62 @@ _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); +} + +/* 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; - bool pen_up; 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]; + data = acre->data[i]; - pen_up = true; - cairo_new_path (cr); - - for (i = 0; i < data->num_points; i++) { - if (data->points[i].x >= acre->x_axis.view_min && - data->points[i].x <= acre->x_axis.view_max && - data->points[i].y >= acre->y_axis.view_min && - data->points[i].y <= acre->y_axis.view_max) - { - if (pen_up) - cairo_move_to (cr, - data->points[i].x, - data->points[i].y); - else - cairo_line_to (cr, - data->points[i].x, - data->points[i].y); - - pen_up = false; - } else { - pen_up = true; - } - } - cairo_save (cr); - { - cairo_identity_matrix (cr); - cairo_set_line_width (cr, 1.0); - cairo_stroke (cr); + switch (data->style) { + case ACRE_STYLE_LINE: + _draw_data_line (acre, data); + break; } - cairo_restore (cr); } cairo_restore (cr); @@ -955,10 +915,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); @@ -1115,6 +1079,8 @@ acre_data_create (void) data->name = NULL; + data->style = ACRE_STYLE_LINE; + data->points = NULL; data->points_size = 0; data->num_points = 0; @@ -1127,6 +1093,16 @@ 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); @@ -1177,3 +1153,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); +}