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