ValueHistory.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #include "ValueHistory.h"
  2. #include "imgui.h"
  3. #include <string.h>
  4. #include <stdio.h>
  5. #ifdef WIN32
  6. # define snprintf _snprintf
  7. #endif
  8. ValueHistory::ValueHistory() :
  9. m_hsamples(0)
  10. {
  11. for (int i = 0; i < MAX_HISTORY; ++i)
  12. m_samples[i] = 0;
  13. }
  14. float ValueHistory::getSampleMin() const
  15. {
  16. float val = m_samples[0];
  17. for (int i = 1; i < MAX_HISTORY; ++i)
  18. if (m_samples[i] < val)
  19. val = m_samples[i];
  20. return val;
  21. }
  22. float ValueHistory::getSampleMax() const
  23. {
  24. float val = m_samples[0];
  25. for (int i = 1; i < MAX_HISTORY; ++i)
  26. if (m_samples[i] > val)
  27. val = m_samples[i];
  28. return val;
  29. }
  30. float ValueHistory::getAverage() const
  31. {
  32. float val = 0;
  33. for (int i = 0; i < MAX_HISTORY; ++i)
  34. val += m_samples[i];
  35. return val/(float)MAX_HISTORY;
  36. }
  37. void GraphParams::setRect(int ix, int iy, int iw, int ih, int ipad)
  38. {
  39. x = ix;
  40. y = iy;
  41. w = iw;
  42. h = ih;
  43. pad = ipad;
  44. }
  45. void GraphParams::setValueRange(float ivmin, float ivmax, int indiv, const char* iunits)
  46. {
  47. vmin = ivmin;
  48. vmax = ivmax;
  49. ndiv = indiv;
  50. strcpy(units, iunits);
  51. }
  52. void drawGraphBackground(const GraphParams* p)
  53. {
  54. // BG
  55. imguiDrawRoundedRect((float)p->x, (float)p->y, (float)p->w, (float)p->h, (float)p->pad, imguiRGBA(64,64,64,128));
  56. const float sy = (p->h-p->pad*2) / (p->vmax-p->vmin);
  57. const float oy = p->y+p->pad-p->vmin*sy;
  58. char text[64];
  59. // Divider Lines
  60. for (int i = 0; i <= p->ndiv; ++i)
  61. {
  62. const float u = (float)i/(float)p->ndiv;
  63. const float v = p->vmin + (p->vmax-p->vmin)*u;
  64. snprintf(text, 64, "%.2f %s", v, p->units);
  65. const float fy = oy + v*sy;
  66. imguiDrawText(p->x + p->w - p->pad, (int)fy-4, IMGUI_ALIGN_RIGHT, text, imguiRGBA(0,0,0,255));
  67. imguiDrawLine((float)p->x + (float)p->pad, fy, (float)p->x + (float)p->w - (float)p->pad - 50, fy, 1.0f, imguiRGBA(0,0,0,64));
  68. }
  69. }
  70. void drawGraph(const GraphParams* p, const ValueHistory* graph,
  71. int idx, const char* label, const unsigned int col)
  72. {
  73. const float sx = (p->w - p->pad*2) / (float)graph->getSampleCount();
  74. const float sy = (p->h - p->pad*2) / (p->vmax - p->vmin);
  75. const float ox = (float)p->x + (float)p->pad;
  76. const float oy = (float)p->y + (float)p->pad - p->vmin*sy;
  77. // Values
  78. float px=0, py=0;
  79. for (int i = 0; i < graph->getSampleCount()-1; ++i)
  80. {
  81. const float x = ox + i*sx;
  82. const float y = oy + graph->getSample(i)*sy;
  83. if (i > 0)
  84. imguiDrawLine(px,py, x,y, 2.0f, col);
  85. px = x;
  86. py = y;
  87. }
  88. // Label
  89. const int size = 15;
  90. const int spacing = 10;
  91. int ix = p->x + p->w + 5;
  92. int iy = p->y + p->h - (idx+1)*(size+spacing);
  93. imguiDrawRoundedRect((float)ix, (float)iy, (float)size, (float)size, 2.0f, col);
  94. char text[64];
  95. snprintf(text, 64, "%.2f %s", graph->getAverage(), p->units);
  96. imguiDrawText(ix+size+5, iy+3, IMGUI_ALIGN_LEFT, label, imguiRGBA(255,255,255,192));
  97. imguiDrawText(ix+size+150, iy+3, IMGUI_ALIGN_RIGHT, text, imguiRGBA(255,255,255,128));
  98. }