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