blob: 415c676cd792e2b43c9372c3aaccf448a1a1dcd0 [file] [log] [blame]
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +01001/*
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 Axboe93e2db22012-03-13 09:45:22 +010027#include <stdlib.h>
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +010028
29#include <cairo.h>
30#include <gtk/gtk.h>
31
32#include "tickmarks.h"
Stephen M. Cameron09ad20f2012-03-11 11:34:38 +010033#include "graph.h"
Jens Axboeb65c7ec2012-03-21 17:17:45 +010034#include "flist.h"
35#include "lib/prio_tree.h"
Jens Axboeb65c7ec2012-03-21 17:17:45 +010036
37/*
38 * Allowable difference to show tooltip
39 */
Jens Axboeb7a69ad2012-03-21 21:55:21 +010040#define TOOLTIP_DELTA 0.08
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +010041
42struct xyvalue {
43 double x, y;
44};
45
Jens Axboecdae5ff2012-03-22 09:24:05 +010046enum {
47 GV_F_ON_PRIO = 1,
Jens Axboe8dfd6072012-03-22 22:10:37 +010048 GV_F_PRIO_SKIP = 2,
Jens Axboecdae5ff2012-03-22 09:24:05 +010049};
50
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +010051struct graph_value {
Jens Axboecdae5ff2012-03-22 09:24:05 +010052 struct flist_head list;
Jens Axboeb65c7ec2012-03-21 17:17:45 +010053 struct prio_tree_node node;
Jens Axboecdae5ff2012-03-22 09:24:05 +010054 struct flist_head alias;
55 unsigned int flags;
Jens Axboe93e2db22012-03-13 09:45:22 +010056 char *tooltip;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +010057 void *value;
58};
59
60struct graph_label {
Jens Axboecdae5ff2012-03-22 09:24:05 +010061 struct flist_head list;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +010062 char *label;
Jens Axboecdae5ff2012-03-22 09:24:05 +010063 struct flist_head value_list;
Jens Axboeb65c7ec2012-03-21 17:17:45 +010064 struct prio_tree_root prio_tree;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +010065 double r, g, b;
Jens Axboe01a947f2012-03-22 21:21:00 +010066 int hide;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +010067 int value_count;
68 struct graph *parent;
69};
70
Jens Axboeb65c7ec2012-03-21 17:17:45 +010071struct tick_value {
72 unsigned int offset;
73 double value;
74};
75
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +010076struct graph {
77 char *title;
78 char *xtitle;
79 char *ytitle;
Jens Axboe87d5f272012-03-07 12:31:40 +010080 unsigned int xdim, ydim;
Stephen M. Cameron57f9d282012-03-11 11:36:51 +010081 double xoffset, yoffset;
Jens Axboecdae5ff2012-03-22 09:24:05 +010082 struct flist_head label_list;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +010083 int per_label_limit;
Jens Axboef3e84402012-03-07 13:14:32 +010084 const char *font;
Stephen M. Cameron7175d912012-03-11 11:35:10 +010085 graph_axis_unit_change_callback x_axis_unit_change_callback;
86 graph_axis_unit_change_callback y_axis_unit_change_callback;
Jens Axboed8fbeef2012-03-14 10:25:44 +010087 unsigned int base_offset;
Jens Axboe01a947f2012-03-22 21:21:00 +010088 unsigned int dont_graph_all_zeroes;
Stephen M. Camerondef0ac22012-03-12 07:32:57 +010089 double left_extra;
90 double right_extra;
91 double top_extra;
92 double bottom_extra;
Jens Axboeb65c7ec2012-03-21 17:17:45 +010093
94 double xtick_zero;
95 double xtick_delta;
96 double xtick_zero_val;
Jens Axboeb7a69ad2012-03-21 21:55:21 +010097 double xtick_one_val;
Jens Axboeb65c7ec2012-03-21 17:17:45 +010098 double ytick_zero;
99 double ytick_delta;
100 double ytick_zero_val;
Jens Axboeb7a69ad2012-03-21 21:55:21 +0100101 double ytick_one_val;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100102};
103
Stephen M. Cameron3ea48b82012-03-07 19:40:58 +0100104void graph_set_size(struct graph *g, unsigned int xdim, unsigned int ydim)
105{
106 g->xdim = xdim;
107 g->ydim = ydim;
108}
109
Stephen M. Cameron57f9d282012-03-11 11:36:51 +0100110void graph_set_position(struct graph *g, double xoffset, double yoffset)
111{
112 g->xoffset = xoffset;
113 g->yoffset = yoffset;
114}
115
Jens Axboef3e84402012-03-07 13:14:32 +0100116struct graph *graph_new(unsigned int xdim, unsigned int ydim, const char *font)
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100117{
118 struct graph *g;
119
120 g = calloc(1, sizeof(*g));
Jens Axboecdae5ff2012-03-22 09:24:05 +0100121 INIT_FLIST_HEAD(&g->label_list);
Stephen M. Cameron3ea48b82012-03-07 19:40:58 +0100122 graph_set_size(g, xdim, ydim);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100123 g->per_label_limit = -1;
Jens Axboef3e84402012-03-07 13:14:32 +0100124 g->font = font;
125 if (!g->font)
126 g->font = "Sans";
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100127 return g;
128}
129
Stephen M. Cameron7175d912012-03-11 11:35:10 +0100130void graph_x_axis_unit_change_notify(struct graph *g, graph_axis_unit_change_callback f)
131{
132 g->x_axis_unit_change_callback = f;
133}
134
135void graph_y_axis_unit_change_notify(struct graph *g, graph_axis_unit_change_callback f)
136{
137 g->y_axis_unit_change_callback = f;
138}
139
Jens Axboecdae5ff2012-03-22 09:24:05 +0100140static int count_labels(struct graph *g)
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100141{
Jens Axboecdae5ff2012-03-22 09:24:05 +0100142 struct flist_head *entry;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100143 int count = 0;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100144
Jens Axboecdae5ff2012-03-22 09:24:05 +0100145 flist_for_each(entry, &g->label_list)
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100146 count++;
Jens Axboecdae5ff2012-03-22 09:24:05 +0100147
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100148 return count;
149}
150
Jens Axboecdae5ff2012-03-22 09:24:05 +0100151static int count_values(struct graph_label *l)
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100152{
Jens Axboecdae5ff2012-03-22 09:24:05 +0100153 struct flist_head *entry;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100154 int count = 0;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100155
Jens Axboecdae5ff2012-03-22 09:24:05 +0100156 flist_for_each(entry, &l->value_list)
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100157 count++;
Jens Axboecdae5ff2012-03-22 09:24:05 +0100158
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100159 return count;
160}
161
162typedef double (*double_comparator)(double a, double b);
163
164static double mindouble(double a, double b)
165{
166 return a < b ? a : b;
167}
168
169static double maxdouble(double a, double b)
170{
171 return a < b ? b : a;
172}
173
Jens Axboecdae5ff2012-03-22 09:24:05 +0100174static double find_double_values(struct graph_label *l, double_comparator cmp)
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100175{
Jens Axboecdae5ff2012-03-22 09:24:05 +0100176 struct flist_head *entry;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100177 double answer, tmp;
Jens Axboecdae5ff2012-03-22 09:24:05 +0100178 int first = 1;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100179
Jens Axboe01a947f2012-03-22 21:21:00 +0100180 if (flist_empty(&l->value_list))
181 return 0.0;
182
Jens Axboecdae5ff2012-03-22 09:24:05 +0100183 flist_for_each(entry, &l->value_list) {
184 struct graph_value *i;
185
186 i = flist_entry(entry, struct graph_value, list);
187 tmp = *(double *) i->value;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100188 if (first) {
189 answer = tmp;
190 first = 0;
191 } else {
192 answer = cmp(answer, tmp);
193 }
194 }
195 return answer;
196}
197
Jens Axboecdae5ff2012-03-22 09:24:05 +0100198static double find_double_data(struct graph *g, double_comparator cmp)
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100199{
Jens Axboecdae5ff2012-03-22 09:24:05 +0100200 struct flist_head *entry;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100201 struct graph_label *i;
202 int first = 1;
203 double answer, tmp;
204
Jens Axboe01a947f2012-03-22 21:21:00 +0100205 if (flist_empty(&g->label_list))
206 return 0.0;
207
Jens Axboecdae5ff2012-03-22 09:24:05 +0100208 flist_for_each(entry, &g->label_list) {
209 i = flist_entry(entry, struct graph_label, list);
210 tmp = find_double_values(i, cmp);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100211 if (first) {
212 answer = tmp;
213 first = 0;
214 } else {
215 answer = cmp(tmp, answer);
216 }
217 }
218 return answer;
219}
220
Jens Axboecdae5ff2012-03-22 09:24:05 +0100221static double find_min_data(struct graph *g)
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100222{
Jens Axboecdae5ff2012-03-22 09:24:05 +0100223 return find_double_data(g, mindouble);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100224}
225
Jens Axboecdae5ff2012-03-22 09:24:05 +0100226static double find_max_data(struct graph *g)
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100227{
Jens Axboecdae5ff2012-03-22 09:24:05 +0100228 return find_double_data(g, maxdouble);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100229}
230
231static void draw_bars(struct graph *bg, cairo_t *cr, struct graph_label *lb,
232 double label_offset, double bar_width,
233 double mindata, double maxdata)
234{
Jens Axboecdae5ff2012-03-22 09:24:05 +0100235 struct flist_head *entry;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100236 double x1, y1, x2, y2;
237 int bar_num = 0;
238 double domain, range, v;
239
240 domain = (maxdata - mindata);
241 range = (double) bg->ydim * 0.80; /* FIXME */
242 cairo_stroke(cr);
Jens Axboecdae5ff2012-03-22 09:24:05 +0100243 flist_for_each(entry, &lb->value_list) {
244 struct graph_value *i;
245
246 i = flist_entry(entry, struct graph_value, list);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100247
248 x1 = label_offset + (double) bar_num * bar_width + (bar_width * 0.05);
249 x2 = x1 + bar_width * 0.90;
250 y2 = bg->ydim * 0.90;
251 v = *(double *) i->value;
252 y1 = y2 - (((v - mindata) / domain) * range);
253 cairo_move_to(cr, x1, y1);
254 cairo_line_to(cr, x1, y2);
255 cairo_line_to(cr, x2, y2);
256 cairo_line_to(cr, x2, y1);
257 cairo_close_path(cr);
258 cairo_fill(cr);
259 cairo_stroke(cr);
260 bar_num++;
261 }
262}
263
Stephen M. Cameron10e54cd2012-03-07 19:39:55 +0100264static void draw_aligned_text(struct graph *g, cairo_t *cr, double x, double y,
265 double fontsize, const char *text, int alignment)
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100266{
Stephen M. Cameron10e54cd2012-03-07 19:39:55 +0100267#define CENTERED 0
268#define LEFT_JUSTIFIED 1
269#define RIGHT_JUSTIFIED 2
270
271 double factor, direction;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100272 cairo_text_extents_t extents;
273
Stephen M. Cameron10e54cd2012-03-07 19:39:55 +0100274 switch(alignment) {
275 case CENTERED:
276 direction = -1.0;
277 factor = 0.5;
278 break;
279 case RIGHT_JUSTIFIED:
280 direction = -1.0;
281 factor = 1.0;
282 break;
283 case LEFT_JUSTIFIED:
284 default:
285 direction = 1.0;
286 factor = 1.0;
287 break;
288 }
Jens Axboef3e84402012-03-07 13:14:32 +0100289 cairo_select_font_face (cr, g->font, CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100290
291 cairo_set_font_size(cr, fontsize);
292 cairo_text_extents(cr, text, &extents);
Stephen M. Cameron10e54cd2012-03-07 19:39:55 +0100293 x = x + direction * (factor * extents.width + extents.x_bearing);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100294 y = y - (extents.height / 2 + extents.y_bearing);
295
296 cairo_move_to(cr, x, y);
297 cairo_show_text(cr, text);
298}
299
Stephen M. Cameron10e54cd2012-03-07 19:39:55 +0100300static inline void draw_centered_text(struct graph *g, cairo_t *cr, double x, double y,
301 double fontsize, const char *text)
302{
303 draw_aligned_text(g, cr, x, y, fontsize, text, CENTERED);
304}
305
306static inline void draw_right_justified_text(struct graph *g, cairo_t *cr,
307 double x, double y,
308 double fontsize, const char *text)
309{
310 draw_aligned_text(g, cr, x, y, fontsize, text, RIGHT_JUSTIFIED);
311}
312
313static inline void draw_left_justified_text(struct graph *g, cairo_t *cr,
314 double x, double y,
315 double fontsize, const char *text)
316{
317 draw_aligned_text(g, cr, x, y, fontsize, text, LEFT_JUSTIFIED);
318}
319
Jens Axboef3e84402012-03-07 13:14:32 +0100320static void draw_vertical_centered_text(struct graph *g, cairo_t *cr, double x,
321 double y, double fontsize,
322 const char *text)
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100323{
324 double sx, sy;
325 cairo_text_extents_t extents;
326
Jens Axboef3e84402012-03-07 13:14:32 +0100327 cairo_select_font_face(cr, g->font, CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100328
329 cairo_set_font_size(cr, fontsize);
330 cairo_text_extents(cr, text, &extents);
331 sx = x;
332 sy = y;
333 y = y + (extents.width / 2.0 + extents.x_bearing);
334 x = x - (extents.height / 2.0 + extents.y_bearing);
335
336 cairo_move_to(cr, x, y);
337 cairo_save(cr);
338 cairo_translate(cr, -sx, -sy);
339 cairo_rotate(cr, -90.0 * M_PI / 180.0);
340 cairo_translate(cr, sx, sy);
341 cairo_show_text(cr, text);
342 cairo_restore(cr);
343}
344
345static void graph_draw_common(struct graph *g, cairo_t *cr,
346 double *x1, double *y1, double *x2, double *y2)
347{
Jens Axboe4e14f012012-03-23 08:18:57 +0100348 cairo_set_source_rgb(cr, 0, 0, 0);
349 cairo_set_line_width (cr, 0.8);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100350
Jens Axboe9ede9c92012-03-14 10:43:20 +0100351 *x1 = 0.10 * g->xdim;
Stephen M. Cameron6bf86002012-03-08 16:58:50 +0100352 *x2 = 0.95 * g->xdim;
353 *y1 = 0.10 * g->ydim;
354 *y2 = 0.90 * g->ydim;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100355
356 cairo_move_to(cr, *x1, *y1);
357 cairo_line_to(cr, *x1, *y2);
358 cairo_line_to(cr, *x2, *y2);
359 cairo_line_to(cr, *x2, *y1);
360 cairo_line_to(cr, *x1, *y1);
361 cairo_stroke(cr);
362
Jens Axboef3e84402012-03-07 13:14:32 +0100363 draw_centered_text(g, cr, g->xdim / 2, g->ydim / 20, 20.0, g->title);
364 draw_centered_text(g, cr, g->xdim / 2, g->ydim * 0.97, 14.0, g->xtitle);
365 draw_vertical_centered_text(g, cr, g->xdim * 0.02, g->ydim / 2, 14.0, g->ytitle);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100366 cairo_stroke(cr);
367}
368
369static void graph_draw_x_ticks(struct graph *g, cairo_t *cr,
370 double x1, double y1, double x2, double y2,
Jens Axboed8fbeef2012-03-14 10:25:44 +0100371 double minx, double maxx, int nticks, int add_tm_text)
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100372{
373 struct tickmark *tm;
374 double tx;
Stephen M. Cameron7175d912012-03-11 11:35:10 +0100375 int i, power_of_ten;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100376 static double dash[] = { 1.0, 2.0 };
377
Stephen M. Cameron7175d912012-03-11 11:35:10 +0100378 nticks = calc_tickmarks(minx, maxx, nticks, &tm, &power_of_ten,
Jens Axboed8fbeef2012-03-14 10:25:44 +0100379 g->x_axis_unit_change_callback == NULL, g->base_offset);
Stephen M. Cameron7175d912012-03-11 11:35:10 +0100380 if (g->x_axis_unit_change_callback)
381 g->x_axis_unit_change_callback(g, power_of_ten);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100382
383 for (i = 0; i < nticks; i++) {
384 tx = (((tm[i].value) - minx) / (maxx - minx)) * (x2 - x1) + x1;
Jens Axboed8fbeef2012-03-14 10:25:44 +0100385
Jens Axboeb65c7ec2012-03-21 17:17:45 +0100386 /*
387 * Update tick delta
388 */
389 if (!i) {
390 g->xtick_zero = tx;
391 g->xtick_zero_val = tm[0].value;
Jens Axboeb7a69ad2012-03-21 21:55:21 +0100392 } else if (i == 1) {
Jens Axboeb65c7ec2012-03-21 17:17:45 +0100393 g->xtick_delta = (tm[1].value - tm[0].value) / (tx - g->xtick_zero);
Jens Axboeb7a69ad2012-03-21 21:55:21 +0100394 g->xtick_one_val = tm[1].value;
395 }
Jens Axboeb65c7ec2012-03-21 17:17:45 +0100396
Jens Axboed8fbeef2012-03-14 10:25:44 +0100397 /* really tx < yx || tx > x2, but protect against rounding */
398 if (x1 - tx > 0.01 || tx - x2 > 0.01)
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100399 continue;
400
401 /* Draw tick mark */
Jens Axboef3e84402012-03-07 13:14:32 +0100402 cairo_set_line_width(cr, 0.8);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100403 cairo_move_to(cr, tx, y2);
404 cairo_line_to(cr, tx, y2 + (y2 - y1) * 0.03);
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, tx, y1);
412 cairo_line_to(cr, tx, y2);
413 cairo_stroke(cr);
414 cairo_restore(cr);
415
Jens Axboed8fbeef2012-03-14 10:25:44 +0100416 if (!add_tm_text)
417 continue;
418
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100419 /* draw tickmark label */
Jens Axboef3e84402012-03-07 13:14:32 +0100420 draw_centered_text(g, cr, tx, y2 * 1.04, 12.0, tm[i].string);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100421 cairo_stroke(cr);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100422 }
423}
424
Jens Axboed8fbeef2012-03-14 10:25:44 +0100425static double graph_draw_y_ticks(struct graph *g, cairo_t *cr,
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100426 double x1, double y1, double x2, double y2,
Jens Axboed8fbeef2012-03-14 10:25:44 +0100427 double miny, double maxy, int nticks, int add_tm_text)
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100428{
429 struct tickmark *tm;
430 double ty;
Stephen M. Cameron7175d912012-03-11 11:35:10 +0100431 int i, power_of_ten;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100432 static double dash[] = { 2.0, 2.0 };
433
Stephen M. Cameron7175d912012-03-11 11:35:10 +0100434 nticks = calc_tickmarks(miny, maxy, nticks, &tm, &power_of_ten,
Jens Axboed8fbeef2012-03-14 10:25:44 +0100435 g->y_axis_unit_change_callback == NULL, g->base_offset);
Stephen M. Cameron7175d912012-03-11 11:35:10 +0100436 if (g->y_axis_unit_change_callback)
437 g->y_axis_unit_change_callback(g, power_of_ten);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100438
Jens Axboed8fbeef2012-03-14 10:25:44 +0100439 /*
440 * Use highest tickmark as top of graph, not highest value. Otherwise
441 * it's impossible to see what the max value is, if the graph is
442 * fairly flat.
443 */
444 maxy = tm[nticks - 1].value;
445
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100446 for (i = 0; i < nticks; i++) {
447 ty = y2 - (((tm[i].value) - miny) / (maxy - miny)) * (y2 - y1);
Jens Axboed8fbeef2012-03-14 10:25:44 +0100448
Jens Axboeb65c7ec2012-03-21 17:17:45 +0100449 /*
450 * Update tick delta
451 */
452 if (!i) {
453 g->ytick_zero = ty;
454 g->ytick_zero_val = tm[0].value;
Jens Axboeb7a69ad2012-03-21 21:55:21 +0100455 } else if (i == 1) {
Jens Axboeb65c7ec2012-03-21 17:17:45 +0100456 g->ytick_delta = (tm[1].value - tm[0].value) / (ty - g->ytick_zero);
Jens Axboeb7a69ad2012-03-21 21:55:21 +0100457 g->ytick_one_val = tm[1].value;
458 }
Jens Axboeb65c7ec2012-03-21 17:17:45 +0100459
Jens Axboed8fbeef2012-03-14 10:25:44 +0100460 /* really ty < y1 || ty > y2, but protect against rounding */
461 if (y1 - ty > 0.01 || ty - y2 > 0.01)
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100462 continue;
Jens Axboed8fbeef2012-03-14 10:25:44 +0100463
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100464 /* draw tick mark */
465 cairo_move_to(cr, x1, ty);
466 cairo_line_to(cr, x1 - (x2 - x1) * 0.02, ty);
467 cairo_stroke(cr);
468
469 /* draw grid lines */
470 cairo_save(cr);
471 cairo_set_dash(cr, dash, 2, 2.0);
472 cairo_set_line_width(cr, 0.5);
473 cairo_move_to(cr, x1, ty);
474 cairo_line_to(cr, x2, ty);
475 cairo_stroke(cr);
476 cairo_restore(cr);
477
Jens Axboed8fbeef2012-03-14 10:25:44 +0100478 if (!add_tm_text)
479 continue;
480
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100481 /* draw tickmark label */
Stephen M. Cameron10e54cd2012-03-07 19:39:55 +0100482 draw_right_justified_text(g, cr, x1 - (x2 - x1) * 0.025, ty, 12.0, tm[i].string);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100483 cairo_stroke(cr);
484 }
Jens Axboed8fbeef2012-03-14 10:25:44 +0100485
486 /*
487 * Return new max to use
488 */
489 return maxy;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100490}
491
492void bar_graph_draw(struct graph *bg, cairo_t *cr)
493{
494 double x1, y1, x2, y2;
495 double space_per_label, bar_width;
496 double label_offset, mindata, maxdata;
497 int i, nlabels;
498 struct graph_label *lb;
Jens Axboecdae5ff2012-03-22 09:24:05 +0100499 struct flist_head *entry;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100500
501 cairo_save(cr);
Stephen M. Cameron57f9d282012-03-11 11:36:51 +0100502 cairo_translate(cr, bg->xoffset, bg->yoffset);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100503 graph_draw_common(bg, cr, &x1, &y1, &x2, &y2);
504
Jens Axboecdae5ff2012-03-22 09:24:05 +0100505 nlabels = count_labels(bg);
Jens Axboed8fbeef2012-03-14 10:25:44 +0100506 space_per_label = (x2 - x1) / (double) nlabels;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100507
Jens Axboebb393792012-03-15 10:52:38 +0100508 /*
509 * Start bars at 0 unless we have negative values, otherwise we
510 * present a skewed picture comparing label X and X+1.
511 */
Jens Axboecdae5ff2012-03-22 09:24:05 +0100512 mindata = find_min_data(bg);
Jens Axboebb393792012-03-15 10:52:38 +0100513 if (mindata > 0)
514 mindata = 0;
515
Jens Axboecdae5ff2012-03-22 09:24:05 +0100516 maxdata = find_max_data(bg);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100517
518 if (fabs(maxdata - mindata) < 1e-20) {
Jens Axboef3e84402012-03-07 13:14:32 +0100519 draw_centered_text(bg, cr,
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100520 x1 + (x2 - x1) / 2.0,
521 y1 + (y2 - y1) / 2.0, 20.0, "No good data");
522 return;
523 }
524
Jens Axboebb393792012-03-15 10:52:38 +0100525 maxdata = graph_draw_y_ticks(bg, cr, x1, y1, x2, y2, mindata, maxdata, 10, 1);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100526 i = 0;
Jens Axboecdae5ff2012-03-22 09:24:05 +0100527 flist_for_each(entry, &bg->label_list) {
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100528 int nvalues;
Jens Axboecdae5ff2012-03-22 09:24:05 +0100529
530 lb = flist_entry(entry, struct graph_label, list);
531 nvalues = count_values(lb);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100532 bar_width = (space_per_label - space_per_label * 0.2) / (double) nvalues;
533 label_offset = bg->xdim * 0.1 + space_per_label * (double) i + space_per_label * 0.1;
534 draw_bars(bg, cr, lb, label_offset, bar_width, mindata, maxdata);
535 // draw_centered_text(cr, label_offset + (bar_width / 2.0 + bar_width * 0.1), bg->ydim * 0.93,
Jens Axboef3e84402012-03-07 13:14:32 +0100536 draw_centered_text(bg, cr, x1 + space_per_label * (i + 0.5), bg->ydim * 0.93,
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100537 12.0, lb->label);
538 i++;
539 }
540 cairo_stroke(cr);
541 cairo_restore(cr);
542}
543
544typedef double (*xy_value_extractor)(struct graph_value *v);
545
546static double getx(struct graph_value *v)
547{
548 struct xyvalue *xy = v->value;
549 return xy->x;
550}
551
552static double gety(struct graph_value *v)
553{
554 struct xyvalue *xy = v->value;
555 return xy->y;
556}
557
558static double find_xy_value(struct graph *g, xy_value_extractor getvalue, double_comparator cmp)
559{
Stephen M. Cameron10e54cd2012-03-07 19:39:55 +0100560 double tmp, answer = 0.0;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100561 struct graph_label *i;
562 struct graph_value *j;
Jens Axboecdae5ff2012-03-22 09:24:05 +0100563 struct flist_head *jentry, *entry;
Stephen M. Camerond582bf72012-03-07 19:37:57 +0100564 int first = 1;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100565
Jens Axboecdae5ff2012-03-22 09:24:05 +0100566 flist_for_each(entry, &g->label_list) {
567 i = flist_entry(entry, struct graph_label, list);
568
569 flist_for_each(jentry, &i->value_list) {
570 j = flist_entry(jentry, struct graph_value, list);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100571 tmp = getvalue(j);
Stephen M. Camerond582bf72012-03-07 19:37:57 +0100572 if (first) {
573 first = 0;
574 answer = tmp;
575 }
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100576 answer = cmp(tmp, answer);
577 }
Jens Axboecdae5ff2012-03-22 09:24:05 +0100578 }
579
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100580 return answer;
Jens Axboe01a947f2012-03-22 21:21:00 +0100581}
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100582
583void line_graph_draw(struct graph *g, cairo_t *cr)
584{
585 double x1, y1, x2, y2;
Stephen M. Camerondef0ac22012-03-12 07:32:57 +0100586 double minx, miny, maxx, maxy, gminx, gminy, gmaxx, gmaxy;
587 double tx, ty, top_extra, bottom_extra, left_extra, right_extra;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100588 struct graph_label *i;
589 struct graph_value *j;
Stephen M. Cameron9ce9cfb2012-03-07 19:37:25 +0100590 int good_data = 1, first = 1;
Jens Axboecdae5ff2012-03-22 09:24:05 +0100591 struct flist_head *entry, *lentry;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100592
593 cairo_save(cr);
Stephen M. Cameron57f9d282012-03-11 11:36:51 +0100594 cairo_translate(cr, g->xoffset, g->yoffset);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100595 graph_draw_common(g, cr, &x1, &y1, &x2, &y2);
596
597 minx = find_xy_value(g, getx, mindouble);
598 maxx = find_xy_value(g, getx, maxdouble);
599 miny = find_xy_value(g, gety, mindouble);
Jens Axboe5aec6682012-03-16 10:17:08 +0100600
601 /*
602 * Start graphs at zero, unless we have a value below. Otherwise
603 * it's hard to visually compare the read and write graph, since
604 * the lowest valued one will be the floor of the graph view.
605 */
606 if (miny > 0)
607 miny = 0;
608
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100609 maxy = find_xy_value(g, gety, maxdouble);
610
611 if (fabs(maxx - minx) < 1e-20 || fabs(maxy - miny) < 1e-20) {
Stephen M. Cameron9ce9cfb2012-03-07 19:37:25 +0100612 good_data = 0;
613 minx = 0.0;
614 miny = 0.0;
615 maxx = 10.0;
616 maxy = 100.0;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100617 }
618
Stephen M. Camerondef0ac22012-03-12 07:32:57 +0100619 top_extra = 0.0;
620 bottom_extra = 0.0;
621 left_extra = 0.0;
622 right_extra = 0.0;
623
624 if (g->top_extra > 0.001)
625 top_extra = fabs(maxy - miny) * g->top_extra;
626 if (g->bottom_extra > 0.001)
627 bottom_extra = fabs(maxy - miny) * g->bottom_extra;
628 if (g->left_extra > 0.001)
629 left_extra = fabs(maxx - minx) * g->left_extra;
630 if (g->right_extra > 0.001)
631 right_extra = fabs(maxx - minx) * g->right_extra;
632
633 gminx = minx - left_extra;
634 gmaxx = maxx + right_extra;
635 gminy = miny - bottom_extra;
636 gmaxy = maxy + top_extra;
637
Jens Axboed8fbeef2012-03-14 10:25:44 +0100638 graph_draw_x_ticks(g, cr, x1, y1, x2, y2, gminx, gmaxx, 10, good_data);
639 gmaxy = graph_draw_y_ticks(g, cr, x1, y1, x2, y2, gminy, gmaxy, 10, good_data);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100640
Stephen M. Cameron9ce9cfb2012-03-07 19:37:25 +0100641 if (!good_data)
642 goto skip_data;
643
Jens Axboef3e84402012-03-07 13:14:32 +0100644 cairo_set_line_width(cr, 1.5);
Jens Axboecdae5ff2012-03-22 09:24:05 +0100645 flist_for_each(lentry, &g->label_list) {
646 i = flist_entry(lentry, struct graph_label, list);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100647 first = 1;
Jens Axboe01a947f2012-03-22 21:21:00 +0100648 if (i->hide || i->r < 0) /* invisible data */
Stephen M. Cameroncae08722012-03-07 14:47:38 +0100649 continue;
Jens Axboeb65c7ec2012-03-21 17:17:45 +0100650
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100651 cairo_set_source_rgb(cr, i->r, i->g, i->b);
Jens Axboecdae5ff2012-03-22 09:24:05 +0100652 flist_for_each(entry, &i->value_list) {
653 j = flist_entry(entry, struct graph_value, list);
Stephen M. Camerondef0ac22012-03-12 07:32:57 +0100654 tx = ((getx(j) - gminx) / (gmaxx - gminx)) * (x2 - x1) + x1;
655 ty = y2 - ((gety(j) - gminy) / (gmaxy - gminy)) * (y2 - y1);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100656 if (first) {
657 cairo_move_to(cr, tx, ty);
658 first = 0;
659 } else {
660 cairo_line_to(cr, tx, ty);
661 }
662 }
663 cairo_stroke(cr);
664 }
Stephen M. Cameron9ce9cfb2012-03-07 19:37:25 +0100665
666skip_data:
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100667 cairo_restore(cr);
668}
669
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100670static void setstring(char **str, const char *value)
671{
Jens Axboeb65c7ec2012-03-21 17:17:45 +0100672 free(*str);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100673 *str = strdup(value);
674}
675
676void graph_title(struct graph *bg, const char *title)
677{
678 setstring(&bg->title, title);
679}
680
681void graph_x_title(struct graph *bg, const char *title)
682{
683 setstring(&bg->xtitle, title);
684}
685
686void graph_y_title(struct graph *bg, const char *title)
687{
688 setstring(&bg->ytitle, title);
689}
690
691static struct graph_label *graph_find_label(struct graph *bg,
692 const char *label)
693{
Jens Axboecdae5ff2012-03-22 09:24:05 +0100694 struct flist_head *entry;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100695 struct graph_label *i;
696
Jens Axboecdae5ff2012-03-22 09:24:05 +0100697 flist_for_each(entry, &bg->label_list) {
698 i = flist_entry(entry, struct graph_label, list);
699
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100700 if (strcmp(label, i->label) == 0)
701 return i;
Jens Axboecdae5ff2012-03-22 09:24:05 +0100702 }
703
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100704 return NULL;
705}
706
Jens Axboe8dfd6072012-03-22 22:10:37 +0100707graph_label_t graph_add_label(struct graph *bg, const char *label)
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100708{
709 struct graph_label *i;
710
711 i = graph_find_label(bg, label);
712 if (i)
Jens Axboe8dfd6072012-03-22 22:10:37 +0100713 return i; /* already present. */
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100714 i = calloc(1, sizeof(*i));
Jens Axboecdae5ff2012-03-22 09:24:05 +0100715 INIT_FLIST_HEAD(&i->value_list);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100716 i->parent = bg;
717 setstring(&i->label, label);
Jens Axboecdae5ff2012-03-22 09:24:05 +0100718 flist_add_tail(&i->list, &bg->label_list);
Jens Axboeb65c7ec2012-03-21 17:17:45 +0100719 INIT_PRIO_TREE_ROOT(&i->prio_tree);
Jens Axboe8dfd6072012-03-22 22:10:37 +0100720 return i;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100721}
722
Jens Axboecdae5ff2012-03-22 09:24:05 +0100723static void __graph_value_drop(struct graph_label *l, struct graph_value *v)
724{
Jens Axboed0db8192012-03-22 20:48:02 +0100725 flist_del_init(&v->list);
Jens Axboecdae5ff2012-03-22 09:24:05 +0100726 if (v->tooltip)
727 free(v->tooltip);
728 free(v->value);
729 free(v);
730 l->value_count--;
731}
732
733static void graph_value_drop(struct graph_label *l, struct graph_value *v)
734{
Jens Axboe8dfd6072012-03-22 22:10:37 +0100735 if (v->flags & GV_F_PRIO_SKIP) {
736 __graph_value_drop(l, v);
737 return;
738 }
739
Jens Axboecdae5ff2012-03-22 09:24:05 +0100740 /*
741 * Find head, the guy that's on the prio tree
742 */
743 while (!(v->flags & GV_F_ON_PRIO)) {
744 assert(!flist_empty(&v->alias));
745 v = flist_entry(v->alias.next, struct graph_value, alias);
746 }
747
748 prio_tree_remove(&l->prio_tree, &v->node);
749
750 /*
751 * Free aliases
752 */
Jens Axboed0db8192012-03-22 20:48:02 +0100753 while (!flist_empty(&v->alias)) {
Jens Axboecdae5ff2012-03-22 09:24:05 +0100754 struct graph_value *a;
755
Jens Axboed0db8192012-03-22 20:48:02 +0100756 a = flist_entry(v->alias.next, struct graph_value, alias);
757 flist_del_init(&a->alias);
Jens Axboecdae5ff2012-03-22 09:24:05 +0100758
759 __graph_value_drop(l, a);
760 }
761
762 __graph_value_drop(l, v);
763}
764
Jens Axboeb7a69ad2012-03-21 21:55:21 +0100765static void graph_label_add_value(struct graph_label *i, void *value,
766 const char *tooltip)
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100767{
Jens Axboeb7a69ad2012-03-21 21:55:21 +0100768 struct graph *g = i->parent;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100769 struct graph_value *x;
770
771 x = malloc(sizeof(*x));
Jens Axboe3ca30e62012-03-21 18:54:30 +0100772 memset(x, 0, sizeof(*x));
Jens Axboecdae5ff2012-03-22 09:24:05 +0100773 INIT_FLIST_HEAD(&x->alias);
774 INIT_FLIST_HEAD(&x->list);
775 flist_add_tail(&x->list, &i->value_list);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100776 i->value_count++;
Jens Axboecdae5ff2012-03-22 09:24:05 +0100777 x->value = value;
Jens Axboeb65c7ec2012-03-21 17:17:45 +0100778
Jens Axboef00e43f2012-03-21 19:53:32 +0100779 if (tooltip) {
Jens Axboeb7a69ad2012-03-21 21:55:21 +0100780 double xval = getx(x);
781 double minx = xval - (g->xtick_one_val * TOOLTIP_DELTA);
782 double maxx = xval + (g->xtick_one_val * TOOLTIP_DELTA);
Jens Axboe4c0cd532012-03-21 19:48:32 +0100783 struct prio_tree_node *ret;
Jens Axboeb65c7ec2012-03-21 17:17:45 +0100784
Jens Axboe5fb8d792012-03-21 22:00:43 +0100785 /*
786 * use msec to avoid dropping too much precision when
787 * storing as an integer.
788 */
Jens Axboeb7a69ad2012-03-21 21:55:21 +0100789 minx = minx * 1000.0;
790 maxx = maxx * 1000.0;
791
Jens Axboe3ca30e62012-03-21 18:54:30 +0100792 INIT_PRIO_TREE_NODE(&x->node);
Jens Axboeb7a69ad2012-03-21 21:55:21 +0100793 x->node.start = minx;
794 x->node.last = maxx;
Jens Axboecdae5ff2012-03-22 09:24:05 +0100795 x->tooltip = strdup(tooltip);
Jens Axboeb5526822012-03-21 20:20:35 +0100796 if (x->node.last == x->node.start) {
Jens Axboeb7a69ad2012-03-21 21:55:21 +0100797 x->node.last += fabs(g->xtick_delta);
798 if (x->node.last == x->node.start)
799 x->node.last++;
Jens Axboeb5526822012-03-21 20:20:35 +0100800 }
Jens Axboeb65c7ec2012-03-21 17:17:45 +0100801
Jens Axboe4c0cd532012-03-21 19:48:32 +0100802 /*
803 * If ret != &x->node, we have an alias. Since the values
804 * should be identical, we can drop it
805 */
806 ret = prio_tree_insert(&i->prio_tree, &x->node);
Jens Axboecdae5ff2012-03-22 09:24:05 +0100807 if (ret != &x->node) {
808 struct graph_value *alias;
809
810 alias = container_of(ret, struct graph_value, node);
811 flist_add_tail(&x->alias, &alias->alias);
Jens Axboebd10a062012-03-22 09:33:33 +0100812 } else
Jens Axboecdae5ff2012-03-22 09:24:05 +0100813 x->flags = GV_F_ON_PRIO;
Jens Axboe8dfd6072012-03-22 22:10:37 +0100814 } else
815 x->flags = GV_F_PRIO_SKIP;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100816
Jens Axboeb7a69ad2012-03-21 21:55:21 +0100817 if (g->per_label_limit != -1 &&
818 i->value_count > g->per_label_limit) {
Jens Axboec148dae2012-03-11 21:35:47 +0100819 int to_drop = 1;
820
821 /*
822 * If the limit was dynamically reduced, making us more
823 * than 1 entry ahead after adding this one, drop two
824 * entries. This will make us (eventually) reach the
825 * specified limit.
826 */
Jens Axboeb7a69ad2012-03-21 21:55:21 +0100827 if (i->value_count - g->per_label_limit >= 2)
Jens Axboec148dae2012-03-11 21:35:47 +0100828 to_drop = 2;
829
Jens Axboecdae5ff2012-03-22 09:24:05 +0100830 while (to_drop-- && !flist_empty(&i->value_list)) {
831 x = flist_entry(i->value_list.next, struct graph_value, list);
832 graph_value_drop(i, x);
833
834 /*
835 * If we have aliases, we could drop > 1 above.
836 */
837 if (i->value_count <= g->per_label_limit)
838 break;
Jens Axboec148dae2012-03-11 21:35:47 +0100839 }
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100840 }
841}
842
Jens Axboe8dfd6072012-03-22 22:10:37 +0100843int graph_add_data(struct graph *bg, graph_label_t label, const double value)
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100844{
Jens Axboe8dfd6072012-03-22 22:10:37 +0100845 struct graph_label *i = label;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100846 double *d;
847
848 d = malloc(sizeof(*d));
849 *d = value;
850
Jens Axboeb7a69ad2012-03-21 21:55:21 +0100851 graph_label_add_value(i, d, NULL);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100852 return 0;
853}
854
Jens Axboe01a947f2012-03-22 21:21:00 +0100855static int graph_nonzero_y(struct graph_label *l)
856{
857 struct flist_head *entry;
858
859 flist_for_each(entry, &l->value_list) {
860 struct graph_value *v;
861
862 v = flist_entry(entry, struct graph_value, list);
863 if (gety(v) != 0.0)
864 return 1;
865 }
866
867 return 0;
868}
869
Jens Axboe8dfd6072012-03-22 22:10:37 +0100870int graph_add_xy_data(struct graph *bg, graph_label_t label,
Jens Axboe93e2db22012-03-13 09:45:22 +0100871 const double x, const double y, const char *tooltip)
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100872{
Jens Axboe8dfd6072012-03-22 22:10:37 +0100873 struct graph_label *i = label;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100874 struct xyvalue *xy;
875
Jens Axboe01a947f2012-03-22 21:21:00 +0100876 if (bg->dont_graph_all_zeroes && y == 0.0 && !graph_nonzero_y(i))
877 i->hide = 1;
878 else
879 i->hide = 0;
880
881 xy = malloc(sizeof(*xy));
882 xy->x = x;
883 xy->y = y;
884
Jens Axboeb7a69ad2012-03-21 21:55:21 +0100885 graph_label_add_value(i, xy, tooltip);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100886 return 0;
887}
888
Jens Axboecdae5ff2012-03-22 09:24:05 +0100889static void graph_free_values(struct graph_label *l)
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100890{
Jens Axboecdae5ff2012-03-22 09:24:05 +0100891 struct graph_value *i;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100892
Jens Axboecdae5ff2012-03-22 09:24:05 +0100893 while (!flist_empty(&l->value_list)) {
894 i = flist_entry(l->value_list.next, struct graph_value, list);
895 graph_value_drop(l, i);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100896 }
897}
898
Jens Axboecdae5ff2012-03-22 09:24:05 +0100899static void graph_free_labels(struct graph *g)
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100900{
Jens Axboecdae5ff2012-03-22 09:24:05 +0100901 struct graph_label *i;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100902
Jens Axboecdae5ff2012-03-22 09:24:05 +0100903 while (!flist_empty(&g->label_list)) {
904 i = flist_entry(g->label_list.next, struct graph_label, list);
905 flist_del(&i->list);
906 graph_free_values(i);
Jens Axboeb65c7ec2012-03-21 17:17:45 +0100907 free(i);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100908 }
909}
910
Jens Axboe8dfd6072012-03-22 22:10:37 +0100911void graph_set_color(struct graph *gr, graph_label_t label, double red,
912 double green, double blue)
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100913{
Jens Axboe8dfd6072012-03-22 22:10:37 +0100914 struct graph_label *i = label;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100915 double r, g, b;
916
Stephen M. Cameroncae08722012-03-07 14:47:38 +0100917 if (red < 0.0) { /* invisible color */
918 r = -1.0;
919 g = -1.0;
920 b = -1.0;
921 } else {
922 r = fabs(red);
923 g = fabs(green);
924 b = fabs(blue);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100925
Stephen M. Cameroncae08722012-03-07 14:47:38 +0100926 if (r > 1.0)
927 r = 1.0;
928 if (g > 1.0)
929 g = 1.0;
930 if (b > 1.0)
Jens Axboeb65c7ec2012-03-21 17:17:45 +0100931 b = 1.0;
Stephen M. Cameroncae08722012-03-07 14:47:38 +0100932 }
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100933
Jens Axboe8dfd6072012-03-22 22:10:37 +0100934 i->r = r;
935 i->g = g;
936 i->b = b;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100937}
938
939void graph_free(struct graph *bg)
940{
Jens Axboeb65c7ec2012-03-21 17:17:45 +0100941 free(bg->title);
942 free(bg->xtitle);
943 free(bg->ytitle);
Jens Axboecdae5ff2012-03-22 09:24:05 +0100944 graph_free_labels(bg);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100945}
946
947/* For each line in the line graph, up to per_label_limit segments may
948 * be added. After that, adding more data to the end of the line
949 * causes data to drop off of the front of the line.
950 */
951void line_graph_set_data_count_limit(struct graph *g, int per_label_limit)
952{
953 g->per_label_limit = per_label_limit;
954}
955
Stephen M. Camerondef0ac22012-03-12 07:32:57 +0100956void graph_add_extra_space(struct graph *g, double left_percent, double right_percent,
957 double top_percent, double bottom_percent)
958{
959 g->left_extra = left_percent;
960 g->right_extra = right_percent;
961 g->top_extra = top_percent;
962 g->bottom_extra = bottom_percent;
963}
964
Jens Axboed8fbeef2012-03-14 10:25:44 +0100965/*
966 * Normally values are logged in a base unit of 0, but for other purposes
967 * it makes more sense to log in higher unit. For instance for bandwidth
968 * purposes, you may want to log in KB/sec (or MB/sec) rather than bytes/sec.
969 */
970void graph_set_base_offset(struct graph *g, unsigned int base_offset)
971{
972 g->base_offset = base_offset;
973}
974
Jens Axboe93e2db22012-03-13 09:45:22 +0100975int graph_has_tooltips(struct graph *g)
976{
Jens Axboecdae5ff2012-03-22 09:24:05 +0100977 struct flist_head *entry;
Jens Axboe93e2db22012-03-13 09:45:22 +0100978 struct graph_label *i;
Stephen M. Camerondef0ac22012-03-12 07:32:57 +0100979
Jens Axboecdae5ff2012-03-22 09:24:05 +0100980 flist_for_each(entry, &g->label_list) {
981 i = flist_entry(entry, struct graph_label, list);
982
983 if (!prio_tree_empty(&i->prio_tree))
Jens Axboe93e2db22012-03-13 09:45:22 +0100984 return 1;
Jens Axboecdae5ff2012-03-22 09:24:05 +0100985 }
Jens Axboe93e2db22012-03-13 09:45:22 +0100986
987 return 0;
988}
989
990int graph_contains_xy(struct graph *g, int x, int y)
991{
992 int first_x = g->xoffset;
993 int last_x = g->xoffset + g->xdim;
994 int first_y = g->yoffset;
995 int last_y = g->yoffset + g->ydim;
996
997 return (x >= first_x && x <= last_x) && (y >= first_y && y <= last_y);
998}
999
Jens Axboeb65c7ec2012-03-21 17:17:45 +01001000const char *graph_find_tooltip(struct graph *g, int ix, int iy)
Jens Axboe93e2db22012-03-13 09:45:22 +01001001{
Jens Axboeb65c7ec2012-03-21 17:17:45 +01001002 double x = ix, y = iy;
1003 struct prio_tree_iter iter;
1004 struct prio_tree_node *n;
Jens Axboeb65c7ec2012-03-21 17:17:45 +01001005 struct graph_value *best = NULL;
Jens Axboecdae5ff2012-03-22 09:24:05 +01001006 struct flist_head *entry;
Jens Axboeb65c7ec2012-03-21 17:17:45 +01001007 double best_delta;
Jens Axboeb7a69ad2012-03-21 21:55:21 +01001008 double maxy, miny;
Jens Axboe93e2db22012-03-13 09:45:22 +01001009
Jens Axboeb65c7ec2012-03-21 17:17:45 +01001010 x -= g->xoffset;
1011 y -= g->yoffset;
1012
1013 x = g->xtick_zero_val + ((x - g->xtick_zero) * g->xtick_delta);
1014 y = g->ytick_zero_val + ((y - g->ytick_zero) * g->ytick_delta);
1015
Jens Axboeb7a69ad2012-03-21 21:55:21 +01001016 x = x * 1000.0;
1017 maxy = y + (g->ytick_one_val * TOOLTIP_DELTA);
1018 miny = y - (g->ytick_one_val * TOOLTIP_DELTA);
Jens Axboeb65c7ec2012-03-21 17:17:45 +01001019 best_delta = UINT_MAX;
Jens Axboecdae5ff2012-03-22 09:24:05 +01001020 flist_for_each(entry, &g->label_list) {
1021 struct graph_label *i;
1022
1023 i = flist_entry(entry, struct graph_label, list);
Jens Axboe01a947f2012-03-22 21:21:00 +01001024 if (i->hide)
1025 continue;
Jens Axboecdae5ff2012-03-22 09:24:05 +01001026
Jens Axboe08d7d5a2012-03-21 19:33:32 +01001027 INIT_PRIO_TREE_ITER(&iter);
Jens Axboeb7a69ad2012-03-21 21:55:21 +01001028 prio_tree_iter_init(&iter, &i->prio_tree, x, x);
Jens Axboeb65c7ec2012-03-21 17:17:45 +01001029
1030 n = prio_tree_next(&iter);
1031 if (!n)
1032 continue;
1033
1034 do {
Jens Axboecdae5ff2012-03-22 09:24:05 +01001035 struct graph_value *v, *rootv;
Jens Axboeb7a69ad2012-03-21 21:55:21 +01001036 double yval, ydiff;
Jens Axboeb65c7ec2012-03-21 17:17:45 +01001037
1038 v = container_of(n, struct graph_value, node);
Jens Axboecdae5ff2012-03-22 09:24:05 +01001039 rootv = v;
1040 do {
1041 yval = gety(v);
1042 ydiff = fabs(yval - y);
Jens Axboe93e2db22012-03-13 09:45:22 +01001043
Jens Axboecdae5ff2012-03-22 09:24:05 +01001044 /*
1045 * zero delta, or within or match critera, break
1046 */
1047 if (ydiff < best_delta) {
1048 best_delta = ydiff;
1049 if (!best_delta ||
1050 (yval >= miny && yval <= maxy)) {
1051 best = v;
1052 break;
1053 }
Jens Axboeb65c7ec2012-03-21 17:17:45 +01001054 }
Jens Axboecdae5ff2012-03-22 09:24:05 +01001055 if (!flist_empty(&v->alias))
1056 v = flist_entry(v->alias.next, struct graph_value, alias);
1057 } while (v != rootv);
Jens Axboeb65c7ec2012-03-21 17:17:45 +01001058 } while ((n = prio_tree_next(&iter)) != NULL);
1059
1060 /*
1061 * If we got matches in one label, don't check others.
1062 */
Jens Axboed0db8192012-03-22 20:48:02 +01001063 if (best)
1064 break;
Jens Axboecdae5ff2012-03-22 09:24:05 +01001065 }
Jens Axboeb65c7ec2012-03-21 17:17:45 +01001066
1067 if (best)
1068 return best->tooltip;
Jens Axboe93e2db22012-03-13 09:45:22 +01001069
1070 return NULL;
1071}
Jens Axboe01a947f2012-03-22 21:21:00 +01001072
1073void graph_set_graph_all_zeroes(struct graph *g, unsigned int set)
1074{
1075 g->dont_graph_all_zeroes = !set;
1076}