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"
39 struct graph_value *next;
45 struct graph_value *tail;
46 struct graph_value *values;
47 struct graph_label *next;
57 unsigned int xdim, ydim;
58 double xoffset, yoffset;
59 struct graph_label *labels;
60 struct graph_label *tail;
63 graph_axis_unit_change_callback x_axis_unit_change_callback;
64 graph_axis_unit_change_callback y_axis_unit_change_callback;
67 void graph_set_size(struct graph *g, unsigned int xdim, unsigned int ydim)
73 void graph_set_position(struct graph *g, double xoffset, double yoffset)
79 struct graph *graph_new(unsigned int xdim, unsigned int ydim, const char *font)
83 g = calloc(1, sizeof(*g));
84 graph_set_size(g, xdim, ydim);
85 g->per_label_limit = -1;
92 void graph_x_axis_unit_change_notify(struct graph *g, graph_axis_unit_change_callback f)
94 g->x_axis_unit_change_callback = f;
97 void graph_y_axis_unit_change_notify(struct graph *g, graph_axis_unit_change_callback f)
99 g->y_axis_unit_change_callback = f;
102 static int count_labels(struct graph_label *labels)
105 struct graph_label *i;
107 for (i = labels; i; i = i->next)
112 static int count_values(struct graph_value *values)
115 struct graph_value *i;
117 for (i = values; i; i = i->next)
122 typedef double (*double_comparator)(double a, double b);
124 static double mindouble(double a, double b)
126 return a < b ? a : b;
129 static double maxdouble(double a, double b)
131 return a < b ? b : a;
134 static double find_double_values(struct graph_value *values, double_comparator cmp)
136 struct graph_value *i;
140 assert(values != NULL);
141 answer = 0.0; /* shut the compiler up, might need to think harder though. */
142 for (i = values; i; i = i->next) {
143 tmp = *(double *) i->value;
148 answer = cmp(answer, tmp);
154 static double find_double_data(struct graph_label *labels, double_comparator cmp)
156 struct graph_label *i;
160 assert(labels != NULL);
161 answer = 0.0; /* shut the compiler up, might need to think harder though. */
162 for (i = labels; i; i = i->next) {
163 tmp = find_double_values(i->values, cmp);
168 answer = cmp(tmp, answer);
174 static double find_min_data(struct graph_label *labels)
176 return find_double_data(labels, mindouble);
179 static double find_max_data(struct graph_label *labels)
181 return find_double_data(labels, maxdouble);
184 static void draw_bars(struct graph *bg, cairo_t *cr, struct graph_label *lb,
185 double label_offset, double bar_width,
186 double mindata, double maxdata)
188 struct graph_value *i;
189 double x1, y1, x2, y2;
191 double domain, range, v;
193 domain = (maxdata - mindata);
194 range = (double) bg->ydim * 0.80; /* FIXME */
196 for (i = lb->values; i; i = i->next) {
198 x1 = label_offset + (double) bar_num * bar_width + (bar_width * 0.05);
199 x2 = x1 + bar_width * 0.90;
200 y2 = bg->ydim * 0.90;
201 v = *(double *) i->value;
202 y1 = y2 - (((v - mindata) / domain) * range);
203 cairo_move_to(cr, x1, y1);
204 cairo_line_to(cr, x1, y2);
205 cairo_line_to(cr, x2, y2);
206 cairo_line_to(cr, x2, y1);
207 cairo_close_path(cr);
214 static void draw_aligned_text(struct graph *g, cairo_t *cr, double x, double y,
215 double fontsize, const char *text, int alignment)
218 #define LEFT_JUSTIFIED 1
219 #define RIGHT_JUSTIFIED 2
221 double factor, direction;
222 cairo_text_extents_t extents;
229 case RIGHT_JUSTIFIED:
239 cairo_select_font_face (cr, g->font, CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
241 cairo_set_font_size(cr, fontsize);
242 cairo_text_extents(cr, text, &extents);
243 x = x + direction * (factor * extents.width + extents.x_bearing);
244 y = y - (extents.height / 2 + extents.y_bearing);
246 cairo_move_to(cr, x, y);
247 cairo_show_text(cr, text);
250 static inline void draw_centered_text(struct graph *g, cairo_t *cr, double x, double y,
251 double fontsize, const char *text)
253 draw_aligned_text(g, cr, x, y, fontsize, text, CENTERED);
256 static inline void draw_right_justified_text(struct graph *g, cairo_t *cr,
258 double fontsize, const char *text)
260 draw_aligned_text(g, cr, x, y, fontsize, text, RIGHT_JUSTIFIED);
263 static inline void draw_left_justified_text(struct graph *g, cairo_t *cr,
265 double fontsize, const char *text)
267 draw_aligned_text(g, cr, x, y, fontsize, text, LEFT_JUSTIFIED);
270 static void draw_vertical_centered_text(struct graph *g, cairo_t *cr, double x,
271 double y, double fontsize,
275 cairo_text_extents_t extents;
277 cairo_select_font_face(cr, g->font, CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
279 cairo_set_font_size(cr, fontsize);
280 cairo_text_extents(cr, text, &extents);
283 y = y + (extents.width / 2.0 + extents.x_bearing);
284 x = x - (extents.height / 2.0 + extents.y_bearing);
286 cairo_move_to(cr, x, y);
288 cairo_translate(cr, -sx, -sy);
289 cairo_rotate(cr, -90.0 * M_PI / 180.0);
290 cairo_translate(cr, sx, sy);
291 cairo_show_text(cr, text);
295 static void graph_draw_common(struct graph *g, cairo_t *cr,
296 double *x1, double *y1, double *x2, double *y2)
298 cairo_set_source_rgb(cr, 0, 0, 0);
299 cairo_set_line_width (cr, 0.8);
301 *x1 = 0.15 * g->xdim;
302 *x2 = 0.95 * g->xdim;
303 *y1 = 0.10 * g->ydim;
304 *y2 = 0.90 * g->ydim;
306 cairo_move_to(cr, *x1, *y1);
307 cairo_line_to(cr, *x1, *y2);
308 cairo_line_to(cr, *x2, *y2);
309 cairo_line_to(cr, *x2, *y1);
310 cairo_line_to(cr, *x1, *y1);
313 draw_centered_text(g, cr, g->xdim / 2, g->ydim / 20, 20.0, g->title);
314 draw_centered_text(g, cr, g->xdim / 2, g->ydim * 0.97, 14.0, g->xtitle);
315 draw_vertical_centered_text(g, cr, g->xdim * 0.02, g->ydim / 2, 14.0, g->ytitle);
319 static void graph_draw_x_ticks(struct graph *g, cairo_t *cr,
320 double x1, double y1, double x2, double y2,
321 double minx, double maxx, int nticks)
326 static double dash[] = { 1.0, 2.0 };
328 nticks = calc_tickmarks(minx, maxx, nticks, &tm, &power_of_ten,
329 g->x_axis_unit_change_callback == NULL);
330 if (g->x_axis_unit_change_callback)
331 g->x_axis_unit_change_callback(g, power_of_ten);
333 for (i = 0; i < nticks; i++) {
334 tx = (((tm[i].value) - minx) / (maxx - minx)) * (x2 - x1) + x1;
335 if (tx < x1 || tx > x2)
339 cairo_set_line_width(cr, 0.8);
340 cairo_move_to(cr, tx, y2);
341 cairo_line_to(cr, tx, y2 + (y2 - y1) * 0.03);
344 /* draw grid lines */
346 cairo_set_dash(cr, dash, 2, 2.0);
347 cairo_set_line_width(cr, 0.5);
348 cairo_move_to(cr, tx, y1);
349 cairo_line_to(cr, tx, y2);
353 /* draw tickmark label */
354 draw_centered_text(g, cr, tx, y2 * 1.04, 12.0, tm[i].string);
360 static void graph_draw_y_ticks(struct graph *g, cairo_t *cr,
361 double x1, double y1, double x2, double y2,
362 double miny, double maxy, int nticks)
367 static double dash[] = { 2.0, 2.0 };
369 nticks = calc_tickmarks(miny, maxy, nticks, &tm, &power_of_ten,
370 g->y_axis_unit_change_callback == NULL);
371 if (g->y_axis_unit_change_callback)
372 g->y_axis_unit_change_callback(g, power_of_ten);
374 for (i = 0; i < nticks; i++) {
375 ty = y2 - (((tm[i].value) - miny) / (maxy - miny)) * (y2 - y1);
376 if (ty < y1 || ty > y2)
379 cairo_move_to(cr, x1, ty);
380 cairo_line_to(cr, x1 - (x2 - x1) * 0.02, ty);
383 /* draw grid lines */
385 cairo_set_dash(cr, dash, 2, 2.0);
386 cairo_set_line_width(cr, 0.5);
387 cairo_move_to(cr, x1, ty);
388 cairo_line_to(cr, x2, ty);
392 /* draw tickmark label */
393 draw_right_justified_text(g, cr, x1 - (x2 - x1) * 0.025, ty, 12.0, tm[i].string);
398 void bar_graph_draw(struct graph *bg, cairo_t *cr)
400 double x1, y1, x2, y2;
401 double space_per_label, bar_width;
402 double label_offset, mindata, maxdata;
404 struct graph_label *lb;
407 cairo_translate(cr, bg->xoffset, bg->yoffset);
408 graph_draw_common(bg, cr, &x1, &y1, &x2, &y2);
410 nlabels = count_labels(bg->labels);
411 space_per_label = (x2 - x1) / (double) nlabels;
413 mindata = find_min_data(bg->labels);
414 maxdata = find_max_data(bg->labels);
416 if (fabs(maxdata - mindata) < 1e-20) {
417 draw_centered_text(bg, cr,
418 x1 + (x2 - x1) / 2.0,
419 y1 + (y2 - y1) / 2.0, 20.0, "No good data");
423 graph_draw_y_ticks(bg, cr, x1, y1, x2, y2, mindata, maxdata, 10);
426 for (lb = bg->labels; lb; lb = lb->next) {
428 nvalues = count_values(lb->values);
429 bar_width = (space_per_label - space_per_label * 0.2) / (double) nvalues;
430 label_offset = bg->xdim * 0.1 + space_per_label * (double) i + space_per_label * 0.1;
431 draw_bars(bg, cr, lb, label_offset, bar_width, mindata, maxdata);
432 // draw_centered_text(cr, label_offset + (bar_width / 2.0 + bar_width * 0.1), bg->ydim * 0.93,
433 draw_centered_text(bg, cr, x1 + space_per_label * (i + 0.5), bg->ydim * 0.93,
441 typedef double (*xy_value_extractor)(struct graph_value *v);
443 static double getx(struct graph_value *v)
445 struct xyvalue *xy = v->value;
449 static double gety(struct graph_value *v)
451 struct xyvalue *xy = v->value;
455 static double find_xy_value(struct graph *g, xy_value_extractor getvalue, double_comparator cmp)
457 double tmp, answer = 0.0;
458 struct graph_label *i;
459 struct graph_value *j;
462 for (i = g->labels; i; i = i->next)
463 for (j = i->values; j; j = j->next) {
469 answer = cmp(tmp, answer);
474 void line_graph_draw(struct graph *g, cairo_t *cr)
476 double x1, y1, x2, y2;
477 double minx, miny, maxx, maxy;
479 struct graph_label *i;
480 struct graph_value *j;
481 int good_data = 1, first = 1;
484 cairo_translate(cr, g->xoffset, g->yoffset);
485 graph_draw_common(g, cr, &x1, &y1, &x2, &y2);
487 minx = find_xy_value(g, getx, mindouble);
488 maxx = find_xy_value(g, getx, maxdouble);
489 miny = find_xy_value(g, gety, mindouble);
490 maxy = find_xy_value(g, gety, maxdouble);
492 if (fabs(maxx - minx) < 1e-20 || fabs(maxy - miny) < 1e-20) {
500 graph_draw_x_ticks(g, cr, x1, y1, x2, y2, minx, maxx, 10);
501 graph_draw_y_ticks(g, cr, x1, y1, x2, y2, miny, maxy, 10);
506 cairo_set_line_width(cr, 1.5);
507 for (i = g->labels; i; i = i->next) {
509 if (i->r < 0) /* invisible data */
511 cairo_set_source_rgb(cr, i->r, i->g, i->b);
512 for (j = i->values; j; j = j->next) {
513 tx = ((getx(j) - minx) / (maxx - minx)) * (x2 - x1) + x1;
514 ty = y2 - ((gety(j) - miny) / (maxy - miny)) * (y2 - y1);
516 cairo_move_to(cr, tx, ty);
519 cairo_line_to(cr, tx, ty);
530 static void gfree(void *f)
536 static void setstring(char **str, const char *value)
539 *str = strdup(value);
542 void graph_title(struct graph *bg, const char *title)
544 setstring(&bg->title, title);
547 void graph_x_title(struct graph *bg, const char *title)
549 setstring(&bg->xtitle, title);
552 void graph_y_title(struct graph *bg, const char *title)
554 setstring(&bg->ytitle, title);
557 static struct graph_label *graph_find_label(struct graph *bg,
560 struct graph_label *i;
562 for (i = bg->labels; i; i = i->next)
563 if (strcmp(label, i->label) == 0)
568 void graph_add_label(struct graph *bg, const char *label)
570 struct graph_label *i;
572 i = graph_find_label(bg, label);
574 return; /* already present. */
575 i = calloc(1, sizeof(*i));
577 setstring(&i->label, label);
586 static void graph_label_add_value(struct graph_label *i, void *value)
588 struct graph_value *x;
590 x = malloc(sizeof(*x));
601 if (i->parent->per_label_limit != -1 &&
602 i->value_count > i->parent->per_label_limit) {
604 i->values = i->values->next;
611 int graph_add_data(struct graph *bg, const char *label, const double value)
613 struct graph_label *i;
616 d = malloc(sizeof(*d));
619 i = graph_find_label(bg, label);
622 graph_label_add_value(i, d);
626 int graph_add_xy_data(struct graph *bg, const char *label,
627 const double x, const double y)
629 struct graph_label *i;
632 xy = malloc(sizeof(*xy));
636 i = graph_find_label(bg, label);
639 graph_label_add_value(i, xy);
643 static void graph_free_values(struct graph_value *values)
645 struct graph_value *i, *next;
647 for (i = values; i; i = next) {
654 static void graph_free_labels(struct graph_label *labels)
656 struct graph_label *i, *next;
658 for (i = labels; i; i = next) {
660 graph_free_values(i->values);
665 void graph_set_color(struct graph *gr, const char *label,
666 double red, double green, double blue)
668 struct graph_label *i;
671 if (red < 0.0) { /* invisible color */
688 for (i = gr->labels; i; i = i->next)
689 if (strcmp(i->label, label) == 0) {
697 void graph_free(struct graph *bg)
702 graph_free_labels(bg->labels);
705 /* For each line in the line graph, up to per_label_limit segments may
706 * be added. After that, adding more data to the end of the line
707 * causes data to drop off of the front of the line.
709 void line_graph_set_data_count_limit(struct graph *g, int per_label_limit)
711 g->per_label_limit = per_label_limit;