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