• Cedric Roux's avatar
    T: some work on XY plot · 5b6a83fc
    Cedric Roux authored
    - change paint function:
      when the XY plot is resized we scale. Previously
      the last range was put in the middle of the new range
      (say when we increase the size).
      We may get aspect ratio changes if the resize is not identical
      vertically and horizontally, but I think this behaviour is more
      'natural'.
    - fix a bug:
      the last horizontal tick label was printed to far on the right,
      out of the bouding box of the XY plot. This is not totally fixed
      in the case the label is larger than the XY plot. Now the part
      out of the bounding box will be printed on the left. No big deal,
      make the plot big enough. (Before, even if big enough you had a
      problem.)
    - add a new vertical tick display, to be used for throughput mostly.
      See in enb.c the difference between 'input signal' and throughput
      plots (those throughput plots will come in later commits).
    5b6a83fc
xy_plot.c 17 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
#include "gui.h"
#include "gui_defs.h"
#include "x.h"
#include "../utils.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

#if 0
/* this version behaves differently when you resize the XY plot. Say
 * you increase the size. The old (smaller) view is put at the center
 * of the new view. Everything inside keeps the same size/aspect ratio.
 * The other version below resizes the old view so that it fully occupies
 * the new view. It may introduce aspect ratio changes, but usage seems
 * to suggest it's a better behaviour.
 */
