graph: return opaque graph_label_t for each label added
[fio.git] / graph.c
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>
27 #include <stdlib.h>
28
29 #include <cairo.h>
30 #include <gtk/gtk.h>
31
32 #include "tickmarks.h"
33 #include "graph.h"
34 #include "flist.h"
35 #include "lib/prio_tree.h"
36
37 /*
38  * Allowable difference to show tooltip
39  */
40 #define TOOLTIP_DELTA   0.08
41
42 struct xyvalue {
43         double x, y;
44 };
45
46 enum {
47         GV_F_ON_PRIO    = 1,
48         GV_F_PRIO_SKIP  = 2,
49 };
50
51 struct graph_value {
52         struct flist_head list;
53         struct prio_tree_node node;
54         struct flist_head alias;
55         unsigned int flags;
56         char *tooltip;
57         void *value;
58 };
59
60 struct graph_label {
61         struct flist_head list;
62         char *label;
63         struct flist_head value_list;
64         struct prio_tree_root prio_tree;
65         double r, g, b;
66         int hide;
67         int value_count;
68         struct graph *parent;
69 };
70
71 struct tick_value {
72         unsigned int offset;
73         double value;
74 };
75
76 struct graph {
77         char *title;
78         char *xtitle;
79         char *ytitle;
80         unsigned int xdim, ydim;
81         double xoffset, yoffset;
82         struct flist_head label_list;
83         int per_label_limit;
84         const char *font;
85         graph_axis_unit_change_callback x_axis_unit_change_callback;
86         graph_axis_unit_change_callback y_axis_unit_change_callback;
87         unsigned int base_offset;
88         unsigned int dont_graph_all_zeroes;
89         double left_extra;      
90         double right_extra;     
91         double top_extra;       
92         double bottom_extra;    
93
94         double xtick_zero;
95         double xtick_delta;
96         double xtick_zero_val;
97         double xtick_one_val;
98         double ytick_zero;
99         double ytick_delta;
100         double ytick_zero_val;
101         double ytick_one_val;
102 };
103
104 void graph_set_size(struct graph *g, unsigned int xdim, unsigned int ydim)
105 {
106         g->xdim = xdim;
107         g->ydim = ydim;
108 }
109
110 void graph_set_position(struct graph *g, double xoffset, double yoffset)
111 {
112         g->xoffset = xoffset;
113         g->yoffset = yoffset;
114 }
115
116 struct graph *graph_new(unsigned int xdim, unsigned int ydim, const char *font)
117 {
118         struct graph *g;
119
120         g = calloc(1, sizeof(*g));
121         INIT_FLIST_HEAD(&g->label_list);
122         graph_set_size(g, xdim, ydim);
123         g->per_label_limit = -1;
124         g->font = font;
125         if (!g->font)
126                 g->font = "Sans";
127         return g;
128 }
129
130 void 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
135 void 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
140 static int count_labels(struct graph *g)
141 {
142         struct flist_head *entry;
143         int count = 0;
144
145         flist_for_each(entry, &g->label_list)
146                 count++;
147
148         return count;
149 }
150
151 static int count_values(struct graph_label *l)
152 {
153         struct flist_head *entry;
154         int count = 0;
155
156         flist_for_each(entry, &l->value_list)
157                 count++;
158
159         return count;
160 }
161
162 typedef double (*double_comparator)(double a, double b);
163
164 static double mindouble(double a, double b)
165 {
166         return a < b ? a : b;
167 }
168
169 static double maxdouble(double a, double b)
170 {
171         return a < b ? b : a;
172 }
173
174 static double find_double_values(struct graph_label *l, double_comparator cmp)
175 {
176         struct flist_head *entry;
177         double answer, tmp;
178         int first = 1;
179
180         if (flist_empty(&l->value_list))
181                 return 0.0;
182
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;
188                 if (first) {
189                         answer = tmp;
190                         first = 0;
191                 } else {
192                         answer = cmp(answer, tmp);
193                 }
194         }
195         return answer;
196 }
197
198 static double find_double_data(struct graph *g, double_comparator cmp)
199 {
200         struct flist_head *entry;
201         struct graph_label *i;
202         int first = 1;
203         double answer, tmp;
204
205         if (flist_empty(&g->label_list))
206                 return 0.0;
207
208         flist_for_each(entry, &g->label_list) {
209                 i = flist_entry(entry, struct graph_label, list);
210                 tmp = find_double_values(i, cmp);
211                 if (first) {
212                         answer = tmp;
213                         first = 0;
214                 } else {
215                         answer = cmp(tmp, answer);
216                 }
217         }
218         return answer;
219 }
220
221 static double find_min_data(struct graph *g)
222 {
223         return find_double_data(g, mindouble);
224 }
225
226 static double find_max_data(struct graph *g)
227 {
228         return find_double_data(g, maxdouble);
229 }
230
231 static 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 {
235         struct flist_head *entry;
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);
243         flist_for_each(entry, &lb->value_list) {
244                 struct graph_value *i;
245
246                 i = flist_entry(entry, struct graph_value, list);
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
264 static void draw_aligned_text(struct graph *g, cairo_t *cr, double x, double y,
265                                double fontsize, const char *text, int alignment)
266 {
267 #define CENTERED 0
268 #define LEFT_JUSTIFIED 1
269 #define RIGHT_JUSTIFIED 2
270
271         double factor, direction;
272         cairo_text_extents_t extents;
273
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         }
289         cairo_select_font_face (cr, g->font, CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
290
291         cairo_set_font_size(cr, fontsize);
292         cairo_text_extents(cr, text, &extents);
293         x = x + direction * (factor * extents.width  + extents.x_bearing);
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
300 static 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
306 static 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
313 static 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
320 static void draw_vertical_centered_text(struct graph *g, cairo_t *cr, double x,
321                                         double y, double fontsize,
322                                         const char *text)
323 {
324         double sx, sy;
325         cairo_text_extents_t extents;
326
327         cairo_select_font_face(cr, g->font, CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
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
345 static void graph_draw_common(struct graph *g, cairo_t *cr,
346         double *x1, double *y1, double *x2, double *y2)
347 {
348         cairo_set_source_rgb(cr, 0, 0, 0);
349         cairo_set_line_width (cr, 0.8);
350
351         *x1 = 0.10 * g->xdim;   
352         *x2 = 0.95 * g->xdim;
353         *y1 = 0.10 * g->ydim;   
354         *y2 = 0.90 * g->ydim;
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
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);
366         cairo_stroke(cr);
367 }
368
369 static void graph_draw_x_ticks(struct graph *g, cairo_t *cr,
370         double x1, double y1, double x2, double y2,
371         double minx, double maxx, int nticks, int add_tm_text)
372 {
373         struct tickmark *tm;
374         double tx;
375         int i, power_of_ten;
376         static double dash[] = { 1.0, 2.0 };
377
378         nticks = calc_tickmarks(minx, maxx, nticks, &tm, &power_of_ten,
379                 g->x_axis_unit_change_callback == NULL, g->base_offset);
380         if (g->x_axis_unit_change_callback)
381                 g->x_axis_unit_change_callback(g, power_of_ten);
382
383         for (i = 0; i < nticks; i++) {
384                 tx = (((tm[i].value) - minx) / (maxx - minx)) * (x2 - x1) + x1;
385
386                 /*
387                  * Update tick delta
388                  */
389                 if (!i) {
390                         g->xtick_zero = tx;
391                         g->xtick_zero_val = tm[0].value;
392                 } else if (i == 1) {
393                         g->xtick_delta = (tm[1].value - tm[0].value) / (tx - g->xtick_zero);
394                         g->xtick_one_val = tm[1].value;
395                 }
396
397                 /* really tx < yx || tx > x2, but protect against rounding */
398                 if (x1 - tx > 0.01 || tx - x2 > 0.01)
399                         continue;
400
401                 /* Draw tick mark */
402                 cairo_set_line_width(cr, 0.8);
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
416                 if (!add_tm_text)
417                         continue;
418
419                 /* draw tickmark label */
420                 draw_centered_text(g, cr, tx, y2 * 1.04, 12.0, tm[i].string);
421                 cairo_stroke(cr);
422         }
423 }
424
425 static double graph_draw_y_ticks(struct graph *g, cairo_t *cr,
426         double x1, double y1, double x2, double y2,
427         double miny, double maxy, int nticks, int add_tm_text)
428 {
429         struct tickmark *tm;
430         double ty;
431         int i, power_of_ten;
432         static double dash[] = { 2.0, 2.0 };
433
434         nticks = calc_tickmarks(miny, maxy, nticks, &tm, &power_of_ten,
435                 g->y_axis_unit_change_callback == NULL, g->base_offset);
436         if (g->y_axis_unit_change_callback)
437                 g->y_axis_unit_change_callback(g, power_of_ten);
438
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
446         for (i = 0; i < nticks; i++) {
447                 ty = y2 - (((tm[i].value) - miny) / (maxy - miny)) * (y2 - y1);
448
449                 /*
450                  * Update tick delta
451                  */
452                 if (!i) {
453                         g->ytick_zero = ty;
454                         g->ytick_zero_val = tm[0].value;
455                 } else if (i == 1) {
456                         g->ytick_delta = (tm[1].value - tm[0].value) / (ty - g->ytick_zero);
457                         g->ytick_one_val = tm[1].value;
458                 }
459
460                 /* really ty < y1 || ty > y2, but protect against rounding */
461                 if (y1 - ty > 0.01 || ty - y2 > 0.01)
462                         continue;
463
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
478                 if (!add_tm_text)
479                         continue;
480
481                 /* draw tickmark label */
482                 draw_right_justified_text(g, cr, x1 - (x2 - x1) * 0.025, ty, 12.0, tm[i].string);
483                 cairo_stroke(cr);
484         }
485
486         /*
487          * Return new max to use
488          */
489         return maxy;
490 }
491
492 void 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;
499         struct flist_head *entry;
500
501         cairo_save(cr);
502         cairo_translate(cr, bg->xoffset, bg->yoffset);
503         graph_draw_common(bg, cr, &x1, &y1, &x2, &y2);
504
505         nlabels = count_labels(bg);
506         space_per_label = (x2 - x1) / (double) nlabels;
507
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          */
512         mindata = find_min_data(bg);
513         if (mindata > 0)
514                 mindata = 0;
515
516         maxdata = find_max_data(bg);
517
518         if (fabs(maxdata - mindata) < 1e-20) {
519                 draw_centered_text(bg, cr,
520                         x1 + (x2 - x1) / 2.0,
521                         y1 + (y2 - y1) / 2.0, 20.0, "No good data");
522                 return;
523         }
524
525         maxdata = graph_draw_y_ticks(bg, cr, x1, y1, x2, y2, mindata, maxdata, 10, 1);
526         i = 0;
527         flist_for_each(entry, &bg->label_list) {
528                 int nvalues;
529
530                 lb = flist_entry(entry, struct graph_label, list);
531                 nvalues = count_values(lb);
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,
536                 draw_centered_text(bg, cr, x1 + space_per_label * (i + 0.5), bg->ydim * 0.93,
537                         12.0, lb->label); 
538                 i++;
539         }
540         cairo_stroke(cr);
541         cairo_restore(cr);
542 }
543
544 typedef double (*xy_value_extractor)(struct graph_value *v);
545
546 static double getx(struct graph_value *v)
547 {
548         struct xyvalue *xy = v->value;
549         return xy->x;
550 }
551
552 static double gety(struct graph_value *v)
553 {
554         struct xyvalue *xy = v->value;
555         return xy->y;
556 }
557
558 static double find_xy_value(struct graph *g, xy_value_extractor getvalue, double_comparator cmp)
559 {
560         double tmp, answer = 0.0;
561         struct graph_label *i;
562         struct graph_value *j;
563         struct flist_head *jentry, *entry;
564         int first = 1;
565
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);
571                         tmp = getvalue(j);
572                         if (first) {
573                                 first = 0;
574                                 answer = tmp;
575                         }
576                         answer = cmp(tmp, answer);      
577                 }
578         }
579
580         return answer;
581 }
582
583 void line_graph_draw(struct graph *g, cairo_t *cr)
584 {
585         double x1, y1, x2, y2;
586         double minx, miny, maxx, maxy, gminx, gminy, gmaxx, gmaxy;
587         double tx, ty, top_extra, bottom_extra, left_extra, right_extra;
588         struct graph_label *i;
589         struct graph_value *j;
590         int good_data = 1, first = 1;
591         struct flist_head *entry, *lentry;
592
593         cairo_save(cr);
594         cairo_translate(cr, g->xoffset, g->yoffset);
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);
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
609         maxy = find_xy_value(g, gety, maxdouble);
610
611         if (fabs(maxx - minx) < 1e-20 || fabs(maxy - miny) < 1e-20) {
612                 good_data = 0;
613                 minx = 0.0;
614                 miny = 0.0;
615                 maxx = 10.0;
616                 maxy = 100.0;
617         }
618
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
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);
640
641         if (!good_data)
642                 goto skip_data;
643
644         cairo_set_line_width(cr, 1.5);
645         flist_for_each(lentry, &g->label_list) {
646                 i = flist_entry(lentry, struct graph_label, list);
647                 first = 1;
648                 if (i->hide || i->r < 0) /* invisible data */
649                         continue;
650
651                 cairo_set_source_rgb(cr, i->r, i->g, i->b);
652                 flist_for_each(entry, &i->value_list) {
653                         j = flist_entry(entry, struct graph_value, list);
654                         tx = ((getx(j) - gminx) / (gmaxx - gminx)) * (x2 - x1) + x1;
655                         ty = y2 - ((gety(j) - gminy) / (gmaxy - gminy)) * (y2 - y1);
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         }
665
666 skip_data:
667         cairo_restore(cr);
668 }
669
670 static void setstring(char **str, const char *value)
671 {
672         free(*str);
673         *str = strdup(value);
674 }
675
676 void graph_title(struct graph *bg, const char *title)
677 {
678         setstring(&bg->title, title);
679 }
680
681 void graph_x_title(struct graph *bg, const char *title)
682 {
683         setstring(&bg->xtitle, title);
684 }
685
686 void graph_y_title(struct graph *bg, const char *title)
687 {
688         setstring(&bg->ytitle, title);
689 }
690
691 static struct graph_label *graph_find_label(struct graph *bg,
692                                 const char *label)
693 {
694         struct flist_head *entry;
695         struct graph_label *i;
696         
697         flist_for_each(entry, &bg->label_list) {
698                 i = flist_entry(entry, struct graph_label, list);
699
700                 if (strcmp(label, i->label) == 0)
701                         return i;
702         }
703
704         return NULL;
705 }
706
707 graph_label_t graph_add_label(struct graph *bg, const char *label)
708 {
709         struct graph_label *i;
710         
711         i = graph_find_label(bg, label);
712         if (i)
713                 return i; /* already present. */
714         i = calloc(1, sizeof(*i));
715         INIT_FLIST_HEAD(&i->value_list);
716         i->parent = bg;
717         setstring(&i->label, label);
718         flist_add_tail(&i->list, &bg->label_list);
719         INIT_PRIO_TREE_ROOT(&i->prio_tree);
720         return i;
721 }
722
723 static void __graph_value_drop(struct graph_label *l, struct graph_value *v)
724 {
725         flist_del_init(&v->list);
726         if (v->tooltip)
727                 free(v->tooltip);
728         free(v->value);
729         free(v);
730         l->value_count--;
731 }
732
733 static void graph_value_drop(struct graph_label *l, struct graph_value *v)
734 {
735         if (v->flags & GV_F_PRIO_SKIP) {
736                 __graph_value_drop(l, v);
737                 return;
738         }
739
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          */
753         while (!flist_empty(&v->alias)) {
754                 struct graph_value *a;
755
756                 a = flist_entry(v->alias.next, struct graph_value, alias);
757                 flist_del_init(&a->alias);
758
759                 __graph_value_drop(l, a);
760         }
761
762         __graph_value_drop(l, v);
763 }
764
765 static void graph_label_add_value(struct graph_label *i, void *value,
766                                   const char *tooltip)
767 {
768         struct graph *g = i->parent;
769         struct graph_value *x;
770
771         x = malloc(sizeof(*x));
772         memset(x, 0, sizeof(*x));
773         INIT_FLIST_HEAD(&x->alias);
774         INIT_FLIST_HEAD(&x->list);
775         flist_add_tail(&x->list, &i->value_list);
776         i->value_count++;
777         x->value = value;
778
779         if (tooltip) {
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);
783                 struct prio_tree_node *ret;
784
785                 /*
786                  * use msec to avoid dropping too much precision when
787                  * storing as an integer.
788                  */
789                 minx = minx * 1000.0;
790                 maxx = maxx * 1000.0;
791
792                 INIT_PRIO_TREE_NODE(&x->node);
793                 x->node.start = minx;
794                 x->node.last = maxx;
795                 x->tooltip = strdup(tooltip);
796                 if (x->node.last == x->node.start) {
797                         x->node.last += fabs(g->xtick_delta);
798                         if (x->node.last == x->node.start)
799                                 x->node.last++;
800                 }
801
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);
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);
812                 } else
813                         x->flags = GV_F_ON_PRIO;
814         } else
815                 x->flags = GV_F_PRIO_SKIP;
816
817         if (g->per_label_limit != -1 &&
818                 i->value_count > g->per_label_limit) {
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                  */
827                 if (i->value_count - g->per_label_limit >= 2)
828                         to_drop = 2;
829
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;
839                 }
840         }
841 }
842
843 int graph_add_data(struct graph *bg, graph_label_t label, const double value)
844 {
845         struct graph_label *i = label;
846         double *d;
847
848         d = malloc(sizeof(*d));
849         *d = value;
850
851         graph_label_add_value(i, d, NULL);
852         return 0;
853 }
854
855 static 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
870 int graph_add_xy_data(struct graph *bg, graph_label_t label,
871                       const double x, const double y, const char *tooltip)
872 {
873         struct graph_label *i = label;
874         struct xyvalue *xy;
875
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
885         graph_label_add_value(i, xy, tooltip);
886         return 0;
887 }
888
889 static void graph_free_values(struct graph_label *l)
890 {
891         struct graph_value *i;
892
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);
896         }       
897 }
898
899 static void graph_free_labels(struct graph *g)
900 {
901         struct graph_label *i;
902
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);
907                 free(i);
908         }       
909 }
910
911 void graph_set_color(struct graph *gr, graph_label_t label, double red,
912                      double green, double blue)
913 {
914         struct graph_label *i = label;
915         double r, g, b;
916
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)
931                         b = 1.0;
932         }
933
934         i->r = r;
935         i->g = g;
936         i->b = b;
937 }
938
939 void graph_free(struct graph *bg)
940 {
941         free(bg->title);
942         free(bg->xtitle);
943         free(bg->ytitle);
944         graph_free_labels(bg);
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  */
951 void line_graph_set_data_count_limit(struct graph *g, int per_label_limit)
952 {
953         g->per_label_limit = per_label_limit;
954 }
955
956 void 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
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  */
970 void graph_set_base_offset(struct graph *g, unsigned int base_offset)
971 {
972         g->base_offset = base_offset;
973 }
974
975 int graph_has_tooltips(struct graph *g)
976 {
977         struct flist_head *entry;
978         struct graph_label *i;
979
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))
984                         return 1;
985         }
986
987         return 0;
988 }
989
990 int 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 }
999
1000 const char *graph_find_tooltip(struct graph *g, int ix, int iy)
1001 {
1002         double x = ix, y = iy;
1003         struct prio_tree_iter iter;
1004         struct prio_tree_node *n;
1005         struct graph_value *best = NULL;
1006         struct flist_head *entry;
1007         double best_delta;
1008         double maxy, miny;
1009
1010         x -= g->xoffset;
1011         y -= g->yoffset;
1012
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);
1015
1016         x = x * 1000.0;
1017         maxy = y + (g->ytick_one_val * TOOLTIP_DELTA);
1018         miny = y - (g->ytick_one_val * TOOLTIP_DELTA);
1019         best_delta = UINT_MAX;
1020         flist_for_each(entry, &g->label_list) {
1021                 struct graph_label *i;
1022
1023                 i = flist_entry(entry, struct graph_label, list);
1024                 if (i->hide)
1025                         continue;
1026
1027                 INIT_PRIO_TREE_ITER(&iter);
1028                 prio_tree_iter_init(&iter, &i->prio_tree, x, x);
1029
1030                 n = prio_tree_next(&iter);
1031                 if (!n)
1032                         continue;
1033
1034                 do {
1035                         struct graph_value *v, *rootv;
1036                         double yval, ydiff;
1037
1038                         v = container_of(n, struct graph_value, node);
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                                         }
1054                                 }
1055                                 if (!flist_empty(&v->alias))
1056                                         v = flist_entry(v->alias.next, struct graph_value, alias);
1057                         } while (v != rootv);
1058                 } while ((n = prio_tree_next(&iter)) != NULL);
1059
1060                 /*
1061                  * If we got matches in one label, don't check others.
1062                  */
1063                 if (best)
1064                         break;
1065         }
1066
1067         if (best)
1068                 return best->tooltip;
1069
1070         return NULL;
1071 }
1072
1073 void graph_set_graph_all_zeroes(struct graph *g, unsigned int set)
1074 {
1075         g->dont_graph_all_zeroes = !set;
1076 }