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