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 struct graph_label *labels;
59 struct graph_label *tail;
62 graph_axis_unit_change_callback x_axis_unit_change_callback;
63 graph_axis_unit_change_callback y_axis_unit_change_callback;
66 void graph_set_size(struct graph *g, unsigned int xdim, unsigned int ydim)
72 struct graph *graph_new(unsigned int xdim, unsigned int ydim, const char *font)
76 g = calloc(1, sizeof(*g));
77 graph_set_size(g, xdim, ydim);
78 g->per_label_limit = -1;
85 void graph_x_axis_unit_change_notify(struct graph *g, graph_axis_unit_change_callback f)
87 g->x_axis_unit_change_callback = f;
90 void graph_y_axis_unit_change_notify(struct graph *g, graph_axis_unit_change_callback f)
92 g->y_axis_unit_change_callback = f;
95 static int count_labels(struct graph_label *labels)
98 struct graph_label *i;
100 for (i = labels; i; i = i->next)
105 static int count_values(struct graph_value *values)
108 struct graph_value *i;
110 for (i = values; i; i = i->next)
115 typedef double (*double_comparator)(double a, double b);
117 static double mindouble(double a, double b)
119 return a < b ? a : b;
122 static double maxdouble(double a, double b)
124 return a < b ? b : a;
127 static double find_double_values(struct graph_value *values, double_comparator cmp)
129 struct graph_value *i;
133 assert(values != NULL);
134 answer = 0.0; /* shut the compiler up, might need to think harder though. */
135 for (i = values; i; i = i->next) {
136 tmp = *(double *) i->value;
141 answer = cmp(answer, tmp);
147 static double find_double_data(struct graph_label *labels, double_comparator cmp)
149 struct graph_label *i;
153 assert(labels != NULL);
154 answer = 0.0; /* shut the compiler up, might need to think harder though. */
155 for (i = labels; i; i = i->next) {
156 tmp = find_double_values(i->values, cmp);
161 answer = cmp(tmp, answer);
167 static double find_min_data(struct graph_label *labels)
169 return find_double_data(labels, mindouble);
172 static double find_max_data(struct graph_label *labels)
174 return find_double_data(labels, maxdouble);
177 static void draw_bars(struct graph *bg, cairo_t *cr, struct graph_label *lb,
178 double label_offset, double bar_width,
179 double mindata, double maxdata)
181 struct graph_value *i;
182 double x1, y1, x2, y2;
184 double domain, range, v;
186 domain = (maxdata - mindata);
187 range = (double) bg->ydim * 0.80; /* FIXME */
189 for (i = lb->values; i; i = i->next) {
191 x1 = label_offset + (double) bar_num * bar_width + (bar_width * 0.05);
192 x2 = x1 + bar_width * 0.90;
193 y2 = bg->ydim * 0.90;
194 v = *(double *) i->value;
195 y1 = y2 - (((v - mindata) / domain) * range);
196 cairo_move_to(cr, x1, y1);
197 cairo_line_to(cr, x1, y2);
198 cairo_line_to(cr, x2, y2);
199 cairo_line_to(cr, x2, y1);
200 cairo_close_path(cr);
207 static void draw_aligned_text(struct graph *g, cairo_t *cr, double x, double y,
208 double fontsize, const char *text, int alignment)
211 #define LEFT_JUSTIFIED 1
212 #define RIGHT_JUSTIFIED 2
214 double factor, direction;
215 cairo_text_extents_t extents;
222 case RIGHT_JUSTIFIED:
232 cairo_select_font_face (cr, g->font, CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
234 cairo_set_font_size(cr, fontsize);
235 cairo_text_extents(cr, text, &extents);
236 x = x + direction * (factor * extents.width + extents.x_bearing);
237 y = y - (extents.height / 2 + extents.y_bearing);
239 cairo_move_to(cr, x, y);
240 cairo_show_text(cr, text);
243 static inline void draw_centered_text(struct graph *g, cairo_t *cr, double x, double y,
244 double fontsize, const char *text)
246 draw_aligned_text(g, cr, x, y, fontsize, text, CENTERED);
249 static inline void draw_right_justified_text(struct graph *g, cairo_t *cr,
251 double fontsize, const char *text)
253 draw_aligned_text(g, cr, x, y, fontsize, text, RIGHT_JUSTIFIED);
256 static inline void draw_left_justified_text(struct graph *g, cairo_t *cr,
258 double fontsize, const char *text)
260 draw_aligned_text(g, cr, x, y, fontsize, text, LEFT_JUSTIFIED);
263 static void draw_vertical_centered_text(struct graph *g, cairo_t *cr, double x,
264 double y, double fontsize,
268 cairo_text_extents_t extents;
270 cairo_select_font_face(cr, g->font, CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
272 cairo_set_font_size(cr, fontsize);
273 cairo_text_extents(cr, text, &extents);
276 y = y + (extents.width / 2.0 + extents.x_bearing);
277 x = x - (extents.height / 2.0 + extents.y_bearing);
279 cairo_move_to(cr, x, y);
281 cairo_translate(cr, -sx, -sy);
282 cairo_rotate(cr, -90.0 * M_PI / 180.0);
283 cairo_translate(cr, sx, sy);
284 cairo_show_text(cr, text);
288 static void graph_draw_common(struct graph *g, cairo_t *cr,
289 double *x1, double *y1, double *x2, double *y2)
291 cairo_set_source_rgb(cr, 0, 0, 0);
292 cairo_set_line_width (cr, 0.8);
294 *x1 = 0.15 * g->xdim;
295 *x2 = 0.95 * g->xdim;
296 *y1 = 0.10 * g->ydim;
297 *y2 = 0.90 * g->ydim;
299 cairo_move_to(cr, *x1, *y1);
300 cairo_line_to(cr, *x1, *y2);
301 cairo_line_to(cr, *x2, *y2);
302 cairo_line_to(cr, *x2, *y1);
303 cairo_line_to(cr, *x1, *y1);
306 draw_centered_text(g, cr, g->xdim / 2, g->ydim / 20, 20.0, g->title);
307 draw_centered_text(g, cr, g->xdim / 2, g->ydim * 0.97, 14.0, g->xtitle);
308 draw_vertical_centered_text(g, cr, g->xdim * 0.02, g->ydim / 2, 14.0, g->ytitle);
312 static void graph_draw_x_ticks(struct graph *g, cairo_t *cr,
313 double x1, double y1, double x2, double y2,
314 double minx, double maxx, int nticks)
319 static double dash[] = { 1.0, 2.0 };
321 nticks = calc_tickmarks(minx, maxx, nticks, &tm, &power_of_ten,
322 g->x_axis_unit_change_callback == NULL);
323 if (g->x_axis_unit_change_callback)
324 g->x_axis_unit_change_callback(g, power_of_ten);
326 for (i = 0; i < nticks; i++) {
327 tx = (((tm[i].value) - minx) / (maxx - minx)) * (x2 - x1) + x1;
328 if (tx < x1 || tx > x2)
332 cairo_set_line_width(cr, 0.8);
333 cairo_move_to(cr, tx, y2);
334 cairo_line_to(cr, tx, y2 + (y2 - y1) * 0.03);
337 /* draw grid lines */
339 cairo_set_dash(cr, dash, 2, 2.0);
340 cairo_set_line_width(cr, 0.5);
341 cairo_move_to(cr, tx, y1);
342 cairo_line_to(cr, tx, y2);
346 /* draw tickmark label */
347 draw_centered_text(g, cr, tx, y2 * 1.04, 12.0, tm[i].string);
353 static void graph_draw_y_ticks(struct graph *g, cairo_t *cr,
354 double x1, double y1, double x2, double y2,
355 double miny, double maxy, int nticks)
360 static double dash[] = { 2.0, 2.0 };
362 nticks = calc_tickmarks(miny, maxy, nticks, &tm, &power_of_ten,
363 g->y_axis_unit_change_callback == NULL);
364 if (g->y_axis_unit_change_callback)
365 g->y_axis_unit_change_callback(g, power_of_ten);
367 for (i = 0; i < nticks; i++) {
368 ty = y2 - (((tm[i].value) - miny) / (maxy - miny)) * (y2 - y1);
369 if (ty < y1 || ty > y2)
372 cairo_move_to(cr, x1, ty);
373 cairo_line_to(cr, x1 - (x2 - x1) * 0.02, ty);
376 /* draw grid lines */
378 cairo_set_dash(cr, dash, 2, 2.0);
379 cairo_set_line_width(cr, 0.5);
380 cairo_move_to(cr, x1, ty);
381 cairo_line_to(cr, x2, ty);
385 /* draw tickmark label */
386 draw_right_justified_text(g, cr, x1 - (x2 - x1) * 0.025, ty, 12.0, tm[i].string);
391 void bar_graph_draw(struct graph *bg, cairo_t *cr)
393 double x1, y1, x2, y2;
394 double space_per_label, bar_width;
395 double label_offset, mindata, maxdata;
397 struct graph_label *lb;
400 graph_draw_common(bg, cr, &x1, &y1, &x2, &y2);
402 nlabels = count_labels(bg->labels);
403 space_per_label = (x2 - x1) / (double) nlabels;
405 mindata = find_min_data(bg->labels);
406 maxdata = find_max_data(bg->labels);
408 if (fabs(maxdata - mindata) < 1e-20) {
409 draw_centered_text(bg, cr,
410 x1 + (x2 - x1) / 2.0,
411 y1 + (y2 - y1) / 2.0, 20.0, "No good data");
415 graph_draw_y_ticks(bg, cr, x1, y1, x2, y2, mindata, maxdata, 10);
418 for (lb = bg->labels; lb; lb = lb->next) {
420 nvalues = count_values(lb->values);
421 bar_width = (space_per_label - space_per_label * 0.2) / (double) nvalues;
422 label_offset = bg->xdim * 0.1 + space_per_label * (double) i + space_per_label * 0.1;
423 draw_bars(bg, cr, lb, label_offset, bar_width, mindata, maxdata);
424 // draw_centered_text(cr, label_offset + (bar_width / 2.0 + bar_width * 0.1), bg->ydim * 0.93,
425 draw_centered_text(bg, cr, x1 + space_per_label * (i + 0.5), bg->ydim * 0.93,
433 typedef double (*xy_value_extractor)(struct graph_value *v);
435 static double getx(struct graph_value *v)
437 struct xyvalue *xy = v->value;
441 static double gety(struct graph_value *v)
443 struct xyvalue *xy = v->value;
447 static double find_xy_value(struct graph *g, xy_value_extractor getvalue, double_comparator cmp)
449 double tmp, answer = 0.0;
450 struct graph_label *i;
451 struct graph_value *j;
454 for (i = g->labels; i; i = i->next)
455 for (j = i->values; j; j = j->next) {
461 answer = cmp(tmp, answer);
466 void line_graph_draw(struct graph *g, cairo_t *cr)
468 double x1, y1, x2, y2;
469 double minx, miny, maxx, maxy;
471 struct graph_label *i;
472 struct graph_value *j;
473 int good_data = 1, first = 1;
476 graph_draw_common(g, cr, &x1, &y1, &x2, &y2);
478 minx = find_xy_value(g, getx, mindouble);
479 maxx = find_xy_value(g, getx, maxdouble);
480 miny = find_xy_value(g, gety, mindouble);
481 maxy = find_xy_value(g, gety, maxdouble);
483 if (fabs(maxx - minx) < 1e-20 || fabs(maxy - miny) < 1e-20) {
491 graph_draw_x_ticks(g, cr, x1, y1, x2, y2, minx, maxx, 10);
492 graph_draw_y_ticks(g, cr, x1, y1, x2, y2, miny, maxy, 10);
497 cairo_set_line_width(cr, 1.5);
498 for (i = g->labels; i; i = i->next) {
500 if (i->r < 0) /* invisible data */
502 cairo_set_source_rgb(cr, i->r, i->g, i->b);
503 for (j = i->values; j; j = j->next) {
504 tx = ((getx(j) - minx) / (maxx - minx)) * (x2 - x1) + x1;
505 ty = y2 - ((gety(j) - miny) / (maxy - miny)) * (y2 - y1);
507 cairo_move_to(cr, tx, ty);
510 cairo_line_to(cr, tx, ty);
521 static void gfree(void *f)
527 static void setstring(char **str, const char *value)
530 *str = strdup(value);
533 void graph_title(struct graph *bg, const char *title)
535 setstring(&bg->title, title);
538 void graph_x_title(struct graph *bg, const char *title)
540 setstring(&bg->xtitle, title);
543 void graph_y_title(struct graph *bg, const char *title)
545 setstring(&bg->ytitle, title);
548 static struct graph_label *graph_find_label(struct graph *bg,
551 struct graph_label *i;
553 for (i = bg->labels; i; i = i->next)
554 if (strcmp(label, i->label) == 0)
559 void graph_add_label(struct graph *bg, const char *label)
561 struct graph_label *i;
563 i = graph_find_label(bg, label);
565 return; /* already present. */
566 i = calloc(1, sizeof(*i));
568 setstring(&i->label, label);
577 static void graph_label_add_value(struct graph_label *i, void *value)
579 struct graph_value *x;
581 x = malloc(sizeof(*x));
592 if (i->parent->per_label_limit != -1 &&
593 i->value_count > i->parent->per_label_limit) {
595 i->values = i->values->next;
602 int graph_add_data(struct graph *bg, const char *label, const double value)
604 struct graph_label *i;
607 d = malloc(sizeof(*d));
610 i = graph_find_label(bg, label);
613 graph_label_add_value(i, d);
617 int graph_add_xy_data(struct graph *bg, const char *label,
618 const double x, const double y)
620 struct graph_label *i;
623 xy = malloc(sizeof(*xy));
627 i = graph_find_label(bg, label);
630 graph_label_add_value(i, xy);
634 static void graph_free_values(struct graph_value *values)
636 struct graph_value *i, *next;
638 for (i = values; i; i = next) {
645 static void graph_free_labels(struct graph_label *labels)
647 struct graph_label *i, *next;
649 for (i = labels; i; i = next) {
651 graph_free_values(i->values);
656 void graph_set_color(struct graph *gr, const char *label,
657 double red, double green, double blue)
659 struct graph_label *i;
662 if (red < 0.0) { /* invisible color */
679 for (i = gr->labels; i; i = i->next)
680 if (strcmp(i->label, label) == 0) {
688 void graph_free(struct graph *bg)
693 graph_free_labels(bg->labels);
696 /* For each line in the line graph, up to per_label_limit segments may
697 * be added. After that, adding more data to the end of the line
698 * causes data to drop off of the front of the line.
700 void line_graph_set_data_count_limit(struct graph *g, int per_label_limit)
702 g->per_label_limit = per_label_limit;