gfio: Add mini library to draw bar graphs and line graphs
[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
28 #include <cairo.h>
29 #include <gtk/gtk.h>
30
31 #include "tickmarks.h"
32
33 struct xyvalue {
34         double x, y;
35 };
36
37 struct graph_value {
38         struct graph_value *next;
39         void *value;
40 };
41
42 struct graph_label {
43         char *label;
44         struct graph_value *tail;
45         struct graph_value *values;
46         struct graph_label *next;
47         double r, g, b;
48         int value_count;
49         struct graph *parent;
50 };
51
52 struct graph {
53         char *title;
54         char *xtitle;
55         char *ytitle;
56         int xdim, ydim;
57         struct graph_label *labels;
58         struct graph_label *tail;
59         int per_label_limit;
60 };
61
62 struct graph *graph_new(int xdim, int ydim)
63 {
64         struct graph *g;
65
66         g = calloc(1, sizeof(*g));
67         g->xdim = xdim;
68         g->ydim = ydim;
69         g->per_label_limit = -1;
70         return g;
71 }
72
73 static int count_labels(struct graph_label *labels)
74 {
75         int count = 0;
76         struct graph_label *i;
77
78         for (i = labels; i; i = i->next)
79                 count++;
80         return count;
81 }
82
83 static int count_values(struct graph_value *values)
84 {
85         int count = 0;
86         struct graph_value *i;
87
88         for (i = values; i; i = i->next)
89                 count++;
90         return count;
91 }
92
93 typedef double (*double_comparator)(double a, double b);
94
95 static double mindouble(double a, double b)
96 {
97         return a < b ? a : b;
98 }
99
100 static double maxdouble(double a, double b)
101 {
102         return a < b ? b : a;
103 }
104
105 static double find_double_values(struct graph_value *values, double_comparator cmp)
106 {
107         struct graph_value *i;
108         int first = 1;
109         double answer, tmp;
110
111         assert(values != NULL);
112         answer = 0.0; /* shut the compiler up, might need to think harder though. */
113         for (i = values; i; i = i->next) {
114                 tmp = *(double *) i->value; 
115                 if (first) {
116                         answer = tmp;
117                         first = 0;
118                 } else {
119                         answer = cmp(answer, tmp);
120                 }
121         }
122         return answer;
123 }
124
125 static double find_double_data(struct graph_label *labels, double_comparator cmp)
126 {
127         struct graph_label *i;
128         int first = 1;
129         double answer, tmp;
130
131         assert(labels != NULL);
132         answer = 0.0; /* shut the compiler up, might need to think harder though. */
133         for (i = labels; i; i = i->next) {
134                 tmp = find_double_values(i->values, cmp);
135                 if (first) {
136                         answer = tmp;
137                         first = 0;
138                 } else {
139                         answer = cmp(tmp, answer);
140                 }
141         }
142         return answer;
143 }
144
145 static double find_min_data(struct graph_label *labels)
146 {
147         return find_double_data(labels, mindouble);
148 }
149
150 static double find_max_data(struct graph_label *labels)
151 {
152         return find_double_data(labels, maxdouble);
153 }
154
155 static void draw_bars(struct graph *bg, cairo_t *cr, struct graph_label *lb,
156                         double label_offset, double bar_width,
157                         double mindata, double maxdata)
158 {
159         struct graph_value *i;
160         double x1, y1, x2, y2;
161         int bar_num = 0;
162         double domain, range, v;
163
164         domain = (maxdata - mindata);
165         range = (double) bg->ydim * 0.80; /* FIXME */
166         cairo_stroke(cr);
167         for (i = lb->values; i; i = i->next) {
168
169                 x1 = label_offset + (double) bar_num * bar_width + (bar_width * 0.05);
170                 x2 = x1 + bar_width * 0.90;
171                 y2 = bg->ydim * 0.90;
172                 v = *(double *) i->value;
173                 y1 = y2 - (((v - mindata) / domain) * range);
174                 cairo_move_to(cr, x1, y1);
175                 cairo_line_to(cr, x1, y2);
176                 cairo_line_to(cr, x2, y2);
177                 cairo_line_to(cr, x2, y1);
178                 cairo_close_path(cr);
179                 cairo_fill(cr);
180                 cairo_stroke(cr);
181                 bar_num++;      
182         }
183 }
184
185 static void draw_centered_text(cairo_t *cr, double x, double y,
186                 double fontsize, const char *text)
187 {
188         cairo_text_extents_t extents;
189
190         cairo_select_font_face (cr, "Sans",
191                 CAIRO_FONT_SLANT_NORMAL,
192                 CAIRO_FONT_WEIGHT_NORMAL);
193
194         cairo_set_font_size(cr, fontsize);
195         cairo_text_extents(cr, text, &extents);
196         x = x - (extents.width / 2 + extents.x_bearing);
197         y = y - (extents.height / 2 + extents.y_bearing);
198
199         cairo_move_to(cr, x, y);
200         cairo_show_text(cr, text);
201 }
202
203 static void draw_vertical_centered_text(cairo_t *cr, double x, double y,
204                 double fontsize, const char *text)
205 {
206         double sx, sy;
207         cairo_text_extents_t extents;
208
209         cairo_select_font_face (cr, "Sans",
210                 CAIRO_FONT_SLANT_NORMAL,
211                 CAIRO_FONT_WEIGHT_NORMAL);
212
213         cairo_set_font_size(cr, fontsize);
214         cairo_text_extents(cr, text, &extents);
215         sx = x;
216         sy = y;
217         y = y + (extents.width / 2.0 + extents.x_bearing);
218         x = x - (extents.height / 2.0 + extents.y_bearing);
219
220         cairo_move_to(cr, x, y);
221         cairo_save(cr);
222         cairo_translate(cr, -sx, -sy);
223         cairo_rotate(cr, -90.0 * M_PI / 180.0);
224         cairo_translate(cr, sx, sy);
225         cairo_show_text(cr, text);
226         cairo_restore(cr);
227 }
228
229 static void graph_draw_common(struct graph *g, cairo_t *cr,
230         double *x1, double *y1, double *x2, double *y2)
231 {
232         cairo_set_source_rgb(cr, 0, 0, 0);
233         cairo_set_line_width (cr, 1.0);
234
235         cairo_move_to(cr, 0, 0);
236         cairo_line_to(cr, 0, g->ydim);
237         cairo_line_to(cr, g->xdim, g->ydim);
238         cairo_line_to(cr, g->xdim, 0);
239         cairo_line_to(cr, 0, 0);
240
241         /* for now just set margins at 10% of width.  This is not very good. */
242         *x1 = g->xdim / 10.0;   
243         *x2 = 9.0 * *x1; 
244         *y1 = g->ydim / 10.0;   
245         *y2 = 9.0 * *y1; 
246
247         cairo_move_to(cr, *x1, *y1);
248         cairo_line_to(cr, *x1, *y2);
249         cairo_line_to(cr, *x2, *y2);
250         cairo_line_to(cr, *x2, *y1);
251         cairo_line_to(cr, *x1, *y1);
252         cairo_stroke(cr);
253
254         draw_centered_text(cr, g->xdim / 2, g->ydim / 20, 20.0, g->title);
255         draw_centered_text(cr, g->xdim / 2, g->ydim * 0.97, 14.0, g->xtitle);
256         draw_vertical_centered_text(cr, g->xdim * 0.02, g->ydim / 2, 14.0, g->ytitle);
257         cairo_stroke(cr);
258 }
259
260 static void graph_draw_x_ticks(struct graph *g, cairo_t *cr,
261         double x1, double y1, double x2, double y2,
262         double minx, double maxx, int nticks)
263 {
264         struct tickmark *tm;
265         double tx;
266         int i;
267         static double dash[] = { 1.0, 2.0 };
268
269         nticks = calc_tickmarks(minx, maxx, nticks, &tm);
270
271         for (i = 0; i < nticks; i++) {
272                 tx = (((tm[i].value) - minx) / (maxx - minx)) * (x2 - x1) + x1;
273                 if (tx < x1 || tx > x2)
274                         continue;
275
276                 /* Draw tick mark */
277                 cairo_set_line_width(cr, 1.0);
278                 cairo_move_to(cr, tx, y2);
279                 cairo_line_to(cr, tx, y2 + (y2 - y1) * 0.03);
280                 cairo_stroke(cr);
281
282                 /* draw grid lines */
283                 cairo_save(cr);
284                 cairo_set_dash(cr, dash, 2, 2.0);
285                 cairo_set_line_width(cr, 0.5);
286                 cairo_move_to(cr, tx, y1);
287                 cairo_line_to(cr, tx, y2);
288                 cairo_stroke(cr);
289                 cairo_restore(cr);
290
291                 /* draw tickmark label */
292                 draw_centered_text(cr, tx, y2 * 1.04, 12.0, tm[i].string);
293                 cairo_stroke(cr);
294                 
295         }
296 }
297
298 static void graph_draw_y_ticks(struct graph *g, cairo_t *cr,
299         double x1, double y1, double x2, double y2,
300         double miny, double maxy, int nticks)
301 {
302         struct tickmark *tm;
303         double ty;
304         int i;
305         static double dash[] = { 2.0, 2.0 };
306
307         nticks = calc_tickmarks(miny, maxy, nticks, &tm);
308
309         for (i = 0; i < nticks; i++) {
310                 ty = y2 - (((tm[i].value) - miny) / (maxy - miny)) * (y2 - y1);
311                 if (ty < y1 || ty > y2)
312                         continue;
313                 /* draw tick mark */
314                 cairo_move_to(cr, x1, ty);
315                 cairo_line_to(cr, x1 - (x2 - x1) * 0.02, ty);
316                 cairo_stroke(cr);
317
318                 /* draw grid lines */
319                 cairo_save(cr);
320                 cairo_set_dash(cr, dash, 2, 2.0);
321                 cairo_set_line_width(cr, 0.5);
322                 cairo_move_to(cr, x1, ty);
323                 cairo_line_to(cr, x2, ty);
324                 cairo_stroke(cr);
325                 cairo_restore(cr);
326
327                 /* draw tickmark label */
328                 draw_centered_text(cr, x1 - (x2 - x1) * 0.04, ty, 12.0, tm[i].string);
329                 cairo_stroke(cr);
330         }
331 }
332
333 void bar_graph_draw(struct graph *bg, cairo_t *cr)
334 {
335         double x1, y1, x2, y2;
336         double space_per_label, bar_width;
337         double label_offset, mindata, maxdata;
338         int i, nlabels;
339         struct graph_label *lb;
340
341         cairo_save(cr);
342         graph_draw_common(bg, cr, &x1, &y1, &x2, &y2);
343
344         nlabels = count_labels(bg->labels);
345         space_per_label = (x2 - x1) / (double) nlabels; 
346
347         mindata = find_min_data(bg->labels);
348         maxdata = find_max_data(bg->labels);
349
350         if (fabs(maxdata - mindata) < 1e-20) {
351                 draw_centered_text(cr,
352                         x1 + (x2 - x1) / 2.0,
353                         y1 + (y2 - y1) / 2.0, 20.0, "No good data");
354                 return;
355         }
356
357         graph_draw_y_ticks(bg, cr, x1, y1, x2, y2, mindata, maxdata, 10);
358
359         i = 0;
360         for (lb = bg->labels; lb; lb = lb->next) {
361                 int nvalues;
362                 nvalues = count_values(lb->values);
363                 bar_width = (space_per_label - space_per_label * 0.2) / (double) nvalues;
364                 label_offset = bg->xdim * 0.1 + space_per_label * (double) i + space_per_label * 0.1;
365                 draw_bars(bg, cr, lb, label_offset, bar_width, mindata, maxdata);
366                 // draw_centered_text(cr, label_offset + (bar_width / 2.0 + bar_width * 0.1), bg->ydim * 0.93,
367                 draw_centered_text(cr, x1 + space_per_label * (i + 0.5), bg->ydim * 0.93,
368                         12.0, lb->label); 
369                 i++;
370         }
371         cairo_stroke(cr);
372         cairo_restore(cr);
373 }
374
375 typedef double (*xy_value_extractor)(struct graph_value *v);
376
377 static double getx(struct graph_value *v)
378 {
379         struct xyvalue *xy = v->value;
380         return xy->x;
381 }
382
383 static double gety(struct graph_value *v)
384 {
385         struct xyvalue *xy = v->value;
386         return xy->y;
387 }
388
389 static double find_xy_value(struct graph *g, xy_value_extractor getvalue, double_comparator cmp)
390 {
391         double tmp, answer = 0.0;
392         struct graph_label *i;
393         struct graph_value *j;
394
395         for (i = g->labels; i; i = i->next)
396                 for (j = i->values; j; j = j->next) {
397                         tmp = getvalue(j);
398                         answer = cmp(tmp, answer);      
399                 }
400         return answer;
401
402
403 void line_graph_draw(struct graph *g, cairo_t *cr)
404 {
405         double x1, y1, x2, y2;
406         double minx, miny, maxx, maxy;
407         double tx, ty;
408         struct graph_label *i;
409         struct graph_value *j;
410         int first = 1;
411
412         cairo_save(cr);
413         graph_draw_common(g, cr, &x1, &y1, &x2, &y2);
414
415         minx = find_xy_value(g, getx, mindouble);
416         maxx = find_xy_value(g, getx, maxdouble);
417         miny = find_xy_value(g, gety, mindouble);
418         maxy = find_xy_value(g, gety, maxdouble);
419
420         if (fabs(maxx - minx) < 1e-20 || fabs(maxy - miny) < 1e-20) {
421                 draw_centered_text(cr,
422                         x1 + (x2 - x1) / 2.0,
423                         y1 + (y2 - y1) / 2.0, 20.0, "No good Data");
424                 return;
425         }
426
427         graph_draw_x_ticks(g, cr, x1, y1, x2, y2, minx, maxx, 10);
428         graph_draw_y_ticks(g, cr, x1, y1, x2, y2, miny, maxy, 10);
429
430         cairo_set_line_width(cr, 2.5);
431         for (i = g->labels; i; i = i->next) {
432                 first = 1;
433                 cairo_set_source_rgb(cr, i->r, i->g, i->b);
434                 for (j = i->values; j; j = j->next) {
435                         tx = ((getx(j) - minx) / (maxx - minx)) * (x2 - x1) + x1;
436                         ty = y2 - ((gety(j) - miny) / (maxy - miny)) * (y2 - y1);
437                         if (first) {
438                                 cairo_move_to(cr, tx, ty);
439                                 first = 0;
440                         } else {
441                                 cairo_line_to(cr, tx, ty);
442                         }
443                 }
444                 cairo_stroke(cr);
445         }
446         cairo_restore(cr);
447 }
448
449 static void gfree(void *f)
450 {
451         if (f)
452                 free(f);
453 }
454
455 static void setstring(char **str, const char *value)
456 {
457         gfree(*str);
458         *str = strdup(value);
459 }
460
461 void graph_title(struct graph *bg, const char *title)
462 {
463         setstring(&bg->title, title);
464 }
465
466 void graph_x_title(struct graph *bg, const char *title)
467 {
468         setstring(&bg->xtitle, title);
469 }
470
471 void graph_y_title(struct graph *bg, const char *title)
472 {
473         setstring(&bg->ytitle, title);
474 }
475
476 static struct graph_label *graph_find_label(struct graph *bg,
477                                 const char *label)
478 {
479         struct graph_label *i;
480         
481         for (i = bg->labels; i; i = i->next)
482                 if (strcmp(label, i->label) == 0)
483                         return i;
484         return NULL;
485 }
486
487 void graph_add_label(struct graph *bg, const char *label)
488 {
489         struct graph_label *i;
490         
491         i = graph_find_label(bg, label);
492         if (i)
493                 return; /* already present. */
494         i = calloc(1, sizeof(*i));
495         i->parent = bg;
496         setstring(&i->label, label);
497         i->next = NULL;
498         if (!bg->tail)
499                 bg->labels = i;
500         else
501                 bg->tail->next = i;
502         bg->tail = i;
503 }
504
505 static void graph_label_add_value(struct graph_label *i, void *value)
506 {
507         struct graph_value *x;
508
509         x = malloc(sizeof(*x));
510         x->value = value;
511         x->next = NULL;
512         if (!i->tail) {
513                 i->values = x;
514         } else {
515                 i->tail->next = x;
516         }
517         i->tail = x;
518         i->value_count++;
519
520         if (i->parent->per_label_limit != -1 &&
521                 i->value_count > i->parent->per_label_limit) {
522                 x = i->values;
523                 i->values = i->values->next;
524                 free(x->value);
525                 free(x);
526                 i->value_count--;
527         }
528 }
529
530 int graph_add_data(struct graph *bg, const char *label, const double value)
531 {
532         struct graph_label *i;
533         double *d;
534
535         d = malloc(sizeof(*d));
536         *d = value;
537
538         i = graph_find_label(bg, label);
539         if (!i)
540                 return -1;
541         graph_label_add_value(i, d);
542         return 0;
543 }
544
545 int graph_add_xy_data(struct graph *bg, const char *label,
546                 const double x, const double y)
547 {
548         struct graph_label *i;
549         struct xyvalue *xy;
550
551         xy = malloc(sizeof(*xy));
552         xy->x = x;
553         xy->y = y;
554
555         i = graph_find_label(bg, label);
556         if (!i)
557                 return -1;
558         graph_label_add_value(i, xy);
559         return 0;
560 }
561
562 static void graph_free_values(struct graph_value *values)
563 {
564         struct graph_value *i, *next;
565
566         for (i = values; i; i = next) {
567                 next = i->next;
568                 gfree(i->value);
569                 gfree(i);
570         }       
571 }
572
573 static void graph_free_labels(struct graph_label *labels)
574 {
575         struct graph_label *i, *next;
576
577         for (i = labels; i; i = next) {
578                 next = i->next;
579                 graph_free_values(i->values);
580                 gfree(i);
581         }       
582 }
583
584 void graph_set_color(struct graph *gr, const char *label,
585         double red, double green, double blue)
586 {
587         struct graph_label *i;
588         double r, g, b;
589
590         r = fabs(red);
591         g = fabs(green);
592         b = fabs(blue);
593
594         if (r > 1.0)
595                 r = 1.0;
596         if (g > 1.0)
597                 g = 1.0;
598         if (b > 1.0)
599                 b =1.0;
600
601         for (i = gr->labels; i; i = i->next)
602                 if (strcmp(i->label, label) == 0) {
603                         i->r = r;       
604                         i->g = g;       
605                         i->b = b;       
606                         break;
607                 }
608 }
609
610 void graph_free(struct graph *bg)
611 {
612         gfree(bg->title);
613         gfree(bg->xtitle);
614         gfree(bg->ytitle);
615         graph_free_labels(bg->labels);
616 }
617
618 /* For each line in the line graph, up to per_label_limit segments may
619  * be added.  After that, adding more data to the end of the line
620  * causes data to drop off of the front of the line.
621  */
622 void line_graph_set_data_count_limit(struct graph *g, int per_label_limit)
623 {
624         g->per_label_limit = per_label_limit;
625 }
626