blob: ba013da449d162a38311ccc7950decb86bc16131 [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"
Stephen M. Cameronee2f55b2012-03-27 08:14:09 +020036#include "cairo_text_helpers.h"
Jens Axboeb65c7ec2012-03-21 17:17:45 +010037
38/*
39 * Allowable difference to show tooltip
40 */
Jens Axboeb7a69ad2012-03-21 21:55:21 +010041#define TOOLTIP_DELTA 0.08
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +010042
43struct xyvalue {
44 double x, y;
45};
46
Jens Axboecdae5ff2012-03-22 09:24:05 +010047enum {
48 GV_F_ON_PRIO = 1,
Jens Axboe8dfd6072012-03-22 22:10:37 +010049 GV_F_PRIO_SKIP = 2,
Jens Axboecdae5ff2012-03-22 09:24:05 +010050};
51
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +010052struct graph_value {
Jens Axboecdae5ff2012-03-22 09:24:05 +010053 struct flist_head list;
Jens Axboeb65c7ec2012-03-21 17:17:45 +010054 struct prio_tree_node node;
Jens Axboecdae5ff2012-03-22 09:24:05 +010055 struct flist_head alias;
56 unsigned int flags;
Jens Axboe93e2db22012-03-13 09:45:22 +010057 char *tooltip;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +010058 void *value;
59};
60
61struct graph_label {
Jens Axboecdae5ff2012-03-22 09:24:05 +010062 struct flist_head list;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +010063 char *label;
Jens Axboecdae5ff2012-03-22 09:24:05 +010064 struct flist_head value_list;
Jens Axboeb65c7ec2012-03-21 17:17:45 +010065 struct prio_tree_root prio_tree;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +010066 double r, g, b;
Jens Axboe01a947f2012-03-22 21:21:00 +010067 int hide;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +010068 int value_count;
69 struct graph *parent;
70};
71
Jens Axboeb65c7ec2012-03-21 17:17:45 +010072struct tick_value {
73 unsigned int offset;
74 double value;
75};
76
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +010077struct graph {
78 char *title;
79 char *xtitle;
80 char *ytitle;
Jens Axboe87d5f272012-03-07 12:31:40 +010081 unsigned int xdim, ydim;
Stephen M. Cameron57f9d282012-03-11 11:36:51 +010082 double xoffset, yoffset;
Jens Axboecdae5ff2012-03-22 09:24:05 +010083 struct flist_head label_list;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +010084 int per_label_limit;
Jens Axboef3e84402012-03-07 13:14:32 +010085 const char *font;
Stephen M. Cameron7175d912012-03-11 11:35:10 +010086 graph_axis_unit_change_callback x_axis_unit_change_callback;
87 graph_axis_unit_change_callback y_axis_unit_change_callback;
Jens Axboed8fbeef2012-03-14 10:25:44 +010088 unsigned int base_offset;
Jens Axboe01a947f2012-03-22 21:21:00 +010089 unsigned int dont_graph_all_zeroes;
Stephen M. Camerondef0ac22012-03-12 07:32:57 +010090 double left_extra;
91 double right_extra;
92 double top_extra;
93 double bottom_extra;
Jens Axboeb65c7ec2012-03-21 17:17:45 +010094
95 double xtick_zero;
96 double xtick_delta;
97 double xtick_zero_val;
Jens Axboeb7a69ad2012-03-21 21:55:21 +010098 double xtick_one_val;
Jens Axboeb65c7ec2012-03-21 17:17:45 +010099 double ytick_zero;
100 double ytick_delta;
101 double ytick_zero_val;
Jens Axboeb7a69ad2012-03-21 21:55:21 +0100102 double ytick_one_val;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100103};
104
Stephen M. Cameron3ea48b82012-03-07 19:40:58 +0100105void 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. Cameron57f9d282012-03-11 11:36:51 +0100111void graph_set_position(struct graph *g, double xoffset, double yoffset)
112{
113 g->xoffset = xoffset;
114 g->yoffset = yoffset;
115}
116
Jens Axboef3e84402012-03-07 13:14:32 +0100117struct graph *graph_new(unsigned int xdim, unsigned int ydim, const char *font)
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100118{
119 struct graph *g;
120
121 g = calloc(1, sizeof(*g));
Jens Axboecdae5ff2012-03-22 09:24:05 +0100122 INIT_FLIST_HEAD(&g->label_list);
Stephen M. Cameron3ea48b82012-03-07 19:40:58 +0100123 graph_set_size(g, xdim, ydim);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100124 g->per_label_limit = -1;
Jens Axboef3e84402012-03-07 13:14:32 +0100125 g->font = font;
126 if (!g->font)
Jens Axboea1e79722012-03-23 10:52:25 +0100127 g->font = GRAPH_DEFAULT_FONT;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100128 return g;
129}
130
Jens Axboea1e79722012-03-23 10:52:25 +0100131void graph_set_font(struct graph *g, const char *font)
132{
133 g->font = font;
134}
135
Stephen M. Cameron7175d912012-03-11 11:35:10 +0100136void 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
141void 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 Axboecdae5ff2012-03-22 09:24:05 +0100146static int count_labels(struct graph *g)
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100147{
Jens Axboecdae5ff2012-03-22 09:24:05 +0100148 struct flist_head *entry;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100149 int count = 0;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100150
Jens Axboecdae5ff2012-03-22 09:24:05 +0100151 flist_for_each(entry, &g->label_list)
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100152 count++;
Jens Axboecdae5ff2012-03-22 09:24:05 +0100153
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100154 return count;
155}
156
Jens Axboecdae5ff2012-03-22 09:24:05 +0100157static int count_values(struct graph_label *l)
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100158{
Jens Axboecdae5ff2012-03-22 09:24:05 +0100159 struct flist_head *entry;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100160 int count = 0;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100161
Jens Axboecdae5ff2012-03-22 09:24:05 +0100162 flist_for_each(entry, &l->value_list)
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100163 count++;
Jens Axboecdae5ff2012-03-22 09:24:05 +0100164
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100165 return count;
166}
167
168typedef double (*double_comparator)(double a, double b);
169
170static double mindouble(double a, double b)
171{
172 return a < b ? a : b;
173}
174
175static double maxdouble(double a, double b)
176{
177 return a < b ? b : a;
178}
179
Jens Axboecdae5ff2012-03-22 09:24:05 +0100180static double find_double_values(struct graph_label *l, double_comparator cmp)
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100181{
Jens Axboecdae5ff2012-03-22 09:24:05 +0100182 struct flist_head *entry;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100183 double answer, tmp;
Jens Axboecdae5ff2012-03-22 09:24:05 +0100184 int first = 1;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100185
Jens Axboe01a947f2012-03-22 21:21:00 +0100186 if (flist_empty(&l->value_list))
187 return 0.0;
188
Jens Axboecdae5ff2012-03-22 09:24:05 +0100189 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. Cameronaf58ef32012-03-07 07:56:16 +0100194 if (first) {
195 answer = tmp;
196 first = 0;
197 } else {
198 answer = cmp(answer, tmp);
199 }
200 }
201 return answer;
202}
203
Jens Axboecdae5ff2012-03-22 09:24:05 +0100204static double find_double_data(struct graph *g, double_comparator cmp)
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100205{
Jens Axboecdae5ff2012-03-22 09:24:05 +0100206 struct flist_head *entry;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100207 struct graph_label *i;
208 int first = 1;
209 double answer, tmp;
210
Jens Axboe01a947f2012-03-22 21:21:00 +0100211 if (flist_empty(&g->label_list))
212 return 0.0;
213
Jens Axboecdae5ff2012-03-22 09:24:05 +0100214 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. Cameronaf58ef32012-03-07 07:56:16 +0100217 if (first) {
218 answer = tmp;
219 first = 0;
220 } else {
221 answer = cmp(tmp, answer);
222 }
223 }
224 return answer;
225}
226
Jens Axboecdae5ff2012-03-22 09:24:05 +0100227static double find_min_data(struct graph *g)
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100228{
Jens Axboecdae5ff2012-03-22 09:24:05 +0100229 return find_double_data(g, mindouble);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100230}
231
Jens Axboecdae5ff2012-03-22 09:24:05 +0100232static double find_max_data(struct graph *g)
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100233{
Jens Axboecdae5ff2012-03-22 09:24:05 +0100234 return find_double_data(g, maxdouble);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100235}
236
237static 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 Axboecdae5ff2012-03-22 09:24:05 +0100241 struct flist_head *entry;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100242 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 Axboecdae5ff2012-03-22 09:24:05 +0100249 flist_for_each(entry, &lb->value_list) {
250 struct graph_value *i;
251
252 i = flist_entry(entry, struct graph_value, list);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100253
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);
266 bar_num++;
267 }
268}
269
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100270static void graph_draw_common(struct graph *g, cairo_t *cr,
271 double *x1, double *y1, double *x2, double *y2)
272{
Jens Axboe4e14f012012-03-23 08:18:57 +0100273 cairo_set_source_rgb(cr, 0, 0, 0);
274 cairo_set_line_width (cr, 0.8);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100275
Jens Axboe9ede9c92012-03-14 10:43:20 +0100276 *x1 = 0.10 * g->xdim;
Stephen M. Cameron6bf86002012-03-08 16:58:50 +0100277 *x2 = 0.95 * g->xdim;
278 *y1 = 0.10 * g->ydim;
279 *y2 = 0.90 * g->ydim;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100280
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. Cameronee2f55b2012-03-27 08:14:09 +0200288 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. Cameronaf58ef32012-03-07 07:56:16 +0100291 cairo_stroke(cr);
292}
293
294static void graph_draw_x_ticks(struct graph *g, cairo_t *cr,
295 double x1, double y1, double x2, double y2,
Jens Axboed8fbeef2012-03-14 10:25:44 +0100296 double minx, double maxx, int nticks, int add_tm_text)
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100297{
298 struct tickmark *tm;
299 double tx;
Stephen M. Cameron7175d912012-03-11 11:35:10 +0100300 int i, power_of_ten;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100301 static double dash[] = { 1.0, 2.0 };
302
Stephen M. Cameron7175d912012-03-11 11:35:10 +0100303 nticks = calc_tickmarks(minx, maxx, nticks, &tm, &power_of_ten,
Jens Axboed8fbeef2012-03-14 10:25:44 +0100304 g->x_axis_unit_change_callback == NULL, g->base_offset);
Stephen M. Cameron7175d912012-03-11 11:35:10 +0100305 if (g->x_axis_unit_change_callback)
306 g->x_axis_unit_change_callback(g, power_of_ten);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100307
308 for (i = 0; i < nticks; i++) {
309 tx = (((tm[i].value) - minx) / (maxx - minx)) * (x2 - x1) + x1;
Jens Axboed8fbeef2012-03-14 10:25:44 +0100310
Jens Axboeb65c7ec2012-03-21 17:17:45 +0100311 /*
312 * Update tick delta
313 */
314 if (!i) {
315 g->xtick_zero = tx;
316 g->xtick_zero_val = tm[0].value;
Jens Axboeb7a69ad2012-03-21 21:55:21 +0100317 } else if (i == 1) {
Jens Axboeb65c7ec2012-03-21 17:17:45 +0100318 g->xtick_delta = (tm[1].value - tm[0].value) / (tx - g->xtick_zero);
Jens Axboeb7a69ad2012-03-21 21:55:21 +0100319 g->xtick_one_val = tm[1].value;
320 }
Jens Axboeb65c7ec2012-03-21 17:17:45 +0100321
Jens Axboed8fbeef2012-03-14 10:25:44 +0100322 /* really tx < yx || tx > x2, but protect against rounding */
323 if (x1 - tx > 0.01 || tx - x2 > 0.01)
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100324 continue;
325
326 /* Draw tick mark */
Jens Axboef3e84402012-03-07 13:14:32 +0100327 cairo_set_line_width(cr, 0.8);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100328 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 Axboed8fbeef2012-03-14 10:25:44 +0100341 if (!add_tm_text)
342 continue;
343
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100344 /* draw tickmark label */
Stephen M. Cameronee2f55b2012-03-27 08:14:09 +0200345 draw_centered_text(cr, g->font, tx, y2 * 1.04, 12.0, tm[i].string);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100346 cairo_stroke(cr);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100347 }
348}
349
Jens Axboed8fbeef2012-03-14 10:25:44 +0100350static double graph_draw_y_ticks(struct graph *g, cairo_t *cr,
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100351 double x1, double y1, double x2, double y2,
Jens Axboed8fbeef2012-03-14 10:25:44 +0100352 double miny, double maxy, int nticks, int add_tm_text)
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100353{
354 struct tickmark *tm;
355 double ty;
Stephen M. Cameron7175d912012-03-11 11:35:10 +0100356 int i, power_of_ten;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100357 static double dash[] = { 2.0, 2.0 };
358
Stephen M. Cameron7175d912012-03-11 11:35:10 +0100359 nticks = calc_tickmarks(miny, maxy, nticks, &tm, &power_of_ten,
Jens Axboed8fbeef2012-03-14 10:25:44 +0100360 g->y_axis_unit_change_callback == NULL, g->base_offset);
Stephen M. Cameron7175d912012-03-11 11:35:10 +0100361 if (g->y_axis_unit_change_callback)
362 g->y_axis_unit_change_callback(g, power_of_ten);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100363
Jens Axboed8fbeef2012-03-14 10:25:44 +0100364 /*
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. Cameronaf58ef32012-03-07 07:56:16 +0100371 for (i = 0; i < nticks; i++) {
372 ty = y2 - (((tm[i].value) - miny) / (maxy - miny)) * (y2 - y1);
Jens Axboed8fbeef2012-03-14 10:25:44 +0100373
Jens Axboeb65c7ec2012-03-21 17:17:45 +0100374 /*
375 * Update tick delta
376 */
377 if (!i) {
378 g->ytick_zero = ty;
379 g->ytick_zero_val = tm[0].value;
Jens Axboeb7a69ad2012-03-21 21:55:21 +0100380 } else if (i == 1) {
Jens Axboeb65c7ec2012-03-21 17:17:45 +0100381 g->ytick_delta = (tm[1].value - tm[0].value) / (ty - g->ytick_zero);
Jens Axboeb7a69ad2012-03-21 21:55:21 +0100382 g->ytick_one_val = tm[1].value;
383 }
Jens Axboeb65c7ec2012-03-21 17:17:45 +0100384
Jens Axboed8fbeef2012-03-14 10:25:44 +0100385 /* really ty < y1 || ty > y2, but protect against rounding */
386 if (y1 - ty > 0.01 || ty - y2 > 0.01)
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100387 continue;
Jens Axboed8fbeef2012-03-14 10:25:44 +0100388
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100389 /* 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 Axboed8fbeef2012-03-14 10:25:44 +0100403 if (!add_tm_text)
404 continue;
405
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100406 /* draw tickmark label */
Stephen M. Cameronee2f55b2012-03-27 08:14:09 +0200407 draw_right_justified_text(cr, g->font, x1 - (x2 - x1) * 0.025, ty, 12.0, tm[i].string);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100408 cairo_stroke(cr);
409 }
Jens Axboed8fbeef2012-03-14 10:25:44 +0100410
411 /*
412 * Return new max to use
413 */
414 return maxy;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100415}
416
417void 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 Axboecdae5ff2012-03-22 09:24:05 +0100424 struct flist_head *entry;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100425
426 cairo_save(cr);
Stephen M. Cameron57f9d282012-03-11 11:36:51 +0100427 cairo_translate(cr, bg->xoffset, bg->yoffset);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100428 graph_draw_common(bg, cr, &x1, &y1, &x2, &y2);
429
Jens Axboecdae5ff2012-03-22 09:24:05 +0100430 nlabels = count_labels(bg);
Jens Axboed8fbeef2012-03-14 10:25:44 +0100431 space_per_label = (x2 - x1) / (double) nlabels;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100432
Jens Axboebb393792012-03-15 10:52:38 +0100433 /*
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 Axboecdae5ff2012-03-22 09:24:05 +0100437 mindata = find_min_data(bg);
Jens Axboebb393792012-03-15 10:52:38 +0100438 if (mindata > 0)
439 mindata = 0;
440
Jens Axboecdae5ff2012-03-22 09:24:05 +0100441 maxdata = find_max_data(bg);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100442
443 if (fabs(maxdata - mindata) < 1e-20) {
Stephen M. Cameronee2f55b2012-03-27 08:14:09 +0200444 draw_centered_text(cr, bg->font,
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100445 x1 + (x2 - x1) / 2.0,
446 y1 + (y2 - y1) / 2.0, 20.0, "No good data");
447 return;
448 }
449
Jens Axboebb393792012-03-15 10:52:38 +0100450 maxdata = graph_draw_y_ticks(bg, cr, x1, y1, x2, y2, mindata, maxdata, 10, 1);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100451 i = 0;
Jens Axboecdae5ff2012-03-22 09:24:05 +0100452 flist_for_each(entry, &bg->label_list) {
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100453 int nvalues;
Jens Axboecdae5ff2012-03-22 09:24:05 +0100454
455 lb = flist_entry(entry, struct graph_label, list);
456 nvalues = count_values(lb);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100457 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. Cameronee2f55b2012-03-27 08:14:09 +0200461 draw_centered_text(cr, bg->font, x1 + space_per_label * (i + 0.5), bg->ydim * 0.93,
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100462 12.0, lb->label);
463 i++;
464 }
465 cairo_stroke(cr);
466 cairo_restore(cr);
467}
468
469typedef double (*xy_value_extractor)(struct graph_value *v);
470
471static double getx(struct graph_value *v)
472{
473 struct xyvalue *xy = v->value;
474 return xy->x;
475}
476
477static double gety(struct graph_value *v)
478{
479 struct xyvalue *xy = v->value;
480 return xy->y;
481}
482
483static double find_xy_value(struct graph *g, xy_value_extractor getvalue, double_comparator cmp)
484{
Stephen M. Cameron10e54cd2012-03-07 19:39:55 +0100485 double tmp, answer = 0.0;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100486 struct graph_label *i;
487 struct graph_value *j;
Jens Axboecdae5ff2012-03-22 09:24:05 +0100488 struct flist_head *jentry, *entry;
Stephen M. Camerond582bf72012-03-07 19:37:57 +0100489 int first = 1;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100490
Jens Axboecdae5ff2012-03-22 09:24:05 +0100491 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. Cameronaf58ef32012-03-07 07:56:16 +0100496 tmp = getvalue(j);
Stephen M. Camerond582bf72012-03-07 19:37:57 +0100497 if (first) {
498 first = 0;
499 answer = tmp;
500 }
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100501 answer = cmp(tmp, answer);
502 }
Jens Axboecdae5ff2012-03-22 09:24:05 +0100503 }
504
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100505 return answer;
Jens Axboe01a947f2012-03-22 21:21:00 +0100506}
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100507
508void line_graph_draw(struct graph *g, cairo_t *cr)
509{
510 double x1, y1, x2, y2;
Stephen M. Camerondef0ac22012-03-12 07:32:57 +0100511 double minx, miny, maxx, maxy, gminx, gminy, gmaxx, gmaxy;
512 double tx, ty, top_extra, bottom_extra, left_extra, right_extra;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100513 struct graph_label *i;
514 struct graph_value *j;
Stephen M. Cameron9ce9cfb2012-03-07 19:37:25 +0100515 int good_data = 1, first = 1;
Jens Axboecdae5ff2012-03-22 09:24:05 +0100516 struct flist_head *entry, *lentry;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100517
518 cairo_save(cr);
Stephen M. Cameron57f9d282012-03-11 11:36:51 +0100519 cairo_translate(cr, g->xoffset, g->yoffset);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100520 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 Axboe5aec6682012-03-16 10:17:08 +0100525
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. Cameronaf58ef32012-03-07 07:56:16 +0100534 maxy = find_xy_value(g, gety, maxdouble);
535
536 if (fabs(maxx - minx) < 1e-20 || fabs(maxy - miny) < 1e-20) {
Stephen M. Cameron9ce9cfb2012-03-07 19:37:25 +0100537 good_data = 0;
538 minx = 0.0;
539 miny = 0.0;
540 maxx = 10.0;
541 maxy = 100.0;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100542 }
543
Stephen M. Camerondef0ac22012-03-12 07:32:57 +0100544 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 Axboed8fbeef2012-03-14 10:25:44 +0100563 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. Cameronaf58ef32012-03-07 07:56:16 +0100565
Stephen M. Cameron9ce9cfb2012-03-07 19:37:25 +0100566 if (!good_data)
567 goto skip_data;
568
Jens Axboef3e84402012-03-07 13:14:32 +0100569 cairo_set_line_width(cr, 1.5);
Jens Axboecdae5ff2012-03-22 09:24:05 +0100570 flist_for_each(lentry, &g->label_list) {
571 i = flist_entry(lentry, struct graph_label, list);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100572 first = 1;
Jens Axboe01a947f2012-03-22 21:21:00 +0100573 if (i->hide || i->r < 0) /* invisible data */
Stephen M. Cameroncae08722012-03-07 14:47:38 +0100574 continue;
Jens Axboeb65c7ec2012-03-21 17:17:45 +0100575
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100576 cairo_set_source_rgb(cr, i->r, i->g, i->b);
Jens Axboecdae5ff2012-03-22 09:24:05 +0100577 flist_for_each(entry, &i->value_list) {
578 j = flist_entry(entry, struct graph_value, list);
Stephen M. Camerondef0ac22012-03-12 07:32:57 +0100579 tx = ((getx(j) - gminx) / (gmaxx - gminx)) * (x2 - x1) + x1;
580 ty = y2 - ((gety(j) - gminy) / (gmaxy - gminy)) * (y2 - y1);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100581 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. Cameron9ce9cfb2012-03-07 19:37:25 +0100590
591skip_data:
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100592 cairo_restore(cr);
593}
594
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100595static void setstring(char **str, const char *value)
596{
Jens Axboeb65c7ec2012-03-21 17:17:45 +0100597 free(*str);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100598 *str = strdup(value);
599}
600
601void graph_title(struct graph *bg, const char *title)
602{
603 setstring(&bg->title, title);
604}
605
606void graph_x_title(struct graph *bg, const char *title)
607{
608 setstring(&bg->xtitle, title);
609}
610
611void graph_y_title(struct graph *bg, const char *title)
612{
613 setstring(&bg->ytitle, title);
614}
615
616static struct graph_label *graph_find_label(struct graph *bg,
617 const char *label)
618{
Jens Axboecdae5ff2012-03-22 09:24:05 +0100619 struct flist_head *entry;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100620 struct graph_label *i;
621
Jens Axboecdae5ff2012-03-22 09:24:05 +0100622 flist_for_each(entry, &bg->label_list) {
623 i = flist_entry(entry, struct graph_label, list);
624
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100625 if (strcmp(label, i->label) == 0)
626 return i;
Jens Axboecdae5ff2012-03-22 09:24:05 +0100627 }
628
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100629 return NULL;
630}
631
Jens Axboe8dfd6072012-03-22 22:10:37 +0100632graph_label_t graph_add_label(struct graph *bg, const char *label)
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100633{
634 struct graph_label *i;
635
636 i = graph_find_label(bg, label);
637 if (i)
Jens Axboe8dfd6072012-03-22 22:10:37 +0100638 return i; /* already present. */
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100639 i = calloc(1, sizeof(*i));
Jens Axboecdae5ff2012-03-22 09:24:05 +0100640 INIT_FLIST_HEAD(&i->value_list);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100641 i->parent = bg;
642 setstring(&i->label, label);
Jens Axboecdae5ff2012-03-22 09:24:05 +0100643 flist_add_tail(&i->list, &bg->label_list);
Jens Axboeb65c7ec2012-03-21 17:17:45 +0100644 INIT_PRIO_TREE_ROOT(&i->prio_tree);
Jens Axboe8dfd6072012-03-22 22:10:37 +0100645 return i;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100646}
647
Jens Axboecdae5ff2012-03-22 09:24:05 +0100648static void __graph_value_drop(struct graph_label *l, struct graph_value *v)
649{
Jens Axboed0db8192012-03-22 20:48:02 +0100650 flist_del_init(&v->list);
Jens Axboecdae5ff2012-03-22 09:24:05 +0100651 if (v->tooltip)
652 free(v->tooltip);
653 free(v->value);
654 free(v);
655 l->value_count--;
656}
657
658static void graph_value_drop(struct graph_label *l, struct graph_value *v)
659{
Jens Axboe8dfd6072012-03-22 22:10:37 +0100660 if (v->flags & GV_F_PRIO_SKIP) {
661 __graph_value_drop(l, v);
662 return;
663 }
664
Jens Axboecdae5ff2012-03-22 09:24:05 +0100665 /*
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 Axboed0db8192012-03-22 20:48:02 +0100678 while (!flist_empty(&v->alias)) {
Jens Axboecdae5ff2012-03-22 09:24:05 +0100679 struct graph_value *a;
680
Jens Axboed0db8192012-03-22 20:48:02 +0100681 a = flist_entry(v->alias.next, struct graph_value, alias);
682 flist_del_init(&a->alias);
Jens Axboecdae5ff2012-03-22 09:24:05 +0100683
684 __graph_value_drop(l, a);
685 }
686
687 __graph_value_drop(l, v);
688}
689
Jens Axboeb7a69ad2012-03-21 21:55:21 +0100690static void graph_label_add_value(struct graph_label *i, void *value,
691 const char *tooltip)
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100692{
Jens Axboeb7a69ad2012-03-21 21:55:21 +0100693 struct graph *g = i->parent;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100694 struct graph_value *x;
695
696 x = malloc(sizeof(*x));
Jens Axboe3ca30e62012-03-21 18:54:30 +0100697 memset(x, 0, sizeof(*x));
Jens Axboecdae5ff2012-03-22 09:24:05 +0100698 INIT_FLIST_HEAD(&x->alias);
699 INIT_FLIST_HEAD(&x->list);
700 flist_add_tail(&x->list, &i->value_list);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100701 i->value_count++;
Jens Axboecdae5ff2012-03-22 09:24:05 +0100702 x->value = value;
Jens Axboeb65c7ec2012-03-21 17:17:45 +0100703
Jens Axboef00e43f2012-03-21 19:53:32 +0100704 if (tooltip) {
Jens Axboeb7a69ad2012-03-21 21:55:21 +0100705 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 Axboe4c0cd532012-03-21 19:48:32 +0100708 struct prio_tree_node *ret;
Jens Axboeb65c7ec2012-03-21 17:17:45 +0100709
Jens Axboe5fb8d792012-03-21 22:00:43 +0100710 /*
711 * use msec to avoid dropping too much precision when
712 * storing as an integer.
713 */
Jens Axboeb7a69ad2012-03-21 21:55:21 +0100714 minx = minx * 1000.0;
715 maxx = maxx * 1000.0;
716
Jens Axboe3ca30e62012-03-21 18:54:30 +0100717 INIT_PRIO_TREE_NODE(&x->node);
Jens Axboeb7a69ad2012-03-21 21:55:21 +0100718 x->node.start = minx;
719 x->node.last = maxx;
Jens Axboecdae5ff2012-03-22 09:24:05 +0100720 x->tooltip = strdup(tooltip);
Jens Axboeb5526822012-03-21 20:20:35 +0100721 if (x->node.last == x->node.start) {
Jens Axboeb7a69ad2012-03-21 21:55:21 +0100722 x->node.last += fabs(g->xtick_delta);
723 if (x->node.last == x->node.start)
724 x->node.last++;
Jens Axboeb5526822012-03-21 20:20:35 +0100725 }
Jens Axboeb65c7ec2012-03-21 17:17:45 +0100726
Jens Axboe4c0cd532012-03-21 19:48:32 +0100727 /*
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 Axboecdae5ff2012-03-22 09:24:05 +0100732 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 Axboebd10a062012-03-22 09:33:33 +0100737 } else
Jens Axboecdae5ff2012-03-22 09:24:05 +0100738 x->flags = GV_F_ON_PRIO;
Jens Axboe8dfd6072012-03-22 22:10:37 +0100739 } else
740 x->flags = GV_F_PRIO_SKIP;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100741
Jens Axboeb7a69ad2012-03-21 21:55:21 +0100742 if (g->per_label_limit != -1 &&
743 i->value_count > g->per_label_limit) {
Jens Axboec148dae2012-03-11 21:35:47 +0100744 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 Axboeb7a69ad2012-03-21 21:55:21 +0100752 if (i->value_count - g->per_label_limit >= 2)
Jens Axboec148dae2012-03-11 21:35:47 +0100753 to_drop = 2;
754
Jens Axboecdae5ff2012-03-22 09:24:05 +0100755 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 Axboec148dae2012-03-11 21:35:47 +0100764 }
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100765 }
766}
767
Jens Axboe8dfd6072012-03-22 22:10:37 +0100768int graph_add_data(struct graph *bg, graph_label_t label, const double value)
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100769{
Jens Axboe8dfd6072012-03-22 22:10:37 +0100770 struct graph_label *i = label;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100771 double *d;
772
773 d = malloc(sizeof(*d));
774 *d = value;
775
Jens Axboeb7a69ad2012-03-21 21:55:21 +0100776 graph_label_add_value(i, d, NULL);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100777 return 0;
778}
779
Jens Axboe01a947f2012-03-22 21:21:00 +0100780static 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 Axboe8dfd6072012-03-22 22:10:37 +0100795int graph_add_xy_data(struct graph *bg, graph_label_t label,
Jens Axboe93e2db22012-03-13 09:45:22 +0100796 const double x, const double y, const char *tooltip)
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100797{
Jens Axboe8dfd6072012-03-22 22:10:37 +0100798 struct graph_label *i = label;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100799 struct xyvalue *xy;
800
Jens Axboe01a947f2012-03-22 21:21:00 +0100801 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 Axboeb7a69ad2012-03-21 21:55:21 +0100810 graph_label_add_value(i, xy, tooltip);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100811 return 0;
812}
813
Jens Axboecdae5ff2012-03-22 09:24:05 +0100814static void graph_free_values(struct graph_label *l)
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100815{
Jens Axboecdae5ff2012-03-22 09:24:05 +0100816 struct graph_value *i;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100817
Jens Axboecdae5ff2012-03-22 09:24:05 +0100818 while (!flist_empty(&l->value_list)) {
819 i = flist_entry(l->value_list.next, struct graph_value, list);
820 graph_value_drop(l, i);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100821 }
822}
823
Jens Axboecdae5ff2012-03-22 09:24:05 +0100824static void graph_free_labels(struct graph *g)
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100825{
Jens Axboecdae5ff2012-03-22 09:24:05 +0100826 struct graph_label *i;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100827
Jens Axboecdae5ff2012-03-22 09:24:05 +0100828 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 Axboeb65c7ec2012-03-21 17:17:45 +0100832 free(i);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100833 }
834}
835
Jens Axboe8dfd6072012-03-22 22:10:37 +0100836void graph_set_color(struct graph *gr, graph_label_t label, double red,
837 double green, double blue)
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100838{
Jens Axboe8dfd6072012-03-22 22:10:37 +0100839 struct graph_label *i = label;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100840 double r, g, b;
841
Stephen M. Cameroncae08722012-03-07 14:47:38 +0100842 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. Cameronaf58ef32012-03-07 07:56:16 +0100850
Stephen M. Cameroncae08722012-03-07 14:47:38 +0100851 if (r > 1.0)
852 r = 1.0;
853 if (g > 1.0)
854 g = 1.0;
855 if (b > 1.0)
Jens Axboeb65c7ec2012-03-21 17:17:45 +0100856 b = 1.0;
Stephen M. Cameroncae08722012-03-07 14:47:38 +0100857 }
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100858
Jens Axboe8dfd6072012-03-22 22:10:37 +0100859 i->r = r;
860 i->g = g;
861 i->b = b;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100862}
863
864void graph_free(struct graph *bg)
865{
Jens Axboeb65c7ec2012-03-21 17:17:45 +0100866 free(bg->title);
867 free(bg->xtitle);
868 free(bg->ytitle);
Jens Axboecdae5ff2012-03-22 09:24:05 +0100869 graph_free_labels(bg);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100870}
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 */
876void line_graph_set_data_count_limit(struct graph *g, int per_label_limit)
877{
878 g->per_label_limit = per_label_limit;
879}
880
Stephen M. Camerondef0ac22012-03-12 07:32:57 +0100881void graph_add_extra_space(struct graph *g, double left_percent, double right_percent,
882 double top_percent, double bottom_percent)
883{
884 g->left_extra = left_percent;
885 g->right_extra = right_percent;
886 g->top_extra = top_percent;
887 g->bottom_extra = bottom_percent;
888}
889
Jens Axboed8fbeef2012-03-14 10:25:44 +0100890/*
891 * Normally values are logged in a base unit of 0, but for other purposes
892 * it makes more sense to log in higher unit. For instance for bandwidth
893 * purposes, you may want to log in KB/sec (or MB/sec) rather than bytes/sec.
894 */
895void graph_set_base_offset(struct graph *g, unsigned int base_offset)
896{
897 g->base_offset = base_offset;
898}
899
Jens Axboe93e2db22012-03-13 09:45:22 +0100900int graph_has_tooltips(struct graph *g)
901{
Jens Axboecdae5ff2012-03-22 09:24:05 +0100902 struct flist_head *entry;
Jens Axboe93e2db22012-03-13 09:45:22 +0100903 struct graph_label *i;
Stephen M. Camerondef0ac22012-03-12 07:32:57 +0100904
Jens Axboecdae5ff2012-03-22 09:24:05 +0100905 flist_for_each(entry, &g->label_list) {
906 i = flist_entry(entry, struct graph_label, list);
907
908 if (!prio_tree_empty(&i->prio_tree))
Jens Axboe93e2db22012-03-13 09:45:22 +0100909 return 1;
Jens Axboecdae5ff2012-03-22 09:24:05 +0100910 }
Jens Axboe93e2db22012-03-13 09:45:22 +0100911
912 return 0;
913}
914
915int graph_contains_xy(struct graph *g, int x, int y)
916{
917 int first_x = g->xoffset;
918 int last_x = g->xoffset + g->xdim;
919 int first_y = g->yoffset;
920 int last_y = g->yoffset + g->ydim;
921
922 return (x >= first_x && x <= last_x) && (y >= first_y && y <= last_y);
923}
924
Jens Axboeb65c7ec2012-03-21 17:17:45 +0100925const char *graph_find_tooltip(struct graph *g, int ix, int iy)
Jens Axboe93e2db22012-03-13 09:45:22 +0100926{
Jens Axboeb65c7ec2012-03-21 17:17:45 +0100927 double x = ix, y = iy;
928 struct prio_tree_iter iter;
929 struct prio_tree_node *n;
Jens Axboeb65c7ec2012-03-21 17:17:45 +0100930 struct graph_value *best = NULL;
Jens Axboecdae5ff2012-03-22 09:24:05 +0100931 struct flist_head *entry;
Jens Axboeb65c7ec2012-03-21 17:17:45 +0100932 double best_delta;
Jens Axboeb7a69ad2012-03-21 21:55:21 +0100933 double maxy, miny;
Jens Axboe93e2db22012-03-13 09:45:22 +0100934
Jens Axboeb65c7ec2012-03-21 17:17:45 +0100935 x -= g->xoffset;
936 y -= g->yoffset;
937
938 x = g->xtick_zero_val + ((x - g->xtick_zero) * g->xtick_delta);
939 y = g->ytick_zero_val + ((y - g->ytick_zero) * g->ytick_delta);
940
Jens Axboeb7a69ad2012-03-21 21:55:21 +0100941 x = x * 1000.0;
942 maxy = y + (g->ytick_one_val * TOOLTIP_DELTA);
943 miny = y - (g->ytick_one_val * TOOLTIP_DELTA);
Jens Axboeb65c7ec2012-03-21 17:17:45 +0100944 best_delta = UINT_MAX;
Jens Axboecdae5ff2012-03-22 09:24:05 +0100945 flist_for_each(entry, &g->label_list) {
946 struct graph_label *i;
947
948 i = flist_entry(entry, struct graph_label, list);
Jens Axboe01a947f2012-03-22 21:21:00 +0100949 if (i->hide)
950 continue;
Jens Axboecdae5ff2012-03-22 09:24:05 +0100951
Jens Axboe08d7d5a2012-03-21 19:33:32 +0100952 INIT_PRIO_TREE_ITER(&iter);
Jens Axboeb7a69ad2012-03-21 21:55:21 +0100953 prio_tree_iter_init(&iter, &i->prio_tree, x, x);
Jens Axboeb65c7ec2012-03-21 17:17:45 +0100954
955 n = prio_tree_next(&iter);
956 if (!n)
957 continue;
958
959 do {
Jens Axboecdae5ff2012-03-22 09:24:05 +0100960 struct graph_value *v, *rootv;
Jens Axboeb7a69ad2012-03-21 21:55:21 +0100961 double yval, ydiff;
Jens Axboeb65c7ec2012-03-21 17:17:45 +0100962
963 v = container_of(n, struct graph_value, node);
Jens Axboecdae5ff2012-03-22 09:24:05 +0100964 rootv = v;
965 do {
966 yval = gety(v);
967 ydiff = fabs(yval - y);
Jens Axboe93e2db22012-03-13 09:45:22 +0100968
Jens Axboecdae5ff2012-03-22 09:24:05 +0100969 /*
970 * zero delta, or within or match critera, break
971 */
972 if (ydiff < best_delta) {
973 best_delta = ydiff;
974 if (!best_delta ||
975 (yval >= miny && yval <= maxy)) {
976 best = v;
977 break;
978 }
Jens Axboeb65c7ec2012-03-21 17:17:45 +0100979 }
Jens Axboecdae5ff2012-03-22 09:24:05 +0100980 if (!flist_empty(&v->alias))
981 v = flist_entry(v->alias.next, struct graph_value, alias);
982 } while (v != rootv);
Jens Axboeb65c7ec2012-03-21 17:17:45 +0100983 } while ((n = prio_tree_next(&iter)) != NULL);
984
985 /*
986 * If we got matches in one label, don't check others.
987 */
Jens Axboed0db8192012-03-22 20:48:02 +0100988 if (best)
989 break;
Jens Axboecdae5ff2012-03-22 09:24:05 +0100990 }
Jens Axboeb65c7ec2012-03-21 17:17:45 +0100991
992 if (best)
993 return best->tooltip;
Jens Axboe93e2db22012-03-13 09:45:22 +0100994
995 return NULL;
996}
Jens Axboe01a947f2012-03-22 21:21:00 +0100997
998void graph_set_graph_all_zeroes(struct graph *g, unsigned int set)
999{
1000 g->dont_graph_all_zeroes = !set;
1001}