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 | * |
Jens Axboe | 3c3ed07 | 2012-03-27 09:12:39 +0200 | [diff] [blame] | 4 | * Copyright (C) 2012 Stephen M. Cameron <stephenmcameron@gmail.com> |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 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" |
Jens Axboe | b65c7ec | 2012-03-21 17:17:45 +0100 | [diff] [blame] | 34 | #include "flist.h" |
| 35 | #include "lib/prio_tree.h" |
Stephen M. Cameron | ee2f55b | 2012-03-27 08:14:09 +0200 | [diff] [blame] | 36 | #include "cairo_text_helpers.h" |
Jens Axboe | b65c7ec | 2012-03-21 17:17:45 +0100 | [diff] [blame] | 37 | |
| 38 | /* |
| 39 | * Allowable difference to show tooltip |
| 40 | */ |
Jens Axboe | b7a69ad | 2012-03-21 21:55:21 +0100 | [diff] [blame] | 41 | #define TOOLTIP_DELTA 0.08 |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 42 | |
| 43 | struct xyvalue { |
| 44 | double x, y; |
| 45 | }; |
| 46 | |
Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 47 | enum { |
| 48 | GV_F_ON_PRIO = 1, |
Jens Axboe | 8dfd607 | 2012-03-22 22:10:37 +0100 | [diff] [blame] | 49 | GV_F_PRIO_SKIP = 2, |
Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 50 | }; |
| 51 | |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 52 | struct graph_value { |
Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 53 | struct flist_head list; |
Jens Axboe | b65c7ec | 2012-03-21 17:17:45 +0100 | [diff] [blame] | 54 | struct prio_tree_node node; |
Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 55 | struct flist_head alias; |
| 56 | unsigned int flags; |
Jens Axboe | 93e2db2 | 2012-03-13 09:45:22 +0100 | [diff] [blame] | 57 | char *tooltip; |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 58 | void *value; |
| 59 | }; |
| 60 | |
| 61 | struct graph_label { |
Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 62 | struct flist_head list; |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 63 | char *label; |
Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 64 | struct flist_head value_list; |
Jens Axboe | b65c7ec | 2012-03-21 17:17:45 +0100 | [diff] [blame] | 65 | struct prio_tree_root prio_tree; |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 66 | double r, g, b; |
Jens Axboe | 01a947f | 2012-03-22 21:21:00 +0100 | [diff] [blame] | 67 | int hide; |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 68 | int value_count; |
| 69 | struct graph *parent; |
| 70 | }; |
| 71 | |
Jens Axboe | b65c7ec | 2012-03-21 17:17:45 +0100 | [diff] [blame] | 72 | struct tick_value { |
| 73 | unsigned int offset; |
| 74 | double value; |
| 75 | }; |
| 76 | |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 77 | struct graph { |
| 78 | char *title; |
| 79 | char *xtitle; |
| 80 | char *ytitle; |
Jens Axboe | 87d5f27 | 2012-03-07 12:31:40 +0100 | [diff] [blame] | 81 | unsigned int xdim, ydim; |
Stephen M. Cameron | 57f9d28 | 2012-03-11 11:36:51 +0100 | [diff] [blame] | 82 | double xoffset, yoffset; |
Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 83 | struct flist_head label_list; |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 84 | int per_label_limit; |
Jens Axboe | f3e8440 | 2012-03-07 13:14:32 +0100 | [diff] [blame] | 85 | const char *font; |
Stephen M. Cameron | 7175d91 | 2012-03-11 11:35:10 +0100 | [diff] [blame] | 86 | graph_axis_unit_change_callback x_axis_unit_change_callback; |
| 87 | graph_axis_unit_change_callback y_axis_unit_change_callback; |
Jens Axboe | d8fbeef | 2012-03-14 10:25:44 +0100 | [diff] [blame] | 88 | unsigned int base_offset; |
Jens Axboe | 01a947f | 2012-03-22 21:21:00 +0100 | [diff] [blame] | 89 | unsigned int dont_graph_all_zeroes; |
Jens Axboe | 3c3ed07 | 2012-03-27 09:12:39 +0200 | [diff] [blame] | 90 | double left_extra; |
| 91 | double right_extra; |
| 92 | double top_extra; |
| 93 | double bottom_extra; |
Jens Axboe | b65c7ec | 2012-03-21 17:17:45 +0100 | [diff] [blame] | 94 | |
| 95 | double xtick_zero; |
| 96 | double xtick_delta; |
| 97 | double xtick_zero_val; |
Jens Axboe | b7a69ad | 2012-03-21 21:55:21 +0100 | [diff] [blame] | 98 | double xtick_one_val; |
Jens Axboe | b65c7ec | 2012-03-21 17:17:45 +0100 | [diff] [blame] | 99 | double ytick_zero; |
| 100 | double ytick_delta; |
| 101 | double ytick_zero_val; |
Jens Axboe | b7a69ad | 2012-03-21 21:55:21 +0100 | [diff] [blame] | 102 | double ytick_one_val; |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 103 | }; |
| 104 | |
Stephen M. Cameron | 3ea48b8 | 2012-03-07 19:40:58 +0100 | [diff] [blame] | 105 | void graph_set_size(struct graph *g, unsigned int xdim, unsigned int ydim) |
| 106 | { |
| 107 | g->xdim = xdim; |
| 108 | g->ydim = ydim; |
| 109 | } |
| 110 | |
Stephen M. Cameron | 57f9d28 | 2012-03-11 11:36:51 +0100 | [diff] [blame] | 111 | void graph_set_position(struct graph *g, double xoffset, double yoffset) |
| 112 | { |
| 113 | g->xoffset = xoffset; |
| 114 | g->yoffset = yoffset; |
| 115 | } |
| 116 | |
Jens Axboe | f3e8440 | 2012-03-07 13:14:32 +0100 | [diff] [blame] | 117 | 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] | 118 | { |
| 119 | struct graph *g; |
| 120 | |
| 121 | g = calloc(1, sizeof(*g)); |
Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 122 | INIT_FLIST_HEAD(&g->label_list); |
Stephen M. Cameron | 3ea48b8 | 2012-03-07 19:40:58 +0100 | [diff] [blame] | 123 | graph_set_size(g, xdim, ydim); |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 124 | g->per_label_limit = -1; |
Jens Axboe | f3e8440 | 2012-03-07 13:14:32 +0100 | [diff] [blame] | 125 | g->font = font; |
| 126 | if (!g->font) |
Jens Axboe | a1e7972 | 2012-03-23 10:52:25 +0100 | [diff] [blame] | 127 | g->font = GRAPH_DEFAULT_FONT; |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 128 | return g; |
| 129 | } |
| 130 | |
Jens Axboe | a1e7972 | 2012-03-23 10:52:25 +0100 | [diff] [blame] | 131 | void graph_set_font(struct graph *g, const char *font) |
| 132 | { |
| 133 | g->font = font; |
| 134 | } |
| 135 | |
Stephen M. Cameron | 7175d91 | 2012-03-11 11:35:10 +0100 | [diff] [blame] | 136 | void graph_x_axis_unit_change_notify(struct graph *g, graph_axis_unit_change_callback f) |
| 137 | { |
| 138 | g->x_axis_unit_change_callback = f; |
| 139 | } |
| 140 | |
| 141 | void graph_y_axis_unit_change_notify(struct graph *g, graph_axis_unit_change_callback f) |
| 142 | { |
| 143 | g->y_axis_unit_change_callback = f; |
| 144 | } |
| 145 | |
Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 146 | static int count_labels(struct graph *g) |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 147 | { |
Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 148 | struct flist_head *entry; |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 149 | int count = 0; |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 150 | |
Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 151 | flist_for_each(entry, &g->label_list) |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 152 | count++; |
Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 153 | |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 154 | return count; |
| 155 | } |
| 156 | |
Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 157 | static int count_values(struct graph_label *l) |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 158 | { |
Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 159 | struct flist_head *entry; |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 160 | int count = 0; |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 161 | |
Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 162 | flist_for_each(entry, &l->value_list) |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 163 | count++; |
Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 164 | |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 165 | return count; |
| 166 | } |
| 167 | |
| 168 | typedef double (*double_comparator)(double a, double b); |
| 169 | |
| 170 | static double mindouble(double a, double b) |
| 171 | { |
| 172 | return a < b ? a : b; |
| 173 | } |
| 174 | |
| 175 | static double maxdouble(double a, double b) |
| 176 | { |
| 177 | return a < b ? b : a; |
| 178 | } |
| 179 | |
Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 180 | static double find_double_values(struct graph_label *l, double_comparator cmp) |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 181 | { |
Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 182 | struct flist_head *entry; |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 183 | double answer, tmp; |
Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 184 | int first = 1; |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 185 | |
Jens Axboe | 01a947f | 2012-03-22 21:21:00 +0100 | [diff] [blame] | 186 | if (flist_empty(&l->value_list)) |
| 187 | return 0.0; |
| 188 | |
Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 189 | flist_for_each(entry, &l->value_list) { |
| 190 | struct graph_value *i; |
| 191 | |
| 192 | i = flist_entry(entry, struct graph_value, list); |
| 193 | tmp = *(double *) i->value; |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 194 | if (first) { |
| 195 | answer = tmp; |
| 196 | first = 0; |
| 197 | } else { |
| 198 | answer = cmp(answer, tmp); |
| 199 | } |
| 200 | } |
| 201 | return answer; |
| 202 | } |
| 203 | |
Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 204 | static double find_double_data(struct graph *g, double_comparator cmp) |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 205 | { |
Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 206 | struct flist_head *entry; |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 207 | struct graph_label *i; |
| 208 | int first = 1; |
| 209 | double answer, tmp; |
| 210 | |
Jens Axboe | 01a947f | 2012-03-22 21:21:00 +0100 | [diff] [blame] | 211 | if (flist_empty(&g->label_list)) |
| 212 | return 0.0; |
| 213 | |
Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 214 | flist_for_each(entry, &g->label_list) { |
| 215 | i = flist_entry(entry, struct graph_label, list); |
| 216 | tmp = find_double_values(i, cmp); |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 217 | if (first) { |
| 218 | answer = tmp; |
| 219 | first = 0; |
| 220 | } else { |
| 221 | answer = cmp(tmp, answer); |
| 222 | } |
| 223 | } |
| 224 | return answer; |
| 225 | } |
| 226 | |
Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 227 | static double find_min_data(struct graph *g) |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 228 | { |
Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 229 | return find_double_data(g, mindouble); |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 230 | } |
| 231 | |
Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 232 | static double find_max_data(struct graph *g) |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 233 | { |
Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 234 | return find_double_data(g, maxdouble); |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | static void draw_bars(struct graph *bg, cairo_t *cr, struct graph_label *lb, |
| 238 | double label_offset, double bar_width, |
| 239 | double mindata, double maxdata) |
| 240 | { |
Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 241 | struct flist_head *entry; |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 242 | double x1, y1, x2, y2; |
| 243 | int bar_num = 0; |
| 244 | double domain, range, v; |
| 245 | |
| 246 | domain = (maxdata - mindata); |
| 247 | range = (double) bg->ydim * 0.80; /* FIXME */ |
| 248 | cairo_stroke(cr); |
Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 249 | flist_for_each(entry, &lb->value_list) { |
| 250 | struct graph_value *i; |
| 251 | |
| 252 | i = flist_entry(entry, struct graph_value, list); |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 253 | |
| 254 | x1 = label_offset + (double) bar_num * bar_width + (bar_width * 0.05); |
| 255 | x2 = x1 + bar_width * 0.90; |
| 256 | y2 = bg->ydim * 0.90; |
| 257 | v = *(double *) i->value; |
| 258 | y1 = y2 - (((v - mindata) / domain) * range); |
| 259 | cairo_move_to(cr, x1, y1); |
| 260 | cairo_line_to(cr, x1, y2); |
| 261 | cairo_line_to(cr, x2, y2); |
| 262 | cairo_line_to(cr, x2, y1); |
| 263 | cairo_close_path(cr); |
| 264 | cairo_fill(cr); |
| 265 | cairo_stroke(cr); |
Jens Axboe | 3c3ed07 | 2012-03-27 09:12:39 +0200 | [diff] [blame] | 266 | bar_num++; |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 267 | } |
| 268 | } |
| 269 | |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 270 | static void graph_draw_common(struct graph *g, cairo_t *cr, |
| 271 | double *x1, double *y1, double *x2, double *y2) |
| 272 | { |
Jens Axboe | 4e14f01 | 2012-03-23 08:18:57 +0100 | [diff] [blame] | 273 | cairo_set_source_rgb(cr, 0, 0, 0); |
Jens Axboe | 3c3ed07 | 2012-03-27 09:12:39 +0200 | [diff] [blame] | 274 | cairo_set_line_width(cr, 0.8); |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 275 | |
Jens Axboe | 3c3ed07 | 2012-03-27 09:12:39 +0200 | [diff] [blame] | 276 | *x1 = 0.10 * g->xdim; |
Stephen M. Cameron | 6bf8600 | 2012-03-08 16:58:50 +0100 | [diff] [blame] | 277 | *x2 = 0.95 * g->xdim; |
Jens Axboe | 3c3ed07 | 2012-03-27 09:12:39 +0200 | [diff] [blame] | 278 | *y1 = 0.10 * g->ydim; |
Stephen M. Cameron | 6bf8600 | 2012-03-08 16:58:50 +0100 | [diff] [blame] | 279 | *y2 = 0.90 * g->ydim; |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 280 | |
| 281 | cairo_move_to(cr, *x1, *y1); |
| 282 | cairo_line_to(cr, *x1, *y2); |
| 283 | cairo_line_to(cr, *x2, *y2); |
| 284 | cairo_line_to(cr, *x2, *y1); |
| 285 | cairo_line_to(cr, *x1, *y1); |
| 286 | cairo_stroke(cr); |
| 287 | |
Stephen M. Cameron | ee2f55b | 2012-03-27 08:14:09 +0200 | [diff] [blame] | 288 | draw_centered_text(cr, g->font, g->xdim / 2, g->ydim / 20, 20.0, g->title); |
| 289 | draw_centered_text(cr, g->font, g->xdim / 2, g->ydim * 0.97, 14.0, g->xtitle); |
| 290 | draw_vertical_centered_text(cr, g->font, g->xdim * 0.02, g->ydim / 2, 14.0, g->ytitle); |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 291 | cairo_stroke(cr); |
| 292 | } |
| 293 | |
| 294 | static void graph_draw_x_ticks(struct graph *g, cairo_t *cr, |
| 295 | double x1, double y1, double x2, double y2, |
Jens Axboe | d8fbeef | 2012-03-14 10:25:44 +0100 | [diff] [blame] | 296 | double minx, double maxx, int nticks, int add_tm_text) |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 297 | { |
| 298 | struct tickmark *tm; |
| 299 | double tx; |
Stephen M. Cameron | 7175d91 | 2012-03-11 11:35:10 +0100 | [diff] [blame] | 300 | int i, power_of_ten; |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 301 | static double dash[] = { 1.0, 2.0 }; |
| 302 | |
Stephen M. Cameron | 7175d91 | 2012-03-11 11:35:10 +0100 | [diff] [blame] | 303 | nticks = calc_tickmarks(minx, maxx, nticks, &tm, &power_of_ten, |
Jens Axboe | d8fbeef | 2012-03-14 10:25:44 +0100 | [diff] [blame] | 304 | g->x_axis_unit_change_callback == NULL, g->base_offset); |
Stephen M. Cameron | 7175d91 | 2012-03-11 11:35:10 +0100 | [diff] [blame] | 305 | if (g->x_axis_unit_change_callback) |
| 306 | g->x_axis_unit_change_callback(g, power_of_ten); |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 307 | |
| 308 | for (i = 0; i < nticks; i++) { |
| 309 | tx = (((tm[i].value) - minx) / (maxx - minx)) * (x2 - x1) + x1; |
Jens Axboe | d8fbeef | 2012-03-14 10:25:44 +0100 | [diff] [blame] | 310 | |
Jens Axboe | b65c7ec | 2012-03-21 17:17:45 +0100 | [diff] [blame] | 311 | /* |
| 312 | * Update tick delta |
| 313 | */ |
| 314 | if (!i) { |
| 315 | g->xtick_zero = tx; |
| 316 | g->xtick_zero_val = tm[0].value; |
Jens Axboe | b7a69ad | 2012-03-21 21:55:21 +0100 | [diff] [blame] | 317 | } else if (i == 1) { |
Jens Axboe | b65c7ec | 2012-03-21 17:17:45 +0100 | [diff] [blame] | 318 | g->xtick_delta = (tm[1].value - tm[0].value) / (tx - g->xtick_zero); |
Jens Axboe | b7a69ad | 2012-03-21 21:55:21 +0100 | [diff] [blame] | 319 | g->xtick_one_val = tm[1].value; |
| 320 | } |
Jens Axboe | b65c7ec | 2012-03-21 17:17:45 +0100 | [diff] [blame] | 321 | |
Jens Axboe | d8fbeef | 2012-03-14 10:25:44 +0100 | [diff] [blame] | 322 | /* really tx < yx || tx > x2, but protect against rounding */ |
| 323 | if (x1 - tx > 0.01 || tx - x2 > 0.01) |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 324 | continue; |
| 325 | |
| 326 | /* Draw tick mark */ |
Jens Axboe | f3e8440 | 2012-03-07 13:14:32 +0100 | [diff] [blame] | 327 | cairo_set_line_width(cr, 0.8); |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 328 | cairo_move_to(cr, tx, y2); |
| 329 | cairo_line_to(cr, tx, y2 + (y2 - y1) * 0.03); |
| 330 | cairo_stroke(cr); |
| 331 | |
| 332 | /* draw grid lines */ |
| 333 | cairo_save(cr); |
| 334 | cairo_set_dash(cr, dash, 2, 2.0); |
| 335 | cairo_set_line_width(cr, 0.5); |
| 336 | cairo_move_to(cr, tx, y1); |
| 337 | cairo_line_to(cr, tx, y2); |
| 338 | cairo_stroke(cr); |
| 339 | cairo_restore(cr); |
| 340 | |
Jens Axboe | d8fbeef | 2012-03-14 10:25:44 +0100 | [diff] [blame] | 341 | if (!add_tm_text) |
| 342 | continue; |
| 343 | |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 344 | /* draw tickmark label */ |
Stephen M. Cameron | ee2f55b | 2012-03-27 08:14:09 +0200 | [diff] [blame] | 345 | draw_centered_text(cr, g->font, tx, y2 * 1.04, 12.0, tm[i].string); |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 346 | cairo_stroke(cr); |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 347 | } |
| 348 | } |
| 349 | |
Jens Axboe | d8fbeef | 2012-03-14 10:25:44 +0100 | [diff] [blame] | 350 | 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] | 351 | double x1, double y1, double x2, double y2, |
Jens Axboe | d8fbeef | 2012-03-14 10:25:44 +0100 | [diff] [blame] | 352 | double miny, double maxy, int nticks, int add_tm_text) |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 353 | { |
| 354 | struct tickmark *tm; |
| 355 | double ty; |
Stephen M. Cameron | 7175d91 | 2012-03-11 11:35:10 +0100 | [diff] [blame] | 356 | int i, power_of_ten; |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 357 | static double dash[] = { 2.0, 2.0 }; |
| 358 | |
Stephen M. Cameron | 7175d91 | 2012-03-11 11:35:10 +0100 | [diff] [blame] | 359 | nticks = calc_tickmarks(miny, maxy, nticks, &tm, &power_of_ten, |
Jens Axboe | d8fbeef | 2012-03-14 10:25:44 +0100 | [diff] [blame] | 360 | g->y_axis_unit_change_callback == NULL, g->base_offset); |
Stephen M. Cameron | 7175d91 | 2012-03-11 11:35:10 +0100 | [diff] [blame] | 361 | if (g->y_axis_unit_change_callback) |
| 362 | g->y_axis_unit_change_callback(g, power_of_ten); |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 363 | |
Jens Axboe | d8fbeef | 2012-03-14 10:25:44 +0100 | [diff] [blame] | 364 | /* |
| 365 | * Use highest tickmark as top of graph, not highest value. Otherwise |
| 366 | * it's impossible to see what the max value is, if the graph is |
| 367 | * fairly flat. |
| 368 | */ |
| 369 | maxy = tm[nticks - 1].value; |
| 370 | |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 371 | for (i = 0; i < nticks; i++) { |
| 372 | ty = y2 - (((tm[i].value) - miny) / (maxy - miny)) * (y2 - y1); |
Jens Axboe | d8fbeef | 2012-03-14 10:25:44 +0100 | [diff] [blame] | 373 | |
Jens Axboe | b65c7ec | 2012-03-21 17:17:45 +0100 | [diff] [blame] | 374 | /* |
| 375 | * Update tick delta |
| 376 | */ |
| 377 | if (!i) { |
| 378 | g->ytick_zero = ty; |
| 379 | g->ytick_zero_val = tm[0].value; |
Jens Axboe | b7a69ad | 2012-03-21 21:55:21 +0100 | [diff] [blame] | 380 | } else if (i == 1) { |
Jens Axboe | b65c7ec | 2012-03-21 17:17:45 +0100 | [diff] [blame] | 381 | g->ytick_delta = (tm[1].value - tm[0].value) / (ty - g->ytick_zero); |
Jens Axboe | b7a69ad | 2012-03-21 21:55:21 +0100 | [diff] [blame] | 382 | g->ytick_one_val = tm[1].value; |
| 383 | } |
Jens Axboe | b65c7ec | 2012-03-21 17:17:45 +0100 | [diff] [blame] | 384 | |
Jens Axboe | d8fbeef | 2012-03-14 10:25:44 +0100 | [diff] [blame] | 385 | /* really ty < y1 || ty > y2, but protect against rounding */ |
| 386 | if (y1 - ty > 0.01 || ty - y2 > 0.01) |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 387 | continue; |
Jens Axboe | d8fbeef | 2012-03-14 10:25:44 +0100 | [diff] [blame] | 388 | |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 389 | /* draw tick mark */ |
| 390 | cairo_move_to(cr, x1, ty); |
| 391 | cairo_line_to(cr, x1 - (x2 - x1) * 0.02, ty); |
| 392 | cairo_stroke(cr); |
| 393 | |
| 394 | /* draw grid lines */ |
| 395 | cairo_save(cr); |
| 396 | cairo_set_dash(cr, dash, 2, 2.0); |
| 397 | cairo_set_line_width(cr, 0.5); |
| 398 | cairo_move_to(cr, x1, ty); |
| 399 | cairo_line_to(cr, x2, ty); |
| 400 | cairo_stroke(cr); |
| 401 | cairo_restore(cr); |
| 402 | |
Jens Axboe | d8fbeef | 2012-03-14 10:25:44 +0100 | [diff] [blame] | 403 | if (!add_tm_text) |
| 404 | continue; |
| 405 | |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 406 | /* draw tickmark label */ |
Stephen M. Cameron | ee2f55b | 2012-03-27 08:14:09 +0200 | [diff] [blame] | 407 | draw_right_justified_text(cr, g->font, x1 - (x2 - x1) * 0.025, ty, 12.0, tm[i].string); |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 408 | cairo_stroke(cr); |
| 409 | } |
Jens Axboe | d8fbeef | 2012-03-14 10:25:44 +0100 | [diff] [blame] | 410 | |
| 411 | /* |
| 412 | * Return new max to use |
| 413 | */ |
| 414 | return maxy; |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | void bar_graph_draw(struct graph *bg, cairo_t *cr) |
| 418 | { |
| 419 | double x1, y1, x2, y2; |
| 420 | double space_per_label, bar_width; |
| 421 | double label_offset, mindata, maxdata; |
| 422 | int i, nlabels; |
| 423 | struct graph_label *lb; |
Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 424 | struct flist_head *entry; |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 425 | |
| 426 | cairo_save(cr); |
Stephen M. Cameron | 57f9d28 | 2012-03-11 11:36:51 +0100 | [diff] [blame] | 427 | cairo_translate(cr, bg->xoffset, bg->yoffset); |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 428 | graph_draw_common(bg, cr, &x1, &y1, &x2, &y2); |
| 429 | |
Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 430 | nlabels = count_labels(bg); |
Jens Axboe | d8fbeef | 2012-03-14 10:25:44 +0100 | [diff] [blame] | 431 | space_per_label = (x2 - x1) / (double) nlabels; |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 432 | |
Jens Axboe | bb39379 | 2012-03-15 10:52:38 +0100 | [diff] [blame] | 433 | /* |
| 434 | * Start bars at 0 unless we have negative values, otherwise we |
| 435 | * present a skewed picture comparing label X and X+1. |
| 436 | */ |
Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 437 | mindata = find_min_data(bg); |
Jens Axboe | bb39379 | 2012-03-15 10:52:38 +0100 | [diff] [blame] | 438 | if (mindata > 0) |
| 439 | mindata = 0; |
| 440 | |
Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 441 | maxdata = find_max_data(bg); |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 442 | |
| 443 | if (fabs(maxdata - mindata) < 1e-20) { |
Stephen M. Cameron | ee2f55b | 2012-03-27 08:14:09 +0200 | [diff] [blame] | 444 | draw_centered_text(cr, bg->font, |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 445 | x1 + (x2 - x1) / 2.0, |
| 446 | y1 + (y2 - y1) / 2.0, 20.0, "No good data"); |
| 447 | return; |
| 448 | } |
| 449 | |
Jens Axboe | bb39379 | 2012-03-15 10:52:38 +0100 | [diff] [blame] | 450 | 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] | 451 | i = 0; |
Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 452 | flist_for_each(entry, &bg->label_list) { |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 453 | int nvalues; |
Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 454 | |
| 455 | lb = flist_entry(entry, struct graph_label, list); |
| 456 | nvalues = count_values(lb); |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 457 | bar_width = (space_per_label - space_per_label * 0.2) / (double) nvalues; |
| 458 | label_offset = bg->xdim * 0.1 + space_per_label * (double) i + space_per_label * 0.1; |
| 459 | draw_bars(bg, cr, lb, label_offset, bar_width, mindata, maxdata); |
| 460 | // draw_centered_text(cr, label_offset + (bar_width / 2.0 + bar_width * 0.1), bg->ydim * 0.93, |
Stephen M. Cameron | ee2f55b | 2012-03-27 08:14:09 +0200 | [diff] [blame] | 461 | draw_centered_text(cr, bg->font, x1 + space_per_label * (i + 0.5), bg->ydim * 0.93, |
Jens Axboe | 3c3ed07 | 2012-03-27 09:12:39 +0200 | [diff] [blame] | 462 | 12.0, lb->label); |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 463 | i++; |
| 464 | } |
| 465 | cairo_stroke(cr); |
| 466 | cairo_restore(cr); |
| 467 | } |
| 468 | |
| 469 | typedef double (*xy_value_extractor)(struct graph_value *v); |
| 470 | |
| 471 | static double getx(struct graph_value *v) |
| 472 | { |
| 473 | struct xyvalue *xy = v->value; |
| 474 | return xy->x; |
| 475 | } |
| 476 | |
| 477 | static double gety(struct graph_value *v) |
| 478 | { |
| 479 | struct xyvalue *xy = v->value; |
| 480 | return xy->y; |
| 481 | } |
| 482 | |
| 483 | static double find_xy_value(struct graph *g, xy_value_extractor getvalue, double_comparator cmp) |
| 484 | { |
Stephen M. Cameron | 10e54cd | 2012-03-07 19:39:55 +0100 | [diff] [blame] | 485 | double tmp, answer = 0.0; |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 486 | struct graph_label *i; |
| 487 | struct graph_value *j; |
Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 488 | struct flist_head *jentry, *entry; |
Stephen M. Cameron | d582bf7 | 2012-03-07 19:37:57 +0100 | [diff] [blame] | 489 | int first = 1; |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 490 | |
Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 491 | flist_for_each(entry, &g->label_list) { |
| 492 | i = flist_entry(entry, struct graph_label, list); |
| 493 | |
| 494 | flist_for_each(jentry, &i->value_list) { |
| 495 | j = flist_entry(jentry, struct graph_value, list); |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 496 | tmp = getvalue(j); |
Stephen M. Cameron | d582bf7 | 2012-03-07 19:37:57 +0100 | [diff] [blame] | 497 | if (first) { |
| 498 | first = 0; |
| 499 | answer = tmp; |
| 500 | } |
Jens Axboe | 3c3ed07 | 2012-03-27 09:12:39 +0200 | [diff] [blame] | 501 | answer = cmp(tmp, answer); |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 502 | } |
Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 503 | } |
| 504 | |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 505 | return answer; |
Jens Axboe | 01a947f | 2012-03-22 21:21:00 +0100 | [diff] [blame] | 506 | } |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 507 | |
| 508 | void line_graph_draw(struct graph *g, cairo_t *cr) |
| 509 | { |
| 510 | double x1, y1, x2, y2; |
Stephen M. Cameron | def0ac2 | 2012-03-12 07:32:57 +0100 | [diff] [blame] | 511 | double minx, miny, maxx, maxy, gminx, gminy, gmaxx, gmaxy; |
| 512 | double tx, ty, top_extra, bottom_extra, left_extra, right_extra; |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 513 | struct graph_label *i; |
| 514 | struct graph_value *j; |
Stephen M. Cameron | 9ce9cfb | 2012-03-07 19:37:25 +0100 | [diff] [blame] | 515 | int good_data = 1, first = 1; |
Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 516 | struct flist_head *entry, *lentry; |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 517 | |
| 518 | cairo_save(cr); |
Stephen M. Cameron | 57f9d28 | 2012-03-11 11:36:51 +0100 | [diff] [blame] | 519 | cairo_translate(cr, g->xoffset, g->yoffset); |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 520 | graph_draw_common(g, cr, &x1, &y1, &x2, &y2); |
| 521 | |
| 522 | minx = find_xy_value(g, getx, mindouble); |
| 523 | maxx = find_xy_value(g, getx, maxdouble); |
| 524 | miny = find_xy_value(g, gety, mindouble); |
Jens Axboe | 5aec668 | 2012-03-16 10:17:08 +0100 | [diff] [blame] | 525 | |
| 526 | /* |
| 527 | * Start graphs at zero, unless we have a value below. Otherwise |
| 528 | * it's hard to visually compare the read and write graph, since |
| 529 | * the lowest valued one will be the floor of the graph view. |
| 530 | */ |
| 531 | if (miny > 0) |
| 532 | miny = 0; |
| 533 | |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 534 | maxy = find_xy_value(g, gety, maxdouble); |
| 535 | |
| 536 | if (fabs(maxx - minx) < 1e-20 || fabs(maxy - miny) < 1e-20) { |
Stephen M. Cameron | 9ce9cfb | 2012-03-07 19:37:25 +0100 | [diff] [blame] | 537 | good_data = 0; |
| 538 | minx = 0.0; |
| 539 | miny = 0.0; |
| 540 | maxx = 10.0; |
| 541 | maxy = 100.0; |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 542 | } |
| 543 | |
Stephen M. Cameron | def0ac2 | 2012-03-12 07:32:57 +0100 | [diff] [blame] | 544 | top_extra = 0.0; |
| 545 | bottom_extra = 0.0; |
| 546 | left_extra = 0.0; |
| 547 | right_extra = 0.0; |
| 548 | |
| 549 | if (g->top_extra > 0.001) |
| 550 | top_extra = fabs(maxy - miny) * g->top_extra; |
| 551 | if (g->bottom_extra > 0.001) |
| 552 | bottom_extra = fabs(maxy - miny) * g->bottom_extra; |
| 553 | if (g->left_extra > 0.001) |
| 554 | left_extra = fabs(maxx - minx) * g->left_extra; |
| 555 | if (g->right_extra > 0.001) |
| 556 | right_extra = fabs(maxx - minx) * g->right_extra; |
| 557 | |
| 558 | gminx = minx - left_extra; |
| 559 | gmaxx = maxx + right_extra; |
| 560 | gminy = miny - bottom_extra; |
| 561 | gmaxy = maxy + top_extra; |
| 562 | |
Jens Axboe | d8fbeef | 2012-03-14 10:25:44 +0100 | [diff] [blame] | 563 | graph_draw_x_ticks(g, cr, x1, y1, x2, y2, gminx, gmaxx, 10, good_data); |
| 564 | 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] | 565 | |
Stephen M. Cameron | 9ce9cfb | 2012-03-07 19:37:25 +0100 | [diff] [blame] | 566 | if (!good_data) |
| 567 | goto skip_data; |
| 568 | |
Jens Axboe | f3e8440 | 2012-03-07 13:14:32 +0100 | [diff] [blame] | 569 | cairo_set_line_width(cr, 1.5); |
Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 570 | flist_for_each(lentry, &g->label_list) { |
| 571 | i = flist_entry(lentry, struct graph_label, list); |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 572 | first = 1; |
Jens Axboe | 01a947f | 2012-03-22 21:21:00 +0100 | [diff] [blame] | 573 | if (i->hide || i->r < 0) /* invisible data */ |
Stephen M. Cameron | cae0872 | 2012-03-07 14:47:38 +0100 | [diff] [blame] | 574 | continue; |
Jens Axboe | b65c7ec | 2012-03-21 17:17:45 +0100 | [diff] [blame] | 575 | |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 576 | cairo_set_source_rgb(cr, i->r, i->g, i->b); |
Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 577 | flist_for_each(entry, &i->value_list) { |
| 578 | j = flist_entry(entry, struct graph_value, list); |
Stephen M. Cameron | def0ac2 | 2012-03-12 07:32:57 +0100 | [diff] [blame] | 579 | tx = ((getx(j) - gminx) / (gmaxx - gminx)) * (x2 - x1) + x1; |
| 580 | ty = y2 - ((gety(j) - gminy) / (gmaxy - gminy)) * (y2 - y1); |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 581 | if (first) { |
| 582 | cairo_move_to(cr, tx, ty); |
| 583 | first = 0; |
| 584 | } else { |
| 585 | cairo_line_to(cr, tx, ty); |
| 586 | } |
| 587 | } |
| 588 | cairo_stroke(cr); |
| 589 | } |
Stephen M. Cameron | 9ce9cfb | 2012-03-07 19:37:25 +0100 | [diff] [blame] | 590 | |
| 591 | skip_data: |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 592 | cairo_restore(cr); |
| 593 | } |
| 594 | |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 595 | static void setstring(char **str, const char *value) |
| 596 | { |
Jens Axboe | b65c7ec | 2012-03-21 17:17:45 +0100 | [diff] [blame] | 597 | free(*str); |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 598 | *str = strdup(value); |
| 599 | } |
| 600 | |
| 601 | void graph_title(struct graph *bg, const char *title) |
| 602 | { |
| 603 | setstring(&bg->title, title); |
| 604 | } |
| 605 | |
| 606 | void graph_x_title(struct graph *bg, const char *title) |
| 607 | { |
| 608 | setstring(&bg->xtitle, title); |
| 609 | } |
| 610 | |
| 611 | void graph_y_title(struct graph *bg, const char *title) |
| 612 | { |
| 613 | setstring(&bg->ytitle, title); |
| 614 | } |
| 615 | |
| 616 | static struct graph_label *graph_find_label(struct graph *bg, |
| 617 | const char *label) |
| 618 | { |
Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 619 | struct flist_head *entry; |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 620 | struct graph_label *i; |
Jens Axboe | 3c3ed07 | 2012-03-27 09:12:39 +0200 | [diff] [blame] | 621 | |
Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 622 | flist_for_each(entry, &bg->label_list) { |
| 623 | i = flist_entry(entry, struct graph_label, list); |
| 624 | |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 625 | if (strcmp(label, i->label) == 0) |
| 626 | return i; |
Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 627 | } |
| 628 | |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 629 | return NULL; |
| 630 | } |
| 631 | |
Jens Axboe | 8dfd607 | 2012-03-22 22:10:37 +0100 | [diff] [blame] | 632 | graph_label_t graph_add_label(struct graph *bg, const char *label) |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 633 | { |
| 634 | struct graph_label *i; |
Jens Axboe | 3c3ed07 | 2012-03-27 09:12:39 +0200 | [diff] [blame] | 635 | |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 636 | i = graph_find_label(bg, label); |
| 637 | if (i) |
Jens Axboe | 8dfd607 | 2012-03-22 22:10:37 +0100 | [diff] [blame] | 638 | return i; /* already present. */ |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 639 | i = calloc(1, sizeof(*i)); |
Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 640 | INIT_FLIST_HEAD(&i->value_list); |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 641 | i->parent = bg; |
| 642 | setstring(&i->label, label); |
Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 643 | flist_add_tail(&i->list, &bg->label_list); |
Jens Axboe | b65c7ec | 2012-03-21 17:17:45 +0100 | [diff] [blame] | 644 | INIT_PRIO_TREE_ROOT(&i->prio_tree); |
Jens Axboe | 8dfd607 | 2012-03-22 22:10:37 +0100 | [diff] [blame] | 645 | return i; |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 646 | } |
| 647 | |
Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 648 | static void __graph_value_drop(struct graph_label *l, struct graph_value *v) |
| 649 | { |
Jens Axboe | d0db819 | 2012-03-22 20:48:02 +0100 | [diff] [blame] | 650 | flist_del_init(&v->list); |
Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 651 | if (v->tooltip) |
| 652 | free(v->tooltip); |
| 653 | free(v->value); |
| 654 | free(v); |
| 655 | l->value_count--; |
| 656 | } |
| 657 | |
| 658 | static void graph_value_drop(struct graph_label *l, struct graph_value *v) |
| 659 | { |
Jens Axboe | 8dfd607 | 2012-03-22 22:10:37 +0100 | [diff] [blame] | 660 | if (v->flags & GV_F_PRIO_SKIP) { |
| 661 | __graph_value_drop(l, v); |
| 662 | return; |
| 663 | } |
| 664 | |
Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 665 | /* |
| 666 | * Find head, the guy that's on the prio tree |
| 667 | */ |
| 668 | while (!(v->flags & GV_F_ON_PRIO)) { |
| 669 | assert(!flist_empty(&v->alias)); |
| 670 | v = flist_entry(v->alias.next, struct graph_value, alias); |
| 671 | } |
| 672 | |
| 673 | prio_tree_remove(&l->prio_tree, &v->node); |
| 674 | |
| 675 | /* |
| 676 | * Free aliases |
| 677 | */ |
Jens Axboe | d0db819 | 2012-03-22 20:48:02 +0100 | [diff] [blame] | 678 | while (!flist_empty(&v->alias)) { |
Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 679 | struct graph_value *a; |
| 680 | |
Jens Axboe | d0db819 | 2012-03-22 20:48:02 +0100 | [diff] [blame] | 681 | a = flist_entry(v->alias.next, struct graph_value, alias); |
| 682 | flist_del_init(&a->alias); |
Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 683 | |
| 684 | __graph_value_drop(l, a); |
| 685 | } |
| 686 | |
| 687 | __graph_value_drop(l, v); |
| 688 | } |
| 689 | |
Jens Axboe | b7a69ad | 2012-03-21 21:55:21 +0100 | [diff] [blame] | 690 | static void graph_label_add_value(struct graph_label *i, void *value, |
| 691 | const char *tooltip) |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 692 | { |
Jens Axboe | b7a69ad | 2012-03-21 21:55:21 +0100 | [diff] [blame] | 693 | struct graph *g = i->parent; |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 694 | struct graph_value *x; |
| 695 | |
| 696 | x = malloc(sizeof(*x)); |
Jens Axboe | 3ca30e6 | 2012-03-21 18:54:30 +0100 | [diff] [blame] | 697 | memset(x, 0, sizeof(*x)); |
Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 698 | INIT_FLIST_HEAD(&x->alias); |
| 699 | INIT_FLIST_HEAD(&x->list); |
| 700 | flist_add_tail(&x->list, &i->value_list); |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 701 | i->value_count++; |
Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 702 | x->value = value; |
Jens Axboe | b65c7ec | 2012-03-21 17:17:45 +0100 | [diff] [blame] | 703 | |
Jens Axboe | f00e43f | 2012-03-21 19:53:32 +0100 | [diff] [blame] | 704 | if (tooltip) { |
Jens Axboe | b7a69ad | 2012-03-21 21:55:21 +0100 | [diff] [blame] | 705 | double xval = getx(x); |
| 706 | double minx = xval - (g->xtick_one_val * TOOLTIP_DELTA); |
| 707 | double maxx = xval + (g->xtick_one_val * TOOLTIP_DELTA); |
Jens Axboe | 4c0cd53 | 2012-03-21 19:48:32 +0100 | [diff] [blame] | 708 | struct prio_tree_node *ret; |
Jens Axboe | b65c7ec | 2012-03-21 17:17:45 +0100 | [diff] [blame] | 709 | |
Jens Axboe | 5fb8d79 | 2012-03-21 22:00:43 +0100 | [diff] [blame] | 710 | /* |
| 711 | * use msec to avoid dropping too much precision when |
| 712 | * storing as an integer. |
| 713 | */ |
Jens Axboe | b7a69ad | 2012-03-21 21:55:21 +0100 | [diff] [blame] | 714 | minx = minx * 1000.0; |
| 715 | maxx = maxx * 1000.0; |
| 716 | |
Jens Axboe | 3ca30e6 | 2012-03-21 18:54:30 +0100 | [diff] [blame] | 717 | INIT_PRIO_TREE_NODE(&x->node); |
Jens Axboe | b7a69ad | 2012-03-21 21:55:21 +0100 | [diff] [blame] | 718 | x->node.start = minx; |
| 719 | x->node.last = maxx; |
Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 720 | x->tooltip = strdup(tooltip); |
Jens Axboe | b552682 | 2012-03-21 20:20:35 +0100 | [diff] [blame] | 721 | if (x->node.last == x->node.start) { |
Jens Axboe | b7a69ad | 2012-03-21 21:55:21 +0100 | [diff] [blame] | 722 | x->node.last += fabs(g->xtick_delta); |
| 723 | if (x->node.last == x->node.start) |
| 724 | x->node.last++; |
Jens Axboe | b552682 | 2012-03-21 20:20:35 +0100 | [diff] [blame] | 725 | } |
Jens Axboe | b65c7ec | 2012-03-21 17:17:45 +0100 | [diff] [blame] | 726 | |
Jens Axboe | 4c0cd53 | 2012-03-21 19:48:32 +0100 | [diff] [blame] | 727 | /* |
| 728 | * If ret != &x->node, we have an alias. Since the values |
| 729 | * should be identical, we can drop it |
| 730 | */ |
| 731 | ret = prio_tree_insert(&i->prio_tree, &x->node); |
Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 732 | if (ret != &x->node) { |
| 733 | struct graph_value *alias; |
| 734 | |
| 735 | alias = container_of(ret, struct graph_value, node); |
| 736 | flist_add_tail(&x->alias, &alias->alias); |
Jens Axboe | bd10a06 | 2012-03-22 09:33:33 +0100 | [diff] [blame] | 737 | } else |
Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 738 | x->flags = GV_F_ON_PRIO; |
Jens Axboe | 8dfd607 | 2012-03-22 22:10:37 +0100 | [diff] [blame] | 739 | } else |
| 740 | x->flags = GV_F_PRIO_SKIP; |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 741 | |
Jens Axboe | b7a69ad | 2012-03-21 21:55:21 +0100 | [diff] [blame] | 742 | if (g->per_label_limit != -1 && |
| 743 | i->value_count > g->per_label_limit) { |
Jens Axboe | c148dae | 2012-03-11 21:35:47 +0100 | [diff] [blame] | 744 | int to_drop = 1; |
| 745 | |
| 746 | /* |
| 747 | * If the limit was dynamically reduced, making us more |
| 748 | * than 1 entry ahead after adding this one, drop two |
| 749 | * entries. This will make us (eventually) reach the |
| 750 | * specified limit. |
| 751 | */ |
Jens Axboe | b7a69ad | 2012-03-21 21:55:21 +0100 | [diff] [blame] | 752 | if (i->value_count - g->per_label_limit >= 2) |
Jens Axboe | c148dae | 2012-03-11 21:35:47 +0100 | [diff] [blame] | 753 | to_drop = 2; |
| 754 | |
Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 755 | while (to_drop-- && !flist_empty(&i->value_list)) { |
| 756 | x = flist_entry(i->value_list.next, struct graph_value, list); |
| 757 | graph_value_drop(i, x); |
| 758 | |
| 759 | /* |
| 760 | * If we have aliases, we could drop > 1 above. |
| 761 | */ |
| 762 | if (i->value_count <= g->per_label_limit) |
| 763 | break; |
Jens Axboe | c148dae | 2012-03-11 21:35:47 +0100 | [diff] [blame] | 764 | } |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 765 | } |
| 766 | } |
| 767 | |
Jens Axboe | 8dfd607 | 2012-03-22 22:10:37 +0100 | [diff] [blame] | 768 | int graph_add_data(struct graph *bg, graph_label_t label, const double value) |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 769 | { |
Jens Axboe | 8dfd607 | 2012-03-22 22:10:37 +0100 | [diff] [blame] | 770 | struct graph_label *i = label; |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 771 | double *d; |
| 772 | |
| 773 | d = malloc(sizeof(*d)); |
| 774 | *d = value; |
| 775 | |
Jens Axboe | b7a69ad | 2012-03-21 21:55:21 +0100 | [diff] [blame] | 776 | graph_label_add_value(i, d, NULL); |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 777 | return 0; |
| 778 | } |
| 779 | |
Jens Axboe | 01a947f | 2012-03-22 21:21:00 +0100 | [diff] [blame] | 780 | static int graph_nonzero_y(struct graph_label *l) |
| 781 | { |
| 782 | struct flist_head *entry; |
| 783 | |
| 784 | flist_for_each(entry, &l->value_list) { |
| 785 | struct graph_value *v; |
| 786 | |
| 787 | v = flist_entry(entry, struct graph_value, list); |
| 788 | if (gety(v) != 0.0) |
| 789 | return 1; |
| 790 | } |
| 791 | |
| 792 | return 0; |
| 793 | } |
| 794 | |
Jens Axboe | 8dfd607 | 2012-03-22 22:10:37 +0100 | [diff] [blame] | 795 | int graph_add_xy_data(struct graph *bg, graph_label_t label, |
Jens Axboe | 93e2db2 | 2012-03-13 09:45:22 +0100 | [diff] [blame] | 796 | const double x, const double y, const char *tooltip) |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 797 | { |
Jens Axboe | 8dfd607 | 2012-03-22 22:10:37 +0100 | [diff] [blame] | 798 | struct graph_label *i = label; |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 799 | struct xyvalue *xy; |
| 800 | |
Jens Axboe | 01a947f | 2012-03-22 21:21:00 +0100 | [diff] [blame] | 801 | if (bg->dont_graph_all_zeroes && y == 0.0 && !graph_nonzero_y(i)) |
| 802 | i->hide = 1; |
| 803 | else |
| 804 | i->hide = 0; |
| 805 | |
| 806 | xy = malloc(sizeof(*xy)); |
| 807 | xy->x = x; |
| 808 | xy->y = y; |
| 809 | |
Jens Axboe | b7a69ad | 2012-03-21 21:55:21 +0100 | [diff] [blame] | 810 | graph_label_add_value(i, xy, tooltip); |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 811 | return 0; |
| 812 | } |
| 813 | |
Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 814 | static void graph_free_values(struct graph_label *l) |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 815 | { |
Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 816 | struct graph_value *i; |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 817 | |
Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 818 | while (!flist_empty(&l->value_list)) { |
| 819 | i = flist_entry(l->value_list.next, struct graph_value, list); |
| 820 | graph_value_drop(l, i); |
Jens Axboe | 3c3ed07 | 2012-03-27 09:12:39 +0200 | [diff] [blame] | 821 | } |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 822 | } |
| 823 | |
Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 824 | static void graph_free_labels(struct graph *g) |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 825 | { |
Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 826 | struct graph_label *i; |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 827 | |
Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 828 | while (!flist_empty(&g->label_list)) { |
| 829 | i = flist_entry(g->label_list.next, struct graph_label, list); |
| 830 | flist_del(&i->list); |
| 831 | graph_free_values(i); |
Jens Axboe | b65c7ec | 2012-03-21 17:17:45 +0100 | [diff] [blame] | 832 | free(i); |
Jens Axboe | 3c3ed07 | 2012-03-27 09:12:39 +0200 | [diff] [blame] | 833 | } |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 834 | } |
| 835 | |
Jens Axboe | 8dfd607 | 2012-03-22 22:10:37 +0100 | [diff] [blame] | 836 | void graph_set_color(struct graph *gr, graph_label_t label, double red, |
| 837 | double green, double blue) |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 838 | { |
Jens Axboe | 8dfd607 | 2012-03-22 22:10:37 +0100 | [diff] [blame] | 839 | struct graph_label *i = label; |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 840 | double r, g, b; |
| 841 | |
Stephen M. Cameron | cae0872 | 2012-03-07 14:47:38 +0100 | [diff] [blame] | 842 | if (red < 0.0) { /* invisible color */ |
| 843 | r = -1.0; |
| 844 | g = -1.0; |
| 845 | b = -1.0; |
| 846 | } else { |
| 847 | r = fabs(red); |
| 848 | g = fabs(green); |
| 849 | b = fabs(blue); |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 850 | |
Stephen M. Cameron | cae0872 | 2012-03-07 14:47:38 +0100 | [diff] [blame] | 851 | if (r > 1.0) |
| 852 | r = 1.0; |
| 853 | if (g > 1.0) |
| 854 | g = 1.0; |
| 855 | if (b > 1.0) |
Jens Axboe | b65c7ec | 2012-03-21 17:17:45 +0100 | [diff] [blame] | 856 | b = 1.0; |
Stephen M. Cameron | cae0872 | 2012-03-07 14:47:38 +0100 | [diff] [blame] | 857 | } |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 858 | |
Jens Axboe | 8dfd607 | 2012-03-22 22:10:37 +0100 | [diff] [blame] | 859 | i->r = r; |
| 860 | i->g = g; |
| 861 | i->b = b; |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 862 | } |
| 863 | |
| 864 | void graph_free(struct graph *bg) |
| 865 | { |
Jens Axboe | b65c7ec | 2012-03-21 17:17:45 +0100 | [diff] [blame] | 866 | free(bg->title); |
| 867 | free(bg->xtitle); |
| 868 | free(bg->ytitle); |
Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 869 | graph_free_labels(bg); |
Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 870 | } |
| 871 | |
| 872 | /* For each line in the line graph, up to per_label_limit segments may |
| 873 | * be added. After that, adding more data to the end of the line |
| 874 | * causes data to drop off of the front of the line. |
| 875 | */ |
| 876 | void line_graph_set_data_count_limit(struct graph *g, int per_label_limit) |
| 877 | { |
| 878 | g->per_label_limit = per_label_limit; |
| 879 | } |
| 880 | |
Jens Axboe | 3c3ed07 | 2012-03-27 09:12:39 +0200 | [diff] [blame] | 881 | void graph_add_extra_space(struct graph *g, double left_percent, |
| 882 | double right_percent, double top_percent, |
| 883 | double bottom_percent) |
Stephen M. Cameron | def0ac2 | 2012-03-12 07:32:57 +0100 | [diff] [blame] | 884 | { |
Jens Axboe | 3c3ed07 | 2012-03-27 09:12:39 +0200 | [diff] [blame] | 885 | g->left_extra = left_percent; |
| 886 | g->right_extra = right_percent; |
| 887 | g->top_extra = top_percent; |
| 888 | g->bottom_extra = bottom_percent; |
Stephen M. Cameron | def0ac2 | 2012-03-12 07:32:57 +0100 | [diff] [blame] | 889 | } |
| 890 | |
Jens Axboe | d8fbeef | 2012-03-14 10:25:44 +0100 | [diff] [blame] | 891 | /* |
| 892 | * Normally values are logged in a base unit of 0, but for other purposes |
| 893 | * it makes more sense to log in higher unit. For instance for bandwidth |
| 894 | * purposes, you may want to log in KB/sec (or MB/sec) rather than bytes/sec. |
| 895 | */ |
| 896 | void graph_set_base_offset(struct graph *g, unsigned int base_offset) |
| 897 | { |
| 898 | g->base_offset = base_offset; |
| 899 | } |
| 900 | |
Jens Axboe | 93e2db2 | 2012-03-13 09:45:22 +0100 | [diff] [blame] | 901 | int graph_has_tooltips(struct graph *g) |
| 902 | { |
Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 903 | struct flist_head *entry; |
Jens Axboe | 93e2db2 | 2012-03-13 09:45:22 +0100 | [diff] [blame] | 904 | struct graph_label *i; |
Stephen M. Cameron | def0ac2 | 2012-03-12 07:32:57 +0100 | [diff] [blame] | 905 | |
Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 906 | flist_for_each(entry, &g->label_list) { |
| 907 | i = flist_entry(entry, struct graph_label, list); |
| 908 | |
| 909 | if (!prio_tree_empty(&i->prio_tree)) |
Jens Axboe | 93e2db2 | 2012-03-13 09:45:22 +0100 | [diff] [blame] | 910 | return 1; |
Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 911 | } |
Jens Axboe | 93e2db2 | 2012-03-13 09:45:22 +0100 | [diff] [blame] | 912 | |
| 913 | return 0; |
| 914 | } |
| 915 | |
| 916 | int graph_contains_xy(struct graph *g, int x, int y) |
| 917 | { |
| 918 | int first_x = g->xoffset; |
| 919 | int last_x = g->xoffset + g->xdim; |
| 920 | int first_y = g->yoffset; |
| 921 | int last_y = g->yoffset + g->ydim; |
| 922 | |
| 923 | return (x >= first_x && x <= last_x) && (y >= first_y && y <= last_y); |
| 924 | } |
| 925 | |
Jens Axboe | b65c7ec | 2012-03-21 17:17:45 +0100 | [diff] [blame] | 926 | const char *graph_find_tooltip(struct graph *g, int ix, int iy) |
Jens Axboe | 93e2db2 | 2012-03-13 09:45:22 +0100 | [diff] [blame] | 927 | { |
Jens Axboe | b65c7ec | 2012-03-21 17:17:45 +0100 | [diff] [blame] | 928 | double x = ix, y = iy; |
| 929 | struct prio_tree_iter iter; |
| 930 | struct prio_tree_node *n; |
Jens Axboe | b65c7ec | 2012-03-21 17:17:45 +0100 | [diff] [blame] | 931 | struct graph_value *best = NULL; |
Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 932 | struct flist_head *entry; |
Jens Axboe | b65c7ec | 2012-03-21 17:17:45 +0100 | [diff] [blame] | 933 | double best_delta; |
Jens Axboe | b7a69ad | 2012-03-21 21:55:21 +0100 | [diff] [blame] | 934 | double maxy, miny; |
Jens Axboe | 93e2db2 | 2012-03-13 09:45:22 +0100 | [diff] [blame] | 935 | |
Jens Axboe | b65c7ec | 2012-03-21 17:17:45 +0100 | [diff] [blame] | 936 | x -= g->xoffset; |
| 937 | y -= g->yoffset; |
| 938 | |
| 939 | x = g->xtick_zero_val + ((x - g->xtick_zero) * g->xtick_delta); |
| 940 | y = g->ytick_zero_val + ((y - g->ytick_zero) * g->ytick_delta); |
| 941 | |
Jens Axboe | b7a69ad | 2012-03-21 21:55:21 +0100 | [diff] [blame] | 942 | x = x * 1000.0; |
| 943 | maxy = y + (g->ytick_one_val * TOOLTIP_DELTA); |
| 944 | miny = y - (g->ytick_one_val * TOOLTIP_DELTA); |
Jens Axboe | b65c7ec | 2012-03-21 17:17:45 +0100 | [diff] [blame] | 945 | best_delta = UINT_MAX; |
Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 946 | flist_for_each(entry, &g->label_list) { |
| 947 | struct graph_label *i; |
| 948 | |
| 949 | i = flist_entry(entry, struct graph_label, list); |
Jens Axboe | 01a947f | 2012-03-22 21:21:00 +0100 | [diff] [blame] | 950 | if (i->hide) |
| 951 | continue; |
Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 952 | |
Jens Axboe | 08d7d5a | 2012-03-21 19:33:32 +0100 | [diff] [blame] | 953 | INIT_PRIO_TREE_ITER(&iter); |
Jens Axboe | b7a69ad | 2012-03-21 21:55:21 +0100 | [diff] [blame] | 954 | prio_tree_iter_init(&iter, &i->prio_tree, x, x); |
Jens Axboe | b65c7ec | 2012-03-21 17:17:45 +0100 | [diff] [blame] | 955 | |
| 956 | n = prio_tree_next(&iter); |
| 957 | if (!n) |
| 958 | continue; |
| 959 | |
| 960 | do { |
Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 961 | struct graph_value *v, *rootv; |
Jens Axboe | b7a69ad | 2012-03-21 21:55:21 +0100 | [diff] [blame] | 962 | double yval, ydiff; |
Jens Axboe | b65c7ec | 2012-03-21 17:17:45 +0100 | [diff] [blame] | 963 | |
| 964 | v = container_of(n, struct graph_value, node); |
Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 965 | rootv = v; |
| 966 | do { |
| 967 | yval = gety(v); |
| 968 | ydiff = fabs(yval - y); |
Jens Axboe | 93e2db2 | 2012-03-13 09:45:22 +0100 | [diff] [blame] | 969 | |
Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 970 | /* |
| 971 | * zero delta, or within or match critera, break |
| 972 | */ |
| 973 | if (ydiff < best_delta) { |
| 974 | best_delta = ydiff; |
| 975 | if (!best_delta || |
| 976 | (yval >= miny && yval <= maxy)) { |
| 977 | best = v; |
| 978 | break; |
| 979 | } |
Jens Axboe | b65c7ec | 2012-03-21 17:17:45 +0100 | [diff] [blame] | 980 | } |
Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 981 | if (!flist_empty(&v->alias)) |
| 982 | v = flist_entry(v->alias.next, struct graph_value, alias); |
| 983 | } while (v != rootv); |
Jens Axboe | b65c7ec | 2012-03-21 17:17:45 +0100 | [diff] [blame] | 984 | } while ((n = prio_tree_next(&iter)) != NULL); |
| 985 | |
| 986 | /* |
| 987 | * If we got matches in one label, don't check others. |
| 988 | */ |
Jens Axboe | d0db819 | 2012-03-22 20:48:02 +0100 | [diff] [blame] | 989 | if (best) |
| 990 | break; |
Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 991 | } |
Jens Axboe | b65c7ec | 2012-03-21 17:17:45 +0100 | [diff] [blame] | 992 | |
| 993 | if (best) |
| 994 | return best->tooltip; |
Jens Axboe | 93e2db2 | 2012-03-13 09:45:22 +0100 | [diff] [blame] | 995 | |
| 996 | return NULL; |
| 997 | } |
Jens Axboe | 01a947f | 2012-03-22 21:21:00 +0100 | [diff] [blame] | 998 | |
| 999 | void graph_set_graph_all_zeroes(struct graph *g, unsigned int set) |
| 1000 | { |
| 1001 | g->dont_graph_all_zeroes = !set; |
| 1002 | } |