static void paint(gui *_gui, widget *_this)
{
  struct gui *g = _gui;
  struct xy_plot_widget *this = _this;
  int wanted_plot_width, allocated_plot_width;
  int wanted_plot_height, allocated_plot_height;
  float pxsize;
  float ticdist;
  float tic;
  float ticstep;
  int k, kmin, kmax;
  float allocated_xmin, allocated_xmax;
  float allocated_ymin, allocated_ymax;
  float center;
  int i;
  int n;

# define FLIP(v) (-(v) + allocated_plot_height-1)

  LOGD("PAINT xy plot xywh %d %d %d %d\n", this->common.x, this->common.y, this->common.width, this->common.height);

//x_draw_rectangle(g->x, g->xwin, 1, this->common.x, this->common.y, this->common.width, this->common.height);

  wanted_plot_width = this->wanted_width;
  allocated_plot_width = this->common.width - this->vrule_width;
  wanted_plot_height = this->wanted_height;
  allocated_plot_height = this->common.height - this->label_height * 2;

  /* plot zone */
  /* TODO: refine height - height of hrule text may be != from label */
  x_draw_rectangle(g->x, g->xwin, 1,
      this->common.x + this->vrule_width,
      this->common.y,
      this->common.width - this->vrule_width -1, /* -1 to see right border */
      this->common.height - this->label_height * 2);

  /* horizontal tics */
  pxsize = (this->xmax - this->xmin) / wanted_plot_width;
  ticdist = 100;
  tic = floor(log10(ticdist * pxsize));
  ticstep = powf(10, tic);
  center = (this->xmax + this->xmin) / 2;
  allocated_xmin = center - ((this->xmax - this->xmin) *
                             allocated_plot_width / wanted_plot_width) / 2;
  allocated_xmax = center + ((this->xmax - this->xmin) *
                             allocated_plot_width / wanted_plot_width) / 2;
  /* adjust tic if too tight */
  LOGD("pre x ticstep %g\n", ticstep);
  while (1) {
    if (ticstep / (allocated_xmax - allocated_xmin)
                * (allocated_plot_width - 1) > 40) break;
    ticstep *= 2;
  }
  LOGD("post x ticstep %g\n", ticstep);
  LOGD("xmin/max %g %g width wanted allocated %d %d alloc xmin/max %g %g ticstep %g\n", this->xmin, this->xmax, wanted_plot_width, allocated_plot_width, allocated_xmin, allocated_xmax, ticstep);
  kmin = ceil(allocated_xmin / ticstep);
  kmax = floor(allocated_xmax / ticstep);
  for (k = kmin; k <= kmax; k++) {
/*
    (k * ticstep - allocated_xmin) / (allocated_max - allocated_xmin) =
    (x - 0) / (allocated_plot_width-1 - 0)
 */
    char v[64];
    int vwidth, dummy;
    int x = (k * ticstep - allocated_xmin) /
            (allocated_xmax - allocated_xmin) *
            (allocated_plot_width - 1);
    x_draw_line(g->x, g->xwin, FOREGROUND_COLOR,
        this->common.x + this->vrule_width + x,
        this->common.y + this->common.height - this->label_height * 2,
        this->common.x + this->vrule_width + x,
        this->common.y + this->common.height - this->label_height * 2 - 5);
    sprintf(v, "%g", k * ticstep);
    x_text_get_dimensions(g->x, DEFAULT_FONT, v, &vwidth, &dummy, &dummy);
    x_draw_string(g->x, g->xwin, DEFAULT_FONT, FOREGROUND_COLOR,
        this->common.x + this->vrule_width + x - vwidth/2,
        this->common.y + this->common.height - this->label_height * 2 +
            this->label_baseline,
        v);
    LOGD("tic k %d val %g x %d\n", k, k * ticstep, x);
  }

  /* vertical tics */
  pxsize = (this->ymax - this->ymin) / wanted_plot_height;
  ticdist = 30;
  tic = floor(log10(ticdist * pxsize));
  ticstep = powf(10, tic);
  center = (this->ymax + this->ymin) / 2;
  allocated_ymin = center - ((this->ymax - this->ymin) *
                             allocated_plot_height / wanted_plot_height) / 2;
  allocated_ymax = center + ((this->ymax - this->ymin) *
                             allocated_plot_height / wanted_plot_height) / 2;
  /* adjust tic if too tight */
  LOGD("pre y ticstep %g\n", ticstep);
  while (1) {
    if (ticstep / (allocated_ymax - allocated_ymin)
                * (allocated_plot_height - 1) > 20) break;
    ticstep *= 2;
  }
  LOGD("post y ticstep %g\n", ticstep);
  LOGD("ymin/max %g %g height wanted allocated %d %d alloc ymin/max %g %g ticstep %g\n", this->ymin, this->ymax, wanted_plot_height, allocated_plot_height, allocated_ymin, allocated_ymax, ticstep);
  kmin = ceil(allocated_ymin / ticstep);
  kmax = floor(allocated_ymax / ticstep);
  for (k = kmin; k <= kmax; k++) {
    char v[64];
    int vwidth, dummy;
    int y = (k * ticstep - allocated_ymin) /
            (allocated_ymax - allocated_ymin) *
            (allocated_plot_height - 1);
    sprintf(v, "%g", k * ticstep);
    x_text_get_dimensions(g->x, DEFAULT_FONT, v, &vwidth, &dummy, &dummy);
    x_draw_line(g->x, g->xwin, FOREGROUND_COLOR,
        this->common.x + this->vrule_width,
        this->common.y + FLIP(y),
        this->common.x + this->vrule_width + 5,
        this->common.y + FLIP(y));
    x_draw_string(g->x, g->xwin, DEFAULT_FONT, FOREGROUND_COLOR,
        this->common.x + this->vrule_width - vwidth - 2,
        this->common.y + FLIP(y) - this->label_height/2+this->label_baseline,
        v);
  }

  /* label at bottom, in the middle */
  x_draw_string(g->x, g->xwin, DEFAULT_FONT, FOREGROUND_COLOR,
      this->common.x + (this->common.width - this->label_width) / 2,
      this->common.y + this->common.height - this->label_height
          + this->label_baseline,
      this->label);

  for (n = 0; n < this->nplots; n++) {
    /* points */
    float ax, bx, ay, by;
    ax = (allocated_plot_width-1) / (allocated_xmax - allocated_xmin);
    bx = -ax * allocated_xmin;
    ay = (allocated_plot_height-1) / (allocated_ymax - allocated_ymin);
    by = -ay * allocated_ymin;
    for (i = 0; i < this->plots[n].npoints; i++) {
      int x, y;
      x = ax * this->plots[n].x[i] + bx;
      y = ay * this->plots[n].y[i] + by;
      if (x >= 0 && x < allocated_plot_width &&
          y >= 0 && y < allocated_plot_height)
        x_add_point(g->x,
            this->common.x + this->vrule_width + x,
            this->common.y + FLIP(y));
    }
    x_plot_points(g->x, g->xwin, this->plots[n].color);
  }
}
#endif

