Commit | Line | Data |
---|---|---|
af58ef32 SC |
1 | /* |
2 | * gfio - gui front end for fio - the flexible io tester | |
3 | * | |
4 | * Copyright (C) 2012 Stephen M. Cameron <stephenmcameron@gmail.com> | |
5 | * | |
6 | * The license below covers all files distributed with fio unless otherwise | |
7 | * noted in the file itself. | |
8 | * | |
9 | * This program is free software; you can redistribute it and/or modify | |
10 | * it under the terms of the GNU General Public License version 2 as | |
11 | * published by the Free Software Foundation. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with this program; if not, write to the Free Software | |
20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
21 | * | |
22 | */ | |
23 | #include <string.h> | |
24 | #include <malloc.h> | |
25 | #include <math.h> | |
26 | #include <assert.h> | |
93e2db2b | 27 | #include <stdlib.h> |
af58ef32 SC |
28 | |
29 | #include <cairo.h> | |
30 | #include <gtk/gtk.h> | |
31 | ||
32 | #include "tickmarks.h" | |
09ad20ff | 33 | #include "graph.h" |
b65c7ec4 JA |
34 | #include "flist.h" |
35 | #include "lib/prio_tree.h" | |
b65c7ec4 JA |
36 | |
37 | /* | |
38 | * Allowable difference to show tooltip | |
39 | */ | |
b7a69ad8 | 40 | #define TOOLTIP_DELTA 0.08 |
af58ef32 SC |
41 | |
42 | struct xyvalue { | |
43 | double x, y; | |
44 | }; | |
45 | ||
46 | struct graph_value { | |
47 | struct graph_value *next; | |
b65c7ec4 | 48 | struct prio_tree_node node; |
93e2db2b | 49 | char *tooltip; |
af58ef32 SC |
50 | void *value; |
51 | }; | |
52 | ||
53 | struct graph_label { | |
54 | char *label; | |
55 | struct graph_value *tail; | |
56 | struct graph_value *values; | |
57 | struct graph_label *next; | |
b65c7ec4 | 58 | struct prio_tree_root prio_tree; |
af58ef32 SC |
59 | double r, g, b; |
60 | int value_count; | |
93e2db2b | 61 | unsigned int tooltip_count; |
af58ef32 SC |
62 | struct graph *parent; |
63 | }; | |
64 | ||
b65c7ec4 JA |
65 | struct tick_value { |
66 | unsigned int offset; | |
67 | double value; | |
68 | }; | |
69 | ||
af58ef32 SC |
70 | struct graph { |
71 | char *title; | |
72 | char *xtitle; | |
73 | char *ytitle; | |
87d5f276 | 74 | unsigned int xdim, ydim; |
57f9d28e | 75 | double xoffset, yoffset; |
af58ef32 SC |
76 | struct graph_label *labels; |
77 | struct graph_label *tail; | |
78 | int per_label_limit; | |
f3e8440f | 79 | const char *font; |
7175d91d SC |
80 | graph_axis_unit_change_callback x_axis_unit_change_callback; |
81 | graph_axis_unit_change_callback y_axis_unit_change_callback; | |
d8fbeefb | 82 | unsigned int base_offset; |
def0ac29 SC |
83 | double left_extra; |
84 | double right_extra; | |
85 | double top_extra; | |
86 | double bottom_extra; | |
b65c7ec4 JA |
87 | |
88 | double xtick_zero; | |
89 | double xtick_delta; | |
90 | double xtick_zero_val; | |
b7a69ad8 | 91 | double xtick_one_val; |
b65c7ec4 JA |
92 | double ytick_zero; |
93 | double ytick_delta; | |
94 | double ytick_zero_val; | |
b7a69ad8 | 95 | double ytick_one_val; |
af58ef32 SC |
96 | }; |
97 | ||
3ea48b88 SC |
98 | void graph_set_size(struct graph *g, unsigned int xdim, unsigned int ydim) |
99 | { | |
100 | g->xdim = xdim; | |
101 | g->ydim = ydim; | |
102 | } | |
103 | ||
57f9d28e SC |
104 | void graph_set_position(struct graph *g, double xoffset, double yoffset) |
105 | { | |
106 | g->xoffset = xoffset; | |
107 | g->yoffset = yoffset; | |
108 | } | |
109 | ||
f3e8440f | 110 | struct graph *graph_new(unsigned int xdim, unsigned int ydim, const char *font) |
af58ef32 SC |
111 | { |
112 | struct graph *g; | |
113 | ||
114 | g = calloc(1, sizeof(*g)); | |
3ea48b88 | 115 | graph_set_size(g, xdim, ydim); |
af58ef32 | 116 | g->per_label_limit = -1; |
f3e8440f JA |
117 | g->font = font; |
118 | if (!g->font) | |
119 | g->font = "Sans"; | |
af58ef32 SC |
120 | return g; |
121 | } | |
122 | ||
7175d91d SC |
123 | void graph_x_axis_unit_change_notify(struct graph *g, graph_axis_unit_change_callback f) |
124 | { | |
125 | g->x_axis_unit_change_callback = f; | |
126 | } | |
127 | ||
128 | void graph_y_axis_unit_change_notify(struct graph *g, graph_axis_unit_change_callback f) | |
129 | { | |
130 | g->y_axis_unit_change_callback = f; | |
131 | } | |
132 | ||
af58ef32 SC |
133 | static int count_labels(struct graph_label *labels) |
134 | { | |
135 | int count = 0; | |
136 | struct graph_label *i; | |
137 | ||
138 | for (i = labels; i; i = i->next) | |
139 | count++; | |
140 | return count; | |
141 | } | |
142 | ||
143 | static int count_values(struct graph_value *values) | |
144 | { | |
145 | int count = 0; | |
146 | struct graph_value *i; | |
147 | ||
148 | for (i = values; i; i = i->next) | |
149 | count++; | |
150 | return count; | |
151 | } | |
152 | ||
153 | typedef double (*double_comparator)(double a, double b); | |
154 | ||
155 | static double mindouble(double a, double b) | |
156 | { | |
157 | return a < b ? a : b; | |
158 | } | |
159 | ||
160 | static double maxdouble(double a, double b) | |
161 | { | |
162 | return a < b ? b : a; | |
163 | } | |
164 | ||
165 | static double find_double_values(struct graph_value *values, double_comparator cmp) | |
166 | { | |
167 | struct graph_value *i; | |
168 | int first = 1; | |
169 | double answer, tmp; | |
170 | ||
171 | assert(values != NULL); | |
172 | answer = 0.0; /* shut the compiler up, might need to think harder though. */ | |
173 | for (i = values; i; i = i->next) { | |
174 | tmp = *(double *) i->value; | |
175 | if (first) { | |
176 | answer = tmp; | |
177 | first = 0; | |
178 | } else { | |
179 | answer = cmp(answer, tmp); | |
180 | } | |
181 | } | |
182 | return answer; | |
183 | } | |
184 | ||
185 | static double find_double_data(struct graph_label *labels, double_comparator cmp) | |
186 | { | |
187 | struct graph_label *i; | |
188 | int first = 1; | |
189 | double answer, tmp; | |
190 | ||
191 | assert(labels != NULL); | |
192 | answer = 0.0; /* shut the compiler up, might need to think harder though. */ | |
193 | for (i = labels; i; i = i->next) { | |
194 | tmp = find_double_values(i->values, cmp); | |
195 | if (first) { | |
196 | answer = tmp; | |
197 | first = 0; | |
198 | } else { | |
199 | answer = cmp(tmp, answer); | |
200 | } | |
201 | } | |
202 | return answer; | |
203 | } | |
204 | ||
205 | static double find_min_data(struct graph_label *labels) | |
206 | { | |
207 | return find_double_data(labels, mindouble); | |
208 | } | |
209 | ||
210 | static double find_max_data(struct graph_label *labels) | |
211 | { | |
212 | return find_double_data(labels, maxdouble); | |
213 | } | |
214 | ||
215 | static void draw_bars(struct graph *bg, cairo_t *cr, struct graph_label *lb, | |
216 | double label_offset, double bar_width, | |
217 | double mindata, double maxdata) | |
218 | { | |
219 | struct graph_value *i; | |
220 | double x1, y1, x2, y2; | |
221 | int bar_num = 0; | |
222 | double domain, range, v; | |
223 | ||
224 | domain = (maxdata - mindata); | |
225 | range = (double) bg->ydim * 0.80; /* FIXME */ | |
226 | cairo_stroke(cr); | |
227 | for (i = lb->values; i; i = i->next) { | |
228 | ||
229 | x1 = label_offset + (double) bar_num * bar_width + (bar_width * 0.05); | |
230 | x2 = x1 + bar_width * 0.90; | |
231 | y2 = bg->ydim * 0.90; | |
232 | v = *(double *) i->value; | |
233 | y1 = y2 - (((v - mindata) / domain) * range); | |
234 | cairo_move_to(cr, x1, y1); | |
235 | cairo_line_to(cr, x1, y2); | |
236 | cairo_line_to(cr, x2, y2); | |
237 | cairo_line_to(cr, x2, y1); | |
238 | cairo_close_path(cr); | |
239 | cairo_fill(cr); | |
240 | cairo_stroke(cr); | |
241 | bar_num++; | |
242 | } | |
243 | } | |
244 | ||
10e54cdc SC |
245 | static void draw_aligned_text(struct graph *g, cairo_t *cr, double x, double y, |
246 | double fontsize, const char *text, int alignment) | |
af58ef32 | 247 | { |
10e54cdc SC |
248 | #define CENTERED 0 |
249 | #define LEFT_JUSTIFIED 1 | |
250 | #define RIGHT_JUSTIFIED 2 | |
251 | ||
252 | double factor, direction; | |
af58ef32 SC |
253 | cairo_text_extents_t extents; |
254 | ||
10e54cdc SC |
255 | switch(alignment) { |
256 | case CENTERED: | |
257 | direction = -1.0; | |
258 | factor = 0.5; | |
259 | break; | |
260 | case RIGHT_JUSTIFIED: | |
261 | direction = -1.0; | |
262 | factor = 1.0; | |
263 | break; | |
264 | case LEFT_JUSTIFIED: | |
265 | default: | |
266 | direction = 1.0; | |
267 | factor = 1.0; | |
268 | break; | |
269 | } | |
f3e8440f | 270 | cairo_select_font_face (cr, g->font, CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL); |
af58ef32 SC |
271 | |
272 | cairo_set_font_size(cr, fontsize); | |
273 | cairo_text_extents(cr, text, &extents); | |
10e54cdc | 274 | x = x + direction * (factor * extents.width + extents.x_bearing); |
af58ef32 SC |
275 | y = y - (extents.height / 2 + extents.y_bearing); |
276 | ||
277 | cairo_move_to(cr, x, y); | |
278 | cairo_show_text(cr, text); | |
279 | } | |
280 | ||
10e54cdc SC |
281 | static inline void draw_centered_text(struct graph *g, cairo_t *cr, double x, double y, |
282 | double fontsize, const char *text) | |
283 | { | |
284 | draw_aligned_text(g, cr, x, y, fontsize, text, CENTERED); | |
285 | } | |
286 | ||
287 | static inline void draw_right_justified_text(struct graph *g, cairo_t *cr, | |
288 | double x, double y, | |
289 | double fontsize, const char *text) | |
290 | { | |
291 | draw_aligned_text(g, cr, x, y, fontsize, text, RIGHT_JUSTIFIED); | |
292 | } | |
293 | ||
294 | static inline void draw_left_justified_text(struct graph *g, cairo_t *cr, | |
295 | double x, double y, | |
296 | double fontsize, const char *text) | |
297 | { | |
298 | draw_aligned_text(g, cr, x, y, fontsize, text, LEFT_JUSTIFIED); | |
299 | } | |
300 | ||
f3e8440f JA |
301 | static void draw_vertical_centered_text(struct graph *g, cairo_t *cr, double x, |
302 | double y, double fontsize, | |
303 | const char *text) | |
af58ef32 SC |
304 | { |
305 | double sx, sy; | |
306 | cairo_text_extents_t extents; | |
307 | ||
f3e8440f | 308 | cairo_select_font_face(cr, g->font, CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL); |
af58ef32 SC |
309 | |
310 | cairo_set_font_size(cr, fontsize); | |
311 | cairo_text_extents(cr, text, &extents); | |
312 | sx = x; | |
313 | sy = y; | |
314 | y = y + (extents.width / 2.0 + extents.x_bearing); | |
315 | x = x - (extents.height / 2.0 + extents.y_bearing); | |
316 | ||
317 | cairo_move_to(cr, x, y); | |
318 | cairo_save(cr); | |
319 | cairo_translate(cr, -sx, -sy); | |
320 | cairo_rotate(cr, -90.0 * M_PI / 180.0); | |
321 | cairo_translate(cr, sx, sy); | |
322 | cairo_show_text(cr, text); | |
323 | cairo_restore(cr); | |
324 | } | |
325 | ||
326 | static void graph_draw_common(struct graph *g, cairo_t *cr, | |
327 | double *x1, double *y1, double *x2, double *y2) | |
328 | { | |
329 | cairo_set_source_rgb(cr, 0, 0, 0); | |
f3e8440f | 330 | cairo_set_line_width (cr, 0.8); |
af58ef32 | 331 | |
9ede9c9c | 332 | *x1 = 0.10 * g->xdim; |
6bf86008 SC |
333 | *x2 = 0.95 * g->xdim; |
334 | *y1 = 0.10 * g->ydim; | |
335 | *y2 = 0.90 * g->ydim; | |
af58ef32 SC |
336 | |
337 | cairo_move_to(cr, *x1, *y1); | |
338 | cairo_line_to(cr, *x1, *y2); | |
339 | cairo_line_to(cr, *x2, *y2); | |
340 | cairo_line_to(cr, *x2, *y1); | |
341 | cairo_line_to(cr, *x1, *y1); | |
342 | cairo_stroke(cr); | |
343 | ||
f3e8440f JA |
344 | draw_centered_text(g, cr, g->xdim / 2, g->ydim / 20, 20.0, g->title); |
345 | draw_centered_text(g, cr, g->xdim / 2, g->ydim * 0.97, 14.0, g->xtitle); | |
346 | draw_vertical_centered_text(g, cr, g->xdim * 0.02, g->ydim / 2, 14.0, g->ytitle); | |
af58ef32 SC |
347 | cairo_stroke(cr); |
348 | } | |
349 | ||
350 | static void graph_draw_x_ticks(struct graph *g, cairo_t *cr, | |
351 | double x1, double y1, double x2, double y2, | |
d8fbeefb | 352 | double minx, double maxx, int nticks, int add_tm_text) |
af58ef32 SC |
353 | { |
354 | struct tickmark *tm; | |
355 | double tx; | |
7175d91d | 356 | int i, power_of_ten; |
af58ef32 SC |
357 | static double dash[] = { 1.0, 2.0 }; |
358 | ||
7175d91d | 359 | nticks = calc_tickmarks(minx, maxx, nticks, &tm, &power_of_ten, |
d8fbeefb | 360 | g->x_axis_unit_change_callback == NULL, g->base_offset); |
7175d91d SC |
361 | if (g->x_axis_unit_change_callback) |
362 | g->x_axis_unit_change_callback(g, power_of_ten); | |
af58ef32 SC |
363 | |
364 | for (i = 0; i < nticks; i++) { | |
365 | tx = (((tm[i].value) - minx) / (maxx - minx)) * (x2 - x1) + x1; | |
d8fbeefb | 366 | |
b65c7ec4 JA |
367 | /* |
368 | * Update tick delta | |
369 | */ | |
370 | if (!i) { | |
371 | g->xtick_zero = tx; | |
372 | g->xtick_zero_val = tm[0].value; | |
b7a69ad8 | 373 | } else if (i == 1) { |
b65c7ec4 | 374 | g->xtick_delta = (tm[1].value - tm[0].value) / (tx - g->xtick_zero); |
b7a69ad8 JA |
375 | g->xtick_one_val = tm[1].value; |
376 | } | |
b65c7ec4 | 377 | |
d8fbeefb JA |
378 | /* really tx < yx || tx > x2, but protect against rounding */ |
379 | if (x1 - tx > 0.01 || tx - x2 > 0.01) | |
af58ef32 SC |
380 | continue; |
381 | ||
382 | /* Draw tick mark */ | |
f3e8440f | 383 | cairo_set_line_width(cr, 0.8); |
af58ef32 SC |
384 | cairo_move_to(cr, tx, y2); |
385 | cairo_line_to(cr, tx, y2 + (y2 - y1) * 0.03); | |
386 | cairo_stroke(cr); | |
387 | ||
388 | /* draw grid lines */ | |
389 | cairo_save(cr); | |
390 | cairo_set_dash(cr, dash, 2, 2.0); | |
391 | cairo_set_line_width(cr, 0.5); | |
392 | cairo_move_to(cr, tx, y1); | |
393 | cairo_line_to(cr, tx, y2); | |
394 | cairo_stroke(cr); | |
395 | cairo_restore(cr); | |
396 | ||
d8fbeefb JA |
397 | if (!add_tm_text) |
398 | continue; | |
399 | ||
af58ef32 | 400 | /* draw tickmark label */ |
f3e8440f | 401 | draw_centered_text(g, cr, tx, y2 * 1.04, 12.0, tm[i].string); |
af58ef32 | 402 | cairo_stroke(cr); |
af58ef32 SC |
403 | } |
404 | } | |
405 | ||
d8fbeefb | 406 | static double graph_draw_y_ticks(struct graph *g, cairo_t *cr, |
af58ef32 | 407 | double x1, double y1, double x2, double y2, |
d8fbeefb | 408 | double miny, double maxy, int nticks, int add_tm_text) |
af58ef32 SC |
409 | { |
410 | struct tickmark *tm; | |
411 | double ty; | |
7175d91d | 412 | int i, power_of_ten; |
af58ef32 SC |
413 | static double dash[] = { 2.0, 2.0 }; |
414 | ||
7175d91d | 415 | nticks = calc_tickmarks(miny, maxy, nticks, &tm, &power_of_ten, |
d8fbeefb | 416 | g->y_axis_unit_change_callback == NULL, g->base_offset); |
7175d91d SC |
417 | if (g->y_axis_unit_change_callback) |
418 | g->y_axis_unit_change_callback(g, power_of_ten); | |
af58ef32 | 419 | |
d8fbeefb JA |
420 | /* |
421 | * Use highest tickmark as top of graph, not highest value. Otherwise | |
422 | * it's impossible to see what the max value is, if the graph is | |
423 | * fairly flat. | |
424 | */ | |
425 | maxy = tm[nticks - 1].value; | |
426 | ||
af58ef32 SC |
427 | for (i = 0; i < nticks; i++) { |
428 | ty = y2 - (((tm[i].value) - miny) / (maxy - miny)) * (y2 - y1); | |
d8fbeefb | 429 | |
b65c7ec4 JA |
430 | /* |
431 | * Update tick delta | |
432 | */ | |
433 | if (!i) { | |
434 | g->ytick_zero = ty; | |
435 | g->ytick_zero_val = tm[0].value; | |
b7a69ad8 | 436 | } else if (i == 1) { |
b65c7ec4 | 437 | g->ytick_delta = (tm[1].value - tm[0].value) / (ty - g->ytick_zero); |
b7a69ad8 JA |
438 | g->ytick_one_val = tm[1].value; |
439 | } | |
b65c7ec4 | 440 | |
d8fbeefb JA |
441 | /* really ty < y1 || ty > y2, but protect against rounding */ |
442 | if (y1 - ty > 0.01 || ty - y2 > 0.01) | |
af58ef32 | 443 | continue; |
d8fbeefb | 444 | |
af58ef32 SC |
445 | /* draw tick mark */ |
446 | cairo_move_to(cr, x1, ty); | |
447 | cairo_line_to(cr, x1 - (x2 - x1) * 0.02, ty); | |
448 | cairo_stroke(cr); | |
449 | ||
450 | /* draw grid lines */ | |
451 | cairo_save(cr); | |
452 | cairo_set_dash(cr, dash, 2, 2.0); | |
453 | cairo_set_line_width(cr, 0.5); | |
454 | cairo_move_to(cr, x1, ty); | |
455 | cairo_line_to(cr, x2, ty); | |
456 | cairo_stroke(cr); | |
457 | cairo_restore(cr); | |
458 | ||
d8fbeefb JA |
459 | if (!add_tm_text) |
460 | continue; | |
461 | ||
af58ef32 | 462 | /* draw tickmark label */ |
10e54cdc | 463 | draw_right_justified_text(g, cr, x1 - (x2 - x1) * 0.025, ty, 12.0, tm[i].string); |
af58ef32 SC |
464 | cairo_stroke(cr); |
465 | } | |
d8fbeefb JA |
466 | |
467 | /* | |
468 | * Return new max to use | |
469 | */ | |
470 | return maxy; | |
af58ef32 SC |
471 | } |
472 | ||
473 | void bar_graph_draw(struct graph *bg, cairo_t *cr) | |
474 | { | |
475 | double x1, y1, x2, y2; | |
476 | double space_per_label, bar_width; | |
477 | double label_offset, mindata, maxdata; | |
478 | int i, nlabels; | |
479 | struct graph_label *lb; | |
480 | ||
481 | cairo_save(cr); | |
57f9d28e | 482 | cairo_translate(cr, bg->xoffset, bg->yoffset); |
af58ef32 SC |
483 | graph_draw_common(bg, cr, &x1, &y1, &x2, &y2); |
484 | ||
485 | nlabels = count_labels(bg->labels); | |
d8fbeefb | 486 | space_per_label = (x2 - x1) / (double) nlabels; |
af58ef32 | 487 | |
bb39379f JA |
488 | /* |
489 | * Start bars at 0 unless we have negative values, otherwise we | |
490 | * present a skewed picture comparing label X and X+1. | |
491 | */ | |
af58ef32 | 492 | mindata = find_min_data(bg->labels); |
bb39379f JA |
493 | if (mindata > 0) |
494 | mindata = 0; | |
495 | ||
af58ef32 SC |
496 | maxdata = find_max_data(bg->labels); |
497 | ||
498 | if (fabs(maxdata - mindata) < 1e-20) { | |
f3e8440f | 499 | draw_centered_text(bg, cr, |
af58ef32 SC |
500 | x1 + (x2 - x1) / 2.0, |
501 | y1 + (y2 - y1) / 2.0, 20.0, "No good data"); | |
502 | return; | |
503 | } | |
504 | ||
bb39379f | 505 | maxdata = graph_draw_y_ticks(bg, cr, x1, y1, x2, y2, mindata, maxdata, 10, 1); |
af58ef32 SC |
506 | i = 0; |
507 | for (lb = bg->labels; lb; lb = lb->next) { | |
508 | int nvalues; | |
509 | nvalues = count_values(lb->values); | |
510 | bar_width = (space_per_label - space_per_label * 0.2) / (double) nvalues; | |
511 | label_offset = bg->xdim * 0.1 + space_per_label * (double) i + space_per_label * 0.1; | |
512 | draw_bars(bg, cr, lb, label_offset, bar_width, mindata, maxdata); | |
513 | // draw_centered_text(cr, label_offset + (bar_width / 2.0 + bar_width * 0.1), bg->ydim * 0.93, | |
f3e8440f | 514 | draw_centered_text(bg, cr, x1 + space_per_label * (i + 0.5), bg->ydim * 0.93, |
af58ef32 SC |
515 | 12.0, lb->label); |
516 | i++; | |
517 | } | |
518 | cairo_stroke(cr); | |
519 | cairo_restore(cr); | |
520 | } | |
521 | ||
522 | typedef double (*xy_value_extractor)(struct graph_value *v); | |
523 | ||
524 | static double getx(struct graph_value *v) | |
525 | { | |
526 | struct xyvalue *xy = v->value; | |
527 | return xy->x; | |
528 | } | |
529 | ||
530 | static double gety(struct graph_value *v) | |
531 | { | |
532 | struct xyvalue *xy = v->value; | |
533 | return xy->y; | |
534 | } | |
535 | ||
536 | static double find_xy_value(struct graph *g, xy_value_extractor getvalue, double_comparator cmp) | |
537 | { | |
10e54cdc | 538 | double tmp, answer = 0.0; |
af58ef32 SC |
539 | struct graph_label *i; |
540 | struct graph_value *j; | |
d582bf70 | 541 | int first = 1; |
af58ef32 SC |
542 | |
543 | for (i = g->labels; i; i = i->next) | |
544 | for (j = i->values; j; j = j->next) { | |
545 | tmp = getvalue(j); | |
d582bf70 SC |
546 | if (first) { |
547 | first = 0; | |
548 | answer = tmp; | |
549 | } | |
af58ef32 SC |
550 | answer = cmp(tmp, answer); |
551 | } | |
552 | return answer; | |
553 | } | |
554 | ||
555 | void line_graph_draw(struct graph *g, cairo_t *cr) | |
556 | { | |
557 | double x1, y1, x2, y2; | |
def0ac29 SC |
558 | double minx, miny, maxx, maxy, gminx, gminy, gmaxx, gmaxy; |
559 | double tx, ty, top_extra, bottom_extra, left_extra, right_extra; | |
af58ef32 SC |
560 | struct graph_label *i; |
561 | struct graph_value *j; | |
9ce9cfbd | 562 | int good_data = 1, first = 1; |
af58ef32 SC |
563 | |
564 | cairo_save(cr); | |
57f9d28e | 565 | cairo_translate(cr, g->xoffset, g->yoffset); |
af58ef32 SC |
566 | graph_draw_common(g, cr, &x1, &y1, &x2, &y2); |
567 | ||
568 | minx = find_xy_value(g, getx, mindouble); | |
569 | maxx = find_xy_value(g, getx, maxdouble); | |
570 | miny = find_xy_value(g, gety, mindouble); | |
5aec6680 JA |
571 | |
572 | /* | |
573 | * Start graphs at zero, unless we have a value below. Otherwise | |
574 | * it's hard to visually compare the read and write graph, since | |
575 | * the lowest valued one will be the floor of the graph view. | |
576 | */ | |
577 | if (miny > 0) | |
578 | miny = 0; | |
579 | ||
af58ef32 SC |
580 | maxy = find_xy_value(g, gety, maxdouble); |
581 | ||
582 | if (fabs(maxx - minx) < 1e-20 || fabs(maxy - miny) < 1e-20) { | |
9ce9cfbd SC |
583 | good_data = 0; |
584 | minx = 0.0; | |
585 | miny = 0.0; | |
586 | maxx = 10.0; | |
587 | maxy = 100.0; | |
af58ef32 SC |
588 | } |
589 | ||
def0ac29 SC |
590 | top_extra = 0.0; |
591 | bottom_extra = 0.0; | |
592 | left_extra = 0.0; | |
593 | right_extra = 0.0; | |
594 | ||
595 | if (g->top_extra > 0.001) | |
596 | top_extra = fabs(maxy - miny) * g->top_extra; | |
597 | if (g->bottom_extra > 0.001) | |
598 | bottom_extra = fabs(maxy - miny) * g->bottom_extra; | |
599 | if (g->left_extra > 0.001) | |
600 | left_extra = fabs(maxx - minx) * g->left_extra; | |
601 | if (g->right_extra > 0.001) | |
602 | right_extra = fabs(maxx - minx) * g->right_extra; | |
603 | ||
604 | gminx = minx - left_extra; | |
605 | gmaxx = maxx + right_extra; | |
606 | gminy = miny - bottom_extra; | |
607 | gmaxy = maxy + top_extra; | |
608 | ||
d8fbeefb JA |
609 | graph_draw_x_ticks(g, cr, x1, y1, x2, y2, gminx, gmaxx, 10, good_data); |
610 | gmaxy = graph_draw_y_ticks(g, cr, x1, y1, x2, y2, gminy, gmaxy, 10, good_data); | |
af58ef32 | 611 | |
9ce9cfbd SC |
612 | if (!good_data) |
613 | goto skip_data; | |
614 | ||
f3e8440f | 615 | cairo_set_line_width(cr, 1.5); |
af58ef32 SC |
616 | for (i = g->labels; i; i = i->next) { |
617 | first = 1; | |
cae08727 SC |
618 | if (i->r < 0) /* invisible data */ |
619 | continue; | |
b65c7ec4 | 620 | |
af58ef32 SC |
621 | cairo_set_source_rgb(cr, i->r, i->g, i->b); |
622 | for (j = i->values; j; j = j->next) { | |
def0ac29 SC |
623 | tx = ((getx(j) - gminx) / (gmaxx - gminx)) * (x2 - x1) + x1; |
624 | ty = y2 - ((gety(j) - gminy) / (gmaxy - gminy)) * (y2 - y1); | |
af58ef32 SC |
625 | if (first) { |
626 | cairo_move_to(cr, tx, ty); | |
627 | first = 0; | |
628 | } else { | |
629 | cairo_line_to(cr, tx, ty); | |
630 | } | |
631 | } | |
632 | cairo_stroke(cr); | |
633 | } | |
9ce9cfbd SC |
634 | |
635 | skip_data: | |
af58ef32 SC |
636 | cairo_restore(cr); |
637 | } | |
638 | ||
af58ef32 SC |
639 | static void setstring(char **str, const char *value) |
640 | { | |
b65c7ec4 | 641 | free(*str); |
af58ef32 SC |
642 | *str = strdup(value); |
643 | } | |
644 | ||
645 | void graph_title(struct graph *bg, const char *title) | |
646 | { | |
647 | setstring(&bg->title, title); | |
648 | } | |
649 | ||
650 | void graph_x_title(struct graph *bg, const char *title) | |
651 | { | |
652 | setstring(&bg->xtitle, title); | |
653 | } | |
654 | ||
655 | void graph_y_title(struct graph *bg, const char *title) | |
656 | { | |
657 | setstring(&bg->ytitle, title); | |
658 | } | |
659 | ||
660 | static struct graph_label *graph_find_label(struct graph *bg, | |
661 | const char *label) | |
662 | { | |
663 | struct graph_label *i; | |
664 | ||
665 | for (i = bg->labels; i; i = i->next) | |
666 | if (strcmp(label, i->label) == 0) | |
667 | return i; | |
668 | return NULL; | |
669 | } | |
670 | ||
671 | void graph_add_label(struct graph *bg, const char *label) | |
672 | { | |
673 | struct graph_label *i; | |
674 | ||
675 | i = graph_find_label(bg, label); | |
676 | if (i) | |
677 | return; /* already present. */ | |
678 | i = calloc(1, sizeof(*i)); | |
679 | i->parent = bg; | |
680 | setstring(&i->label, label); | |
681 | i->next = NULL; | |
682 | if (!bg->tail) | |
683 | bg->labels = i; | |
684 | else | |
685 | bg->tail->next = i; | |
686 | bg->tail = i; | |
b65c7ec4 | 687 | INIT_PRIO_TREE_ROOT(&i->prio_tree); |
af58ef32 SC |
688 | } |
689 | ||
b7a69ad8 JA |
690 | static void graph_label_add_value(struct graph_label *i, void *value, |
691 | const char *tooltip) | |
af58ef32 | 692 | { |
b7a69ad8 | 693 | struct graph *g = i->parent; |
af58ef32 SC |
694 | struct graph_value *x; |
695 | ||
696 | x = malloc(sizeof(*x)); | |
3ca30e63 | 697 | memset(x, 0, sizeof(*x)); |
af58ef32 SC |
698 | x->value = value; |
699 | x->next = NULL; | |
f00e43f2 | 700 | if (!i->tail) |
af58ef32 | 701 | i->values = x; |
f00e43f2 | 702 | else |
af58ef32 | 703 | i->tail->next = x; |
af58ef32 SC |
704 | i->tail = x; |
705 | i->value_count++; | |
b65c7ec4 | 706 | |
f00e43f2 | 707 | if (tooltip) { |
b7a69ad8 JA |
708 | double xval = getx(x); |
709 | double minx = xval - (g->xtick_one_val * TOOLTIP_DELTA); | |
710 | double maxx = xval + (g->xtick_one_val * TOOLTIP_DELTA); | |
4c0cd537 | 711 | struct prio_tree_node *ret; |
b65c7ec4 | 712 | |
5fb8d791 JA |
713 | /* |
714 | * use msec to avoid dropping too much precision when | |
715 | * storing as an integer. | |
716 | */ | |
b7a69ad8 JA |
717 | minx = minx * 1000.0; |
718 | maxx = maxx * 1000.0; | |
719 | ||
3ca30e63 | 720 | INIT_PRIO_TREE_NODE(&x->node); |
b7a69ad8 JA |
721 | x->node.start = minx; |
722 | x->node.last = maxx; | |
b5526827 | 723 | if (x->node.last == x->node.start) { |
b7a69ad8 JA |
724 | x->node.last += fabs(g->xtick_delta); |
725 | if (x->node.last == x->node.start) | |
726 | x->node.last++; | |
b5526827 | 727 | } |
b65c7ec4 | 728 | |
4c0cd537 JA |
729 | /* |
730 | * If ret != &x->node, we have an alias. Since the values | |
731 | * should be identical, we can drop it | |
732 | */ | |
733 | ret = prio_tree_insert(&i->prio_tree, &x->node); | |
f00e43f2 JA |
734 | if (ret == &x->node) { |
735 | x->tooltip = strdup(tooltip); | |
4c0cd537 | 736 | i->tooltip_count++; |
f00e43f2 | 737 | } |
b65c7ec4 | 738 | } |
af58ef32 | 739 | |
b7a69ad8 JA |
740 | if (g->per_label_limit != -1 && |
741 | i->value_count > g->per_label_limit) { | |
c148daed JA |
742 | int to_drop = 1; |
743 | ||
744 | /* | |
745 | * If the limit was dynamically reduced, making us more | |
746 | * than 1 entry ahead after adding this one, drop two | |
747 | * entries. This will make us (eventually) reach the | |
748 | * specified limit. | |
749 | */ | |
b7a69ad8 | 750 | if (i->value_count - g->per_label_limit >= 2) |
c148daed JA |
751 | to_drop = 2; |
752 | ||
753 | while (to_drop--) { | |
754 | x = i->values; | |
755 | i->values = i->values->next; | |
93e2db2b JA |
756 | if (x->tooltip) { |
757 | free(x->tooltip); | |
b65c7ec4 | 758 | prio_tree_remove(&i->prio_tree, &x->node); |
93e2db2b JA |
759 | i->tooltip_count--; |
760 | } | |
c148daed JA |
761 | free(x->value); |
762 | free(x); | |
763 | i->value_count--; | |
764 | } | |
af58ef32 SC |
765 | } |
766 | } | |
767 | ||
768 | int graph_add_data(struct graph *bg, const char *label, const double value) | |
769 | { | |
770 | struct graph_label *i; | |
771 | double *d; | |
772 | ||
773 | d = malloc(sizeof(*d)); | |
774 | *d = value; | |
775 | ||
776 | i = graph_find_label(bg, label); | |
777 | if (!i) | |
778 | return -1; | |
b7a69ad8 | 779 | graph_label_add_value(i, d, NULL); |
af58ef32 SC |
780 | return 0; |
781 | } | |
782 | ||
783 | int graph_add_xy_data(struct graph *bg, const char *label, | |
93e2db2b | 784 | const double x, const double y, const char *tooltip) |
af58ef32 SC |
785 | { |
786 | struct graph_label *i; | |
787 | struct xyvalue *xy; | |
788 | ||
789 | xy = malloc(sizeof(*xy)); | |
790 | xy->x = x; | |
791 | xy->y = y; | |
792 | ||
793 | i = graph_find_label(bg, label); | |
794 | if (!i) | |
795 | return -1; | |
93e2db2b | 796 | |
b7a69ad8 | 797 | graph_label_add_value(i, xy, tooltip); |
af58ef32 SC |
798 | return 0; |
799 | } | |
800 | ||
3ca30e63 | 801 | static void graph_free_values(struct graph_label *l, struct graph_value *values) |
af58ef32 SC |
802 | { |
803 | struct graph_value *i, *next; | |
804 | ||
805 | for (i = values; i; i = next) { | |
806 | next = i->next; | |
b65c7ec4 | 807 | free(i->value); |
3ca30e63 JA |
808 | if (i->tooltip) { |
809 | free(i->tooltip); | |
810 | prio_tree_remove(&l->prio_tree, &i->node); | |
811 | l->tooltip_count--; | |
812 | } | |
b65c7ec4 | 813 | free(i); |
af58ef32 SC |
814 | } |
815 | } | |
816 | ||
817 | static void graph_free_labels(struct graph_label *labels) | |
818 | { | |
819 | struct graph_label *i, *next; | |
820 | ||
821 | for (i = labels; i; i = next) { | |
822 | next = i->next; | |
3ca30e63 | 823 | graph_free_values(i, i->values); |
b65c7ec4 | 824 | free(i); |
af58ef32 SC |
825 | } |
826 | } | |
827 | ||
828 | void graph_set_color(struct graph *gr, const char *label, | |
829 | double red, double green, double blue) | |
830 | { | |
831 | struct graph_label *i; | |
832 | double r, g, b; | |
833 | ||
cae08727 SC |
834 | if (red < 0.0) { /* invisible color */ |
835 | r = -1.0; | |
836 | g = -1.0; | |
837 | b = -1.0; | |
838 | } else { | |
839 | r = fabs(red); | |
840 | g = fabs(green); | |
841 | b = fabs(blue); | |
842 | ||
843 | if (r > 1.0) | |
844 | r = 1.0; | |
845 | if (g > 1.0) | |
846 | g = 1.0; | |
847 | if (b > 1.0) | |
b65c7ec4 | 848 | b = 1.0; |
cae08727 | 849 | } |
af58ef32 SC |
850 | |
851 | for (i = gr->labels; i; i = i->next) | |
852 | if (strcmp(i->label, label) == 0) { | |
853 | i->r = r; | |
854 | i->g = g; | |
855 | i->b = b; | |
856 | break; | |
857 | } | |
858 | } | |
859 | ||
860 | void graph_free(struct graph *bg) | |
861 | { | |
b65c7ec4 JA |
862 | free(bg->title); |
863 | free(bg->xtitle); | |
864 | free(bg->ytitle); | |
af58ef32 SC |
865 | graph_free_labels(bg->labels); |
866 | } | |
867 | ||
868 | /* For each line in the line graph, up to per_label_limit segments may | |
869 | * be added. After that, adding more data to the end of the line | |
870 | * causes data to drop off of the front of the line. | |
871 | */ | |
872 | void line_graph_set_data_count_limit(struct graph *g, int per_label_limit) | |
873 | { | |
874 | g->per_label_limit = per_label_limit; | |
875 | } | |
876 | ||
def0ac29 SC |
877 | void graph_add_extra_space(struct graph *g, double left_percent, double right_percent, |
878 | double top_percent, double bottom_percent) | |
879 | { | |
880 | g->left_extra = left_percent; | |
881 | g->right_extra = right_percent; | |
882 | g->top_extra = top_percent; | |
883 | g->bottom_extra = bottom_percent; | |
884 | } | |
885 | ||
d8fbeefb JA |
886 | /* |
887 | * Normally values are logged in a base unit of 0, but for other purposes | |
888 | * it makes more sense to log in higher unit. For instance for bandwidth | |
889 | * purposes, you may want to log in KB/sec (or MB/sec) rather than bytes/sec. | |
890 | */ | |
891 | void graph_set_base_offset(struct graph *g, unsigned int base_offset) | |
892 | { | |
893 | g->base_offset = base_offset; | |
894 | } | |
895 | ||
93e2db2b JA |
896 | int graph_has_tooltips(struct graph *g) |
897 | { | |
898 | struct graph_label *i; | |
899 | ||
900 | for (i = g->labels; i; i = i->next) | |
901 | if (i->tooltip_count) | |
902 | return 1; | |
903 | ||
904 | return 0; | |
905 | } | |
906 | ||
907 | int graph_contains_xy(struct graph *g, int x, int y) | |
908 | { | |
909 | int first_x = g->xoffset; | |
910 | int last_x = g->xoffset + g->xdim; | |
911 | int first_y = g->yoffset; | |
912 | int last_y = g->yoffset + g->ydim; | |
913 | ||
914 | return (x >= first_x && x <= last_x) && (y >= first_y && y <= last_y); | |
915 | } | |
def0ac29 | 916 | |
b65c7ec4 | 917 | const char *graph_find_tooltip(struct graph *g, int ix, int iy) |
93e2db2b | 918 | { |
b65c7ec4 JA |
919 | double x = ix, y = iy; |
920 | struct prio_tree_iter iter; | |
921 | struct prio_tree_node *n; | |
922 | struct graph_label *i; | |
923 | struct graph_value *best = NULL; | |
924 | double best_delta; | |
b7a69ad8 | 925 | double maxy, miny; |
93e2db2b | 926 | |
b65c7ec4 JA |
927 | x -= g->xoffset; |
928 | y -= g->yoffset; | |
93e2db2b | 929 | |
b65c7ec4 JA |
930 | x = g->xtick_zero_val + ((x - g->xtick_zero) * g->xtick_delta); |
931 | y = g->ytick_zero_val + ((y - g->ytick_zero) * g->ytick_delta); | |
93e2db2b | 932 | |
b7a69ad8 JA |
933 | x = x * 1000.0; |
934 | maxy = y + (g->ytick_one_val * TOOLTIP_DELTA); | |
935 | miny = y - (g->ytick_one_val * TOOLTIP_DELTA); | |
b65c7ec4 JA |
936 | best_delta = UINT_MAX; |
937 | i = g->labels; | |
938 | do { | |
08d7d5a8 | 939 | INIT_PRIO_TREE_ITER(&iter); |
b7a69ad8 | 940 | prio_tree_iter_init(&iter, &i->prio_tree, x, x); |
b65c7ec4 JA |
941 | |
942 | n = prio_tree_next(&iter); | |
943 | if (!n) | |
944 | continue; | |
945 | ||
946 | do { | |
947 | struct graph_value *v; | |
b7a69ad8 | 948 | double yval, ydiff; |
b65c7ec4 JA |
949 | |
950 | v = container_of(n, struct graph_value, node); | |
b7a69ad8 JA |
951 | yval = gety(v); |
952 | ydiff = fabs(yval - y); | |
0deba507 JA |
953 | |
954 | /* | |
b65c7ec4 | 955 | * zero delta, or within or match critera, break |
0deba507 | 956 | */ |
b7a69ad8 JA |
957 | if (ydiff < best_delta) { |
958 | best_delta = ydiff; | |
b65c7ec4 | 959 | if (!best_delta || |
b7a69ad8 | 960 | (yval >= miny && yval <= maxy)) { |
b65c7ec4 JA |
961 | best = v; |
962 | break; | |
963 | } | |
964 | } | |
965 | } while ((n = prio_tree_next(&iter)) != NULL); | |
966 | ||
967 | /* | |
968 | * If we got matches in one label, don't check others. | |
969 | */ | |
970 | break; | |
971 | } while ((i = i->next) != NULL); | |
972 | ||
973 | if (best) | |
974 | return best->tooltip; | |
93e2db2b JA |
975 | |
976 | return NULL; | |
977 | } |