GnuplotVisualization.cpp
Go to the documentation of this file.
1 
19 
20 namespace Visualization {
21 
23  {
24  }
25 
27  {
28  }
29 
30  void GnuplotVisualization::initAnimatedBarChart(const std::vector<std::string>& pBarLabels, const std::string& pBarChartTitle, const std::string& pYLabel, const std::pair<float, float>& pYRange)
31  {
32  //Check whether method argument is a non-empty interval.
33  if(pYRange.second < pYRange.first)
34  throw std::invalid_argument("Bar value range is not allowed to be empty.");
35 
36  //Save value range for later checking of bar value updates.
37  mYRange = pYRange;
38 
39  //Create a clean interface to gnuplot.
40  mGnuplotHandler.reset(new Gnuplot);
41 
42  //Empty buffer with data for gnuplot.
43  mBarChartBuffer.clear();
44 
45  //Set bar chart title
46  *(mGnuplotHandler) << "set title \"" << pBarChartTitle << "\"\n";
47 
48  //Set label for y-axis
49  *(mGnuplotHandler) << "set ylabel \"" << pYLabel << "\"\n";
50 
51  //Set range in both x and y direction
52  *(mGnuplotHandler) << "set auto x\n";
53  *(mGnuplotHandler) << "set yrange [" << pYRange.first << ":" << pYRange.second << "]\n";
54 
55  //Ask for a grid in vertical direction
56  *(mGnuplotHandler) << "set grid ytics\n";
57  //Set width of bars around their respective x values (left float in outer pair in mBarChartBuffer).
58  *(mGnuplotHandler) << "set boxwidth 0.75\n";
59  //Ask for slightly transparent bars uniformly colored with a border.
60  *(mGnuplotHandler) << "set style fill transparent solid 0.75 border -1\n";
61 
62  //Set that there can be both green and red bars in chart.
63  *(mGnuplotHandler) << "set style line 1 lc rgb \"green\"\n";
64  *(mGnuplotHandler) << "set style line 2 lc rgb \"red\"\n";
65 
66  //Access the labels of the different bars
67  std::vector<std::string>::const_iterator barLabelIterator;
68 
69  //Go through all labels
70  for(barLabelIterator = pBarLabels.begin(); barLabelIterator != pBarLabels.end(); barLabelIterator++) {
71  //and create data set containing their position, string identifier surrounded by double quotes and a bar value of zero.
72  std::pair<float, std::pair<std::string, float> > zeroBar = std::make_pair(static_cast<float>(std::distance(pBarLabels.begin(), barLabelIterator)), std::make_pair("\"" + *barLabelIterator + "\"", 0.0f));
73 
74  //Insert initialized data set into visualization buffer.
75  mBarChartBuffer.push_back(zeroBar);
76  }
77  }
78 
79  void GnuplotVisualization::updateBarChartValues(const std::map<std::string, float>& pCurrentData)
80  {
81  //Do not accept to alter visualization with non-existing gnuplot handler, even though this would not lead to null pointer exception.
82  if(!mGnuplotHandler)
83  throw std::runtime_error("Cannot update non-existing gnuplot visualization.");
84 
85  //Access data sets predefined for this gnuplot bar chart visualization.
86  std::vector<std::pair<float, std::pair<std::string, float> > >::iterator barChartBufferIterator;
87 
88  //Access new bar values given to this method.
89  std::map<std::string, float>::const_iterator currentDataIterator;
90 
91  //Go through buffer as contrary to input map, labels can occur there multiple times.
92  for(barChartBufferIterator = mBarChartBuffer.begin(); barChartBufferIterator != mBarChartBuffer.end(); barChartBufferIterator++) {
93  //Look for bar label currently taken into account in input map ignoring its surrounding double quotes.
94  currentDataIterator = pCurrentData.find(barChartBufferIterator->second.first.substr(1, barChartBufferIterator->second.first.size() - 2));
95 
96  //If there is an update for this bar label
97  if(currentDataIterator != pCurrentData.end()) {
98 
99  //Check whether new bar value is within range currently allowed for this visualization.
100  if(currentDataIterator->second < mYRange.first || currentDataIterator->second > mYRange.second)
101  throw std::invalid_argument("Bar chart value " + boost::lexical_cast<std::string>(currentDataIterator->second) + " is outside allowed range.");
102 
103  //Update value of bar label in internal buffer with value associated to this label in input map.
104  barChartBufferIterator->second.second = currentDataIterator->second;
105  }
106  }
107  }
108 
109  void GnuplotVisualization::sendBarChartToGnuplot(const bool pHighlightHighestValue)
110  {
111  //Prevent system from trying to send data to non-initialized gnuplot handler
112  if(!mGnuplotHandler)
113  throw std::runtime_error("Cannot show non-existing gnuplot visualization.");
114 
115  //Check whether there is any data to be shown.
116  if(mBarChartBuffer.empty())
117  throw std::logic_error("Chart with no bars cannot be shown.");
118 
119  //Every data combination has in common it wants to be plotted.
120  *(mGnuplotHandler) << "plot '-' every ::" << 0 << "::" << mBarChartBuffer.size() << " using 1:3:xtic(2) notitle with boxes ls 1";
121 
122  //End of gnuplot instructions for defining how bars are to be plotted
123  *(mGnuplotHandler) << "\n";
124 
125  //Push bar chart data to gnuplot.
127  mGnuplotHandler->flush();
128  }
129 
130 }
f
void updateBarChartValues(const std::map< std::string, float > &pCurrentData)
boost::shared_ptr< Gnuplot > mGnuplotHandler
void sendBarChartToGnuplot(const bool pHighlightHighestValue)
void initAnimatedBarChart(const std::vector< std::string > &pBarLabels, const std::string &pBarChartTitle, const std::string &pYLabel, const std::pair< float, float > &pYRange)
std::vector< std::pair< float, std::pair< std::string, float > > > mBarChartBuffer


asr_psm_visualizations
Author(s): Gehrung Joachim, Meißner Pascal
autogenerated on Sat Nov 9 2019 03:49:13