static void paint(gui *_gui, widget *_this)
{
  struct gui *g = _gui;
  struct xy_plot_widget *this = _this;
  int allocated_plot_width;
  int allocated_plot_height;
  float pxsize;
  float ticdist;
  float tic;
  float ticstep;
  int k, kmin, kmax;
  float allocated_xmin, allocated_xmax;
  float allocated_ymin, allocated_ymax;
  int i;
  int n;
  char v[64];
  int vwidth, dummy;

# define FLIP(v) (-(v) + allocated_plot_height-1)

  LOGD("PAINT xy plot xywh %d %d %d %d\n", this->common.x, this->common.y, this->common.width, this->common.height);

//x_draw_rectangle(g->x, g->xwin, 1, this->common.x, this->common.y, this->common.width, this->common.height);

  allocated_plot_width = this->common.width - this->vrule_width;
  allocated_plot_height = this->common.height - this->label_height * 2;

  if (allocated_plot_width <= 1 || allocated_plot_height <= 1) {
    WARN("PAINT xy plot: width (%d) or height (%d) is wrong, not painting\n",
         this->common.width, this->common.height);
    return;
  }

  /* plot zone */
  /* TODO: refine height - height of hrule text may be != from label */
  x_draw_rectangle(g->x, g->xwin, 1,
      this->common.x + this->vrule_width,
      this->common.y,
      this->common.width - this->vrule_width -1, /* -1 to see right border */
      this->common.height - this->label_height * 2);

  /* horizontal tics */
  pxsize = (this->xmax - this->xmin) / allocated_plot_width;
  ticdist = 100;
  tic = floor(log10(ticdist * pxsize));
  ticstep = powf(10, tic);
  allocated_xmin = this->xmin;
  allocated_xmax = this->xmax;
  /* adjust tic if too tight */
  LOGD("pre x ticstep %g\n", ticstep);
  while (1) {
    if (ticstep / (allocated_xmax - allocated_xmin)
                * (allocated_plot_width - 1) > 40) break;
    ticstep *= 2;
  }
  LOGD("post x ticstep %g\n", ticstep);
  LOGD("xmin/max %g %g width allocated %d alloc xmin/max %g %g ticstep %g\n", this->xmin, this->xmax, allocated_plot_width, allocated_xmin, allocated_xmax, ticstep);
  kmin = ceil(allocated_xmin / ticstep);
  kmax = floor(allocated_xmax / ticstep);
  for (k = kmin; k <= kmax; k++) {
/*
    (k * ticstep - allocated_xmin) / (allocated_max - allocated_xmin) =
    (x - 0) / (allocated_plot_width-1 - 0)
 */
    int x = (k * ticstep - allocated_xmin) /
            (allocated_xmax - allocated_xmin) *
            (allocated_plot_width - 1);
    int px;
    x_draw_line(g->x, g->xwin, FOREGROUND_COLOR,
        this->common.x + this->vrule_width + x,
        this->common.y + this->common.height - this->label_height * 2,
        this->common.x + this->vrule_width + x,
        this->common.y + this->common.height - this->label_height * 2 - 5);
    sprintf(v, "%g", k * ticstep);
    x_text_get_dimensions(g->x, DEFAULT_FONT, v, &vwidth, &dummy, &dummy);

    /* do not draw after the widget ('width-1' for if we use 'width'
     * it is still off by 1 pixel for whatever reason) */
    px = this->vrule_width + x - vwidth/2;
    if (px + vwidth > this->common.width-1)
      px = this->common.width-1 - vwidth;

    x_draw_string(g->x, g->xwin, DEFAULT_FONT, FOREGROUND_COLOR,
        this->common.x + px,
        this->common.y + this->common.height - this->label_height * 2 +
            this->label_baseline,
        v);
    LOGD("tic k %d val %g x %d\n", k, k * ticstep, x);
  }

  /* vertical tics */
  allocated_ymin = this->ymin;
  allocated_ymax = this->ymax;
  switch (this->tick_type) {
  case XY_PLOT_DEFAULT_TICK:
    pxsize = (this->ymax - this->ymin) / allocated_plot_height;
    ticdist = 30;
    tic = floor(log10(ticdist * pxsize));
    ticstep = powf(10, tic);
    /* adjust tic if too tight */
    LOGD("pre y ticstep %g\n", ticstep);
    while (1) {
      if (ticstep / (allocated_ymax - allocated_ymin)
                  * (allocated_plot_height - 1) > 20) break;
      ticstep *= 2;
    }
    LOGD("post y ticstep %g\n", ticstep);
    LOGD("ymin/max %g %g height allocated %d alloc "
         "ymin/max %g %g ticstep %g\n", this->ymin, this->ymax,
         allocated_plot_height, allocated_ymin, allocated_ymax, ticstep);
    kmin = ceil(allocated_ymin / ticstep);
    kmax = floor(allocated_ymax / ticstep);
    for (k = kmin; k <= kmax; k++) {
      int y = (k * ticstep - allocated_ymin) /
              (allocated_ymax - allocated_ymin) *
              (allocated_plot_height - 1);
      sprintf(v, "%g", k * ticstep);
      x_text_get_dimensions(g->x, DEFAULT_FONT, v, &vwidth, &dummy, &dummy);
      x_draw_line(g->x, g->xwin, FOREGROUND_COLOR,
          this->common.x + this->vrule_width,
          this->common.y + FLIP(y),
          this->common.x + this->vrule_width + 5,
          this->common.y + FLIP(y));
      x_draw_string(g->x, g->xwin, DEFAULT_FONT, FOREGROUND_COLOR,
          this->common.x + this->vrule_width - vwidth - 2,
          this->common.y + FLIP(y)-this->label_height/2+this->label_baseline,
          v);
    }
    break;
  case XY_PLOT_SCROLL_TICK:
    /* print only max value, formatted using 'bps' for readability */
    bps(v, this->ymax, "");
    x_text_get_dimensions(g->x, DEFAULT_FONT, v, &vwidth, &dummy, &dummy);
    x_draw_string(g->x, g->xwin, DEFAULT_FONT, FOREGROUND_COLOR,
        this->common.x + this->vrule_width - vwidth - 2,
        this->common.y + +this->label_baseline,
        v);
    /* vertically divide into 5 */
    for (k = 1; k < 5; k++) {
      int y = round((k * (allocated_plot_height - 1)) / 5.);
      x_draw_line(g->x, g->xwin, FOREGROUND_COLOR,
          this->common.x + this->vrule_width,
          this->common.y + y,
          this->common.x + this->vrule_width + 5,
          this->common.y + y);
    }
    break;
  } /* swich (this->tick_type) */

  /* label at bottom, in the middle */
  x_draw_string(g->x, g->xwin, DEFAULT_FONT, FOREGROUND_COLOR,
      this->common.x + (this->common.width - this->label_width) / 2,
      this->common.y + this->common.height - this->label_height
          + this->label_baseline,
      this->label);

  for (n = 0; n < this->nplots; n++) {
    /* points */
    float ax, bx, ay, by;
    ax = (allocated_plot_width-1) / (allocated_xmax - allocated_xmin);
    bx = -ax * allocated_xmin;
    ay = (allocated_plot_height-1) / (allocated_ymax - allocated_ymin);
    by = -ay * allocated_ymin;
    for (i = 0; i < this->plots[n].npoints; i++) {
      int x, y;
      x = ax * this->plots[n].x[i] + bx;
      y = ay * this->plots[n].y[i] + by;
      if (x >= 0 && x < allocated_plot_width &&
          y >= 0 && y < allocated_plot_height)
        x_add_point(g->x,
            this->common.x + this->vrule_width + x,
            this->common.y + FLIP(y));
    }
    x_plot_points(g->x, g->xwin, this->plots[n].color);
  }
}

