5 mygraph_fill_background(graph *mygraph);
8 mygraph_draw_graph (cairo_t *cr,
13 mygraph_new (int width, int height)
16 mygraph = malloc(sizeof(graph));
17 mygraph->width = width;
18 mygraph->height = height;
21 int stride = width * 4;
23 image = (unsigned char *) malloc (sizeof(unsigned char) * stride * height);
25 mygraph->surface = cairo_image_surface_create_for_data (image, CAIRO_FORMAT_ARGB32,
26 width, height, stride);
27 mygraph->cr = cairo_create (mygraph->surface);
29 cairo_set_source_rgb (mygraph->cr, 1.0, 1.0, 1.0);
30 mygraph_fill_background (mygraph);
32 cairo_select_font_face (mygraph->cr, "Sans", CAIRO_FONT_SLANT_NORMAL,
33 CAIRO_FONT_WEIGHT_NORMAL);
34 cairo_set_font_size (mygraph->cr, 10);
36 cairo_set_antialias(mygraph->cr, CAIRO_ANTIALIAS_NONE);
37 cairo_set_line_width (mygraph->cr, 1.0);
43 mygraph_make_graph (graph *mygraph, float min_x, float max_x,
44 float min_y, float max_y, int tickgran)
47 cairo_text_extents_t extents;
49 mygraph->xoffset = xoffset;
50 mygraph->min_x = min_x;
51 mygraph->max_x = max_x;
52 mygraph->min_y = min_y;
53 mygraph->max_y = max_y;
56 /* cairo_surface_t *surface; */
58 float xs = ((float)mygraph->width - (float)(xoffset+2)) /
59 (float)(max_x - min_x);
61 float ys = ((float)mygraph->height - 33.) / (float)(min_y - max_y);
66 float starthour = fmod((min_x + (float)tz_local_offset()) / 3600., 24.);
67 float diff, center, begin, end;
72 for (i = 0; i<24; i++)
73 { // Hour marks and text
75 cairo_set_source_rgb (mygraph->cr, 1.0, 1.0, 1.0);
77 cairo_set_source_rgb (mygraph->cr, 0.90, 0.90, 1.0);
79 diff = fmod((float)i - starthour + 24., 24.);
80 begin = (diff * 3600.) * xs;
81 end = ((fmod(((float)i+1) - starthour + 24., 24.)) * 3600.) * xs;
82 center = (begin + end) / 2.;
87 // printf("i: %d, starthour: %f, diff: %f, begin: %f, end: %f\n", i, starthour, diff, begin, end);
91 if (begin > ((float)mygraph->width - ((float)xoffset)))
93 // printf("drawing\n");
95 cairo_rectangle (mygraph->cr, xoffset+begin, 0,
96 end - begin, mygraph->height);
98 cairo_fill (mygraph->cr);
100 if (begin <= 0.0 || end >= mygraph->width - (xoffset))
103 cairo_set_source_rgb (mygraph->cr, 0.0, 0.0, 0.0);
105 sprintf(string, "%d", i);
106 // printf("showing string %s\n", string);
108 cairo_text_extents (mygraph->cr, string, &extents);
109 cairo_move_to (mygraph->cr, xoffset + center -
111 (mygraph->height - extents.height - 2));
113 cairo_show_text (mygraph->cr, string);
117 cairo_set_source_rgb (mygraph->cr, 0.4, 0.4, 0.4);
121 ytick = ((int)max_y - (int)min_y) / 11;
122 ytick = ceil (ytick / tickgran) * tickgran;
124 } while (((int)max_y - (int)min_y) / ytick < 4);
127 for (i = -11; i<12; i++)
129 y = (i * ytick - (int)max_y) * ys + 10;
130 if (y < 2 || y > mygraph->height - 18)
133 /* printf("draw line at %d\n", y); */
135 cairo_move_to (mygraph->cr, xoffset, y);
136 cairo_line_to (mygraph->cr, mygraph->width-1, y);
141 cairo_set_source_rgb (mygraph->cr, 0.0, 0.0, 1.0);
142 cairo_stroke (mygraph->cr);
143 cairo_set_source_rgb (mygraph->cr, 0.6, 0.6, 0.6);
146 cairo_stroke (mygraph->cr);
149 traf = 8 * (i * ytick);
150 /* printf("traffic: %d\n", traf); */
152 if (traf >= 500000000)
153 sprintf (string, "%.1f Gbit", ((float)traf/1000000000));
154 else if (traf >= 500000)
155 sprintf (string, "%.1f Mbit", ((float)traf/1000000));
157 sprintf (string, "%.1f kbit", ((float)traf/1000));
159 cairo_text_extents (mygraph->cr, string, &extents);
161 if (y - (extents.height/2) < 2 ||
162 y + (extents.height/2) > mygraph->height - (extents.height + 2))
165 cairo_move_to (mygraph->cr,
166 xoffset - 4 - extents.width,
167 y + (extents.height/2));
169 cairo_show_text (mygraph->cr, string);
172 cairo_rectangle (mygraph->cr, xoffset, 0, mygraph->width-xoffset-1, mygraph->height-1);
174 cairo_set_source_rgb (mygraph->cr, 0.0, 0.0, 0.0);
175 cairo_stroke (mygraph->cr);
181 mygraph_plot_series (graph *mygraph, int *xvals, int *yvals, int n_vals,
182 float r, float g, float b)
188 cairo_set_antialias(mygraph->cr, CAIRO_ANTIALIAS_DEFAULT);
189 cairo_set_source_rgb (mygraph->cr, r, g, b);
191 cairo_move_to (mygraph->cr,
192 (int)((float)(x - (int)mygraph->min_x) *
193 mygraph->xs + (float)(mygraph->xoffset + 1)),
194 (int)((float)(y - (int)mygraph->max_y) * mygraph->ys + 10.));
196 /* printf("Plotting from:\n"); */
197 /* printf("(%d, %d) %d (%d, %d)<br/>\n", */
198 /* x, y, (int)((float)(x - (int)mygraph->min_x) * mygraph->xs + (float)(mygraph->xoffset + 1)) , */
199 /* (int)((float)(x - (int)mygraph->min_x) * mygraph->xs + (float)(mygraph->xoffset + 1)), */
200 /* (int)((float)(y - (int)mygraph->max_y) * mygraph->ys + 10.)); */
202 for (i = 1; i < n_vals; i++)
205 if (mygraph->xs * (xvals[i] - x) < 2 &&
206 mygraph->ys * (yvals[i] - y) > -2)
212 cairo_line_to (mygraph->cr,
213 (int)((float)(x - (int)mygraph->min_x) *
214 mygraph->xs + (float)(mygraph->xoffset + 1)),
215 (int)((float)(y - (int)mygraph->max_y) * mygraph->ys + 10.));
217 /* printf("(%d, %d) = > (%d, %d)<br/>\n", */
219 /* (int)((x - (int)x-mygraph->min_x) * (int)mygraph->xs + (int)mygraph->xoffset + 1), */
220 /* (int)((y - (int)mygraph->max_y) * (int)mygraph->ys + 10)); */
224 cairo_stroke(mygraph->cr);
228 mygraph_to_file (graph *mygraph, char *filename)
230 cairo_surface_write_to_png (mygraph->surface, filename);
234 mygraph_cleanup (graph *self)
236 cairo_destroy (self->cr);
237 cairo_surface_destroy (self->surface);
241 mygraph_fill_background (graph *mygraph)
243 cairo_rectangle (mygraph->cr, 0, 0, mygraph->width, mygraph->height);
244 cairo_fill (mygraph->cr);