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 struct graph *graph_new(unsigned int xdim, unsigned int ydim, const char *font)
67 g = calloc(1, sizeof(*g));
70 g->per_label_limit = -1;
77 static int count_labels(struct graph_label *labels)
80 struct graph_label *i;
82 for (i = labels; i; i = i->next)
87 static int count_values(struct graph_value *values)
90 struct graph_value *i;
92 for (i = values; i; i = i->next)
97 typedef double (*double_comparator)(double a, double b);
99 static double mindouble(double a, double b)
101 return a < b ? a : b;
104 static double maxdouble(double a, double b)
106 return a < b ? b : a;
109 static double find_double_values(struct graph_value *values, double_comparator cmp)
111 struct graph_value *i;
115 assert(values != NULL);
116 answer = 0.0; /* shut the compiler up, might need to think harder though. */
117 for (i = values; i; i = i->next) {
118 tmp = *(double *) i->value;
123 answer = cmp(answer, tmp);
129 static double find_double_data(struct graph_label *labels, double_comparator cmp)
131 struct graph_label *i;
135 assert(labels != NULL);
136 answer = 0.0; /* shut the compiler up, might need to think harder though. */
137 for (i = labels; i; i = i->next) {
138 tmp = find_double_values(i->values, cmp);
143 answer = cmp(tmp, answer);
149 static double find_min_data(struct graph_label *labels)
151 return find_double_data(labels, mindouble);
154 static double find_max_data(struct graph_label *labels)
156 return find_double_data(labels, maxdouble);
159 static void draw_bars(struct graph *bg, cairo_t *cr, struct graph_label *lb,
160 double label_offset, double bar_width,
161 double mindata, double maxdata)
163 struct graph_value *i;
164 double x1, y1, x2, y2;
166 double domain, range, v;
168 domain = (maxdata - mindata);
169 range = (double) bg->ydim * 0.80; /* FIXME */
171 for (i = lb->values; i; i = i->next) {
173 x1 = label_offset + (double) bar_num * bar_width + (bar_width * 0.05);
174 x2 = x1 + bar_width * 0.90;
175 y2 = bg->ydim * 0.90;
176 v = *(double *) i->value;
177 y1 = y2 - (((v - mindata) / domain) * range);
178 cairo_move_to(cr, x1, y1);
179 cairo_line_to(cr, x1, y2);
180 cairo_line_to(cr, x2, y2);
181 cairo_line_to(cr, x2, y1);
182 cairo_close_path(cr);
189 static void draw_centered_text(struct graph *g, cairo_t *cr, double x, double y,
190 double fontsize, const char *text)
192 cairo_text_extents_t extents;
194 cairo_select_font_face (cr, g->font, CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
196 cairo_set_font_size(cr, fontsize);
197 cairo_text_extents(cr, text, &extents);
198 x = x - (extents.width / 2 + extents.x_bearing);
199 y = y - (extents.height / 2 + extents.y_bearing);
201 cairo_move_to(cr, x, y);
202 cairo_show_text(cr, text);
205 static void draw_vertical_centered_text(struct graph *g, cairo_t *cr, double x,
206 double y, double fontsize,
210 cairo_text_extents_t extents;
212 cairo_select_font_face(cr, g->font, CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
214 cairo_set_font_size(cr, fontsize);
215 cairo_text_extents(cr, text, &extents);
218 y = y + (extents.width / 2.0 + extents.x_bearing);
219 x = x - (extents.height / 2.0 + extents.y_bearing);
221 cairo_move_to(cr, x, y);
223 cairo_translate(cr, -sx, -sy);
224 cairo_rotate(cr, -90.0 * M_PI / 180.0);
225 cairo_translate(cr, sx, sy);
226 cairo_show_text(cr, text);
230 static void graph_draw_common(struct graph *g, cairo_t *cr,
231 double *x1, double *y1, double *x2, double *y2)
233 cairo_set_source_rgb(cr, 0, 0, 0);
234 cairo_set_line_width (cr, 0.8);
236 /* for now just set margins at 10% of width. This is not very good. */
237 *x1 = g->xdim / 10.0;
239 *y1 = g->ydim / 10.0;
242 cairo_move_to(cr, *x1, *y1);
243 cairo_line_to(cr, *x1, *y2);
244 cairo_line_to(cr, *x2, *y2);
245 cairo_line_to(cr, *x2, *y1);
246 cairo_line_to(cr, *x1, *y1);
249 draw_centered_text(g, cr, g->xdim / 2, g->ydim / 20, 20.0, g->title);
250 draw_centered_text(g, cr, g->xdim / 2, g->ydim * 0.97, 14.0, g->xtitle);
251 draw_vertical_centered_text(g, cr, g->xdim * 0.02, g->ydim / 2, 14.0, g->ytitle);
255 static void graph_draw_x_ticks(struct graph *g, cairo_t *cr,
256 double x1, double y1, double x2, double y2,
257 double minx, double maxx, int nticks)
262 static double dash[] = { 1.0, 2.0 };
264 nticks = calc_tickmarks(minx, maxx, nticks, &tm);
266 for (i = 0; i < nticks; i++) {
267 tx = (((tm[i].value) - minx) / (maxx - minx)) * (x2 - x1) + x1;
268 if (tx < x1 || tx > x2)
272 cairo_set_line_width(cr, 0.8);
273 cairo_move_to(cr, tx, y2);
274 cairo_line_to(cr, tx, y2 + (y2 - y1) * 0.03);
277 /* draw grid lines */
279 cairo_set_dash(cr, dash, 2, 2.0);
280 cairo_set_line_width(cr, 0.5);
281 cairo_move_to(cr, tx, y1);
282 cairo_line_to(cr, tx, y2);
286 /* draw tickmark label */
287 draw_centered_text(g, cr, tx, y2 * 1.04, 12.0, tm[i].string);
293 static void graph_draw_y_ticks(struct graph *g, cairo_t *cr,
294 double x1, double y1, double x2, double y2,
295 double miny, double maxy, int nticks)
300 static double dash[] = { 2.0, 2.0 };
302 nticks = calc_tickmarks(miny, maxy, nticks, &tm);
304 for (i = 0; i < nticks; i++) {
305 ty = y2 - (((tm[i].value) - miny) / (maxy - miny)) * (y2 - y1);
306 if (ty < y1 || ty > y2)
309 cairo_move_to(cr, x1, ty);
310 cairo_line_to(cr, x1 - (x2 - x1) * 0.02, ty);
313 /* draw grid lines */
315 cairo_set_dash(cr, dash, 2, 2.0);
316 cairo_set_line_width(cr, 0.5);
317 cairo_move_to(cr, x1, ty);
318 cairo_line_to(cr, x2, ty);
322 /* draw tickmark label */
323 draw_centered_text(g, cr, x1 - (x2 - x1) * 0.04, ty, 12.0, tm[i].string);
328 void bar_graph_draw(struct graph *bg, cairo_t *cr)
330 double x1, y1, x2, y2;
331 double space_per_label, bar_width;
332 double label_offset, mindata, maxdata;
334 struct graph_label *lb;
337 graph_draw_common(bg, cr, &x1, &y1, &x2, &y2);
339 nlabels = count_labels(bg->labels);
340 space_per_label = (x2 - x1) / (double) nlabels;
342 mindata = find_min_data(bg->labels);
343 maxdata = find_max_data(bg->labels);
345 if (fabs(maxdata - mindata) < 1e-20) {
346 draw_centered_text(bg, cr,
347 x1 + (x2 - x1) / 2.0,
348 y1 + (y2 - y1) / 2.0, 20.0, "No good data");
352 graph_draw_y_ticks(bg, cr, x1, y1, x2, y2, mindata, maxdata, 10);
355 for (lb = bg->labels; lb; lb = lb->next) {
357 nvalues = count_values(lb->values);
358 bar_width = (space_per_label - space_per_label * 0.2) / (double) nvalues;
359 label_offset = bg->xdim * 0.1 + space_per_label * (double) i + space_per_label * 0.1;
360 draw_bars(bg, cr, lb, label_offset, bar_width, mindata, maxdata);
361 // draw_centered_text(cr, label_offset + (bar_width / 2.0 + bar_width * 0.1), bg->ydim * 0.93,
362 draw_centered_text(bg, cr, x1 + space_per_label * (i + 0.5), bg->ydim * 0.93,
370 typedef double (*xy_value_extractor)(struct graph_value *v);
372 static double getx(struct graph_value *v)
374 struct xyvalue *xy = v->value;
378 static double gety(struct graph_value *v)
380 struct xyvalue *xy = v->value;
384 static double find_xy_value(struct graph *g, xy_value_extractor getvalue, double_comparator cmp)
386 double tmp, answer = 0.0;
387 struct graph_label *i;
388 struct graph_value *j;
390 for (i = g->labels; i; i = i->next)
391 for (j = i->values; j; j = j->next) {
393 answer = cmp(tmp, answer);
398 void line_graph_draw(struct graph *g, cairo_t *cr)
400 double x1, y1, x2, y2;
401 double minx, miny, maxx, maxy;
403 struct graph_label *i;
404 struct graph_value *j;
408 graph_draw_common(g, cr, &x1, &y1, &x2, &y2);
410 minx = find_xy_value(g, getx, mindouble);
411 maxx = find_xy_value(g, getx, maxdouble);
412 miny = find_xy_value(g, gety, mindouble);
413 maxy = find_xy_value(g, gety, maxdouble);
415 if (fabs(maxx - minx) < 1e-20 || fabs(maxy - miny) < 1e-20) {
416 draw_centered_text(g, cr,
417 x1 + (x2 - x1) / 2.0,
418 y1 + (y2 - y1) / 2.0, 20.0, "No good Data");
422 graph_draw_x_ticks(g, cr, x1, y1, x2, y2, minx, maxx, 10);
423 graph_draw_y_ticks(g, cr, x1, y1, x2, y2, miny, maxy, 10);
425 cairo_set_line_width(cr, 1.5);
426 for (i = g->labels; i; i = i->next) {
428 if (i->r < 0) /* invisible data */
430 cairo_set_source_rgb(cr, i->r, i->g, i->b);
431 for (j = i->values; j; j = j->next) {
432 tx = ((getx(j) - minx) / (maxx - minx)) * (x2 - x1) + x1;
433 ty = y2 - ((gety(j) - miny) / (maxy - miny)) * (y2 - y1);
435 cairo_move_to(cr, tx, ty);
438 cairo_line_to(cr, tx, ty);
446 static void gfree(void *f)
452 static void setstring(char **str, const char *value)
455 *str = strdup(value);
458 void graph_title(struct graph *bg, const char *title)
460 setstring(&bg->title, title);
463 void graph_x_title(struct graph *bg, const char *title)
465 setstring(&bg->xtitle, title);
468 void graph_y_title(struct graph *bg, const char *title)
470 setstring(&bg->ytitle, title);
473 static struct graph_label *graph_find_label(struct graph *bg,
476 struct graph_label *i;
478 for (i = bg->labels; i; i = i->next)
479 if (strcmp(label, i->label) == 0)
484 void graph_add_label(struct graph *bg, const char *label)
486 struct graph_label *i;
488 i = graph_find_label(bg, label);
490 return; /* already present. */
491 i = calloc(1, sizeof(*i));
493 setstring(&i->label, label);
502 static void graph_label_add_value(struct graph_label *i, void *value)
504 struct graph_value *x;
506 x = malloc(sizeof(*x));
517 if (i->parent->per_label_limit != -1 &&
518 i->value_count > i->parent->per_label_limit) {
520 i->values = i->values->next;
527 int graph_add_data(struct graph *bg, const char *label, const double value)
529 struct graph_label *i;
532 d = malloc(sizeof(*d));
535 i = graph_find_label(bg, label);
538 graph_label_add_value(i, d);
542 int graph_add_xy_data(struct graph *bg, const char *label,
543 const double x, const double y)
545 struct graph_label *i;
548 xy = malloc(sizeof(*xy));
552 i = graph_find_label(bg, label);
555 graph_label_add_value(i, xy);
559 static void graph_free_values(struct graph_value *values)
561 struct graph_value *i, *next;
563 for (i = values; i; i = next) {
570 static void graph_free_labels(struct graph_label *labels)
572 struct graph_label *i, *next;
574 for (i = labels; i; i = next) {
576 graph_free_values(i->values);
581 void graph_set_color(struct graph *gr, const char *label,
582 double red, double green, double blue)
584 struct graph_label *i;
587 if (red < 0.0) { /* invisible color */
604 for (i = gr->labels; i; i = i->next)
605 if (strcmp(i->label, label) == 0) {
613 void graph_free(struct graph *bg)
618 graph_free_labels(bg->labels);
621 /* For each line in the line graph, up to per_label_limit segments may
622 * be added. After that, adding more data to the end of the line
623 * causes data to drop off of the front of the line.
625 void line_graph_set_data_count_limit(struct graph *g, int per_label_limit)
627 g->per_label_limit = per_label_limit;