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> | |
27 | ||
28 | #include <cairo.h> | |
29 | #include <gtk/gtk.h> | |
30 | ||
31 | #include "tickmarks.h" | |
09ad20ff | 32 | #include "graph.h" |
af58ef32 SC |
33 | |
34 | struct xyvalue { | |
35 | double x, y; | |
36 | }; | |
37 | ||
38 | struct graph_value { | |
39 | struct graph_value *next; | |
40 | void *value; | |
41 | }; | |
42 | ||
43 | struct graph_label { | |
44 | char *label; | |
45 | struct graph_value *tail; | |
46 | struct graph_value *values; | |
47 | struct graph_label *next; | |
48 | double r, g, b; | |
49 | int value_count; | |
50 | struct graph *parent; | |
51 | }; | |
52 | ||
53 | struct graph { | |
54 | char *title; | |
55 | char *xtitle; | |
56 | char *ytitle; | |
87d5f276 | 57 | unsigned int xdim, ydim; |
57f9d28e | 58 | double xoffset, yoffset; |
af58ef32 SC |
59 | struct graph_label *labels; |
60 | struct graph_label *tail; | |
61 | int per_label_limit; | |
f3e8440f | 62 | const char *font; |
7175d91d SC |
63 | graph_axis_unit_change_callback x_axis_unit_change_callback; |
64 | graph_axis_unit_change_callback y_axis_unit_change_callback; | |
af58ef32 SC |
65 | }; |
66 | ||
3ea48b88 SC |
67 | void graph_set_size(struct graph *g, unsigned int xdim, unsigned int ydim) |
68 | { | |
69 | g->xdim = xdim; | |
70 | g->ydim = ydim; | |
71 | } | |
72 | ||
57f9d28e SC |
73 | void graph_set_position(struct graph *g, double xoffset, double yoffset) |
74 | { | |
75 | g->xoffset = xoffset; | |
76 | g->yoffset = yoffset; | |
77 | } | |
78 | ||
f3e8440f | 79 | struct graph *graph_new(unsigned int xdim, unsigned int ydim, const char *font) |
af58ef32 SC |
80 | { |
81 | struct graph *g; | |
82 | ||
83 | g = calloc(1, sizeof(*g)); | |
3ea48b88 | 84 | graph_set_size(g, xdim, ydim); |
af58ef32 | 85 | g->per_label_limit = -1; |
f3e8440f JA |
86 | g->font = font; |
87 | if (!g->font) | |
88 | g->font = "Sans"; | |
af58ef32 SC |
89 | return g; |
90 | } | |
91 | ||
7175d91d SC |
92 | void graph_x_axis_unit_change_notify(struct graph *g, graph_axis_unit_change_callback f) |
93 | { | |
94 | g->x_axis_unit_change_callback = f; | |
95 | } | |
96 | ||
97 | void graph_y_axis_unit_change_notify(struct graph *g, graph_axis_unit_change_callback f) | |
98 | { | |
99 | g->y_axis_unit_change_callback = f; | |
100 | } | |
101 | ||
af58ef32 SC |
102 | static int count_labels(struct graph_label *labels) |
103 | { | |
104 | int count = 0; | |
105 | struct graph_label *i; | |
106 | ||
107 | for (i = labels; i; i = i->next) | |
108 | count++; | |
109 | return count; | |
110 | } | |
111 | ||
112 | static int count_values(struct graph_value *values) | |
113 | { | |
114 | int count = 0; | |
115 | struct graph_value *i; | |
116 | ||
117 | for (i = values; i; i = i->next) | |
118 | count++; | |
119 | return count; | |
120 | } | |
121 | ||
122 | typedef double (*double_comparator)(double a, double b); | |
123 | ||
124 | static double mindouble(double a, double b) | |
125 | { | |
126 | return a < b ? a : b; | |
127 | } | |
128 | ||
129 | static double maxdouble(double a, double b) | |
130 | { | |
131 | return a < b ? b : a; | |
132 | } | |
133 | ||
134 | static double find_double_values(struct graph_value *values, double_comparator cmp) | |
135 | { | |
136 | struct graph_value *i; | |
137 | int first = 1; | |
138 | double answer, tmp; | |
139 | ||
140 | assert(values != NULL); | |
141 | answer = 0.0; /* shut the compiler up, might need to think harder though. */ | |
142 | for (i = values; i; i = i->next) { | |
143 | tmp = *(double *) i->value; | |
144 | if (first) { | |
145 | answer = tmp; | |
146 | first = 0; | |
147 | } else { | |
148 | answer = cmp(answer, tmp); | |
149 | } | |
150 | } | |
151 | return answer; | |
152 | } | |
153 | ||
154 | static double find_double_data(struct graph_label *labels, double_comparator cmp) | |
155 | { | |
156 | struct graph_label *i; | |
157 | int first = 1; | |
158 | double answer, tmp; | |
159 | ||
160 | assert(labels != NULL); | |
161 | answer = 0.0; /* shut the compiler up, might need to think harder though. */ | |
162 | for (i = labels; i; i = i->next) { | |
163 | tmp = find_double_values(i->values, cmp); | |
164 | if (first) { | |
165 | answer = tmp; | |
166 | first = 0; | |
167 | } else { | |
168 | answer = cmp(tmp, answer); | |
169 | } | |
170 | } | |
171 | return answer; | |
172 | } | |
173 | ||
174 | static double find_min_data(struct graph_label *labels) | |
175 | { | |
176 | return find_double_data(labels, mindouble); | |
177 | } | |
178 | ||
179 | static double find_max_data(struct graph_label *labels) | |
180 | { | |
181 | return find_double_data(labels, maxdouble); | |
182 | } | |
183 | ||
184 | static void draw_bars(struct graph *bg, cairo_t *cr, struct graph_label *lb, | |
185 | double label_offset, double bar_width, | |
186 | double mindata, double maxdata) | |
187 | { | |
188 | struct graph_value *i; | |
189 | double x1, y1, x2, y2; | |
190 | int bar_num = 0; | |
191 | double domain, range, v; | |
192 | ||
193 | domain = (maxdata - mindata); | |
194 | range = (double) bg->ydim * 0.80; /* FIXME */ | |
195 | cairo_stroke(cr); | |
196 | for (i = lb->values; i; i = i->next) { | |
197 | ||
198 | x1 = label_offset + (double) bar_num * bar_width + (bar_width * 0.05); | |
199 | x2 = x1 + bar_width * 0.90; | |
200 | y2 = bg->ydim * 0.90; | |
201 | v = *(double *) i->value; | |
202 | y1 = y2 - (((v - mindata) / domain) * range); | |
203 | cairo_move_to(cr, x1, y1); | |
204 | cairo_line_to(cr, x1, y2); | |
205 | cairo_line_to(cr, x2, y2); | |
206 | cairo_line_to(cr, x2, y1); | |
207 | cairo_close_path(cr); | |
208 | cairo_fill(cr); | |
209 | cairo_stroke(cr); | |
210 | bar_num++; | |
211 | } | |
212 | } | |
213 | ||
10e54cdc SC |
214 | static void draw_aligned_text(struct graph *g, cairo_t *cr, double x, double y, |
215 | double fontsize, const char *text, int alignment) | |
af58ef32 | 216 | { |
10e54cdc SC |
217 | #define CENTERED 0 |
218 | #define LEFT_JUSTIFIED 1 | |
219 | #define RIGHT_JUSTIFIED 2 | |
220 | ||
221 | double factor, direction; | |
af58ef32 SC |
222 | cairo_text_extents_t extents; |
223 | ||
10e54cdc SC |
224 | switch(alignment) { |
225 | case CENTERED: | |
226 | direction = -1.0; | |
227 | factor = 0.5; | |
228 | break; | |
229 | case RIGHT_JUSTIFIED: | |
230 | direction = -1.0; | |
231 | factor = 1.0; | |
232 | break; | |
233 | case LEFT_JUSTIFIED: | |
234 | default: | |
235 | direction = 1.0; | |
236 | factor = 1.0; | |
237 | break; | |
238 | } | |
f3e8440f | 239 | cairo_select_font_face (cr, g->font, CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL); |
af58ef32 SC |
240 | |
241 | cairo_set_font_size(cr, fontsize); | |
242 | cairo_text_extents(cr, text, &extents); | |
10e54cdc | 243 | x = x + direction * (factor * extents.width + extents.x_bearing); |
af58ef32 SC |
244 | y = y - (extents.height / 2 + extents.y_bearing); |
245 | ||
246 | cairo_move_to(cr, x, y); | |
247 | cairo_show_text(cr, text); | |
248 | } | |
249 | ||
10e54cdc SC |
250 | static inline void draw_centered_text(struct graph *g, cairo_t *cr, double x, double y, |
251 | double fontsize, const char *text) | |
252 | { | |
253 | draw_aligned_text(g, cr, x, y, fontsize, text, CENTERED); | |
254 | } | |
255 | ||
256 | static inline void draw_right_justified_text(struct graph *g, cairo_t *cr, | |
257 | double x, double y, | |
258 | double fontsize, const char *text) | |
259 | { | |
260 | draw_aligned_text(g, cr, x, y, fontsize, text, RIGHT_JUSTIFIED); | |
261 | } | |
262 | ||
263 | static inline void draw_left_justified_text(struct graph *g, cairo_t *cr, | |
264 | double x, double y, | |
265 | double fontsize, const char *text) | |
266 | { | |
267 | draw_aligned_text(g, cr, x, y, fontsize, text, LEFT_JUSTIFIED); | |
268 | } | |
269 | ||
f3e8440f JA |
270 | static void draw_vertical_centered_text(struct graph *g, cairo_t *cr, double x, |
271 | double y, double fontsize, | |
272 | const char *text) | |
af58ef32 SC |
273 | { |
274 | double sx, sy; | |
275 | cairo_text_extents_t extents; | |
276 | ||
f3e8440f | 277 | cairo_select_font_face(cr, g->font, CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL); |
af58ef32 SC |
278 | |
279 | cairo_set_font_size(cr, fontsize); | |
280 | cairo_text_extents(cr, text, &extents); | |
281 | sx = x; | |
282 | sy = y; | |
283 | y = y + (extents.width / 2.0 + extents.x_bearing); | |
284 | x = x - (extents.height / 2.0 + extents.y_bearing); | |
285 | ||
286 | cairo_move_to(cr, x, y); | |
287 | cairo_save(cr); | |
288 | cairo_translate(cr, -sx, -sy); | |
289 | cairo_rotate(cr, -90.0 * M_PI / 180.0); | |
290 | cairo_translate(cr, sx, sy); | |
291 | cairo_show_text(cr, text); | |
292 | cairo_restore(cr); | |
293 | } | |
294 | ||
295 | static void graph_draw_common(struct graph *g, cairo_t *cr, | |
296 | double *x1, double *y1, double *x2, double *y2) | |
297 | { | |
298 | cairo_set_source_rgb(cr, 0, 0, 0); | |
f3e8440f | 299 | cairo_set_line_width (cr, 0.8); |
af58ef32 | 300 | |
6bf86008 SC |
301 | *x1 = 0.15 * g->xdim; |
302 | *x2 = 0.95 * g->xdim; | |
303 | *y1 = 0.10 * g->ydim; | |
304 | *y2 = 0.90 * g->ydim; | |
af58ef32 SC |
305 | |
306 | cairo_move_to(cr, *x1, *y1); | |
307 | cairo_line_to(cr, *x1, *y2); | |
308 | cairo_line_to(cr, *x2, *y2); | |
309 | cairo_line_to(cr, *x2, *y1); | |
310 | cairo_line_to(cr, *x1, *y1); | |
311 | cairo_stroke(cr); | |
312 | ||
f3e8440f JA |
313 | draw_centered_text(g, cr, g->xdim / 2, g->ydim / 20, 20.0, g->title); |
314 | draw_centered_text(g, cr, g->xdim / 2, g->ydim * 0.97, 14.0, g->xtitle); | |
315 | draw_vertical_centered_text(g, cr, g->xdim * 0.02, g->ydim / 2, 14.0, g->ytitle); | |
af58ef32 SC |
316 | cairo_stroke(cr); |
317 | } | |
318 | ||
319 | static void graph_draw_x_ticks(struct graph *g, cairo_t *cr, | |
320 | double x1, double y1, double x2, double y2, | |
321 | double minx, double maxx, int nticks) | |
322 | { | |
323 | struct tickmark *tm; | |
324 | double tx; | |
7175d91d | 325 | int i, power_of_ten; |
af58ef32 SC |
326 | static double dash[] = { 1.0, 2.0 }; |
327 | ||
7175d91d SC |
328 | nticks = calc_tickmarks(minx, maxx, nticks, &tm, &power_of_ten, |
329 | g->x_axis_unit_change_callback == NULL); | |
330 | if (g->x_axis_unit_change_callback) | |
331 | g->x_axis_unit_change_callback(g, power_of_ten); | |
af58ef32 SC |
332 | |
333 | for (i = 0; i < nticks; i++) { | |
334 | tx = (((tm[i].value) - minx) / (maxx - minx)) * (x2 - x1) + x1; | |
335 | if (tx < x1 || tx > x2) | |
336 | continue; | |
337 | ||
338 | /* Draw tick mark */ | |
f3e8440f | 339 | cairo_set_line_width(cr, 0.8); |
af58ef32 SC |
340 | cairo_move_to(cr, tx, y2); |
341 | cairo_line_to(cr, tx, y2 + (y2 - y1) * 0.03); | |
342 | cairo_stroke(cr); | |
343 | ||
344 | /* draw grid lines */ | |
345 | cairo_save(cr); | |
346 | cairo_set_dash(cr, dash, 2, 2.0); | |
347 | cairo_set_line_width(cr, 0.5); | |
348 | cairo_move_to(cr, tx, y1); | |
349 | cairo_line_to(cr, tx, y2); | |
350 | cairo_stroke(cr); | |
351 | cairo_restore(cr); | |
352 | ||
353 | /* draw tickmark label */ | |
f3e8440f | 354 | draw_centered_text(g, cr, tx, y2 * 1.04, 12.0, tm[i].string); |
af58ef32 SC |
355 | cairo_stroke(cr); |
356 | ||
357 | } | |
358 | } | |
359 | ||
360 | static void graph_draw_y_ticks(struct graph *g, cairo_t *cr, | |
361 | double x1, double y1, double x2, double y2, | |
362 | double miny, double maxy, int nticks) | |
363 | { | |
364 | struct tickmark *tm; | |
365 | double ty; | |
7175d91d | 366 | int i, power_of_ten; |
af58ef32 SC |
367 | static double dash[] = { 2.0, 2.0 }; |
368 | ||
7175d91d SC |
369 | nticks = calc_tickmarks(miny, maxy, nticks, &tm, &power_of_ten, |
370 | g->y_axis_unit_change_callback == NULL); | |
371 | if (g->y_axis_unit_change_callback) | |
372 | g->y_axis_unit_change_callback(g, power_of_ten); | |
af58ef32 SC |
373 | |
374 | for (i = 0; i < nticks; i++) { | |
375 | ty = y2 - (((tm[i].value) - miny) / (maxy - miny)) * (y2 - y1); | |
376 | if (ty < y1 || ty > y2) | |
377 | continue; | |
378 | /* draw tick mark */ | |
379 | cairo_move_to(cr, x1, ty); | |
380 | cairo_line_to(cr, x1 - (x2 - x1) * 0.02, ty); | |
381 | cairo_stroke(cr); | |
382 | ||
383 | /* draw grid lines */ | |
384 | cairo_save(cr); | |
385 | cairo_set_dash(cr, dash, 2, 2.0); | |
386 | cairo_set_line_width(cr, 0.5); | |
387 | cairo_move_to(cr, x1, ty); | |
388 | cairo_line_to(cr, x2, ty); | |
389 | cairo_stroke(cr); | |
390 | cairo_restore(cr); | |
391 | ||
392 | /* draw tickmark label */ | |
10e54cdc | 393 | draw_right_justified_text(g, cr, x1 - (x2 - x1) * 0.025, ty, 12.0, tm[i].string); |
af58ef32 SC |
394 | cairo_stroke(cr); |
395 | } | |
396 | } | |
397 | ||
398 | void bar_graph_draw(struct graph *bg, cairo_t *cr) | |
399 | { | |
400 | double x1, y1, x2, y2; | |
401 | double space_per_label, bar_width; | |
402 | double label_offset, mindata, maxdata; | |
403 | int i, nlabels; | |
404 | struct graph_label *lb; | |
405 | ||
406 | cairo_save(cr); | |
57f9d28e | 407 | cairo_translate(cr, bg->xoffset, bg->yoffset); |
af58ef32 SC |
408 | graph_draw_common(bg, cr, &x1, &y1, &x2, &y2); |
409 | ||
410 | nlabels = count_labels(bg->labels); | |
411 | space_per_label = (x2 - x1) / (double) nlabels; | |
412 | ||
413 | mindata = find_min_data(bg->labels); | |
414 | maxdata = find_max_data(bg->labels); | |
415 | ||
416 | if (fabs(maxdata - mindata) < 1e-20) { | |
f3e8440f | 417 | draw_centered_text(bg, cr, |
af58ef32 SC |
418 | x1 + (x2 - x1) / 2.0, |
419 | y1 + (y2 - y1) / 2.0, 20.0, "No good data"); | |
420 | return; | |
421 | } | |
422 | ||
423 | graph_draw_y_ticks(bg, cr, x1, y1, x2, y2, mindata, maxdata, 10); | |
424 | ||
425 | i = 0; | |
426 | for (lb = bg->labels; lb; lb = lb->next) { | |
427 | int nvalues; | |
428 | nvalues = count_values(lb->values); | |
429 | bar_width = (space_per_label - space_per_label * 0.2) / (double) nvalues; | |
430 | label_offset = bg->xdim * 0.1 + space_per_label * (double) i + space_per_label * 0.1; | |
431 | draw_bars(bg, cr, lb, label_offset, bar_width, mindata, maxdata); | |
432 | // draw_centered_text(cr, label_offset + (bar_width / 2.0 + bar_width * 0.1), bg->ydim * 0.93, | |
f3e8440f | 433 | draw_centered_text(bg, cr, x1 + space_per_label * (i + 0.5), bg->ydim * 0.93, |
af58ef32 SC |
434 | 12.0, lb->label); |
435 | i++; | |
436 | } | |
437 | cairo_stroke(cr); | |
438 | cairo_restore(cr); | |
439 | } | |
440 | ||
441 | typedef double (*xy_value_extractor)(struct graph_value *v); | |
442 | ||
443 | static double getx(struct graph_value *v) | |
444 | { | |
445 | struct xyvalue *xy = v->value; | |
446 | return xy->x; | |
447 | } | |
448 | ||
449 | static double gety(struct graph_value *v) | |
450 | { | |
451 | struct xyvalue *xy = v->value; | |
452 | return xy->y; | |
453 | } | |
454 | ||
455 | static double find_xy_value(struct graph *g, xy_value_extractor getvalue, double_comparator cmp) | |
456 | { | |
10e54cdc | 457 | double tmp, answer = 0.0; |
af58ef32 SC |
458 | struct graph_label *i; |
459 | struct graph_value *j; | |
d582bf70 | 460 | int first = 1; |
af58ef32 SC |
461 | |
462 | for (i = g->labels; i; i = i->next) | |
463 | for (j = i->values; j; j = j->next) { | |
464 | tmp = getvalue(j); | |
d582bf70 SC |
465 | if (first) { |
466 | first = 0; | |
467 | answer = tmp; | |
468 | } | |
af58ef32 SC |
469 | answer = cmp(tmp, answer); |
470 | } | |
471 | return answer; | |
472 | } | |
473 | ||
474 | void line_graph_draw(struct graph *g, cairo_t *cr) | |
475 | { | |
476 | double x1, y1, x2, y2; | |
477 | double minx, miny, maxx, maxy; | |
478 | double tx, ty; | |
479 | struct graph_label *i; | |
480 | struct graph_value *j; | |
9ce9cfbd | 481 | int good_data = 1, first = 1; |
af58ef32 SC |
482 | |
483 | cairo_save(cr); | |
57f9d28e | 484 | cairo_translate(cr, g->xoffset, g->yoffset); |
af58ef32 SC |
485 | graph_draw_common(g, cr, &x1, &y1, &x2, &y2); |
486 | ||
487 | minx = find_xy_value(g, getx, mindouble); | |
488 | maxx = find_xy_value(g, getx, maxdouble); | |
489 | miny = find_xy_value(g, gety, mindouble); | |
490 | maxy = find_xy_value(g, gety, maxdouble); | |
491 | ||
492 | if (fabs(maxx - minx) < 1e-20 || fabs(maxy - miny) < 1e-20) { | |
9ce9cfbd SC |
493 | good_data = 0; |
494 | minx = 0.0; | |
495 | miny = 0.0; | |
496 | maxx = 10.0; | |
497 | maxy = 100.0; | |
af58ef32 SC |
498 | } |
499 | ||
500 | graph_draw_x_ticks(g, cr, x1, y1, x2, y2, minx, maxx, 10); | |
501 | graph_draw_y_ticks(g, cr, x1, y1, x2, y2, miny, maxy, 10); | |
502 | ||
9ce9cfbd SC |
503 | if (!good_data) |
504 | goto skip_data; | |
505 | ||
f3e8440f | 506 | cairo_set_line_width(cr, 1.5); |
af58ef32 SC |
507 | for (i = g->labels; i; i = i->next) { |
508 | first = 1; | |
cae08727 SC |
509 | if (i->r < 0) /* invisible data */ |
510 | continue; | |
af58ef32 SC |
511 | cairo_set_source_rgb(cr, i->r, i->g, i->b); |
512 | for (j = i->values; j; j = j->next) { | |
513 | tx = ((getx(j) - minx) / (maxx - minx)) * (x2 - x1) + x1; | |
514 | ty = y2 - ((gety(j) - miny) / (maxy - miny)) * (y2 - y1); | |
515 | if (first) { | |
516 | cairo_move_to(cr, tx, ty); | |
517 | first = 0; | |
518 | } else { | |
519 | cairo_line_to(cr, tx, ty); | |
520 | } | |
521 | } | |
522 | cairo_stroke(cr); | |
523 | } | |
9ce9cfbd SC |
524 | |
525 | skip_data: | |
af58ef32 | 526 | cairo_restore(cr); |
9ce9cfbd | 527 | |
af58ef32 SC |
528 | } |
529 | ||
530 | static void gfree(void *f) | |
531 | { | |
532 | if (f) | |
533 | free(f); | |
534 | } | |
535 | ||
536 | static void setstring(char **str, const char *value) | |
537 | { | |
538 | gfree(*str); | |
539 | *str = strdup(value); | |
540 | } | |
541 | ||
542 | void graph_title(struct graph *bg, const char *title) | |
543 | { | |
544 | setstring(&bg->title, title); | |
545 | } | |
546 | ||
547 | void graph_x_title(struct graph *bg, const char *title) | |
548 | { | |
549 | setstring(&bg->xtitle, title); | |
550 | } | |
551 | ||
552 | void graph_y_title(struct graph *bg, const char *title) | |
553 | { | |
554 | setstring(&bg->ytitle, title); | |
555 | } | |
556 | ||
557 | static struct graph_label *graph_find_label(struct graph *bg, | |
558 | const char *label) | |
559 | { | |
560 | struct graph_label *i; | |
561 | ||
562 | for (i = bg->labels; i; i = i->next) | |
563 | if (strcmp(label, i->label) == 0) | |
564 | return i; | |
565 | return NULL; | |
566 | } | |
567 | ||
568 | void graph_add_label(struct graph *bg, const char *label) | |
569 | { | |
570 | struct graph_label *i; | |
571 | ||
572 | i = graph_find_label(bg, label); | |
573 | if (i) | |
574 | return; /* already present. */ | |
575 | i = calloc(1, sizeof(*i)); | |
576 | i->parent = bg; | |
577 | setstring(&i->label, label); | |
578 | i->next = NULL; | |
579 | if (!bg->tail) | |
580 | bg->labels = i; | |
581 | else | |
582 | bg->tail->next = i; | |
583 | bg->tail = i; | |
584 | } | |
585 | ||
586 | static void graph_label_add_value(struct graph_label *i, void *value) | |
587 | { | |
588 | struct graph_value *x; | |
589 | ||
590 | x = malloc(sizeof(*x)); | |
591 | x->value = value; | |
592 | x->next = NULL; | |
593 | if (!i->tail) { | |
594 | i->values = x; | |
595 | } else { | |
596 | i->tail->next = x; | |
597 | } | |
598 | i->tail = x; | |
599 | i->value_count++; | |
600 | ||
601 | if (i->parent->per_label_limit != -1 && | |
602 | i->value_count > i->parent->per_label_limit) { | |
603 | x = i->values; | |
604 | i->values = i->values->next; | |
605 | free(x->value); | |
606 | free(x); | |
607 | i->value_count--; | |
608 | } | |
609 | } | |
610 | ||
611 | int graph_add_data(struct graph *bg, const char *label, const double value) | |
612 | { | |
613 | struct graph_label *i; | |
614 | double *d; | |
615 | ||
616 | d = malloc(sizeof(*d)); | |
617 | *d = value; | |
618 | ||
619 | i = graph_find_label(bg, label); | |
620 | if (!i) | |
621 | return -1; | |
622 | graph_label_add_value(i, d); | |
623 | return 0; | |
624 | } | |
625 | ||
626 | int graph_add_xy_data(struct graph *bg, const char *label, | |
627 | const double x, const double y) | |
628 | { | |
629 | struct graph_label *i; | |
630 | struct xyvalue *xy; | |
631 | ||
632 | xy = malloc(sizeof(*xy)); | |
633 | xy->x = x; | |
634 | xy->y = y; | |
635 | ||
636 | i = graph_find_label(bg, label); | |
637 | if (!i) | |
638 | return -1; | |
639 | graph_label_add_value(i, xy); | |
640 | return 0; | |
641 | } | |
642 | ||
643 | static void graph_free_values(struct graph_value *values) | |
644 | { | |
645 | struct graph_value *i, *next; | |
646 | ||
647 | for (i = values; i; i = next) { | |
648 | next = i->next; | |
649 | gfree(i->value); | |
650 | gfree(i); | |
651 | } | |
652 | } | |
653 | ||
654 | static void graph_free_labels(struct graph_label *labels) | |
655 | { | |
656 | struct graph_label *i, *next; | |
657 | ||
658 | for (i = labels; i; i = next) { | |
659 | next = i->next; | |
660 | graph_free_values(i->values); | |
661 | gfree(i); | |
662 | } | |
663 | } | |
664 | ||
665 | void graph_set_color(struct graph *gr, const char *label, | |
666 | double red, double green, double blue) | |
667 | { | |
668 | struct graph_label *i; | |
669 | double r, g, b; | |
670 | ||
cae08727 SC |
671 | if (red < 0.0) { /* invisible color */ |
672 | r = -1.0; | |
673 | g = -1.0; | |
674 | b = -1.0; | |
675 | } else { | |
676 | r = fabs(red); | |
677 | g = fabs(green); | |
678 | b = fabs(blue); | |
679 | ||
680 | if (r > 1.0) | |
681 | r = 1.0; | |
682 | if (g > 1.0) | |
683 | g = 1.0; | |
684 | if (b > 1.0) | |
685 | b =1.0; | |
686 | } | |
af58ef32 SC |
687 | |
688 | for (i = gr->labels; i; i = i->next) | |
689 | if (strcmp(i->label, label) == 0) { | |
690 | i->r = r; | |
691 | i->g = g; | |
692 | i->b = b; | |
693 | break; | |
694 | } | |
695 | } | |
696 | ||
697 | void graph_free(struct graph *bg) | |
698 | { | |
699 | gfree(bg->title); | |
700 | gfree(bg->xtitle); | |
701 | gfree(bg->ytitle); | |
702 | graph_free_labels(bg->labels); | |
703 | } | |
704 | ||
705 | /* For each line in the line graph, up to per_label_limit segments may | |
706 | * be added. After that, adding more data to the end of the line | |
707 | * causes data to drop off of the front of the line. | |
708 | */ | |
709 | void line_graph_set_data_count_limit(struct graph *g, int per_label_limit) | |
710 | { | |
711 | g->per_label_limit = per_label_limit; | |
712 | } | |
713 |