static void hints(gui *_gui, widget *_w, int *width, int *height)
{
  struct xy_plot_widget *w = _w;
  *width = w->wanted_width + w->vrule_width;
  *height = w->wanted_height + w->label_height * 2; /* TODO: refine */
  LOGD("HINTS xy plot wh %d %d (vrule_width %d) (wanted wh %d %d)\n", *width, *height, w->vrule_width, w->wanted_width, w->wanted_height);
}

widget *new_xy_plot(gui *_gui, int width, int height, char *label,
    int vruler_width)
{
  struct gui *g = _gui;
  struct xy_plot_widget *w;

  glock(g);

  w = new_widget(g, XY_PLOT, sizeof(struct xy_plot_widget));

  w->label = strdup(label); if (w->label == NULL) OOM;
  /* TODO: be sure calling X there is valid wrt "global model" (we are
   * not in the "gui thread") */
  x_text_get_dimensions(g->x, DEFAULT_FONT, label,
      &w->label_width, &w->label_height, &w->label_baseline);
  LOGD("XY PLOT label wh %d %d\n", w->label_width, w->label_height);

  w->wanted_width = width;
  w->wanted_height = height;
  w->vrule_width = vruler_width;

  w->xmin = -1;
  w->xmax = 1;
  w->ymin = -1;
  w->ymax = 1;
  w->plots = NULL;
  w->nplots = 0;
  w->tick_type = XY_PLOT_DEFAULT_TICK;

  w->common.paint = paint;
  w->common.hints = hints;

  gunlock(g);

  return w;
}

