VTKFIG  0.25.0
Easy VTK based in situ visualization
vtkfigXYPlot.h
Go to the documentation of this file.
1 
6 #ifndef VTKFIG_XYPLOT_H
7 #define VTKFIG_XYPLOT_H
8 
9 #include <vtkDoubleArray.h>
10 #include <vtkXYPlotActor.h>
11 #include <vtkRectilinearGrid.h>
12 #include <vtkPointData.h>
13 #include <vtkXYPlotWidget.h>
14 #include <vtkLegendBoxActor.h>
15 
16 
17 #include "vtkfigFigure.h"
18 
19 namespace vtkfig
20 {
21 
30  class XYPlot: public Figure
31  {
32 
33  public:
34 
35 
37  static std::shared_ptr<XYPlot> New();
38 
39 
40  virtual std::string SubClassName() override final;
41 
43  void Clear();
44 
46  void SetTitle(const std::string t);
47 
49  void SetXTitle(const std::string t);
50 
52  void SetYTitle(const std::string t );
53 
57  void SetXRange(double x0, double x1);
58 
59 
63  void SetYRange(double y0, double y1);
64 
66  void SetNumberOfXLabels(int n);
67 
69  void SetNumberOfYLabels(int n);
70 
72  void SetXAxisLabelFormat(const std::string fmt);
73 
75  void SetYAxisLabelFormat(const std::string fmt);
76 
81  void AdjustLabels(bool b );
82 
84  void ShowGrid(bool b );
85 
87  void SetGridColor(double r, double g, double b);
88 
89 
91  void SetAxesColor(double r, double g, double b);
92 
93 
95  void ShowLegend(bool b );
96 
97 
101  void SetLegendPosition(double x, double y);
102 
103 
107  void SetLegendSize(double w, double h);
108 
113  void SetPlotLineType(const std::string type);
114 
115 
117  void SetPlotLegend(const std::string legend);
118 
120  void SetPlotColor(double r, double g, double b);
121 
123  void SetLineWidth(double w);
124 
126  void SetMarkerSize(double s);
127 
131  void SetPlotMarkerType(const std::string type);
132 
133 
134  // void SetPlotMarkerSequence(offset,stride);
135  // void SetPlotMarkerColor();
136 
143  template<typename V> void AddPlot(const V &x, const V &y);
144 
145 
146  protected:
147  XYPlot();
148  ~XYPlot(){};
149 
150 
151  private:
152 
155  void AddPlot();
156 
158  void PlotGrid();
159 
161  void ServerMPSend(vtkSmartPointer<internals::Communicator> communicator) override final;
162 
164  void ClientMPReceive(vtkSmartPointer<internals::Communicator> communicator) override final;
165 
167  void RTBuildAllVTKPipelines(vtkSmartPointer<vtkRenderer> renderer) override final;
168 
170  void RTPreRender() override final;
171 
173  static std::map<std::string,int> marker_types;
174 
176  size_t num_plots=0;
177 
179  int num_gridlines=0;
180 
182  int num_curves=0;
183 
185  vtkSmartPointer<vtkXYPlotActor> XYPlotActor;
186 
190  vtkSmartPointer<vtkLegendBoxActor> LegendActor;
191 
192 
194  static const int desclen=32;
195  std::string title="";
196  std::string ytitle="y";
197  std::string xtitle="x";
198 
201  class GridLine
202  {
203  vtkSmartPointer<vtkDoubleArray> X;
204  vtkSmartPointer<vtkDoubleArray> Y;
205  vtkSmartPointer<vtkRectilinearGrid> curve;
206  const int ds_num;
207  public:
209  int GetDataSetNumber(){return ds_num;}
210  void SetYLine(double xrange[2], double y);
211  void SetXLine(double yrange[2], double x);
212  GridLine(vtkSmartPointer<vtkXYPlotActor> plot, int & ds_num, double rgb[3]);
213  };
214 
216  std::vector<GridLine> XGridLines;
217 
219  std::vector<GridLine> YGridLines;
220 
222  class PlotData
223  {
224  vtkSmartPointer<vtkRectilinearGrid> curve;
225  const int ds_num;
226  public:
227  vtkSmartPointer<vtkDoubleArray> X;
228  vtkSmartPointer<vtkDoubleArray> Y;
230  void FixSize();
231  int GetDataSetNumber();
232  PlotData(vtkSmartPointer<vtkXYPlotActor> plot, int & ds_num);
233  };
234 
235 
237  std::vector<PlotData> AllPlotData;
238 
240  struct
241  {
242  // has to have fixed size for easy c-s transfer
243 
244  char xlabel_format[desclen]="%8.2e";
245  char ylabel_format[desclen]="%8.2e";
246  bool grid_show=true;
247  double grid_rgb[3]={0.8,0.8,0.8};
248  double line_width=1;
249  double marker_size=1;
250 
251  double axes_rgb[3]={0,0,0};
252  bool legend_show=true;
253  double legend_posx=0.8;
254  double legend_posy=0.7;
255  double legend_w=0.1;
256  double legend_h=0.2;
257  bool adjust_labels=false;
258 
259  double dynXMin=1.0e100;
260  double dynXMax=-1.0e100;
261  double dynYMin=1.0e100;
262  double dynYMax=-1.0e100;
263  double fixXMin=1.0e100;
264  double fixXMax=-1.0e100;
265  double fixYMin=1.0e100;
266  double fixYMax=-1.0e100;
267  int nxlabels=5;
268  int nylabels=5;
269  } PlotState;
270 
272  struct PlotInfo
273  {
274  // has to have fixed size for easy c-s transfer
275  char line_type[desclen]={'-',0};
276  char marker_type[desclen]={0};
277  char legend[desclen]={0};
278  double line_rgb[3]={0,0,0};
279  PlotInfo(){};
280  ~PlotInfo(){};
281  };
282 
284  std::vector<PlotInfo> AllPlotInfo;
285 
288  PlotInfo NextPlotInfo;
289 
290  };
291 
292 
293  template<typename V>
294  inline
295  void XYPlot::AddPlot(const V &x, const V &y)
296  {
297 
298  while (num_plots>=AllPlotData.size())
299  {
300  AllPlotData.emplace_back(XYPlotActor,num_curves);
301  }
302  auto plot=AllPlotData[num_plots];
303 
304  int N=x.size();
305  assert(x.size()==y.size());
306 
307  plot.X->SetNumberOfTuples(N);
308  plot.Y->SetNumberOfTuples(N);
309 
310  double xmin=1.0e100;
311  double xmax=-1.0e100;
312  double ymin=1.0e100;
313  double ymax=-1.0e100;
314 
315 
316  for (int i=0; i<N; i++)
317  {
318  plot.X->InsertTuple1(i,x[i]);
319  plot.Y->InsertTuple1(i,y[i]);
320  xmin=std::min(xmin,x[i]);
321  ymin=std::min(ymin,y[i]);
322  xmax=std::max(xmax,x[i]);
323  ymax=std::max(ymax,y[i]);
324  }
325 
326  PlotState.dynXMin=std::min(xmin,PlotState.dynXMin);
327  PlotState.dynXMax=std::max(xmax,PlotState.dynXMax);
328  PlotState.dynYMin=std::min(ymin,PlotState.dynYMin);
329  PlotState.dynYMax=std::max(ymax,PlotState.dynYMax);
330 
331  AddPlot();
332  }
333 
334 
335 }
336 
337 #endif
vtkfig::XYPlot::ShowGrid
void ShowGrid(bool b)
Show grid lines in the plot ?
vtkfig::XYPlot::SetPlotLineType
void SetPlotLineType(const std::string type)
Set the line type of the next plot.
vtkfig::XYPlot::SetXAxisLabelFormat
void SetXAxisLabelFormat(const std::string fmt)
Set the format string for x axis labels.
vtkfig::XYPlot::SetPlotLegend
void SetPlotLegend(const std::string legend)
Set the information for the legend entry for the next plot.
vtkfig::XYPlot::SetLineWidth
void SetLineWidth(double w)
Set the width of all lines in the plot.
vtkfig::XYPlot::SetPlotMarkerType
void SetPlotMarkerType(const std::string type)
Set marker type.
vtkfig::XYPlot::AdjustLabels
void AdjustLabels(bool b)
Adjust labels to "nice values" ?
vtkfig::XYPlot::SetNumberOfYLabels
void SetNumberOfYLabels(int n)
Set the number of y axis labels.
vtkfig::XYPlot::ShowLegend
void ShowLegend(bool b)
Show a legend box ?
vtkfig::XYPlot::AddPlot
void AddPlot(const V &x, const V &y)
Add a pair of x/y data to the figure.
Definition: vtkfigXYPlot.h:295
vtkfig::XYPlot::SetAxesColor
void SetAxesColor(double r, double g, double b)
Set the color of the axes.
vtkfig::XYPlot::SetYAxisLabelFormat
void SetYAxisLabelFormat(const std::string fmt)
Set the format string for y axis labels.
vtkfigFigure.h
vtkfig::XYPlot::SetMarkerSize
void SetMarkerSize(double s)
Set the size of all markers in the plot.
vtkfig::XYPlot::SetLegendPosition
void SetLegendPosition(double x, double y)
Set the position of the legend box.
vtkfig::Figure::xmin
double xmin
View volume data.
Definition: vtkfigFigure.h:475
vtkfig::XYPlot::SetYTitle
void SetYTitle(const std::string t)
Set the title of the y axis.
vtkfig::XYPlot::SetNumberOfXLabels
void SetNumberOfXLabels(int n)
Set the number of x axis labels.
vtkfig::XYPlot::SetYRange
void SetYRange(double y0, double y1)
Set the range of the y values.
vtkfig::XYPlot::SubClassName
virtual std::string SubClassName() override final
Get subclass name (for s-c communication, should be replaced by tag.
vtkfig::XYPlot
XY Function plot.
Definition: vtkfigXYPlot.h:30
vtkfig::XYPlot::SetXRange
void SetXRange(double x0, double x1)
Set the range of the x values.
vtkfig::XYPlot::SetPlotColor
void SetPlotColor(double r, double g, double b)
Set the color of the next plot.
vtkfig::Figure
Base class for all figures.
Definition: vtkfigFigure.h:61
vtkfig::XYPlot::SetXTitle
void SetXTitle(const std::string t)
Set the title of the x axis.
vtkfig::XYPlot::SetLegendSize
void SetLegendSize(double w, double h)
Set the size of the legend box.
vtkfig::XYPlot::SetTitle
void SetTitle(const std::string t)
Set the title of the plot.
vtkfig::XYPlot::SetGridColor
void SetGridColor(double r, double g, double b)
Set the color of the grid lines.
vtkfig::XYPlot::New
static std::shared_ptr< XYPlot > New()
Smartpointer construtor.
vtkfig::XYPlot::Clear
void Clear()
Remove all data from plot.