2 * gfio - gui front end for fio - the flexible io tester
4 * Copyright (C) 2012 Stephen M. Cameron <stephenmcameron@gmail.com>
6 * The license below covers all files distributed with fio unless otherwise
7 * noted in the file itself.
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.
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.
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
31 #include "tickmarks.h"
38 struct graph_value *next;
44 struct graph_value *tail;
45 struct graph_value *values;
46 struct graph_label *next;
56 unsigned int xdim, ydim;
57 struct graph_label *labels;
58 struct graph_label *tail;
63 void graph_set_size(struct graph *g, unsigned int xdim, unsigned int ydim)
69 struct graph *graph_new(unsigned int xdim, unsigned int ydim, const char *font)
73 g = calloc(1, sizeof(*g));
74 graph_set_size(g, xdim, ydim);
75 g->per_label_limit = -1;
82 static int count_labels(struct graph_label *labels)
85 struct graph_label *i;
87 for (i = labels; i; i = i->next)
92 static int count_values(struct graph_value *values)
95 struct graph_value *i;
97 for (i = values; i; i = i->next)
102 typedef double (*double_comparator)(double a, double b);
104 static double mindouble(double a, double b)
106 return a < b ? a : b;
109 static double maxdouble(double a, double b)
111 return a < b ? b : a;
114 static double find_double_values(struct graph_value *values, double_comparator cmp)
116 struct graph_value *i;
120 assert(values != NULL);
121 answer = 0.0; /* shut the compiler up, might need to think harder though. */
122 for (i = values; i; i = i->next) {
123 tmp = *(double *) i->value;
128 answer = cmp(answer, tmp);
134 static double find_double_data(struct graph_label *labels, double_comparator cmp)
136 struct graph_label *i;
140 assert(labels != NULL);
141 answer = 0.0; /* shut the compiler up, might need to think harder though. */
142 for (i = labels; i; i = i->next) {
143 tmp = find_double_values(i->values, cmp);
148 answer = cmp(tmp, answer);
154 static double find_min_data(struct graph_label *labels)
156 return find_double_data(labels, mindouble);
159 static double find_max_data(struct graph_label *labels)
161 return find_double_data(labels, maxdouble);
164 static void draw_bars(struct graph *bg, cairo_t *cr, struct graph_label *lb,
165 double label_offset, double bar_width,
166 double mindata, double maxdata)
168 struct graph_value *i;
169 double x1, y1, x2, y2;
171 double domain, range, v;
173 domain = (maxdata - mindata);
174 range = (double) bg->ydim * 0.80; /* FIXME */
176 for (i = lb->values; i; i = i->next) {
178 x1 = label_offset + (double) bar_num * bar_width + (bar_width * 0.05);
179 x2 = x1 + bar_width * 0.90;
180 y2 = bg->ydim * 0.90;
181 v = *(double *) i->value;
182 y1 = y2 - (((v - mindata) / domain) * range);
183 cairo_move_to(cr, x1, y1);
184 cairo_line_to(cr, x1, y2);
185 cairo_line_to(cr, x2, y2);
186 cairo_line_to(cr, x2, y1);
187 cairo_close_path(cr);
194 static void draw_aligned_text(struct graph *g, cairo_t *cr, double x, double y,
195 double fontsize, const char *text, int alignment)
198 #define LEFT_JUSTIFIED 1
199 #define RIGHT_JUSTIFIED 2
201 double factor, direction;
202 cairo_text_extents_t extents;
209 case RIGHT_JUSTIFIED:
219 cairo_select_font_face (cr, g->font, CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
221 cairo_set_font_size(cr, fontsize);
222 cairo_text_extents(cr, text, &extents);
223 x = x + direction * (factor * extents.width + extents.x_bearing);
224 y = y - (extents.height / 2 + extents.y_bearing);
226 cairo_move_to(cr, x, y);
227 cairo_show_text(cr, text);
230 static inline void draw_centered_text(struct graph *g, cairo_t *cr, double x, double y,
231 double fontsize, const char *text)
233 draw_aligned_text(g, cr, x, y, fontsize, text, CENTERED);
236 static inline void draw_right_justified_text(struct graph *g, cairo_t *cr,
238 double fontsize, const char *text)
240 draw_aligned_text(g, cr, x, y, fontsize, text, RIGHT_JUSTIFIED);
243 static inline void draw_left_justified_text(struct graph *g, cairo_t *cr,
245 double fontsize, const char *text)
247 draw_aligned_text(g, cr, x, y, fontsize, text, LEFT_JUSTIFIED);
250 static void draw_vertical_centered_text(struct graph *g, cairo_t *cr, double x,
251 double y, double fontsize,
255 cairo_text_extents_t extents;
257 cairo_select_font_face(cr, g->font, CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
259 cairo_set_font_size(cr, fontsize);
260 cairo_text_extents(cr, text, &extents);
263 y = y + (extents.width / 2.0 + extents.x_bearing);
264 x = x - (extents.height / 2.0 + extents.y_bearing);
266 cairo_move_to(cr, x, y);
268 cairo_translate(cr, -sx, -sy);
269 cairo_rotate(cr, -90.0 * M_PI / 180.0);
270 cairo_translate(cr, sx, sy);
271 cairo_show_text(cr, text);
275 static void graph_draw_common(struct graph *g, cairo_t *cr,
276 double *x1, double *y1, double *x2, double *y2)
278 cairo_set_source_rgb(cr, 0, 0, 0);
279 cairo_set_line_width (cr, 0.8);
281 *x1 = 0.15 * g->xdim;
282 *x2 = 0.95 * g->xdim;
283 *y1 = 0.10 * g->ydim;
284 *y2 = 0.90 * g->ydim;
286 cairo_move_to(cr, *x1, *y1);
287 cairo_line_to(cr, *x1, *y2);
288 cairo_line_to(cr, *x2, *y2);
289 cairo_line_to(cr, *x2, *y1);
290 cairo_line_to(cr, *x1, *y1);
293 draw_centered_text(g, cr, g->xdim / 2, g->ydim / 20, 20.0, g->title);
294 draw_centered_text(g, cr, g->xdim / 2, g->ydim * 0.97, 14.0, g->xtitle);
295 draw_vertical_centered_text(g, cr, g->xdim * 0.02, g->ydim / 2, 14.0, g->ytitle);
299 static void graph_draw_x_ticks(struct graph *g, cairo_t *cr,
300 double x1, double y1, double x2, double y2,
301 double minx, double maxx, int nticks)
306 static double dash[] = { 1.0, 2.0 };
308 nticks = calc_tickmarks(minx, maxx, nticks, &tm);
310 for (i = 0; i < nticks; i++) {
311 tx = (((tm[i].value) - minx) / (maxx - minx)) * (x2 - x1) + x1;
312 if (tx < x1 || tx > x2)
316 cairo_set_line_width(cr, 0.8);
317 cairo_move_to(cr, tx, y2);
318 cairo_line_to(cr, tx, y2 + (y2 - y1) * 0.03);
321 /* draw grid lines */
323 cairo_set_dash(cr, dash, 2, 2.0);
324 cairo_set_line_width(cr, 0.5);
325 cairo_move_to(cr, tx, y1);
326 cairo_line_to(cr, tx, y2);
330 /* draw tickmark label */
331 draw_centered_text(g, cr, tx, y2 * 1.04, 12.0, tm[i].string);
337 static void graph_draw_y_ticks(struct graph *g, cairo_t *cr,
338 double x1, double y1, double x2, double y2,
339 double miny, double maxy, int nticks)
344 static double dash[] = { 2.0, 2.0 };
346 nticks = calc_tickmarks(miny, maxy, nticks, &tm);
348 for (i = 0; i < nticks; i++) {
349 ty = y2 - (((tm[i].value) - miny) / (maxy - miny)) * (y2 - y1);
350 if (ty < y1 || ty > y2)
353 cairo_move_to(cr, x1, ty);
354 cairo_line_to(cr, x1 - (x2 - x1) * 0.02, ty);
357 /* draw grid lines */
359 cairo_set_dash(cr, dash, 2, 2.0);
360 cairo_set_line_width(cr, 0.5);
361 cairo_move_to(cr, x1, ty);
362 cairo_line_to(cr, x2, ty);
366 /* draw tickmark label */
367 draw_right_justified_text(g, cr, x1 - (x2 - x1) * 0.025, ty, 12.0, tm[i].string);
372 void bar_graph_draw(struct graph *bg, cairo_t *cr)
374 double x1, y1, x2, y2;
375 double space_per_label, bar_width;
376 double label_offset, mindata, maxdata;
378 struct graph_label *lb;
381 graph_draw_common(bg, cr, &x1, &y1, &x2, &y2);
383 nlabels = count_labels(bg->labels);
384 space_per_label = (x2 - x1) / (double) nlabels;
386 mindata = find_min_data(bg->labels);
387 maxdata = find_max_data(bg->labels);
389 if (fabs(maxdata - mindata) < 1e-20) {
390 draw_centered_text(bg, cr,
391 x1 + (x2 - x1) / 2.0,
392 y1 + (y2 - y1) / 2.0, 20.0, "No good data");
396 graph_draw_y_ticks(bg, cr, x1, y1, x2, y2, mindata, maxdata, 10);
399 for (lb = bg->labels; lb; lb = lb->next) {
401 nvalues = count_values(lb->values);
402 bar_width = (space_per_label - space_per_label * 0.2) / (double) nvalues;
403 label_offset = bg->xdim * 0.1 + space_per_label * (double) i + space_per_label * 0.1;
404 draw_bars(bg, cr, lb, label_offset, bar_width, mindata, maxdata);
405 // draw_centered_text(cr, label_offset + (bar_width / 2.0 + bar_width * 0.1), bg->ydim * 0.93,
406 draw_centered_text(bg, cr, x1 + space_per_label * (i + 0.5), bg->ydim * 0.93,
414 typedef double (*xy_value_extractor)(struct graph_value *v);
416 static double getx(struct graph_value *v)
418 struct xyvalue *xy = v->value;
422 static double gety(struct graph_value *v)
424 struct xyvalue *xy = v->value;
428 static double find_xy_value(struct graph *g, xy_value_extractor getvalue, double_comparator cmp)
430 double tmp, answer = 0.0;
431 struct graph_label *i;
432 struct graph_value *j;
435 for (i = g->labels; i; i = i->next)
436 for (j = i->values; j; j = j->next) {
442 answer = cmp(tmp, answer);
447 void line_graph_draw(struct graph *g, cairo_t *cr)
449 double x1, y1, x2, y2;
450 double minx, miny, maxx, maxy;
452 struct graph_label *i;
453 struct graph_value *j;
454 int good_data = 1, first = 1;
457 graph_draw_common(g, cr, &x1, &y1, &x2, &y2);
459 minx = find_xy_value(g, getx, mindouble);
460 maxx = find_xy_value(g, getx, maxdouble);
461 miny = find_xy_value(g, gety, mindouble);
462 maxy = find_xy_value(g, gety, maxdouble);
464 if (fabs(maxx - minx) < 1e-20 || fabs(maxy - miny) < 1e-20) {
472 graph_draw_x_ticks(g, cr, x1, y1, x2, y2, minx, maxx, 10);
473 graph_draw_y_ticks(g, cr, x1, y1, x2, y2, miny, maxy, 10);
478 cairo_set_line_width(cr, 1.5);
479 for (i = g->labels; i; i = i->next) {
481 if (i->r < 0) /* invisible data */
483 cairo_set_source_rgb(cr, i->r, i->g, i->b);
484 for (j = i->values; j; j = j->next) {
485 tx = ((getx(j) - minx) / (maxx - minx)) * (x2 - x1) + x1;
486 ty = y2 - ((gety(j) - miny) / (maxy - miny)) * (y2 - y1);
488 cairo_move_to(cr, tx, ty);
491 cairo_line_to(cr, tx, ty);
502 static void gfree(void *f)
508 static void setstring(char **str, const char *value)
511 *str = strdup(value);
514 void graph_title(struct graph *bg, const char *title)
516 setstring(&bg->title, title);
519 void graph_x_title(struct graph *bg, const char *title)
521 setstring(&bg->xtitle, title);
524 void graph_y_title(struct graph *bg, const char *title)
526 setstring(&bg->ytitle, title);
529 static struct graph_label *graph_find_label(struct graph *bg,
532 struct graph_label *i;
534 for (i = bg->labels; i; i = i->next)
535 if (strcmp(label, i->label) == 0)
540 void graph_add_label(struct graph *bg, const char *label)
542 struct graph_label *i;
544 i = graph_find_label(bg, label);
546 return; /* already present. */
547 i = calloc(1, sizeof(*i));
549 setstring(&i->label, label);
558 static void graph_label_add_value(struct graph_label *i, void *value)
560 struct graph_value *x;
562 x = malloc(sizeof(*x));
573 if (i->parent->per_label_limit != -1 &&
574 i->value_count > i->parent->per_label_limit) {
576 i->values = i->values->next;
583 int graph_add_data(struct graph *bg, const char *label, const double value)
585 struct graph_label *i;
588 d = malloc(sizeof(*d));
591 i = graph_find_label(bg, label);
594 graph_label_add_value(i, d);
598 int graph_add_xy_data(struct graph *bg, const char *label,
599 const double x, const double y)
601 struct graph_label *i;
604 xy = malloc(sizeof(*xy));
608 i = graph_find_label(bg, label);
611 graph_label_add_value(i, xy);
615 static void graph_free_values(struct graph_value *values)
617 struct graph_value *i, *next;
619 for (i = values; i; i = next) {
626 static void graph_free_labels(struct graph_label *labels)
628 struct graph_label *i, *next;
630 for (i = labels; i; i = next) {
632 graph_free_values(i->values);
637 void graph_set_color(struct graph *gr, const char *label,
638 double red, double green, double blue)
640 struct graph_label *i;
643 if (red < 0.0) { /* invisible color */
660 for (i = gr->labels; i; i = i->next)
661 if (strcmp(i->label, label) == 0) {
669 void graph_free(struct graph *bg)
674 graph_free_labels(bg->labels);
677 /* For each line in the line graph, up to per_label_limit segments may
678 * be added. After that, adding more data to the end of the line
679 * causes data to drop off of the front of the line.
681 void line_graph_set_data_count_limit(struct graph *g, int per_label_limit)
683 g->per_label_limit = per_label_limit;