ofxPlotter.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. //
  2. // ofxPlotter.cpp
  3. //
  4. // Created by Juan Carlos Carvajal B.
  5. // juan-carlos.info
  6. //
  7. // Based on the plugin by zebra on 06/05/16.
  8. //
  9. //
  10. #include "ofxPlotter.h"
  11. ofxPlotter::dataCollection::dataCollection() : plotColor ( ofColor(0) ) ,
  12. autoRange ( true ),
  13. drawOverlay ( true ),
  14. drawInfo ( true ),
  15. plotLength ( 400 ),
  16. maxValue ( 0 ),
  17. minValue ( 0 ) {
  18. records.clear();
  19. singleValue tmpValue;
  20. records.assign( plotLength, tmpValue );
  21. }
  22. void ofxPlotter::dataCollection::setPlotLength (int plotLength){
  23. this->plotLength = plotLength;
  24. records.clear();
  25. singleValue tmpValue;
  26. records.assign( plotLength, tmpValue );
  27. }
  28. ofxPlotter::ofxPlotter() {
  29. ofAddListener(ofEvents().update, this, &::ofxPlotter::update);
  30. }
  31. void ofxPlotter::updateHistory() {
  32. map<string, dataCollection>::iterator it;
  33. for ( it = plotData.begin(); it != plotData.end(); it++) {
  34. dataCollection *itDataCollection = &(it->second);
  35. itDataCollection->records.push_back( itDataCollection->currentValue );
  36. if ( int( itDataCollection->records.size() ) > itDataCollection->plotLength ){
  37. itDataCollection->records.erase( itDataCollection->records.begin() );
  38. }
  39. }
  40. }
  41. void ofxPlotter::drawPlot(string plot){
  42. drawPlot (plot, 0, 0, ofGetHeight(), ofGetWidth() ) ;
  43. }
  44. void ofxPlotter::drawPlot(string plot, int x, int y, int width, int height) {
  45. vector<singleValue>* records = &(plotData[plot].records);
  46. float sum = 0;
  47. float & max = plotData[plot].maxValue;
  48. float & min = plotData[plot].minValue;
  49. // Measuring the function scale
  50. if ( plotData[plot].autoRange ) {
  51. for ( unsigned long i = 0; i < records->size(); i++ ) {
  52. float value = (*records)[i].getF();
  53. sum += value;
  54. if (value > max) max = value;
  55. if (value < min) min = value;
  56. }
  57. }
  58. float stepWidth = width / records->size();
  59. ofPushMatrix();
  60. ofPushStyle();
  61. ofTranslate(x, y);
  62. ofPoint p, p2;
  63. for ( int j = 0; j < records->size(); j++) {
  64. float mappedY = 1 - ofMap( (*records)[j].getF(), min, max, 0, 1);
  65. float mappedX = ofMap(j, 0,records->size(),0, width );
  66. p = ofPoint( mappedX , mappedY * float(height) );
  67. ofSetColor( plotData[plot].plotColor );
  68. ofSetLineWidth(2);
  69. if (j != 0) ofDrawLine(p2, p);
  70. p2 = p;
  71. }
  72. if ( plotData[plot].drawOverlay) {
  73. drawOverlay(plot, x, y, width, height);
  74. }
  75. if ( plotData[plot].drawInfo) {
  76. drawInfo(plot, x, y, width, height);
  77. }
  78. if ( plotData[plot].drawGuidelines) {
  79. drawGuidelines(plot, x, y, width, height);
  80. }
  81. ofPopMatrix();
  82. ofPopStyle();
  83. }
  84. /*
  85. *
  86. * Draw all the plots contained in the map in a vertical layout
  87. *
  88. *
  89. */
  90. void ofxPlotter::drawAllVertical() {
  91. drawAllVertical(0, 0, ofGetHeight(), ofGetWidth() );
  92. }
  93. void ofxPlotter::drawAllVertical(float x, float y, float width, float height) {
  94. map<string, dataCollection>::iterator it;
  95. int plotDataSize = plotData.size();
  96. int i = 0;
  97. float dy = height / plotDataSize + 1;
  98. for ( it = plotData.begin(); it != plotData.end(); it++) {
  99. drawPlot(it->first, x, dy * i , width, dy);
  100. i++;
  101. }
  102. }
  103. /*
  104. *
  105. * Draw all the plots contained in the map in a horizontal layout
  106. *
  107. *
  108. */
  109. void ofxPlotter::drawAllHorizontal() {
  110. drawAllHorizontal(0, 0, ofGetHeight(), ofGetWidth() );
  111. }
  112. void ofxPlotter::drawAllHorizontal(float x, float y, float width, float height) {
  113. map<string, dataCollection>::iterator it;
  114. int plotDataSize = plotData.size();
  115. int i = 0;
  116. float dx = width / plotDataSize + 1;
  117. // float dy = height / plotDataSize + 1;
  118. for ( it = plotData.begin(); it != plotData.end(); it++) {
  119. drawPlot(it->first, dx * i, y, dx, height);
  120. i++;
  121. }
  122. }
  123. /*
  124. *
  125. * *** DRAW EXTRAS ***
  126. *
  127. *
  128. */
  129. void ofxPlotter::drawOverlay(string plot, float x, float y, float width, float height) {
  130. vector<singleValue>* records = &(plotData[plot].records);
  131. if (!ofRectangle(x, y, width, height).inside(ofGetMouseX(), ofGetMouseY())) return;
  132. float mx = (ofGetMouseX() - x);
  133. float my = (ofGetMouseY() - y);
  134. //ofSetColor(128);
  135. int position = int ( ofMap( mx, 0, width, 0,records->size() ) );
  136. string tmpString = ofToString( (*records)[position].getF() );
  137. ofDrawBitmapString( tmpString, mx, my);
  138. }
  139. void ofxPlotter::drawGuidelines(string plot, float x, float y, float width, float height) {
  140. vector<float>* guidelines = &(plotData[plot].guidelines);
  141. if ( !guidelines->size() ) return;
  142. float & max = plotData[plot].maxValue;
  143. float & min = plotData[plot].minValue;
  144. for (vector<float>::iterator it = guidelines->begin(); it != guidelines->end(); it++) {
  145. float mappedY = ofMap( *it, min, max, 0, 1) * float(height);
  146. ofDrawLine(0, mappedY, width, mappedY );
  147. }
  148. }
  149. void ofxPlotter::drawInfo (string plot, float x, float y, float width, float height) {
  150. dataCollection *iDataCollection = &(plotData[plot]);
  151. ofSetColor(iDataCollection->plotColor);
  152. // Plot name
  153. // ofDrawRectangle(width - plot.length() * 8, yspace / 2 - 12, plot.length() * 8, 15);
  154. ofDrawBitmapString( plot , width - plot.length() * 8, height / 2);
  155. // Max value
  156. string maxValue = ofToString ( iDataCollection->maxValue );
  157. //ofDrawRectangle(0, 0, maxValue.length() * 8, 15);
  158. ofDrawBitmapString( maxValue, 0, 12 );
  159. // Min value
  160. string minValue = ofToString ( iDataCollection->minValue );
  161. //ofDrawRectangle(0, height - 12, minValue.length() * 8, 15);
  162. ofDrawBitmapString(minValue, 0, height);
  163. }