/*************************************************************************/
/*                           public functions                            */
/*************************************************************************/

int xy_plot_new_plot(gui *_gui, widget *_this, int color)
{
  int ret;
  struct gui *g = _gui;
  struct xy_plot_widget *this = _this;

  glock(g);

  ret = this->nplots;

  this->nplots++;
  this->plots = realloc(this->plots,
      this->nplots * sizeof(struct xy_plot_plot));
  if (this->plots == NULL) abort();

  this->plots[ret].x = NULL;
  this->plots[ret].y = NULL;
  this->plots[ret].npoints = 0;
  this->plots[ret].color = color;

  gunlock(g);

  return ret;
}

void xy_plot_set_range(gui *_gui, widget *_this,
    float xmin, float xmax, float ymin, float ymax)
{
  struct gui *g = _gui;
  struct xy_plot_widget *this = _this;

  glock(g);

  this->xmin = xmin;
  this->xmax = xmax;
  this->ymin = ymin;
  this->ymax = ymax;

  send_event(g, DIRTY, this->common.id);

  gunlock(g);
}

void xy_plot_set_points(gui *_gui, widget *_this, int plot,
    int npoints, float *x, float *y)
{
  struct gui *g = _gui;
  struct xy_plot_widget *this = _this;

  glock(g);

  if (npoints != this->plots[plot].npoints) {
    free(this->plots[plot].x);
    free(this->plots[plot].y);
    this->plots[plot].x = calloc(npoints, sizeof(float));
    if (this->plots[plot].x == NULL) abort();
    this->plots[plot].y = calloc(npoints, sizeof(float));
    if (this->plots[plot].y == NULL) abort();
    this->plots[plot].npoints = npoints;
  }

  memcpy(this->plots[plot].x, x, npoints * sizeof(float));
  memcpy(this->plots[plot].y, y, npoints * sizeof(float));

  send_event(g, DIRTY, this->common.id);

  gunlock(g);
}

void xy_plot_get_dimensions(gui *_gui, widget *_this, int *width, int *height)
{
  struct gui *g = _gui;
  struct xy_plot_widget *this = _this;

  glock(g);

  if (this->common.width == 0 || this->common.height == 0) {
    *width = this->wanted_width;
    *height = this->wanted_height;
  } else {
    *width = this->common.width - this->vrule_width;
    *height = this->common.height - this->label_height * 2;
  }

  gunlock(g);
}

void xy_plot_set_title(gui *_gui, widget *_this, char *label)
{
  struct gui *g = _gui;
  struct xy_plot_widget *this = _this;

  glock(g);

  free(this->label);
  this->label = strdup(label); if (this->label == NULL) OOM;
  /* TODO: be sure calling X there is valid wrt "global model" (we are
   * not in the "gui thread") */
  x_text_get_dimensions(g->x, DEFAULT_FONT, label,
      &this->label_width, &this->label_height, &this->label_baseline);

  send_event(g, REPACK, this->common.id);

  gunlock(g);
}

void xy_plot_set_tick_type(gui *_gui, widget *_this, int type)
{
  struct gui *g = _gui;
  struct xy_plot_widget *this = _this;

  glock(g);

  this->tick_type = type;

  send_event(g, DIRTY, this->common.id);

  gunlock(g);
}