blob: 146ed7b148d9c7594c0002be5cfff0884896c888 [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,
48};
49
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +010050struct graph_value {
Jens Axboecdae5ff2012-03-22 09:24:05 +010051 struct flist_head list;
Jens Axboeb65c7ec2012-03-21 17:17:45 +010052 struct prio_tree_node node;
Jens Axboecdae5ff2012-03-22 09:24:05 +010053 struct flist_head alias;
54 unsigned int flags;
Jens Axboe93e2db22012-03-13 09:45:22 +010055 char *tooltip;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +010056 void *value;
57};
58
59struct graph_label {
Jens Axboecdae5ff2012-03-22 09:24:05 +010060 struct flist_head list;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +010061 char *label;
Jens Axboecdae5ff2012-03-22 09:24:05 +010062 struct flist_head value_list;
Jens Axboeb65c7ec2012-03-21 17:17:45 +010063 struct prio_tree_root prio_tree;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +010064 double r, g, b;
65 int value_count;
66 struct graph *parent;
67};
68
Jens Axboeb65c7ec2012-03-21 17:17:45 +010069struct tick_value {
70 unsigned int offset;
71 double value;
72};
73
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +010074struct graph {
75 char *title;
76 char *xtitle;
77 char *ytitle;
Jens Axboe87d5f272012-03-07 12:31:40 +010078 unsigned int xdim, ydim;
Stephen M. Cameron57f9d282012-03-11 11:36:51 +010079 double xoffset, yoffset;
Jens Axboecdae5ff2012-03-22 09:24:05 +010080 struct flist_head label_list;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +010081 int per_label_limit;
Jens Axboef3e84402012-03-07 13:14:32 +010082 const char *font;
Stephen M. Cameron7175d912012-03-11 11:35:10 +010083 graph_axis_unit_change_callback x_axis_unit_change_callback;
84 graph_axis_unit_change_callback y_axis_unit_change_callback;
Jens Axboed8fbeef2012-03-14 10:25:44 +010085 unsigned int base_offset;
Stephen M. Camerondef0ac22012-03-12 07:32:57 +010086 double left_extra;
87 double right_extra;
88 double top_extra;
89 double bottom_extra;
Jens Axboeb65c7ec2012-03-21 17:17:45 +010090
91 double xtick_zero;
92 double xtick_delta;
93 double xtick_zero_val;
Jens Axboeb7a69ad2012-03-21 21:55:21 +010094 double xtick_one_val;
Jens Axboeb65c7ec2012-03-21 17:17:45 +010095 double ytick_zero;
96 double ytick_delta;
97 double ytick_zero_val;
Jens Axboeb7a69ad2012-03-21 21:55:21 +010098 double ytick_one_val;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +010099};
100
Stephen M. Cameron3ea48b82012-03-07 19:40:58 +0100101void graph_set_size(struct graph *g, unsigned int xdim, unsigned int ydim)
102{
103 g->xdim = xdim;
104 g->ydim = ydim;
105}
106
Stephen M. Cameron57f9d282012-03-11 11:36:51 +0100107void graph_set_position(struct graph *g, double xoffset, double yoffset)
108{
109 g->xoffset = xoffset;
110 g->yoffset = yoffset;
111}
112
Jens Axboef3e84402012-03-07 13:14:32 +0100113struct graph *graph_new(unsigned int xdim, unsigned int ydim, const char *font)
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100114{
115 struct graph *g;
116
117 g = calloc(1, sizeof(*g));
Jens Axboecdae5ff2012-03-22 09:24:05 +0100118 INIT_FLIST_HEAD(&g->label_list);
Stephen M. Cameron3ea48b82012-03-07 19:40:58 +0100119 graph_set_size(g, xdim, ydim);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100120 g->per_label_limit = -1;
Jens Axboef3e84402012-03-07 13:14:32 +0100121 g->font = font;
122 if (!g->font)
123 g->font = "Sans";
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100124 return g;
125}
126
Stephen M. Cameron7175d912012-03-11 11:35:10 +0100127void graph_x_axis_unit_change_notify(struct graph *g, graph_axis_unit_change_callback f)
128{
129 g->x_axis_unit_change_callback = f;
130}
131
132void graph_y_axis_unit_change_notify(struct graph *g, graph_axis_unit_change_callback f)
133{
134 g->y_axis_unit_change_callback = f;
135}
136
Jens Axboecdae5ff2012-03-22 09:24:05 +0100137static int count_labels(struct graph *g)
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100138{
Jens Axboecdae5ff2012-03-22 09:24:05 +0100139 struct flist_head *entry;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100140 int count = 0;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100141
Jens Axboecdae5ff2012-03-22 09:24:05 +0100142 flist_for_each(entry, &g->label_list)
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100143 count++;
Jens Axboecdae5ff2012-03-22 09:24:05 +0100144
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100145 return count;
146}
147
Jens Axboecdae5ff2012-03-22 09:24:05 +0100148static int count_values(struct graph_label *l)
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100149{
Jens Axboecdae5ff2012-03-22 09:24:05 +0100150 struct flist_head *entry;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100151 int count = 0;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100152
Jens Axboecdae5ff2012-03-22 09:24:05 +0100153 flist_for_each(entry, &l->value_list)
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100154 count++;
Jens Axboecdae5ff2012-03-22 09:24:05 +0100155
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100156 return count;
157}
158
159typedef double (*double_comparator)(double a, double b);
160
161static double mindouble(double a, double b)
162{
163 return a < b ? a : b;
164}
165
166static double maxdouble(double a, double b)
167{
168 return a < b ? b : a;
169}
170
Jens Axboecdae5ff2012-03-22 09:24:05 +0100171static double find_double_values(struct graph_label *l, double_comparator cmp)
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100172{
Jens Axboecdae5ff2012-03-22 09:24:05 +0100173 struct flist_head *entry;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100174 double answer, tmp;
Jens Axboecdae5ff2012-03-22 09:24:05 +0100175 int first = 1;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100176
Jens Axboecdae5ff2012-03-22 09:24:05 +0100177 assert(!flist_empty(&l->value_list));
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100178 answer = 0.0; /* shut the compiler up, might need to think harder though. */
Jens Axboecdae5ff2012-03-22 09:24:05 +0100179 flist_for_each(entry, &l->value_list) {
180 struct graph_value *i;
181
182 i = flist_entry(entry, struct graph_value, list);
183 tmp = *(double *) i->value;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100184 if (first) {
185 answer = tmp;
186 first = 0;
187 } else {
188 answer = cmp(answer, tmp);
189 }
190 }
191 return answer;
192}
193
Jens Axboecdae5ff2012-03-22 09:24:05 +0100194static double find_double_data(struct graph *g, double_comparator cmp)
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100195{
Jens Axboecdae5ff2012-03-22 09:24:05 +0100196 struct flist_head *entry;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100197 struct graph_label *i;
198 int first = 1;
199 double answer, tmp;
200
Jens Axboecdae5ff2012-03-22 09:24:05 +0100201 assert(!flist_empty(&g->label_list));
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100202 answer = 0.0; /* shut the compiler up, might need to think harder though. */
Jens Axboecdae5ff2012-03-22 09:24:05 +0100203 flist_for_each(entry, &g->label_list) {
204 i = flist_entry(entry, struct graph_label, list);
205 tmp = find_double_values(i, cmp);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100206 if (first) {
207 answer = tmp;
208 first = 0;
209 } else {
210 answer = cmp(tmp, answer);
211 }
212 }
213 return answer;
214}
215
Jens Axboecdae5ff2012-03-22 09:24:05 +0100216static double find_min_data(struct graph *g)
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100217{
Jens Axboecdae5ff2012-03-22 09:24:05 +0100218 return find_double_data(g, mindouble);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100219}
220
Jens Axboecdae5ff2012-03-22 09:24:05 +0100221static double find_max_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, maxdouble);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100224}
225
226static void draw_bars(struct graph *bg, cairo_t *cr, struct graph_label *lb,
227 double label_offset, double bar_width,
228 double mindata, double maxdata)
229{
Jens Axboecdae5ff2012-03-22 09:24:05 +0100230 struct flist_head *entry;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100231 double x1, y1, x2, y2;
232 int bar_num = 0;
233 double domain, range, v;
234
235 domain = (maxdata - mindata);
236 range = (double) bg->ydim * 0.80; /* FIXME */
237 cairo_stroke(cr);
Jens Axboecdae5ff2012-03-22 09:24:05 +0100238 flist_for_each(entry, &lb->value_list) {
239 struct graph_value *i;
240
241 i = flist_entry(entry, struct graph_value, list);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100242
243 x1 = label_offset + (double) bar_num * bar_width + (bar_width * 0.05);
244 x2 = x1 + bar_width * 0.90;
245 y2 = bg->ydim * 0.90;
246 v = *(double *) i->value;
247 y1 = y2 - (((v - mindata) / domain) * range);
248 cairo_move_to(cr, x1, y1);
249 cairo_line_to(cr, x1, y2);
250 cairo_line_to(cr, x2, y2);
251 cairo_line_to(cr, x2, y1);
252 cairo_close_path(cr);
253 cairo_fill(cr);
254 cairo_stroke(cr);
255 bar_num++;
256 }
257}
258
Stephen M. Cameron10e54cd2012-03-07 19:39:55 +0100259static void draw_aligned_text(struct graph *g, cairo_t *cr, double x, double y,
260 double fontsize, const char *text, int alignment)
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100261{
Stephen M. Cameron10e54cd2012-03-07 19:39:55 +0100262#define CENTERED 0
263#define LEFT_JUSTIFIED 1
264#define RIGHT_JUSTIFIED 2
265
266 double factor, direction;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100267 cairo_text_extents_t extents;
268
Stephen M. Cameron10e54cd2012-03-07 19:39:55 +0100269 switch(alignment) {
270 case CENTERED:
271 direction = -1.0;
272 factor = 0.5;
273 break;
274 case RIGHT_JUSTIFIED:
275 direction = -1.0;
276 factor = 1.0;
277 break;
278 case LEFT_JUSTIFIED:
279 default:
280 direction = 1.0;
281 factor = 1.0;
282 break;
283 }
Jens Axboef3e84402012-03-07 13:14:32 +0100284 cairo_select_font_face (cr, g->font, CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100285
286 cairo_set_font_size(cr, fontsize);
287 cairo_text_extents(cr, text, &extents);
Stephen M. Cameron10e54cd2012-03-07 19:39:55 +0100288 x = x + direction * (factor * extents.width + extents.x_bearing);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100289 y = y - (extents.height / 2 + extents.y_bearing);
290
291 cairo_move_to(cr, x, y);
292 cairo_show_text(cr, text);
293}
294
Stephen M. Cameron10e54cd2012-03-07 19:39:55 +0100295static inline void draw_centered_text(struct graph *g, cairo_t *cr, double x, double y,
296 double fontsize, const char *text)
297{
298 draw_aligned_text(g, cr, x, y, fontsize, text, CENTERED);
299}
300
301static inline void draw_right_justified_text(struct graph *g, cairo_t *cr,
302 double x, double y,
303 double fontsize, const char *text)
304{
305 draw_aligned_text(g, cr, x, y, fontsize, text, RIGHT_JUSTIFIED);
306}
307
308static inline void draw_left_justified_text(struct graph *g, cairo_t *cr,
309 double x, double y,
310 double fontsize, const char *text)
311{
312 draw_aligned_text(g, cr, x, y, fontsize, text, LEFT_JUSTIFIED);
313}
314
Jens Axboef3e84402012-03-07 13:14:32 +0100315static void draw_vertical_centered_text(struct graph *g, cairo_t *cr, double x,
316 double y, double fontsize,
317 const char *text)
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100318{
319 double sx, sy;
320 cairo_text_extents_t extents;
321
Jens Axboef3e84402012-03-07 13:14:32 +0100322 cairo_select_font_face(cr, g->font, CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100323
324 cairo_set_font_size(cr, fontsize);
325 cairo_text_extents(cr, text, &extents);
326 sx = x;
327 sy = y;
328 y = y + (extents.width / 2.0 + extents.x_bearing);
329 x = x - (extents.height / 2.0 + extents.y_bearing);
330
331 cairo_move_to(cr, x, y);
332 cairo_save(cr);
333 cairo_translate(cr, -sx, -sy);
334 cairo_rotate(cr, -90.0 * M_PI / 180.0);
335 cairo_translate(cr, sx, sy);
336 cairo_show_text(cr, text);
337 cairo_restore(cr);
338}
339
340static void graph_draw_common(struct graph *g, cairo_t *cr,
341 double *x1, double *y1, double *x2, double *y2)
342{
343 cairo_set_source_rgb(cr, 0, 0, 0);
Jens Axboef3e84402012-03-07 13:14:32 +0100344 cairo_set_line_width (cr, 0.8);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100345
Jens Axboe9ede9c92012-03-14 10:43:20 +0100346 *x1 = 0.10 * g->xdim;
Stephen M. Cameron6bf86002012-03-08 16:58:50 +0100347 *x2 = 0.95 * g->xdim;
348 *y1 = 0.10 * g->ydim;
349 *y2 = 0.90 * g->ydim;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100350
351 cairo_move_to(cr, *x1, *y1);
352 cairo_line_to(cr, *x1, *y2);
353 cairo_line_to(cr, *x2, *y2);
354 cairo_line_to(cr, *x2, *y1);
355 cairo_line_to(cr, *x1, *y1);
356 cairo_stroke(cr);
357
Jens Axboef3e84402012-03-07 13:14:32 +0100358 draw_centered_text(g, cr, g->xdim / 2, g->ydim / 20, 20.0, g->title);
359 draw_centered_text(g, cr, g->xdim / 2, g->ydim * 0.97, 14.0, g->xtitle);
360 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 +0100361 cairo_stroke(cr);
362}
363
364static void graph_draw_x_ticks(struct graph *g, cairo_t *cr,
365 double x1, double y1, double x2, double y2,
Jens Axboed8fbeef2012-03-14 10:25:44 +0100366 double minx, double maxx, int nticks, int add_tm_text)
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100367{
368 struct tickmark *tm;
369 double tx;
Stephen M. Cameron7175d912012-03-11 11:35:10 +0100370 int i, power_of_ten;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100371 static double dash[] = { 1.0, 2.0 };
372
Stephen M. Cameron7175d912012-03-11 11:35:10 +0100373 nticks = calc_tickmarks(minx, maxx, nticks, &tm, &power_of_ten,
Jens Axboed8fbeef2012-03-14 10:25:44 +0100374 g->x_axis_unit_change_callback == NULL, g->base_offset);
Stephen M. Cameron7175d912012-03-11 11:35:10 +0100375 if (g->x_axis_unit_change_callback)
376 g->x_axis_unit_change_callback(g, power_of_ten);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100377
378 for (i = 0; i < nticks; i++) {
379 tx = (((tm[i].value) - minx) / (maxx - minx)) * (x2 - x1) + x1;
Jens Axboed8fbeef2012-03-14 10:25:44 +0100380
Jens Axboeb65c7ec2012-03-21 17:17:45 +0100381 /*
382 * Update tick delta
383 */
384 if (!i) {
385 g->xtick_zero = tx;
386 g->xtick_zero_val = tm[0].value;
Jens Axboeb7a69ad2012-03-21 21:55:21 +0100387 } else if (i == 1) {
Jens Axboeb65c7ec2012-03-21 17:17:45 +0100388 g->xtick_delta = (tm[1].value - tm[0].value) / (tx - g->xtick_zero);
Jens Axboeb7a69ad2012-03-21 21:55:21 +0100389 g->xtick_one_val = tm[1].value;
390 }
Jens Axboeb65c7ec2012-03-21 17:17:45 +0100391
Jens Axboed8fbeef2012-03-14 10:25:44 +0100392 /* really tx < yx || tx > x2, but protect against rounding */
393 if (x1 - tx > 0.01 || tx - x2 > 0.01)
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100394 continue;
395
396 /* Draw tick mark */
Jens Axboef3e84402012-03-07 13:14:32 +0100397 cairo_set_line_width(cr, 0.8);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100398 cairo_move_to(cr, tx, y2);
399 cairo_line_to(cr, tx, y2 + (y2 - y1) * 0.03);
400 cairo_stroke(cr);
401
402 /* draw grid lines */
403 cairo_save(cr);
404 cairo_set_dash(cr, dash, 2, 2.0);
405 cairo_set_line_width(cr, 0.5);
406 cairo_move_to(cr, tx, y1);
407 cairo_line_to(cr, tx, y2);
408 cairo_stroke(cr);
409 cairo_restore(cr);
410
Jens Axboed8fbeef2012-03-14 10:25:44 +0100411 if (!add_tm_text)
412 continue;
413
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100414 /* draw tickmark label */
Jens Axboef3e84402012-03-07 13:14:32 +0100415 draw_centered_text(g, cr, tx, y2 * 1.04, 12.0, tm[i].string);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100416 cairo_stroke(cr);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100417 }
418}
419
Jens Axboed8fbeef2012-03-14 10:25:44 +0100420static double graph_draw_y_ticks(struct graph *g, cairo_t *cr,
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100421 double x1, double y1, double x2, double y2,
Jens Axboed8fbeef2012-03-14 10:25:44 +0100422 double miny, double maxy, int nticks, int add_tm_text)
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100423{
424 struct tickmark *tm;
425 double ty;
Stephen M. Cameron7175d912012-03-11 11:35:10 +0100426 int i, power_of_ten;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100427 static double dash[] = { 2.0, 2.0 };
428
Stephen M. Cameron7175d912012-03-11 11:35:10 +0100429 nticks = calc_tickmarks(miny, maxy, nticks, &tm, &power_of_ten,
Jens Axboed8fbeef2012-03-14 10:25:44 +0100430 g->y_axis_unit_change_callback == NULL, g->base_offset);
Stephen M. Cameron7175d912012-03-11 11:35:10 +0100431 if (g->y_axis_unit_change_callback)
432 g->y_axis_unit_change_callback(g, power_of_ten);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100433
Jens Axboed8fbeef2012-03-14 10:25:44 +0100434 /*
435 * Use highest tickmark as top of graph, not highest value. Otherwise
436 * it's impossible to see what the max value is, if the graph is
437 * fairly flat.
438 */
439 maxy = tm[nticks - 1].value;
440
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100441 for (i = 0; i < nticks; i++) {
442 ty = y2 - (((tm[i].value) - miny) / (maxy - miny)) * (y2 - y1);
Jens Axboed8fbeef2012-03-14 10:25:44 +0100443
Jens Axboeb65c7ec2012-03-21 17:17:45 +0100444 /*
445 * Update tick delta
446 */
447 if (!i) {
448 g->ytick_zero = ty;
449 g->ytick_zero_val = tm[0].value;
Jens Axboeb7a69ad2012-03-21 21:55:21 +0100450 } else if (i == 1) {
Jens Axboeb65c7ec2012-03-21 17:17:45 +0100451 g->ytick_delta = (tm[1].value - tm[0].value) / (ty - g->ytick_zero);
Jens Axboeb7a69ad2012-03-21 21:55:21 +0100452 g->ytick_one_val = tm[1].value;
453 }
Jens Axboeb65c7ec2012-03-21 17:17:45 +0100454
Jens Axboed8fbeef2012-03-14 10:25:44 +0100455 /* really ty < y1 || ty > y2, but protect against rounding */
456 if (y1 - ty > 0.01 || ty - y2 > 0.01)
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100457 continue;
Jens Axboed8fbeef2012-03-14 10:25:44 +0100458
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100459 /* draw tick mark */
460 cairo_move_to(cr, x1, ty);
461 cairo_line_to(cr, x1 - (x2 - x1) * 0.02, ty);
462 cairo_stroke(cr);
463
464 /* draw grid lines */
465 cairo_save(cr);
466 cairo_set_dash(cr, dash, 2, 2.0);
467 cairo_set_line_width(cr, 0.5);
468 cairo_move_to(cr, x1, ty);
469 cairo_line_to(cr, x2, ty);
470 cairo_stroke(cr);
471 cairo_restore(cr);
472
Jens Axboed8fbeef2012-03-14 10:25:44 +0100473 if (!add_tm_text)
474 continue;
475
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100476 /* draw tickmark label */
Stephen M. Cameron10e54cd2012-03-07 19:39:55 +0100477 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 +0100478 cairo_stroke(cr);
479 }
Jens Axboed8fbeef2012-03-14 10:25:44 +0100480
481 /*
482 * Return new max to use
483 */
484 return maxy;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100485}
486
487void bar_graph_draw(struct graph *bg, cairo_t *cr)
488{
489 double x1, y1, x2, y2;
490 double space_per_label, bar_width;
491 double label_offset, mindata, maxdata;
492 int i, nlabels;
493 struct graph_label *lb;
Jens Axboecdae5ff2012-03-22 09:24:05 +0100494 struct flist_head *entry;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100495
496 cairo_save(cr);
Stephen M. Cameron57f9d282012-03-11 11:36:51 +0100497 cairo_translate(cr, bg->xoffset, bg->yoffset);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100498 graph_draw_common(bg, cr, &x1, &y1, &x2, &y2);
499
Jens Axboecdae5ff2012-03-22 09:24:05 +0100500 nlabels = count_labels(bg);
Jens Axboed8fbeef2012-03-14 10:25:44 +0100501 space_per_label = (x2 - x1) / (double) nlabels;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100502
Jens Axboebb393792012-03-15 10:52:38 +0100503 /*
504 * Start bars at 0 unless we have negative values, otherwise we
505 * present a skewed picture comparing label X and X+1.
506 */
Jens Axboecdae5ff2012-03-22 09:24:05 +0100507 mindata = find_min_data(bg);
Jens Axboebb393792012-03-15 10:52:38 +0100508 if (mindata > 0)
509 mindata = 0;
510
Jens Axboecdae5ff2012-03-22 09:24:05 +0100511 maxdata = find_max_data(bg);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100512
513 if (fabs(maxdata - mindata) < 1e-20) {
Jens Axboef3e84402012-03-07 13:14:32 +0100514 draw_centered_text(bg, cr,
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100515 x1 + (x2 - x1) / 2.0,
516 y1 + (y2 - y1) / 2.0, 20.0, "No good data");
517 return;
518 }
519
Jens Axboebb393792012-03-15 10:52:38 +0100520 maxdata = graph_draw_y_ticks(bg, cr, x1, y1, x2, y2, mindata, maxdata, 10, 1);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100521 i = 0;
Jens Axboecdae5ff2012-03-22 09:24:05 +0100522 flist_for_each(entry, &bg->label_list) {
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100523 int nvalues;
Jens Axboecdae5ff2012-03-22 09:24:05 +0100524
525 lb = flist_entry(entry, struct graph_label, list);
526 nvalues = count_values(lb);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100527 bar_width = (space_per_label - space_per_label * 0.2) / (double) nvalues;
528 label_offset = bg->xdim * 0.1 + space_per_label * (double) i + space_per_label * 0.1;
529 draw_bars(bg, cr, lb, label_offset, bar_width, mindata, maxdata);
530 // 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 +0100531 draw_centered_text(bg, cr, x1 + space_per_label * (i + 0.5), bg->ydim * 0.93,
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100532 12.0, lb->label);
533 i++;
534 }
535 cairo_stroke(cr);
536 cairo_restore(cr);
537}
538
539typedef double (*xy_value_extractor)(struct graph_value *v);
540
541static double getx(struct graph_value *v)
542{
543 struct xyvalue *xy = v->value;
544 return xy->x;
545}
546
547static double gety(struct graph_value *v)
548{
549 struct xyvalue *xy = v->value;
550 return xy->y;
551}
552
553static double find_xy_value(struct graph *g, xy_value_extractor getvalue, double_comparator cmp)
554{
Stephen M. Cameron10e54cd2012-03-07 19:39:55 +0100555 double tmp, answer = 0.0;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100556 struct graph_label *i;
557 struct graph_value *j;
Jens Axboecdae5ff2012-03-22 09:24:05 +0100558 struct flist_head *jentry, *entry;
Stephen M. Camerond582bf72012-03-07 19:37:57 +0100559 int first = 1;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100560
Jens Axboecdae5ff2012-03-22 09:24:05 +0100561 flist_for_each(entry, &g->label_list) {
562 i = flist_entry(entry, struct graph_label, list);
563
564 flist_for_each(jentry, &i->value_list) {
565 j = flist_entry(jentry, struct graph_value, list);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100566 tmp = getvalue(j);
Stephen M. Camerond582bf72012-03-07 19:37:57 +0100567 if (first) {
568 first = 0;
569 answer = tmp;
570 }
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100571 answer = cmp(tmp, answer);
572 }
Jens Axboecdae5ff2012-03-22 09:24:05 +0100573 }
574
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100575 return answer;
576}
577
578void line_graph_draw(struct graph *g, cairo_t *cr)
579{
580 double x1, y1, x2, y2;
Stephen M. Camerondef0ac22012-03-12 07:32:57 +0100581 double minx, miny, maxx, maxy, gminx, gminy, gmaxx, gmaxy;
582 double tx, ty, top_extra, bottom_extra, left_extra, right_extra;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100583 struct graph_label *i;
584 struct graph_value *j;
Stephen M. Cameron9ce9cfb2012-03-07 19:37:25 +0100585 int good_data = 1, first = 1;
Jens Axboecdae5ff2012-03-22 09:24:05 +0100586 struct flist_head *entry, *lentry;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100587
588 cairo_save(cr);
Stephen M. Cameron57f9d282012-03-11 11:36:51 +0100589 cairo_translate(cr, g->xoffset, g->yoffset);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100590 graph_draw_common(g, cr, &x1, &y1, &x2, &y2);
591
592 minx = find_xy_value(g, getx, mindouble);
593 maxx = find_xy_value(g, getx, maxdouble);
594 miny = find_xy_value(g, gety, mindouble);
Jens Axboe5aec6682012-03-16 10:17:08 +0100595
596 /*
597 * Start graphs at zero, unless we have a value below. Otherwise
598 * it's hard to visually compare the read and write graph, since
599 * the lowest valued one will be the floor of the graph view.
600 */
601 if (miny > 0)
602 miny = 0;
603
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100604 maxy = find_xy_value(g, gety, maxdouble);
605
606 if (fabs(maxx - minx) < 1e-20 || fabs(maxy - miny) < 1e-20) {
Stephen M. Cameron9ce9cfb2012-03-07 19:37:25 +0100607 good_data = 0;
608 minx = 0.0;
609 miny = 0.0;
610 maxx = 10.0;
611 maxy = 100.0;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100612 }
613
Stephen M. Camerondef0ac22012-03-12 07:32:57 +0100614 top_extra = 0.0;
615 bottom_extra = 0.0;
616 left_extra = 0.0;
617 right_extra = 0.0;
618
619 if (g->top_extra > 0.001)
620 top_extra = fabs(maxy - miny) * g->top_extra;
621 if (g->bottom_extra > 0.001)
622 bottom_extra = fabs(maxy - miny) * g->bottom_extra;
623 if (g->left_extra > 0.001)
624 left_extra = fabs(maxx - minx) * g->left_extra;
625 if (g->right_extra > 0.001)
626 right_extra = fabs(maxx - minx) * g->right_extra;
627
628 gminx = minx - left_extra;
629 gmaxx = maxx + right_extra;
630 gminy = miny - bottom_extra;
631 gmaxy = maxy + top_extra;
632
Jens Axboed8fbeef2012-03-14 10:25:44 +0100633 graph_draw_x_ticks(g, cr, x1, y1, x2, y2, gminx, gmaxx, 10, good_data);
634 gmaxy = graph_draw_y_ticks(g, cr, x1, y1, x2, y2, gminy, gmaxy, 10, good_data);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100635
Stephen M. Cameron9ce9cfb2012-03-07 19:37:25 +0100636 if (!good_data)
637 goto skip_data;
638
Jens Axboef3e84402012-03-07 13:14:32 +0100639 cairo_set_line_width(cr, 1.5);
Jens Axboecdae5ff2012-03-22 09:24:05 +0100640 flist_for_each(lentry, &g->label_list) {
641 i = flist_entry(lentry, struct graph_label, list);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100642 first = 1;
Stephen M. Cameroncae08722012-03-07 14:47:38 +0100643 if (i->r < 0) /* invisible data */
644 continue;
Jens Axboeb65c7ec2012-03-21 17:17:45 +0100645
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100646 cairo_set_source_rgb(cr, i->r, i->g, i->b);
Jens Axboecdae5ff2012-03-22 09:24:05 +0100647 flist_for_each(entry, &i->value_list) {
648 j = flist_entry(entry, struct graph_value, list);
Stephen M. Camerondef0ac22012-03-12 07:32:57 +0100649 tx = ((getx(j) - gminx) / (gmaxx - gminx)) * (x2 - x1) + x1;
650 ty = y2 - ((gety(j) - gminy) / (gmaxy - gminy)) * (y2 - y1);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100651 if (first) {
652 cairo_move_to(cr, tx, ty);
653 first = 0;
654 } else {
655 cairo_line_to(cr, tx, ty);
656 }
657 }
658 cairo_stroke(cr);
659 }
Stephen M. Cameron9ce9cfb2012-03-07 19:37:25 +0100660
661skip_data:
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100662 cairo_restore(cr);
663}
664
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100665static void setstring(char **str, const char *value)
666{
Jens Axboeb65c7ec2012-03-21 17:17:45 +0100667 free(*str);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100668 *str = strdup(value);
669}
670
671void graph_title(struct graph *bg, const char *title)
672{
673 setstring(&bg->title, title);
674}
675
676void graph_x_title(struct graph *bg, const char *title)
677{
678 setstring(&bg->xtitle, title);
679}
680
681void graph_y_title(struct graph *bg, const char *title)
682{
683 setstring(&bg->ytitle, title);
684}
685
686static struct graph_label *graph_find_label(struct graph *bg,
687 const char *label)
688{
Jens Axboecdae5ff2012-03-22 09:24:05 +0100689 struct flist_head *entry;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100690 struct graph_label *i;
691
Jens Axboecdae5ff2012-03-22 09:24:05 +0100692 flist_for_each(entry, &bg->label_list) {
693 i = flist_entry(entry, struct graph_label, list);
694
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100695 if (strcmp(label, i->label) == 0)
696 return i;
Jens Axboecdae5ff2012-03-22 09:24:05 +0100697 }
698
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100699 return NULL;
700}
701
702void graph_add_label(struct graph *bg, const char *label)
703{
704 struct graph_label *i;
705
706 i = graph_find_label(bg, label);
707 if (i)
708 return; /* already present. */
709 i = calloc(1, sizeof(*i));
Jens Axboecdae5ff2012-03-22 09:24:05 +0100710 INIT_FLIST_HEAD(&i->value_list);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100711 i->parent = bg;
712 setstring(&i->label, label);
Jens Axboecdae5ff2012-03-22 09:24:05 +0100713 flist_add_tail(&i->list, &bg->label_list);
Jens Axboeb65c7ec2012-03-21 17:17:45 +0100714 INIT_PRIO_TREE_ROOT(&i->prio_tree);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100715}
716
Jens Axboecdae5ff2012-03-22 09:24:05 +0100717static void __graph_value_drop(struct graph_label *l, struct graph_value *v)
718{
719 flist_del(&v->list);
720 if (v->tooltip)
721 free(v->tooltip);
722 free(v->value);
723 free(v);
724 l->value_count--;
725}
726
727static void graph_value_drop(struct graph_label *l, struct graph_value *v)
728{
729 struct flist_head *entry, *tmp;
730
731 /*
732 * Find head, the guy that's on the prio tree
733 */
734 while (!(v->flags & GV_F_ON_PRIO)) {
735 assert(!flist_empty(&v->alias));
736 v = flist_entry(v->alias.next, struct graph_value, alias);
737 }
738
739 prio_tree_remove(&l->prio_tree, &v->node);
740
741 /*
742 * Free aliases
743 */
744 flist_for_each_safe(entry, tmp, &v->alias) {
745 struct graph_value *a;
746
747 a = flist_entry(entry, struct graph_value, alias);
748 flist_del(&a->alias);
749
750 __graph_value_drop(l, a);
751 }
752
753 __graph_value_drop(l, v);
754}
755
Jens Axboeb7a69ad2012-03-21 21:55:21 +0100756static void graph_label_add_value(struct graph_label *i, void *value,
757 const char *tooltip)
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100758{
Jens Axboeb7a69ad2012-03-21 21:55:21 +0100759 struct graph *g = i->parent;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100760 struct graph_value *x;
761
762 x = malloc(sizeof(*x));
Jens Axboe3ca30e62012-03-21 18:54:30 +0100763 memset(x, 0, sizeof(*x));
Jens Axboecdae5ff2012-03-22 09:24:05 +0100764 INIT_FLIST_HEAD(&x->alias);
765 INIT_FLIST_HEAD(&x->list);
766 flist_add_tail(&x->list, &i->value_list);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100767 i->value_count++;
Jens Axboecdae5ff2012-03-22 09:24:05 +0100768 x->value = value;
Jens Axboeb65c7ec2012-03-21 17:17:45 +0100769
Jens Axboef00e43f2012-03-21 19:53:32 +0100770 if (tooltip) {
Jens Axboeb7a69ad2012-03-21 21:55:21 +0100771 double xval = getx(x);
772 double minx = xval - (g->xtick_one_val * TOOLTIP_DELTA);
773 double maxx = xval + (g->xtick_one_val * TOOLTIP_DELTA);
Jens Axboe4c0cd532012-03-21 19:48:32 +0100774 struct prio_tree_node *ret;
Jens Axboeb65c7ec2012-03-21 17:17:45 +0100775
Jens Axboe5fb8d792012-03-21 22:00:43 +0100776 /*
777 * use msec to avoid dropping too much precision when
778 * storing as an integer.
779 */
Jens Axboeb7a69ad2012-03-21 21:55:21 +0100780 minx = minx * 1000.0;
781 maxx = maxx * 1000.0;
782
Jens Axboe3ca30e62012-03-21 18:54:30 +0100783 INIT_PRIO_TREE_NODE(&x->node);
Jens Axboeb7a69ad2012-03-21 21:55:21 +0100784 x->node.start = minx;
785 x->node.last = maxx;
Jens Axboecdae5ff2012-03-22 09:24:05 +0100786 x->tooltip = strdup(tooltip);
Jens Axboeb5526822012-03-21 20:20:35 +0100787 if (x->node.last == x->node.start) {
Jens Axboeb7a69ad2012-03-21 21:55:21 +0100788 x->node.last += fabs(g->xtick_delta);
789 if (x->node.last == x->node.start)
790 x->node.last++;
Jens Axboeb5526822012-03-21 20:20:35 +0100791 }
Jens Axboeb65c7ec2012-03-21 17:17:45 +0100792
Jens Axboe4c0cd532012-03-21 19:48:32 +0100793 /*
794 * If ret != &x->node, we have an alias. Since the values
795 * should be identical, we can drop it
796 */
797 ret = prio_tree_insert(&i->prio_tree, &x->node);
Jens Axboecdae5ff2012-03-22 09:24:05 +0100798 if (ret != &x->node) {
799 struct graph_value *alias;
800
801 alias = container_of(ret, struct graph_value, node);
802 flist_add_tail(&x->alias, &alias->alias);
Jens Axboebd10a062012-03-22 09:33:33 +0100803 } else
Jens Axboecdae5ff2012-03-22 09:24:05 +0100804 x->flags = GV_F_ON_PRIO;
Jens Axboeb65c7ec2012-03-21 17:17:45 +0100805 }
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100806
Jens Axboeb7a69ad2012-03-21 21:55:21 +0100807 if (g->per_label_limit != -1 &&
808 i->value_count > g->per_label_limit) {
Jens Axboec148dae2012-03-11 21:35:47 +0100809 int to_drop = 1;
810
811 /*
812 * If the limit was dynamically reduced, making us more
813 * than 1 entry ahead after adding this one, drop two
814 * entries. This will make us (eventually) reach the
815 * specified limit.
816 */
Jens Axboeb7a69ad2012-03-21 21:55:21 +0100817 if (i->value_count - g->per_label_limit >= 2)
Jens Axboec148dae2012-03-11 21:35:47 +0100818 to_drop = 2;
819
Jens Axboecdae5ff2012-03-22 09:24:05 +0100820 while (to_drop-- && !flist_empty(&i->value_list)) {
821 x = flist_entry(i->value_list.next, struct graph_value, list);
822 graph_value_drop(i, x);
823
824 /*
825 * If we have aliases, we could drop > 1 above.
826 */
827 if (i->value_count <= g->per_label_limit)
828 break;
Jens Axboec148dae2012-03-11 21:35:47 +0100829 }
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100830 }
831}
832
833int graph_add_data(struct graph *bg, const char *label, const double value)
834{
835 struct graph_label *i;
836 double *d;
837
838 d = malloc(sizeof(*d));
839 *d = value;
840
841 i = graph_find_label(bg, label);
842 if (!i)
843 return -1;
Jens Axboeb7a69ad2012-03-21 21:55:21 +0100844 graph_label_add_value(i, d, NULL);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100845 return 0;
846}
847
848int graph_add_xy_data(struct graph *bg, const char *label,
Jens Axboe93e2db22012-03-13 09:45:22 +0100849 const double x, const double y, const char *tooltip)
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100850{
851 struct graph_label *i;
852 struct xyvalue *xy;
853
854 xy = malloc(sizeof(*xy));
855 xy->x = x;
856 xy->y = y;
857
858 i = graph_find_label(bg, label);
859 if (!i)
860 return -1;
Jens Axboe93e2db22012-03-13 09:45:22 +0100861
Jens Axboeb7a69ad2012-03-21 21:55:21 +0100862 graph_label_add_value(i, xy, tooltip);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100863 return 0;
864}
865
Jens Axboecdae5ff2012-03-22 09:24:05 +0100866static void graph_free_values(struct graph_label *l)
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100867{
Jens Axboecdae5ff2012-03-22 09:24:05 +0100868 struct graph_value *i;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100869
Jens Axboecdae5ff2012-03-22 09:24:05 +0100870 while (!flist_empty(&l->value_list)) {
871 i = flist_entry(l->value_list.next, struct graph_value, list);
872 graph_value_drop(l, i);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100873 }
874}
875
Jens Axboecdae5ff2012-03-22 09:24:05 +0100876static void graph_free_labels(struct graph *g)
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100877{
Jens Axboecdae5ff2012-03-22 09:24:05 +0100878 struct graph_label *i;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100879
Jens Axboecdae5ff2012-03-22 09:24:05 +0100880 while (!flist_empty(&g->label_list)) {
881 i = flist_entry(g->label_list.next, struct graph_label, list);
882 flist_del(&i->list);
883 graph_free_values(i);
Jens Axboeb65c7ec2012-03-21 17:17:45 +0100884 free(i);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100885 }
886}
887
888void graph_set_color(struct graph *gr, const char *label,
889 double red, double green, double blue)
890{
Jens Axboecdae5ff2012-03-22 09:24:05 +0100891 struct flist_head *entry;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100892 struct graph_label *i;
893 double r, g, b;
894
Stephen M. Cameroncae08722012-03-07 14:47:38 +0100895 if (red < 0.0) { /* invisible color */
896 r = -1.0;
897 g = -1.0;
898 b = -1.0;
899 } else {
900 r = fabs(red);
901 g = fabs(green);
902 b = fabs(blue);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100903
Stephen M. Cameroncae08722012-03-07 14:47:38 +0100904 if (r > 1.0)
905 r = 1.0;
906 if (g > 1.0)
907 g = 1.0;
908 if (b > 1.0)
Jens Axboeb65c7ec2012-03-21 17:17:45 +0100909 b = 1.0;
Stephen M. Cameroncae08722012-03-07 14:47:38 +0100910 }
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100911
Jens Axboecdae5ff2012-03-22 09:24:05 +0100912 flist_for_each(entry, &gr->label_list) {
913 i = flist_entry(entry, struct graph_label, list);
914
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100915 if (strcmp(i->label, label) == 0) {
916 i->r = r;
917 i->g = g;
918 i->b = b;
919 break;
920 }
Jens Axboecdae5ff2012-03-22 09:24:05 +0100921 }
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100922}
923
924void graph_free(struct graph *bg)
925{
Jens Axboeb65c7ec2012-03-21 17:17:45 +0100926 free(bg->title);
927 free(bg->xtitle);
928 free(bg->ytitle);
Jens Axboecdae5ff2012-03-22 09:24:05 +0100929 graph_free_labels(bg);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100930}
931
932/* For each line in the line graph, up to per_label_limit segments may
933 * be added. After that, adding more data to the end of the line
934 * causes data to drop off of the front of the line.
935 */
936void line_graph_set_data_count_limit(struct graph *g, int per_label_limit)
937{
938 g->per_label_limit = per_label_limit;
939}
940
Stephen M. Camerondef0ac22012-03-12 07:32:57 +0100941void graph_add_extra_space(struct graph *g, double left_percent, double right_percent,
942 double top_percent, double bottom_percent)
943{
944 g->left_extra = left_percent;
945 g->right_extra = right_percent;
946 g->top_extra = top_percent;
947 g->bottom_extra = bottom_percent;
948}
949
Jens Axboed8fbeef2012-03-14 10:25:44 +0100950/*
951 * Normally values are logged in a base unit of 0, but for other purposes
952 * it makes more sense to log in higher unit. For instance for bandwidth
953 * purposes, you may want to log in KB/sec (or MB/sec) rather than bytes/sec.
954 */
955void graph_set_base_offset(struct graph *g, unsigned int base_offset)
956{
957 g->base_offset = base_offset;
958}
959
Jens Axboe93e2db22012-03-13 09:45:22 +0100960int graph_has_tooltips(struct graph *g)
961{
Jens Axboecdae5ff2012-03-22 09:24:05 +0100962 struct flist_head *entry;
Jens Axboe93e2db22012-03-13 09:45:22 +0100963 struct graph_label *i;
Stephen M. Camerondef0ac22012-03-12 07:32:57 +0100964
Jens Axboecdae5ff2012-03-22 09:24:05 +0100965 flist_for_each(entry, &g->label_list) {
966 i = flist_entry(entry, struct graph_label, list);
967
968 if (!prio_tree_empty(&i->prio_tree))
Jens Axboe93e2db22012-03-13 09:45:22 +0100969 return 1;
Jens Axboecdae5ff2012-03-22 09:24:05 +0100970 }
Jens Axboe93e2db22012-03-13 09:45:22 +0100971
972 return 0;
973}
974
975int graph_contains_xy(struct graph *g, int x, int y)
976{
977 int first_x = g->xoffset;
978 int last_x = g->xoffset + g->xdim;
979 int first_y = g->yoffset;
980 int last_y = g->yoffset + g->ydim;
981
982 return (x >= first_x && x <= last_x) && (y >= first_y && y <= last_y);
983}
984
Jens Axboeb65c7ec2012-03-21 17:17:45 +0100985const char *graph_find_tooltip(struct graph *g, int ix, int iy)
Jens Axboe93e2db22012-03-13 09:45:22 +0100986{
Jens Axboeb65c7ec2012-03-21 17:17:45 +0100987 double x = ix, y = iy;
988 struct prio_tree_iter iter;
989 struct prio_tree_node *n;
Jens Axboeb65c7ec2012-03-21 17:17:45 +0100990 struct graph_value *best = NULL;
Jens Axboecdae5ff2012-03-22 09:24:05 +0100991 struct flist_head *entry;
Jens Axboeb65c7ec2012-03-21 17:17:45 +0100992 double best_delta;
Jens Axboeb7a69ad2012-03-21 21:55:21 +0100993 double maxy, miny;
Jens Axboe93e2db22012-03-13 09:45:22 +0100994
Jens Axboeb65c7ec2012-03-21 17:17:45 +0100995 x -= g->xoffset;
996 y -= g->yoffset;
997
998 x = g->xtick_zero_val + ((x - g->xtick_zero) * g->xtick_delta);
999 y = g->ytick_zero_val + ((y - g->ytick_zero) * g->ytick_delta);
1000
Jens Axboeb7a69ad2012-03-21 21:55:21 +01001001 x = x * 1000.0;
1002 maxy = y + (g->ytick_one_val * TOOLTIP_DELTA);
1003 miny = y - (g->ytick_one_val * TOOLTIP_DELTA);
Jens Axboeb65c7ec2012-03-21 17:17:45 +01001004 best_delta = UINT_MAX;
Jens Axboecdae5ff2012-03-22 09:24:05 +01001005 flist_for_each(entry, &g->label_list) {
1006 struct graph_label *i;
1007
1008 i = flist_entry(entry, struct graph_label, list);
1009
Jens Axboe08d7d5a2012-03-21 19:33:32 +01001010 INIT_PRIO_TREE_ITER(&iter);
Jens Axboeb7a69ad2012-03-21 21:55:21 +01001011 prio_tree_iter_init(&iter, &i->prio_tree, x, x);
Jens Axboeb65c7ec2012-03-21 17:17:45 +01001012
1013 n = prio_tree_next(&iter);
1014 if (!n)
1015 continue;
1016
1017 do {
Jens Axboecdae5ff2012-03-22 09:24:05 +01001018 struct graph_value *v, *rootv;
Jens Axboeb7a69ad2012-03-21 21:55:21 +01001019 double yval, ydiff;
Jens Axboeb65c7ec2012-03-21 17:17:45 +01001020
1021 v = container_of(n, struct graph_value, node);
Jens Axboecdae5ff2012-03-22 09:24:05 +01001022 rootv = v;
1023 do {
1024 yval = gety(v);
1025 ydiff = fabs(yval - y);
Jens Axboe93e2db22012-03-13 09:45:22 +01001026
Jens Axboecdae5ff2012-03-22 09:24:05 +01001027 /*
1028 * zero delta, or within or match critera, break
1029 */
1030 if (ydiff < best_delta) {
1031 best_delta = ydiff;
1032 if (!best_delta ||
1033 (yval >= miny && yval <= maxy)) {
1034 best = v;
1035 break;
1036 }
Jens Axboeb65c7ec2012-03-21 17:17:45 +01001037 }
Jens Axboecdae5ff2012-03-22 09:24:05 +01001038 if (!flist_empty(&v->alias))
1039 v = flist_entry(v->alias.next, struct graph_value, alias);
1040 } while (v != rootv);
Jens Axboeb65c7ec2012-03-21 17:17:45 +01001041 } while ((n = prio_tree_next(&iter)) != NULL);
1042
1043 /*
1044 * If we got matches in one label, don't check others.
1045 */
1046 break;
Jens Axboecdae5ff2012-03-22 09:24:05 +01001047 }
Jens Axboeb65c7ec2012-03-21 17:17:45 +01001048
1049 if (best)
1050 return best->tooltip;
Jens Axboe93e2db22012-03-13 09:45:22 +01001051
1052 return NULL;
1053}