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