[graph] simplify 'line' drawing; draw the border after the graph-drawing

Drawing the border after the graph-drawing, allows to draw down to the border itself
when 0 values occur.

Signed-off-by: Julien Danjou <julien@danjou.info>
This commit is contained in:
marco candrian 2008-04-18 00:58:42 +02:00 committed by Julien Danjou
parent 9ba6cfd6c5
commit 2eb6fda36e
2 changed files with 25 additions and 35 deletions

View File

@ -493,51 +493,39 @@ draw_graph_line(DrawCtx *ctx, area_t rect, int *to, int cur_index,
Position grow, area_t patt_rect, Position grow, area_t patt_rect,
XColor *pcolor, XColor *pcolor_center, XColor *pcolor_end) XColor *pcolor, XColor *pcolor_center, XColor *pcolor_end)
{ {
int i, x, y, w; int i, x, w;
int flag = 0; /* used to prevent drawing a line from 0 to 0 values */ float y;
cairo_pattern_t *pat; cairo_pattern_t *pat;
/* some stuff below is based on 'try and error' */
pat = draw_setup_cairo_color_source(ctx, patt_rect, pcolor, pcolor_center, pcolor_end); pat = draw_setup_cairo_color_source(ctx, patt_rect, pcolor, pcolor_center, pcolor_end);
x = rect.x; x = rect.x;
y = rect.y; y = rect.y + 0.5; /* center of a pixel */
w = rect.width; w = rect.width;
/* NOTE: think about about the cairo coordinates pointing to the upper left /* if grow == Right, go through the values from old to new. Setup the oldest below */
* corner of a single pixel (what itself is a square) - and it draws from if(grow == Right && ++cur_index > w - 1)
* there! It fills the pixels placed on the bottom/right of that, or so */ cur_index = 0;
if(grow == Right) /* draw from right to left */
x += w;
/* initial point */
cairo_move_to(ctx->cr, x, y - to[cur_index]); cairo_move_to(ctx->cr, x, y - to[cur_index]);
for(i = 0; i < w; i++) for(i = 0; i < w; i++)
{ {
cairo_line_to(ctx->cr, ++x, y - to[cur_index]);
/* cycles around the index */
if(grow == Right) if(grow == Right)
x--;
else
x++;
if (to[cur_index] > 0)
{ {
cairo_line_to(ctx->cr, x, y - to[cur_index]); if (++cur_index > w-1)
flag = 1; cur_index = 0;
} }
else else
{ {
if(flag) /* only draw from values > 0 to 0-values */ if(--cur_index < 0)
{ cur_index = w - 1;
cairo_line_to(ctx->cr, x, y);
flag = 0;
}
else
cairo_move_to(ctx->cr, x, y);
} }
if (--cur_index < 0) /* cycles around the index */
cur_index = w - 1;
} }
cairo_stroke(ctx->cr); cairo_stroke(ctx->cr);

View File

@ -106,14 +106,9 @@ graph_draw(Widget *widget, DrawCtx *ctx, int offset,
margin_top = (int)((widget->statusbar->height - (d->box_height + 2)) / 2 + 0.5) + widget->area.y; margin_top = (int)((widget->statusbar->height - (d->box_height + 2)) / 2 + 0.5) + widget->area.y;
rectangle.x = widget->area.x; /* draw background */
rectangle.y = margin_top; rectangle.x = widget->area.x + 1;
rectangle.width = d->size + 2; rectangle.y = margin_top + 1;
rectangle.height = d->box_height + 2;
draw_rectangle(ctx, rectangle, 1.0, False, d->bordercolor);
rectangle.x++;
rectangle.y++;
rectangle.width = d->size; rectangle.width = d->size;
rectangle.height = d->box_height; rectangle.height = d->box_height;
draw_rectangle(ctx, rectangle, 1.0, True, d->bg); draw_rectangle(ctx, rectangle, 1.0, True, d->bg);
@ -271,6 +266,13 @@ graph_draw(Widget *widget, DrawCtx *ctx, int offset,
} }
} }
/* draw border (after line-drawing, what paints 0-values to the border) */
rectangle.x = widget->area.x;
rectangle.y = margin_top;
rectangle.width = d->size + 2;
rectangle.height = d->box_height + 2;
draw_rectangle(ctx, rectangle, 1.0, False, d->bordercolor);
widget->area.width = d->width; widget->area.width = d->width;
widget->area.height = widget->statusbar->height; widget->area.height = widget->statusbar->height;
return widget->area.width; return widget->area.width;