Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 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> |
Jens Axboe | 93e2db2 | 2012-03-13 09:45:22 +0100 | [diff] [blame] | 27 | #include <stdlib.h> |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 28 | |
| 29 | #include <cairo.h> |
| 30 | #include <gtk/gtk.h> |
| 31 | |
| 32 | #include "tickmarks.h" |
Stephen M. Cameron | 09ad20f | 2012-03-11 11:34:38 +0100 | [diff] [blame] | 33 | #include "graph.h" |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 34 | |
| 35 | struct xyvalue { |
| 36 | double x, y; |
Jens Axboe | 93e2db2 | 2012-03-13 09:45:22 +0100 | [diff] [blame] | 37 | int gx, gy; |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 38 | }; |
| 39 | |
| 40 | struct graph_value { |
| 41 | struct graph_value *next; |
Jens Axboe | 93e2db2 | 2012-03-13 09:45:22 +0100 | [diff] [blame] | 42 | char *tooltip; |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 43 | void *value; |
| 44 | }; |
| 45 | |
| 46 | struct graph_label { |
| 47 | char *label; |
| 48 | struct graph_value *tail; |
| 49 | struct graph_value *values; |
| 50 | struct graph_label *next; |
| 51 | double r, g, b; |
| 52 | int value_count; |
Jens Axboe | 93e2db2 | 2012-03-13 09:45:22 +0100 | [diff] [blame] | 53 | unsigned int tooltip_count; |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 54 | struct graph *parent; |
| 55 | }; |
| 56 | |
| 57 | struct graph { |
| 58 | char *title; |
| 59 | char *xtitle; |
| 60 | char *ytitle; |
Jens Axboe | 87d5f27 | 2012-03-07 12:31:40 +0100 | [diff] [blame] | 61 | unsigned int xdim, ydim; |
Stephen M. Cameron | 57f9d28 | 2012-03-11 11:36:51 +0100 | [diff] [blame] | 62 | double xoffset, yoffset; |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 63 | struct graph_label *labels; |
| 64 | struct graph_label *tail; |
| 65 | int per_label_limit; |
Jens Axboe | f3e8440 | 2012-03-07 13:14:32 +0100 | [diff] [blame] | 66 | const char *font; |
Stephen M. Cameron | 7175d91 | 2012-03-11 11:35:10 +0100 | [diff] [blame] | 67 | graph_axis_unit_change_callback x_axis_unit_change_callback; |
| 68 | graph_axis_unit_change_callback y_axis_unit_change_callback; |
Jens Axboe | d8fbeef | 2012-03-14 10:25:44 +0100 | [diff] [blame] | 69 | unsigned int base_offset; |
Stephen M. Cameron | def0ac2 | 2012-03-12 07:32:57 +0100 | [diff] [blame] | 70 | double left_extra; |
| 71 | double right_extra; |
| 72 | double top_extra; |
| 73 | double bottom_extra; |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 74 | }; |
| 75 | |
Stephen M. Cameron | 3ea48b8 | 2012-03-07 19:40:58 +0100 | [diff] [blame] | 76 | void graph_set_size(struct graph *g, unsigned int xdim, unsigned int ydim) |
| 77 | { |
| 78 | g->xdim = xdim; |
| 79 | g->ydim = ydim; |
| 80 | } |
| 81 | |
Stephen M. Cameron | 57f9d28 | 2012-03-11 11:36:51 +0100 | [diff] [blame] | 82 | void graph_set_position(struct graph *g, double xoffset, double yoffset) |
| 83 | { |
| 84 | g->xoffset = xoffset; |
| 85 | g->yoffset = yoffset; |
| 86 | } |
| 87 | |
Jens Axboe | f3e8440 | 2012-03-07 13:14:32 +0100 | [diff] [blame] | 88 | struct graph *graph_new(unsigned int xdim, unsigned int ydim, const char *font) |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 89 | { |
| 90 | struct graph *g; |
| 91 | |
| 92 | g = calloc(1, sizeof(*g)); |
Stephen M. Cameron | 3ea48b8 | 2012-03-07 19:40:58 +0100 | [diff] [blame] | 93 | graph_set_size(g, xdim, ydim); |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 94 | g->per_label_limit = -1; |
Jens Axboe | f3e8440 | 2012-03-07 13:14:32 +0100 | [diff] [blame] | 95 | g->font = font; |
| 96 | if (!g->font) |
| 97 | g->font = "Sans"; |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 98 | return g; |
| 99 | } |
| 100 | |
Stephen M. Cameron | 7175d91 | 2012-03-11 11:35:10 +0100 | [diff] [blame] | 101 | void graph_x_axis_unit_change_notify(struct graph *g, graph_axis_unit_change_callback f) |
| 102 | { |
| 103 | g->x_axis_unit_change_callback = f; |
| 104 | } |
| 105 | |
| 106 | void graph_y_axis_unit_change_notify(struct graph *g, graph_axis_unit_change_callback f) |
| 107 | { |
| 108 | g->y_axis_unit_change_callback = f; |
| 109 | } |
| 110 | |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 111 | static int count_labels(struct graph_label *labels) |
| 112 | { |
| 113 | int count = 0; |
| 114 | struct graph_label *i; |
| 115 | |
| 116 | for (i = labels; i; i = i->next) |
| 117 | count++; |
| 118 | return count; |
| 119 | } |
| 120 | |
| 121 | static int count_values(struct graph_value *values) |
| 122 | { |
| 123 | int count = 0; |
| 124 | struct graph_value *i; |
| 125 | |
| 126 | for (i = values; i; i = i->next) |
| 127 | count++; |
| 128 | return count; |
| 129 | } |
| 130 | |
| 131 | typedef double (*double_comparator)(double a, double b); |
| 132 | |
| 133 | static double mindouble(double a, double b) |
| 134 | { |
| 135 | return a < b ? a : b; |
| 136 | } |
| 137 | |
| 138 | static double maxdouble(double a, double b) |
| 139 | { |
| 140 | return a < b ? b : a; |
| 141 | } |
| 142 | |
| 143 | static double find_double_values(struct graph_value *values, double_comparator cmp) |
| 144 | { |
| 145 | struct graph_value *i; |
| 146 | int first = 1; |
| 147 | double answer, tmp; |
| 148 | |
| 149 | assert(values != NULL); |
| 150 | answer = 0.0; /* shut the compiler up, might need to think harder though. */ |
| 151 | for (i = values; i; i = i->next) { |
| 152 | tmp = *(double *) i->value; |
| 153 | if (first) { |
| 154 | answer = tmp; |
| 155 | first = 0; |
| 156 | } else { |
| 157 | answer = cmp(answer, tmp); |
| 158 | } |
| 159 | } |
| 160 | return answer; |
| 161 | } |
| 162 | |
| 163 | static double find_double_data(struct graph_label *labels, double_comparator cmp) |
| 164 | { |
| 165 | struct graph_label *i; |
| 166 | int first = 1; |
| 167 | double answer, tmp; |
| 168 | |
| 169 | assert(labels != NULL); |
| 170 | answer = 0.0; /* shut the compiler up, might need to think harder though. */ |
| 171 | for (i = labels; i; i = i->next) { |
| 172 | tmp = find_double_values(i->values, cmp); |
| 173 | if (first) { |
| 174 | answer = tmp; |
| 175 | first = 0; |
| 176 | } else { |
| 177 | answer = cmp(tmp, answer); |
| 178 | } |
| 179 | } |
| 180 | return answer; |
| 181 | } |
| 182 | |
| 183 | static double find_min_data(struct graph_label *labels) |
| 184 | { |
| 185 | return find_double_data(labels, mindouble); |
| 186 | } |
| 187 | |
| 188 | static double find_max_data(struct graph_label *labels) |
| 189 | { |
| 190 | return find_double_data(labels, maxdouble); |
| 191 | } |
| 192 | |
| 193 | static void draw_bars(struct graph *bg, cairo_t *cr, struct graph_label *lb, |
| 194 | double label_offset, double bar_width, |
| 195 | double mindata, double maxdata) |
| 196 | { |
| 197 | struct graph_value *i; |
| 198 | double x1, y1, x2, y2; |
| 199 | int bar_num = 0; |
| 200 | double domain, range, v; |
| 201 | |
| 202 | domain = (maxdata - mindata); |
| 203 | range = (double) bg->ydim * 0.80; /* FIXME */ |
| 204 | cairo_stroke(cr); |
| 205 | for (i = lb->values; i; i = i->next) { |
| 206 | |
| 207 | x1 = label_offset + (double) bar_num * bar_width + (bar_width * 0.05); |
| 208 | x2 = x1 + bar_width * 0.90; |
| 209 | y2 = bg->ydim * 0.90; |
| 210 | v = *(double *) i->value; |
| 211 | y1 = y2 - (((v - mindata) / domain) * range); |
| 212 | cairo_move_to(cr, x1, y1); |
| 213 | cairo_line_to(cr, x1, y2); |
| 214 | cairo_line_to(cr, x2, y2); |
| 215 | cairo_line_to(cr, x2, y1); |
| 216 | cairo_close_path(cr); |
| 217 | cairo_fill(cr); |
| 218 | cairo_stroke(cr); |
| 219 | bar_num++; |
| 220 | } |
| 221 | } |
| 222 | |
Stephen M. Cameron | 10e54cd | 2012-03-07 19:39:55 +0100 | [diff] [blame] | 223 | static void draw_aligned_text(struct graph *g, cairo_t *cr, double x, double y, |
| 224 | double fontsize, const char *text, int alignment) |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 225 | { |
Stephen M. Cameron | 10e54cd | 2012-03-07 19:39:55 +0100 | [diff] [blame] | 226 | #define CENTERED 0 |
| 227 | #define LEFT_JUSTIFIED 1 |
| 228 | #define RIGHT_JUSTIFIED 2 |
| 229 | |
| 230 | double factor, direction; |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 231 | cairo_text_extents_t extents; |
| 232 | |
Stephen M. Cameron | 10e54cd | 2012-03-07 19:39:55 +0100 | [diff] [blame] | 233 | switch(alignment) { |
| 234 | case CENTERED: |
| 235 | direction = -1.0; |
| 236 | factor = 0.5; |
| 237 | break; |
| 238 | case RIGHT_JUSTIFIED: |
| 239 | direction = -1.0; |
| 240 | factor = 1.0; |
| 241 | break; |
| 242 | case LEFT_JUSTIFIED: |
| 243 | default: |
| 244 | direction = 1.0; |
| 245 | factor = 1.0; |
| 246 | break; |
| 247 | } |
Jens Axboe | f3e8440 | 2012-03-07 13:14:32 +0100 | [diff] [blame] | 248 | cairo_select_font_face (cr, g->font, CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL); |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 249 | |
| 250 | cairo_set_font_size(cr, fontsize); |
| 251 | cairo_text_extents(cr, text, &extents); |
Stephen M. Cameron | 10e54cd | 2012-03-07 19:39:55 +0100 | [diff] [blame] | 252 | x = x + direction * (factor * extents.width + extents.x_bearing); |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 253 | y = y - (extents.height / 2 + extents.y_bearing); |
| 254 | |
| 255 | cairo_move_to(cr, x, y); |
| 256 | cairo_show_text(cr, text); |
| 257 | } |
| 258 | |
Stephen M. Cameron | 10e54cd | 2012-03-07 19:39:55 +0100 | [diff] [blame] | 259 | static inline void draw_centered_text(struct graph *g, cairo_t *cr, double x, double y, |
| 260 | double fontsize, const char *text) |
| 261 | { |
| 262 | draw_aligned_text(g, cr, x, y, fontsize, text, CENTERED); |
| 263 | } |
| 264 | |
| 265 | static inline void draw_right_justified_text(struct graph *g, cairo_t *cr, |
| 266 | double x, double y, |
| 267 | double fontsize, const char *text) |
| 268 | { |
| 269 | draw_aligned_text(g, cr, x, y, fontsize, text, RIGHT_JUSTIFIED); |
| 270 | } |
| 271 | |
| 272 | static inline void draw_left_justified_text(struct graph *g, cairo_t *cr, |
| 273 | double x, double y, |
| 274 | double fontsize, const char *text) |
| 275 | { |
| 276 | draw_aligned_text(g, cr, x, y, fontsize, text, LEFT_JUSTIFIED); |
| 277 | } |
| 278 | |
Jens Axboe | f3e8440 | 2012-03-07 13:14:32 +0100 | [diff] [blame] | 279 | static void draw_vertical_centered_text(struct graph *g, cairo_t *cr, double x, |
| 280 | double y, double fontsize, |
| 281 | const char *text) |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 282 | { |
| 283 | double sx, sy; |
| 284 | cairo_text_extents_t extents; |
| 285 | |
Jens Axboe | f3e8440 | 2012-03-07 13:14:32 +0100 | [diff] [blame] | 286 | cairo_select_font_face(cr, g->font, CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL); |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 287 | |
| 288 | cairo_set_font_size(cr, fontsize); |
| 289 | cairo_text_extents(cr, text, &extents); |
| 290 | sx = x; |
| 291 | sy = y; |
| 292 | y = y + (extents.width / 2.0 + extents.x_bearing); |
| 293 | x = x - (extents.height / 2.0 + extents.y_bearing); |
| 294 | |
| 295 | cairo_move_to(cr, x, y); |
| 296 | cairo_save(cr); |
| 297 | cairo_translate(cr, -sx, -sy); |
| 298 | cairo_rotate(cr, -90.0 * M_PI / 180.0); |
| 299 | cairo_translate(cr, sx, sy); |
| 300 | cairo_show_text(cr, text); |
| 301 | cairo_restore(cr); |
| 302 | } |
| 303 | |
| 304 | static void graph_draw_common(struct graph *g, cairo_t *cr, |
| 305 | double *x1, double *y1, double *x2, double *y2) |
| 306 | { |
| 307 | cairo_set_source_rgb(cr, 0, 0, 0); |
Jens Axboe | f3e8440 | 2012-03-07 13:14:32 +0100 | [diff] [blame] | 308 | cairo_set_line_width (cr, 0.8); |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 309 | |
Jens Axboe | 9ede9c9 | 2012-03-14 10:43:20 +0100 | [diff] [blame] | 310 | *x1 = 0.10 * g->xdim; |
Stephen M. Cameron | 6bf8600 | 2012-03-08 16:58:50 +0100 | [diff] [blame] | 311 | *x2 = 0.95 * g->xdim; |
| 312 | *y1 = 0.10 * g->ydim; |
| 313 | *y2 = 0.90 * g->ydim; |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 314 | |
| 315 | cairo_move_to(cr, *x1, *y1); |
| 316 | cairo_line_to(cr, *x1, *y2); |
| 317 | cairo_line_to(cr, *x2, *y2); |
| 318 | cairo_line_to(cr, *x2, *y1); |
| 319 | cairo_line_to(cr, *x1, *y1); |
| 320 | cairo_stroke(cr); |
| 321 | |
Jens Axboe | f3e8440 | 2012-03-07 13:14:32 +0100 | [diff] [blame] | 322 | draw_centered_text(g, cr, g->xdim / 2, g->ydim / 20, 20.0, g->title); |
| 323 | draw_centered_text(g, cr, g->xdim / 2, g->ydim * 0.97, 14.0, g->xtitle); |
| 324 | draw_vertical_centered_text(g, cr, g->xdim * 0.02, g->ydim / 2, 14.0, g->ytitle); |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 325 | cairo_stroke(cr); |
| 326 | } |
| 327 | |
| 328 | static void graph_draw_x_ticks(struct graph *g, cairo_t *cr, |
| 329 | double x1, double y1, double x2, double y2, |
Jens Axboe | d8fbeef | 2012-03-14 10:25:44 +0100 | [diff] [blame] | 330 | double minx, double maxx, int nticks, int add_tm_text) |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 331 | { |
| 332 | struct tickmark *tm; |
| 333 | double tx; |
Stephen M. Cameron | 7175d91 | 2012-03-11 11:35:10 +0100 | [diff] [blame] | 334 | int i, power_of_ten; |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 335 | static double dash[] = { 1.0, 2.0 }; |
| 336 | |
Stephen M. Cameron | 7175d91 | 2012-03-11 11:35:10 +0100 | [diff] [blame] | 337 | nticks = calc_tickmarks(minx, maxx, nticks, &tm, &power_of_ten, |
Jens Axboe | d8fbeef | 2012-03-14 10:25:44 +0100 | [diff] [blame] | 338 | g->x_axis_unit_change_callback == NULL, g->base_offset); |
Stephen M. Cameron | 7175d91 | 2012-03-11 11:35:10 +0100 | [diff] [blame] | 339 | if (g->x_axis_unit_change_callback) |
| 340 | g->x_axis_unit_change_callback(g, power_of_ten); |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 341 | |
| 342 | for (i = 0; i < nticks; i++) { |
| 343 | tx = (((tm[i].value) - minx) / (maxx - minx)) * (x2 - x1) + x1; |
Jens Axboe | d8fbeef | 2012-03-14 10:25:44 +0100 | [diff] [blame] | 344 | |
| 345 | /* really tx < yx || tx > x2, but protect against rounding */ |
| 346 | if (x1 - tx > 0.01 || tx - x2 > 0.01) |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 347 | continue; |
| 348 | |
| 349 | /* Draw tick mark */ |
Jens Axboe | f3e8440 | 2012-03-07 13:14:32 +0100 | [diff] [blame] | 350 | cairo_set_line_width(cr, 0.8); |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 351 | cairo_move_to(cr, tx, y2); |
| 352 | cairo_line_to(cr, tx, y2 + (y2 - y1) * 0.03); |
| 353 | cairo_stroke(cr); |
| 354 | |
| 355 | /* draw grid lines */ |
| 356 | cairo_save(cr); |
| 357 | cairo_set_dash(cr, dash, 2, 2.0); |
| 358 | cairo_set_line_width(cr, 0.5); |
| 359 | cairo_move_to(cr, tx, y1); |
| 360 | cairo_line_to(cr, tx, y2); |
| 361 | cairo_stroke(cr); |
| 362 | cairo_restore(cr); |
| 363 | |
Jens Axboe | d8fbeef | 2012-03-14 10:25:44 +0100 | [diff] [blame] | 364 | if (!add_tm_text) |
| 365 | continue; |
| 366 | |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 367 | /* draw tickmark label */ |
Jens Axboe | f3e8440 | 2012-03-07 13:14:32 +0100 | [diff] [blame] | 368 | draw_centered_text(g, cr, tx, y2 * 1.04, 12.0, tm[i].string); |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 369 | cairo_stroke(cr); |
| 370 | |
| 371 | } |
| 372 | } |
| 373 | |
Jens Axboe | d8fbeef | 2012-03-14 10:25:44 +0100 | [diff] [blame] | 374 | static double graph_draw_y_ticks(struct graph *g, cairo_t *cr, |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 375 | double x1, double y1, double x2, double y2, |
Jens Axboe | d8fbeef | 2012-03-14 10:25:44 +0100 | [diff] [blame] | 376 | double miny, double maxy, int nticks, int add_tm_text) |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 377 | { |
| 378 | struct tickmark *tm; |
| 379 | double ty; |
Stephen M. Cameron | 7175d91 | 2012-03-11 11:35:10 +0100 | [diff] [blame] | 380 | int i, power_of_ten; |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 381 | static double dash[] = { 2.0, 2.0 }; |
| 382 | |
Stephen M. Cameron | 7175d91 | 2012-03-11 11:35:10 +0100 | [diff] [blame] | 383 | nticks = calc_tickmarks(miny, maxy, nticks, &tm, &power_of_ten, |
Jens Axboe | d8fbeef | 2012-03-14 10:25:44 +0100 | [diff] [blame] | 384 | g->y_axis_unit_change_callback == NULL, g->base_offset); |
Stephen M. Cameron | 7175d91 | 2012-03-11 11:35:10 +0100 | [diff] [blame] | 385 | if (g->y_axis_unit_change_callback) |
| 386 | g->y_axis_unit_change_callback(g, power_of_ten); |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 387 | |
Jens Axboe | d8fbeef | 2012-03-14 10:25:44 +0100 | [diff] [blame] | 388 | /* |
| 389 | * Use highest tickmark as top of graph, not highest value. Otherwise |
| 390 | * it's impossible to see what the max value is, if the graph is |
| 391 | * fairly flat. |
| 392 | */ |
| 393 | maxy = tm[nticks - 1].value; |
| 394 | |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 395 | for (i = 0; i < nticks; i++) { |
| 396 | ty = y2 - (((tm[i].value) - miny) / (maxy - miny)) * (y2 - y1); |
Jens Axboe | d8fbeef | 2012-03-14 10:25:44 +0100 | [diff] [blame] | 397 | |
| 398 | /* really ty < y1 || ty > y2, but protect against rounding */ |
| 399 | if (y1 - ty > 0.01 || ty - y2 > 0.01) |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 400 | continue; |
Jens Axboe | d8fbeef | 2012-03-14 10:25:44 +0100 | [diff] [blame] | 401 | |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 402 | /* draw tick mark */ |
| 403 | cairo_move_to(cr, x1, ty); |
| 404 | cairo_line_to(cr, x1 - (x2 - x1) * 0.02, ty); |
| 405 | cairo_stroke(cr); |
| 406 | |
| 407 | /* draw grid lines */ |
| 408 | cairo_save(cr); |
| 409 | cairo_set_dash(cr, dash, 2, 2.0); |
| 410 | cairo_set_line_width(cr, 0.5); |
| 411 | cairo_move_to(cr, x1, ty); |
| 412 | cairo_line_to(cr, x2, ty); |
| 413 | cairo_stroke(cr); |
| 414 | cairo_restore(cr); |
| 415 | |
Jens Axboe | d8fbeef | 2012-03-14 10:25:44 +0100 | [diff] [blame] | 416 | if (!add_tm_text) |
| 417 | continue; |
| 418 | |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 419 | /* draw tickmark label */ |
Stephen M. Cameron | 10e54cd | 2012-03-07 19:39:55 +0100 | [diff] [blame] | 420 | draw_right_justified_text(g, cr, x1 - (x2 - x1) * 0.025, ty, 12.0, tm[i].string); |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 421 | cairo_stroke(cr); |
| 422 | } |
Jens Axboe | d8fbeef | 2012-03-14 10:25:44 +0100 | [diff] [blame] | 423 | |
| 424 | /* |
| 425 | * Return new max to use |
| 426 | */ |
| 427 | return maxy; |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | void bar_graph_draw(struct graph *bg, cairo_t *cr) |
| 431 | { |
| 432 | double x1, y1, x2, y2; |
| 433 | double space_per_label, bar_width; |
| 434 | double label_offset, mindata, maxdata; |
| 435 | int i, nlabels; |
| 436 | struct graph_label *lb; |
| 437 | |
| 438 | cairo_save(cr); |
Stephen M. Cameron | 57f9d28 | 2012-03-11 11:36:51 +0100 | [diff] [blame] | 439 | cairo_translate(cr, bg->xoffset, bg->yoffset); |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 440 | graph_draw_common(bg, cr, &x1, &y1, &x2, &y2); |
| 441 | |
| 442 | nlabels = count_labels(bg->labels); |
Jens Axboe | d8fbeef | 2012-03-14 10:25:44 +0100 | [diff] [blame] | 443 | space_per_label = (x2 - x1) / (double) nlabels; |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 444 | |
Jens Axboe | bb39379 | 2012-03-15 10:52:38 +0100 | [diff] [blame] | 445 | /* |
| 446 | * Start bars at 0 unless we have negative values, otherwise we |
| 447 | * present a skewed picture comparing label X and X+1. |
| 448 | */ |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 449 | mindata = find_min_data(bg->labels); |
Jens Axboe | bb39379 | 2012-03-15 10:52:38 +0100 | [diff] [blame] | 450 | if (mindata > 0) |
| 451 | mindata = 0; |
| 452 | |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 453 | maxdata = find_max_data(bg->labels); |
| 454 | |
| 455 | if (fabs(maxdata - mindata) < 1e-20) { |
Jens Axboe | f3e8440 | 2012-03-07 13:14:32 +0100 | [diff] [blame] | 456 | draw_centered_text(bg, cr, |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 457 | x1 + (x2 - x1) / 2.0, |
| 458 | y1 + (y2 - y1) / 2.0, 20.0, "No good data"); |
| 459 | return; |
| 460 | } |
| 461 | |
Jens Axboe | bb39379 | 2012-03-15 10:52:38 +0100 | [diff] [blame] | 462 | maxdata = graph_draw_y_ticks(bg, cr, x1, y1, x2, y2, mindata, maxdata, 10, 1); |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 463 | i = 0; |
| 464 | for (lb = bg->labels; lb; lb = lb->next) { |
| 465 | int nvalues; |
| 466 | nvalues = count_values(lb->values); |
| 467 | bar_width = (space_per_label - space_per_label * 0.2) / (double) nvalues; |
| 468 | label_offset = bg->xdim * 0.1 + space_per_label * (double) i + space_per_label * 0.1; |
| 469 | draw_bars(bg, cr, lb, label_offset, bar_width, mindata, maxdata); |
| 470 | // draw_centered_text(cr, label_offset + (bar_width / 2.0 + bar_width * 0.1), bg->ydim * 0.93, |
Jens Axboe | f3e8440 | 2012-03-07 13:14:32 +0100 | [diff] [blame] | 471 | draw_centered_text(bg, cr, x1 + space_per_label * (i + 0.5), bg->ydim * 0.93, |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 472 | 12.0, lb->label); |
| 473 | i++; |
| 474 | } |
| 475 | cairo_stroke(cr); |
| 476 | cairo_restore(cr); |
| 477 | } |
| 478 | |
| 479 | typedef double (*xy_value_extractor)(struct graph_value *v); |
| 480 | |
| 481 | static double getx(struct graph_value *v) |
| 482 | { |
| 483 | struct xyvalue *xy = v->value; |
| 484 | return xy->x; |
| 485 | } |
| 486 | |
| 487 | static double gety(struct graph_value *v) |
| 488 | { |
| 489 | struct xyvalue *xy = v->value; |
| 490 | return xy->y; |
| 491 | } |
| 492 | |
| 493 | static double find_xy_value(struct graph *g, xy_value_extractor getvalue, double_comparator cmp) |
| 494 | { |
Stephen M. Cameron | 10e54cd | 2012-03-07 19:39:55 +0100 | [diff] [blame] | 495 | double tmp, answer = 0.0; |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 496 | struct graph_label *i; |
| 497 | struct graph_value *j; |
Stephen M. Cameron | d582bf7 | 2012-03-07 19:37:57 +0100 | [diff] [blame] | 498 | int first = 1; |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 499 | |
| 500 | for (i = g->labels; i; i = i->next) |
| 501 | for (j = i->values; j; j = j->next) { |
| 502 | tmp = getvalue(j); |
Stephen M. Cameron | d582bf7 | 2012-03-07 19:37:57 +0100 | [diff] [blame] | 503 | if (first) { |
| 504 | first = 0; |
| 505 | answer = tmp; |
| 506 | } |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 507 | answer = cmp(tmp, answer); |
| 508 | } |
| 509 | return answer; |
| 510 | } |
| 511 | |
| 512 | void line_graph_draw(struct graph *g, cairo_t *cr) |
| 513 | { |
| 514 | double x1, y1, x2, y2; |
Stephen M. Cameron | def0ac2 | 2012-03-12 07:32:57 +0100 | [diff] [blame] | 515 | double minx, miny, maxx, maxy, gminx, gminy, gmaxx, gmaxy; |
| 516 | double tx, ty, top_extra, bottom_extra, left_extra, right_extra; |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 517 | struct graph_label *i; |
| 518 | struct graph_value *j; |
Stephen M. Cameron | 9ce9cfb | 2012-03-07 19:37:25 +0100 | [diff] [blame] | 519 | int good_data = 1, first = 1; |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 520 | |
| 521 | cairo_save(cr); |
Stephen M. Cameron | 57f9d28 | 2012-03-11 11:36:51 +0100 | [diff] [blame] | 522 | cairo_translate(cr, g->xoffset, g->yoffset); |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 523 | graph_draw_common(g, cr, &x1, &y1, &x2, &y2); |
| 524 | |
| 525 | minx = find_xy_value(g, getx, mindouble); |
| 526 | maxx = find_xy_value(g, getx, maxdouble); |
| 527 | miny = find_xy_value(g, gety, mindouble); |
Jens Axboe | 5aec668 | 2012-03-16 10:17:08 +0100 | [diff] [blame] | 528 | |
| 529 | /* |
| 530 | * Start graphs at zero, unless we have a value below. Otherwise |
| 531 | * it's hard to visually compare the read and write graph, since |
| 532 | * the lowest valued one will be the floor of the graph view. |
| 533 | */ |
| 534 | if (miny > 0) |
| 535 | miny = 0; |
| 536 | |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 537 | maxy = find_xy_value(g, gety, maxdouble); |
| 538 | |
| 539 | if (fabs(maxx - minx) < 1e-20 || fabs(maxy - miny) < 1e-20) { |
Stephen M. Cameron | 9ce9cfb | 2012-03-07 19:37:25 +0100 | [diff] [blame] | 540 | good_data = 0; |
| 541 | minx = 0.0; |
| 542 | miny = 0.0; |
| 543 | maxx = 10.0; |
| 544 | maxy = 100.0; |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 545 | } |
| 546 | |
Stephen M. Cameron | def0ac2 | 2012-03-12 07:32:57 +0100 | [diff] [blame] | 547 | top_extra = 0.0; |
| 548 | bottom_extra = 0.0; |
| 549 | left_extra = 0.0; |
| 550 | right_extra = 0.0; |
| 551 | |
| 552 | if (g->top_extra > 0.001) |
| 553 | top_extra = fabs(maxy - miny) * g->top_extra; |
| 554 | if (g->bottom_extra > 0.001) |
| 555 | bottom_extra = fabs(maxy - miny) * g->bottom_extra; |
| 556 | if (g->left_extra > 0.001) |
| 557 | left_extra = fabs(maxx - minx) * g->left_extra; |
| 558 | if (g->right_extra > 0.001) |
| 559 | right_extra = fabs(maxx - minx) * g->right_extra; |
| 560 | |
| 561 | gminx = minx - left_extra; |
| 562 | gmaxx = maxx + right_extra; |
| 563 | gminy = miny - bottom_extra; |
| 564 | gmaxy = maxy + top_extra; |
| 565 | |
Jens Axboe | d8fbeef | 2012-03-14 10:25:44 +0100 | [diff] [blame] | 566 | graph_draw_x_ticks(g, cr, x1, y1, x2, y2, gminx, gmaxx, 10, good_data); |
| 567 | gmaxy = graph_draw_y_ticks(g, cr, x1, y1, x2, y2, gminy, gmaxy, 10, good_data); |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 568 | |
Stephen M. Cameron | 9ce9cfb | 2012-03-07 19:37:25 +0100 | [diff] [blame] | 569 | if (!good_data) |
| 570 | goto skip_data; |
| 571 | |
Jens Axboe | f3e8440 | 2012-03-07 13:14:32 +0100 | [diff] [blame] | 572 | cairo_set_line_width(cr, 1.5); |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 573 | for (i = g->labels; i; i = i->next) { |
| 574 | first = 1; |
Stephen M. Cameron | cae0872 | 2012-03-07 14:47:38 +0100 | [diff] [blame] | 575 | if (i->r < 0) /* invisible data */ |
| 576 | continue; |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 577 | cairo_set_source_rgb(cr, i->r, i->g, i->b); |
| 578 | for (j = i->values; j; j = j->next) { |
Jens Axboe | 93e2db2 | 2012-03-13 09:45:22 +0100 | [diff] [blame] | 579 | struct xyvalue *xy = j->value; |
| 580 | |
Stephen M. Cameron | def0ac2 | 2012-03-12 07:32:57 +0100 | [diff] [blame] | 581 | tx = ((getx(j) - gminx) / (gmaxx - gminx)) * (x2 - x1) + x1; |
| 582 | ty = y2 - ((gety(j) - gminy) / (gmaxy - gminy)) * (y2 - y1); |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 583 | if (first) { |
| 584 | cairo_move_to(cr, tx, ty); |
| 585 | first = 0; |
| 586 | } else { |
| 587 | cairo_line_to(cr, tx, ty); |
| 588 | } |
Jens Axboe | 93e2db2 | 2012-03-13 09:45:22 +0100 | [diff] [blame] | 589 | xy->gx = tx; |
| 590 | xy->gy = ty; |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 591 | } |
| 592 | cairo_stroke(cr); |
| 593 | } |
Stephen M. Cameron | 9ce9cfb | 2012-03-07 19:37:25 +0100 | [diff] [blame] | 594 | |
| 595 | skip_data: |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 596 | cairo_restore(cr); |
Stephen M. Cameron | 9ce9cfb | 2012-03-07 19:37:25 +0100 | [diff] [blame] | 597 | |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 598 | } |
| 599 | |
| 600 | static void gfree(void *f) |
| 601 | { |
| 602 | if (f) |
| 603 | free(f); |
| 604 | } |
| 605 | |
| 606 | static void setstring(char **str, const char *value) |
| 607 | { |
| 608 | gfree(*str); |
| 609 | *str = strdup(value); |
| 610 | } |
| 611 | |
| 612 | void graph_title(struct graph *bg, const char *title) |
| 613 | { |
| 614 | setstring(&bg->title, title); |
| 615 | } |
| 616 | |
| 617 | void graph_x_title(struct graph *bg, const char *title) |
| 618 | { |
| 619 | setstring(&bg->xtitle, title); |
| 620 | } |
| 621 | |
| 622 | void graph_y_title(struct graph *bg, const char *title) |
| 623 | { |
| 624 | setstring(&bg->ytitle, title); |
| 625 | } |
| 626 | |
| 627 | static struct graph_label *graph_find_label(struct graph *bg, |
| 628 | const char *label) |
| 629 | { |
| 630 | struct graph_label *i; |
| 631 | |
| 632 | for (i = bg->labels; i; i = i->next) |
| 633 | if (strcmp(label, i->label) == 0) |
| 634 | return i; |
| 635 | return NULL; |
| 636 | } |
| 637 | |
| 638 | void graph_add_label(struct graph *bg, const char *label) |
| 639 | { |
| 640 | struct graph_label *i; |
| 641 | |
| 642 | i = graph_find_label(bg, label); |
| 643 | if (i) |
| 644 | return; /* already present. */ |
| 645 | i = calloc(1, sizeof(*i)); |
| 646 | i->parent = bg; |
| 647 | setstring(&i->label, label); |
| 648 | i->next = NULL; |
| 649 | if (!bg->tail) |
| 650 | bg->labels = i; |
| 651 | else |
| 652 | bg->tail->next = i; |
| 653 | bg->tail = i; |
| 654 | } |
| 655 | |
Jens Axboe | 93e2db2 | 2012-03-13 09:45:22 +0100 | [diff] [blame] | 656 | static void graph_label_add_value(struct graph_label *i, void *value, |
| 657 | const char *tooltip) |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 658 | { |
| 659 | struct graph_value *x; |
| 660 | |
| 661 | x = malloc(sizeof(*x)); |
| 662 | x->value = value; |
Jens Axboe | 93e2db2 | 2012-03-13 09:45:22 +0100 | [diff] [blame] | 663 | if (tooltip) |
| 664 | x->tooltip = strdup(tooltip); |
| 665 | else |
| 666 | x->tooltip = NULL; |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 667 | x->next = NULL; |
| 668 | if (!i->tail) { |
| 669 | i->values = x; |
| 670 | } else { |
| 671 | i->tail->next = x; |
| 672 | } |
| 673 | i->tail = x; |
| 674 | i->value_count++; |
Jens Axboe | 93e2db2 | 2012-03-13 09:45:22 +0100 | [diff] [blame] | 675 | if (x->tooltip) |
| 676 | i->tooltip_count++; |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 677 | |
| 678 | if (i->parent->per_label_limit != -1 && |
| 679 | i->value_count > i->parent->per_label_limit) { |
Jens Axboe | c148dae | 2012-03-11 21:35:47 +0100 | [diff] [blame] | 680 | int to_drop = 1; |
| 681 | |
| 682 | /* |
| 683 | * If the limit was dynamically reduced, making us more |
| 684 | * than 1 entry ahead after adding this one, drop two |
| 685 | * entries. This will make us (eventually) reach the |
| 686 | * specified limit. |
| 687 | */ |
| 688 | if (i->value_count - i->parent->per_label_limit >= 2) |
| 689 | to_drop = 2; |
| 690 | |
| 691 | while (to_drop--) { |
| 692 | x = i->values; |
| 693 | i->values = i->values->next; |
Jens Axboe | 93e2db2 | 2012-03-13 09:45:22 +0100 | [diff] [blame] | 694 | if (x->tooltip) { |
| 695 | free(x->tooltip); |
| 696 | i->tooltip_count--; |
| 697 | } |
Jens Axboe | c148dae | 2012-03-11 21:35:47 +0100 | [diff] [blame] | 698 | free(x->value); |
| 699 | free(x); |
| 700 | i->value_count--; |
| 701 | } |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 702 | } |
| 703 | } |
| 704 | |
| 705 | int graph_add_data(struct graph *bg, const char *label, const double value) |
| 706 | { |
| 707 | struct graph_label *i; |
| 708 | double *d; |
| 709 | |
| 710 | d = malloc(sizeof(*d)); |
| 711 | *d = value; |
| 712 | |
| 713 | i = graph_find_label(bg, label); |
| 714 | if (!i) |
| 715 | return -1; |
Jens Axboe | 93e2db2 | 2012-03-13 09:45:22 +0100 | [diff] [blame] | 716 | graph_label_add_value(i, d, NULL); |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 717 | return 0; |
| 718 | } |
| 719 | |
| 720 | int graph_add_xy_data(struct graph *bg, const char *label, |
Jens Axboe | 93e2db2 | 2012-03-13 09:45:22 +0100 | [diff] [blame] | 721 | const double x, const double y, const char *tooltip) |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 722 | { |
| 723 | struct graph_label *i; |
| 724 | struct xyvalue *xy; |
| 725 | |
| 726 | xy = malloc(sizeof(*xy)); |
| 727 | xy->x = x; |
| 728 | xy->y = y; |
| 729 | |
| 730 | i = graph_find_label(bg, label); |
| 731 | if (!i) |
| 732 | return -1; |
Jens Axboe | 93e2db2 | 2012-03-13 09:45:22 +0100 | [diff] [blame] | 733 | |
| 734 | graph_label_add_value(i, xy, tooltip); |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 735 | return 0; |
| 736 | } |
| 737 | |
| 738 | static void graph_free_values(struct graph_value *values) |
| 739 | { |
| 740 | struct graph_value *i, *next; |
| 741 | |
| 742 | for (i = values; i; i = next) { |
| 743 | next = i->next; |
| 744 | gfree(i->value); |
| 745 | gfree(i); |
| 746 | } |
| 747 | } |
| 748 | |
| 749 | static void graph_free_labels(struct graph_label *labels) |
| 750 | { |
| 751 | struct graph_label *i, *next; |
| 752 | |
| 753 | for (i = labels; i; i = next) { |
| 754 | next = i->next; |
| 755 | graph_free_values(i->values); |
| 756 | gfree(i); |
| 757 | } |
| 758 | } |
| 759 | |
| 760 | void graph_set_color(struct graph *gr, const char *label, |
| 761 | double red, double green, double blue) |
| 762 | { |
| 763 | struct graph_label *i; |
| 764 | double r, g, b; |
| 765 | |
Stephen M. Cameron | cae0872 | 2012-03-07 14:47:38 +0100 | [diff] [blame] | 766 | if (red < 0.0) { /* invisible color */ |
| 767 | r = -1.0; |
| 768 | g = -1.0; |
| 769 | b = -1.0; |
| 770 | } else { |
| 771 | r = fabs(red); |
| 772 | g = fabs(green); |
| 773 | b = fabs(blue); |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 774 | |
Stephen M. Cameron | cae0872 | 2012-03-07 14:47:38 +0100 | [diff] [blame] | 775 | if (r > 1.0) |
| 776 | r = 1.0; |
| 777 | if (g > 1.0) |
| 778 | g = 1.0; |
| 779 | if (b > 1.0) |
| 780 | b =1.0; |
| 781 | } |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 782 | |
| 783 | for (i = gr->labels; i; i = i->next) |
| 784 | if (strcmp(i->label, label) == 0) { |
| 785 | i->r = r; |
| 786 | i->g = g; |
| 787 | i->b = b; |
| 788 | break; |
| 789 | } |
| 790 | } |
| 791 | |
| 792 | void graph_free(struct graph *bg) |
| 793 | { |
| 794 | gfree(bg->title); |
| 795 | gfree(bg->xtitle); |
| 796 | gfree(bg->ytitle); |
| 797 | graph_free_labels(bg->labels); |
| 798 | } |
| 799 | |
| 800 | /* For each line in the line graph, up to per_label_limit segments may |
| 801 | * be added. After that, adding more data to the end of the line |
| 802 | * causes data to drop off of the front of the line. |
| 803 | */ |
| 804 | void line_graph_set_data_count_limit(struct graph *g, int per_label_limit) |
| 805 | { |
| 806 | g->per_label_limit = per_label_limit; |
| 807 | } |
| 808 | |
Stephen M. Cameron | def0ac2 | 2012-03-12 07:32:57 +0100 | [diff] [blame] | 809 | void graph_add_extra_space(struct graph *g, double left_percent, double right_percent, |
| 810 | double top_percent, double bottom_percent) |
| 811 | { |
| 812 | g->left_extra = left_percent; |
| 813 | g->right_extra = right_percent; |
| 814 | g->top_extra = top_percent; |
| 815 | g->bottom_extra = bottom_percent; |
| 816 | } |
| 817 | |
Jens Axboe | d8fbeef | 2012-03-14 10:25:44 +0100 | [diff] [blame] | 818 | /* |
| 819 | * Normally values are logged in a base unit of 0, but for other purposes |
| 820 | * it makes more sense to log in higher unit. For instance for bandwidth |
| 821 | * purposes, you may want to log in KB/sec (or MB/sec) rather than bytes/sec. |
| 822 | */ |
| 823 | void graph_set_base_offset(struct graph *g, unsigned int base_offset) |
| 824 | { |
| 825 | g->base_offset = base_offset; |
| 826 | } |
| 827 | |
Jens Axboe | 93e2db2 | 2012-03-13 09:45:22 +0100 | [diff] [blame] | 828 | int graph_has_tooltips(struct graph *g) |
| 829 | { |
| 830 | struct graph_label *i; |
Stephen M. Cameron | def0ac2 | 2012-03-12 07:32:57 +0100 | [diff] [blame] | 831 | |
Jens Axboe | 93e2db2 | 2012-03-13 09:45:22 +0100 | [diff] [blame] | 832 | for (i = g->labels; i; i = i->next) |
| 833 | if (i->tooltip_count) |
| 834 | return 1; |
| 835 | |
| 836 | return 0; |
| 837 | } |
| 838 | |
| 839 | int graph_contains_xy(struct graph *g, int x, int y) |
| 840 | { |
| 841 | int first_x = g->xoffset; |
| 842 | int last_x = g->xoffset + g->xdim; |
| 843 | int first_y = g->yoffset; |
| 844 | int last_y = g->yoffset + g->ydim; |
| 845 | |
| 846 | return (x >= first_x && x <= last_x) && (y >= first_y && y <= last_y); |
| 847 | } |
| 848 | |
| 849 | static int xy_match(struct xyvalue *xy, int x, int y) |
| 850 | { |
| 851 | int xdiff = abs(xy->gx - x); |
| 852 | int ydiff = abs(xy->gy - y); |
| 853 | |
Jens Axboe | ef4ad58 | 2012-03-15 15:37:36 +0100 | [diff] [blame] | 854 | return xdiff <= 10 && ydiff <= 10; |
Jens Axboe | 93e2db2 | 2012-03-13 09:45:22 +0100 | [diff] [blame] | 855 | } |
| 856 | |
| 857 | const char *graph_find_tooltip(struct graph *g, int x, int y) |
| 858 | { |
| 859 | struct graph_label *i; |
| 860 | struct graph_value *j; |
| 861 | |
| 862 | for (i = g->labels; i; i = i->next) { |
| 863 | for (j = i->values; j; j = j->next) { |
| 864 | struct xyvalue *xy = j->value; |
| 865 | |
| 866 | if (xy_match(xy, x - g->xoffset, y)) |
| 867 | return j->tooltip; |
| 868 | } |
| 869 | } |
| 870 | |
| 871 | return NULL; |
| 872 | } |