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