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