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