aboutsummaryrefslogtreecommitdiff
path: root/src/graph.c
diff options
context:
space:
mode:
authorSergey Poznyakoff <gray@gnu.org.ua>2009-04-25 00:12:49 +0300
committerSergey Poznyakoff <gray@gnu.org.ua>2009-04-25 00:12:49 +0300
commitb8f6eec86f23d3fadd876654bcc145ef892fd1ea (patch)
tree423408e16973574cc921b59270c6bea67d6bca18 /src/graph.c
parentc36ee7102c3095f70e8db52aa55046c1393ff4cd (diff)
downloadtagr-b8f6eec86f23d3fadd876654bcc145ef892fd1ea.tar.gz
tagr-b8f6eec86f23d3fadd876654bcc145ef892fd1ea.tar.bz2
Finish implementation of draw_graph.
* src/grid.c: New file. * src/Makefile.am (tagr_SOURCES): Add grid.c * src/graph.c: Finish implementation of draw_graph. * src/output.c: Update calls to draw_graph. * src/report.c (update_router): Use tr->last.time when updating graphs. * src/tagr.h (grid_t, struct grid_class): New types. (grid_create, grid_next, grid_destroy): New protos. (grid_class_y, grid_class_x_2h, grid_class_x_wday) (grid_class_x_week, grid_class_x_month): New externs. (draw_graph): Change prototype.
Diffstat (limited to 'src/graph.c')
-rw-r--r--src/graph.c176
1 files changed, 150 insertions, 26 deletions
diff --git a/src/graph.c b/src/graph.c
index 1026f35..5933b7f 100644
--- a/src/graph.c
+++ b/src/graph.c
@@ -53,6 +53,9 @@ int color_percent[3] = { 239,159,79 };
int graph_xsize = 460;
int graph_ysize = 100;
+int graph_h_margin[2] = { 100, 14 };
+int graph_v_margin[2] = { 14, 35 };
+
static char *short_suffix[] = {"", "k", "M", "G", "T"};
char **number_suffix = short_suffix;
size_t number_suffix_count = sizeof (short_suffix) / sizeof (short_suffix[0]);
@@ -60,29 +63,48 @@ size_t number_suffix_count = sizeof (short_suffix) / sizeof (short_suffix[0]);
#define make_color_index(g, ar) \
gdImageColorAllocate (g, (ar)[0], (ar)[1], (ar)[2])
+static void
+draw_vtext (gdImagePtr graph, int color, const char *text)
+{
+ gdImageStringUp (graph, gdFontSmall,
+ 8,
+ graph_ysize + graph_v_margin[0] -
+ (graph_ysize - gdFontSmall->w * strlen (text))/2,
+ (unsigned char *) text, color);
+}
+
int
draw_graph (FILE *fp,
queue_t *dataq, const struct avg_acc *avg, time_t now,
int xstep, unsigned long xmax, unsigned long ymax,
- int growright)
+ int growright,
+ struct grid_class *xgrid, struct grid_class *ygrid)
{
- int x;
+ int x, y;
int i, n;
gdImagePtr graph, brush_out, brush_outm, brush_outp;
int i_background, i_light, i_dark;
int i_major, i_in, i_out, i_grid, i_inm, i_outm;
int i_outp, i_outpg;
double xscale, yscale;
-
- yscale = ((double) graph_ysize - 35) / ymax;
- xscale = ((double) graph_xsize - 100 - percent_option * 30) / xmax;
+ int dotted_style[3];
+ char *longup;
+ char *shortup;
+ int full_xsize = graph_xsize + graph_h_margin[0] + graph_h_margin[1];
+ int full_ysize = graph_ysize + graph_v_margin[0] + graph_v_margin[1];
+ grid_t grid;
+
+ yscale = (double) graph_ysize / ymax;
+ xscale = (double) graph_xsize / xmax;
#define ytr(y) \
- (unsigned long) ((ymax - (y)) * yscale + 14)
+ (unsigned long) ((ymax - (y)) * yscale + graph_h_margin[1])
#define xtr(x) \
- (unsigned long) (growright ? ((graph_xsize - (x))) : (x))
+ (unsigned long) (growright ? \
+ ((full_xsize - (x)*xscale)) : \
+ (graph_h_margin[0] + (x)*xscale))
- graph = gdImageCreate (graph_xsize, graph_ysize);
+ graph = gdImageCreate (full_xsize, full_ysize);
brush_out = gdImageCreate (1, 2);
brush_outm = gdImageCreate (1, 2);
brush_outp = gdImageCreate (1, 2);
@@ -106,21 +128,23 @@ draw_graph (FILE *fp,
i_outpg = make_color_index (graph, color_percent);
/* Draw the image border */
- gdImageLine (graph, 0, 0, graph_xsize - 1, 0, i_light);
- gdImageLine (graph, 1, 1, graph_xsize - 2, 1, i_light);
- gdImageLine (graph, 0, 0, 0, graph_ysize - 1, i_light);
- gdImageLine (graph, 1, 1, 1, graph_ysize - 2, i_light);
- gdImageLine (graph, graph_xsize - 1, 0, graph_xsize - 1,
- graph_ysize - 1, i_dark);
- gdImageLine (graph, 0, graph_ysize - 1, graph_xsize - 1,
- graph_ysize - 1, i_dark);
- gdImageLine (graph, graph_xsize - 2, 1, graph_xsize - 2,
- graph_ysize - 2, i_dark);
- gdImageLine (graph, 1, graph_ysize - 2, graph_xsize - 2,
- graph_ysize - 2, i_dark);
+ gdImageLine (graph, 0, 0, full_xsize - 1, 0, i_light);
+ gdImageLine (graph, 1, 1, full_xsize - 2, 1, i_light);
+ gdImageLine (graph, 0, 0, 0, full_ysize - 1, i_light);
+ gdImageLine (graph, 1, 1, 1, full_ysize - 2, i_light);
+ gdImageLine (graph, full_xsize - 1, 0, full_xsize - 1,
+ full_ysize - 1, i_dark);
+ gdImageLine (graph, 0, full_ysize - 1, full_xsize - 1,
+ full_ysize - 1, i_dark);
+ gdImageLine (graph, full_xsize - 2, 1, full_xsize - 2,
+ full_ysize - 2, i_dark);
+ gdImageLine (graph, 1, full_ysize - 2, full_xsize - 2,
+ full_ysize - 2, i_dark);
n = queue_count (dataq);
- for (i = n - 1, x = 0; i > 0; i--, x++)
+
+ /* Incoming traffic */
+ for (i = n - 1, x = 0; i > 0 && x < xmax; i--, x += xstep)
{
struct traffic_history *th = queue_get_ptr (dataq, i);
struct traffic_history *tnext = queue_get_ptr (dataq, i - 1);
@@ -130,13 +154,113 @@ draw_graph (FILE *fp,
xtr (x), ytr (tnext->inrate), i_in);
gdImageLine (graph, xtr (x), ytr (th->inrate),
xtr (x+1), ytr (tnext->inrate), i_in);
-#if 0
- gdImageLine (graph, x + 0.5, ytr (0),
- x + 0.5, ytr (th->inrate), i_in);
-#endif
}
- /* FIXME: Drow outgoing traffic, grids and legends */
+ /* Outgoing traffic */
+ gdImageSetBrush (graph, brush_out);
+ for (i = n - 1, x = 0; i > 0 && x < xmax; i--, x += xstep)
+ {
+ struct traffic_history *th = queue_get_ptr (dataq, i);
+ struct traffic_history *tnext = queue_get_ptr (dataq, i - 1);
+
+ gdImageLine (graph, xtr (x), ytr (th->outrate),
+ xtr (x+1), ytr (tnext->outrate), gdBrushed);
+ }
+
+ /* Border */
+ gdImageRectangle (graph,
+ xtr (0), ytr (0),
+ xtr (xmax), ytr (ymax), i_grid);
+
+
+ dotted_style[0] = i_grid;
+ dotted_style[1] = gdTransparent;
+ dotted_style[2] = gdTransparent;
+ gdImageSetStyle (graph, dotted_style, 3);
+
+ /* draw the horizontal grid */
+ /* FIXME: Both should be configurable and I18N */
+ longup = "Bits per Second";
+ shortup = "Bits/s";
+
+ grid = grid_create (ygrid, dataq, 0, ymax, NULL);
+ if (grid)
+ {
+ unsigned long i;
+ char *str = NULL;
+
+ if (graph_ysize < gdFontSmall->w * 16)
+ draw_vtext (graph, i_grid, shortup);
+ else
+ draw_vtext (graph, i_grid, longup);
+
+ /* Draw vertical grid */
+ while ((i = grid_next (grid, &str, NULL)) < ymax)
+ {
+ int y = ytr (i);
+
+ /* Left tick */
+ gdImageLine (graph,
+ xtr (-2), y,
+ xtr (1), y,
+ i_grid);
+ /* Right tick */
+ gdImageLine (graph,
+ xtr (xmax - 1), y,
+ xtr (xmax + 1), y,
+ i_grid);
+
+ /* Grid line */
+ gdImageLine (graph, xtr (0), y, xtr (xmax), y,
+ gdStyled);
+
+ if (str)
+ gdImageString (graph, gdFontSmall,
+ 23, y - gdFontSmall->h / 2,
+ (unsigned char *) str, i_grid);
+ }
+ grid_destroy (grid);
+ }
+
+ grid = grid_create (xgrid, dataq, 0, xmax, &now);
+ if (grid)
+ {
+ unsigned long i;
+ char *str = NULL;
+ int mark;
+
+ while ((i = grid_next (grid, &str, &mark)) < xmax)
+ {
+ int x = xtr (i);
+
+ /* Tick */
+ gdImageLine (graph,
+ x, ytr (-2), x, ytr (1),
+ i_grid);
+ /* Grid line */
+ gdImageLine (graph,
+ x, ytr (0), x, ytr (ymax),
+ mark ? i_major : gdStyled);
+
+ if (str)
+ gdImageString (graph, gdFontSmall,
+ x - strlen (str) * gdFontSmall->w / 2,
+ graph_ysize + graph_v_margin[0] + 2,
+ (unsigned char *) str, i_grid);
+ }
+ grid_destroy (grid);
+ }
+
+ /* Mark 0,0 */
+ x = graph_h_margin[0];
+ y = graph_ysize + graph_h_margin[1];
+ gdImageLine (graph, x + 2, y + 3, x + 2, y - 3, i_major);
+ gdImageLine (graph, x + 1, y + 3, x + 1, y - 3, i_major);
+ gdImageLine (graph, x, y + 2, x, y - 2, i_major);
+ gdImageLine (graph, x - 1, y + 1, x - 1, y - 1, i_major);
+ gdImageLine (graph, x - 2, y + 1, x - 2, y - 1, i_major);
+ gdImageLine (graph, x - 3, y, x - 3, y, i_major);
+
gdImagePng (graph, fp);
gdImageDestroy (graph);

Return to:

Send suggestions and report system problems to the System administrator.