vtkXYChartRepresentationInternals.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: ParaView
4  Module: vtkXYChartRepresentationInternals.h
5 
6  Copyright (c) Kitware, Inc.
7  All rights reserved.
8  See Copyright.txt or http://www.paraview.org/HTML/Copyright.html for details.
9 
10  This software is distributed WITHOUT ANY WARRANTY; without even
11  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12  PURPOSE. See the above copyright notice for more information.
13 
14 =========================================================================*/
21 #ifndef vtkXYChartRepresentationInternals_h
22 #define vtkXYChartRepresentationInternals_h
23 
24 #include "vtkCSVExporter.h"
25 #include "vtkChartXY.h"
26 #include "vtkColor.h"
27 #include "vtkDataArray.h"
28 #include "vtkPen.h"
29 #include "vtkPlotBar.h"
30 #include "vtkPlotFunctionalBag.h"
31 #include "vtkPlotPoints.h"
32 #include "vtkSmartPointer.h"
33 #include "vtkTable.h"
34 
35 #include <array>
36 #include <map>
37 #include <string>
38 
40 {
41 protected:
42  struct PlotInfo
43  {
45  std::string TableName;
46  std::string ColumnName;
47  };
48 
49  typedef std::map<std::string, PlotInfo> PlotsMapItem;
50  class PlotsMap : public std::map<std::string, PlotsMapItem>
51  {
52  bool Contains(const std::string& key, const std::string& role) const
53  {
54  PlotsMap::const_iterator iter1 = this->find(key);
55  if (iter1 != this->end())
56  {
57  PlotsMapItem::const_iterator iter2 = iter1->second.find(role);
58  return (iter2 != iter1->second.end());
59  }
60  return false;
61  }
62 
63  public:
64  vtkSmartPointer<vtkPlot> GetPlot(vtkChartRepresentation* self, const std::string& tableName,
65  const std::string& columnName, const std::string& role = std::string()) const
66  {
67  const std::string key = self->GetDefaultSeriesLabel(tableName, columnName);
68  PlotsMap::const_iterator iter1 = this->find(key);
69  if (iter1 != this->end())
70  {
71  PlotsMapItem::const_iterator iter2 = iter1->second.find(role);
72  if (iter2 != iter1->second.end())
73  {
74  return iter2->second.Plot;
75  }
76  }
77  return vtkSmartPointer<vtkPlot>();
78  }
79 
80  bool RemovePlot(vtkChartRepresentation* self, const std::string& tableName,
81  const std::string& columnName, const std::string& role = std::string())
82  {
83  const std::string key = self->GetDefaultSeriesLabel(tableName, columnName);
84  PlotsMap::iterator iter1 = this->find(key);
85  if (iter1 != this->end())
86  {
87  PlotsMapItem::iterator iter2 = iter1->second.find(role);
88  if (iter2 != iter1->second.end())
89  {
90  iter1->second.erase(iter2);
91  return true;
92  }
93  }
94  return false;
95  }
96 
97  void AddPlot(vtkChartRepresentation* self, const std::string& tableName,
98  const std::string& columnName, const std::string& role, vtkPlot* plot)
99  {
100  const std::string key = self->GetDefaultSeriesLabel(tableName, columnName);
101  PlotInfo& info = (*this)[key][role];
102  info.TableName = tableName;
103  info.ColumnName = columnName;
104  info.Plot = plot;
105  }
106 
107  void SetPlotVisibility(bool val) const
108  {
109  for (PlotsMap::const_iterator iter1 = this->begin(); iter1 != this->end(); ++iter1)
110  {
111  for (PlotsMapItem::const_iterator iter2 = iter1->second.begin();
112  iter2 != iter1->second.end(); ++iter2)
113  {
114  iter2->second.Plot->SetVisible(val);
115  }
116  }
117  }
118 
119  void RemoveAllPlots(vtkChartXY* chartXY)
120  {
121  for (PlotsMap::const_iterator iter1 = this->begin(); iter1 != this->end(); ++iter1)
122  {
123  for (PlotsMapItem::const_iterator iter2 = iter1->second.begin();
124  iter2 != iter1->second.end(); ++iter2)
125  {
126  chartXY->RemovePlotInstance(iter2->second.Plot.GetPointer());
127  }
128  }
129  this->clear();
130  }
131 
132  void Intersect(const PlotsMap& other, vtkChartXY* chartXY)
133  {
134  for (PlotsMap::iterator iter1 = this->begin(); iter1 != this->end(); ++iter1)
135  {
136  for (PlotsMapItem::iterator iter2 = iter1->second.begin(); iter2 != iter1->second.end();)
137  {
138  if (other.Contains(iter1->first, iter2->first) == false)
139  {
140  chartXY->RemovePlotInstance(iter2->second.Plot.GetPointer());
141  PlotsMapItem::iterator iter2old = iter2;
142  ++iter2;
143  iter1->second.erase(iter2old);
144  }
145  else
146  {
147  ++iter2;
148  }
149  }
150  }
151  }
152  };
153 
155 
156  //---------------------------------------------------------------------------
165  template <class T>
166  T GetSeriesParameter(vtkXYChartRepresentation* self, const std::string& tableName,
167  const std::string& columnName, const std::string& vtkNotUsed(role),
168  const std::map<std::string, T>& parameter_map, const T default_value = T()) const
169  {
170  typename std::map<std::string, T>::const_iterator iter;
171 
172  // when setting properties for a series, I want to support two mechanisms:
173  // simply specifying the array name or suffixing it with the block-name.
174  // This logic makes that possible.
175 
176  // first try most specific form of identifying the series.
177  std::string key = self->GetDefaultSeriesLabel(tableName, columnName);
178  iter = parameter_map.find(key);
179  if (iter != parameter_map.end())
180  {
181  return iter->second;
182  }
183 
184  // now try the cheap form for identifying it.
185  key = self->GetDefaultSeriesLabel(std::string(), columnName);
186  iter = parameter_map.find(key);
187  if (iter != parameter_map.end())
188  {
189  return iter->second;
190  }
191  return default_value;
192  }
193 
194 public:
196 
197  // we have to keep these separate since they are set by different properties
198  // and hence may not always match up.
199  std::map<std::string, bool> SeriesVisibilities;
200  std::map<std::string, int> SeriesOrder;
201  std::map<std::string, int> LineThicknesses;
202  std::map<std::string, int> LineStyles;
203  std::map<std::string, vtkColor3d> Colors;
204  std::map<std::string, int> AxisCorners;
205  std::map<std::string, int> MarkerStyles;
206  std::map<std::string, double> MarkerSizes;
207  std::map<std::string, std::string> Labels;
208  std::map<std::string, bool> UseColorMapping;
209  std::map<std::string, vtkScalarsToColors*> Lut;
210 
211  // These are used to determine when to recalculate chart bounds. If user
212  // changes the X axis, we force recalculation of the chart bounds
213  // automatically.
216 
218  : PreviousUseIndexForXAxis(false)
219  {
220  }
221 
222  virtual ~vtkInternals() {}
223 
224  //---------------------------------------------------------------------------
228  void HideAllPlots() { this->SeriesPlots.SetPlotVisibility(false); }
229 
230  //---------------------------------------------------------------------------
234  void RemoveAllPlots(vtkChartXY* chartXY) { this->SeriesPlots.RemoveAllPlots(chartXY); }
235 
236  //---------------------------------------------------------------------------
242  virtual std::vector<std::string> GetSeriesRoles(
243  const std::string& vtkNotUsed(tableName), const std::string& vtkNotUsed(columnName))
244  {
245  return { std::string() };
246  }
247 
248  //---------------------------------------------------------------------------
252  virtual vtkPlot* NewPlot(vtkXYChartRepresentation* self, const std::string& tableName,
253  const std::string& columnName, const std::string& role)
254  {
255  (void)tableName;
256  (void)columnName;
257  (void)role;
258 
259  assert(self);
260  vtkChartXY* chartXY = self->GetChart();
261 
262  assert(chartXY);
263  return chartXY->AddPlot(self->GetChartType());
264  }
265 
266  //---------------------------------------------------------------------------
267  virtual int GetInputArrayIndex(const std::string& vtkNotUsed(tableName),
268  const std::string& vtkNotUsed(columnName), const std::string& vtkNotUsed(role))
269  {
270  return 1;
271  }
272 
273  //---------------------------------------------------------------------------
277  virtual void UpdatePlots(vtkXYChartRepresentation* self, const MapOfTables& tables)
278  {
279  PlotsMap newPlots;
280  assert(self != nullptr);
281  vtkChartXY* chartXY = self->GetChart();
282  this->RemoveAllPlots(chartXY);
283  std::multimap<int, std::pair<vtkTable*, std::array<std::string, 3> > > orderMap;
284  for (MapOfTables::const_iterator tablesIter = tables.begin(); tablesIter != tables.end();
285  ++tablesIter)
286  {
287  const std::string& tableName = tablesIter->first;
288  vtkTable* table = tablesIter->second.GetPointer();
289  vtkIdType numCols = table->GetNumberOfColumns();
290  for (vtkIdType cc = 0; cc < numCols; ++cc)
291  {
292  std::string columnName = table->GetColumnName(cc);
293  auto roles = this->GetSeriesRoles(tableName, columnName);
294 
295  for (const auto& role : roles)
296  {
297  // recover the order of the current table column
298  const int order =
299  this->GetSeriesParameter(self, tableName, columnName, role, this->SeriesOrder, -1);
300 
301  // store all info in a (sorted) map
302  std::array<std::string, 3> mapValue;
303  mapValue[0] = tableName;
304  mapValue[1] = columnName;
305  mapValue[2] = role;
306  orderMap.insert(std::make_pair(order, std::make_pair(table, mapValue)));
307  }
308  }
309  }
310 
311  // for each ordered column
312  for (auto it = orderMap.begin(); it != orderMap.end(); ++it)
313  {
314  vtkTable* table = it->second.first;
315  const std::string& tableName = it->second.second[0];
316  const std::string& columnName = it->second.second[1];
317  const std::string& role = it->second.second[2];
318 
319  vtkSmartPointer<vtkPlot> plot = this->SeriesPlots.GetPlot(self, tableName, columnName, role);
320  if (!plot)
321  {
322  // Create the plot in the right order
323  plot = this->NewPlot(self, tableName, columnName, role);
324  if (!plot)
325  {
326  continue;
327  }
328  }
329  plot->SetInputData(table);
330  plot->SetUseIndexForXSeries(self->GetUseIndexForXAxis());
331  plot->SetInputArray(0, self->GetXAxisSeriesName());
332  plot->SetInputArray(this->GetInputArrayIndex(tableName, columnName, role), columnName);
333  this->SeriesPlots.AddPlot(self, tableName, columnName, role, plot);
334  newPlots.AddPlot(self, tableName, columnName, role, plot);
335  }
336 
337  // Remove any plots in this->SeriesPlots that are not in newPlots.
338  this->SeriesPlots.Intersect(newPlots, chartXY);
339  }
340 
341  //---------------------------------------------------------------------------
346  {
347  vtkChartXY* chartXY = self->GetChart();
348  vtkPlot* lastFunctionalBagPlot = 0;
349  for (PlotsMap::iterator iter1 = this->SeriesPlots.begin(); iter1 != this->SeriesPlots.end();
350  ++iter1)
351  {
352  for (PlotsMapItem::const_iterator iter2 = iter1->second.begin(); iter2 != iter1->second.end();
353  ++iter2)
354  {
355  const PlotInfo& plotInfo = iter2->second;
356  const std::string& tableName = plotInfo.TableName;
357  const std::string& columnName = plotInfo.ColumnName;
358  vtkPlot* plot = plotInfo.Plot;
359  const std::string& role = iter2->first;
360 
361  if (this->UpdateSinglePlotProperties(self, tableName, columnName, role, plot))
362  {
363  // Functional bag plots shall be stacked under the other plots.
365  if (plotBag)
366  {
367  // We can't select the median line as it may not exist in other dataset.
368  if (columnName == "QMedianLine")
369  {
370  plotBag->SelectableOff();
371  }
372  if (plotBag->IsBag())
373  {
374  if (!lastFunctionalBagPlot)
375  {
376  chartXY->LowerPlot(plotBag);
377  }
378  else
379  {
380  chartXY->StackPlotAbove(plotBag, lastFunctionalBagPlot);
381  }
382  lastFunctionalBagPlot = plotBag;
383  }
384  }
385  }
386  }
387  }
388  }
389 
390  //---------------------------------------------------------------------------
394  virtual bool Export(vtkXYChartRepresentation* self, vtkCSVExporter* exporter)
395  {
396  for (PlotsMap::iterator iter1 = this->SeriesPlots.begin(); iter1 != this->SeriesPlots.end();
397  ++iter1)
398  {
399  for (PlotsMapItem::const_iterator iter2 = iter1->second.begin(); iter2 != iter1->second.end();
400  ++iter2)
401  {
402  const PlotInfo& plotInfo = iter2->second;
403  vtkPlot* plot = plotInfo.Plot;
404  if (!plot->GetVisible())
405  {
406  continue;
407  }
408  const std::string& columnName = plotInfo.ColumnName;
409  vtkTable* table = plot->GetInput();
410  vtkDataArray* xarray = self->GetUseIndexForXAxis()
411  ? nullptr
412  : vtkDataArray::SafeDownCast(table->GetColumnByName(self->GetXAxisSeriesName()));
413  vtkAbstractArray* yarray = table->GetColumnByName(columnName.c_str());
414  if (yarray != nullptr)
415  {
416  exporter->AddColumn(yarray, plot->GetLabel().c_str(), xarray);
417  }
418  }
419  }
420  return true;
421  }
422 
423 protected:
424  //---------------------------------------------------------------------------
429  const std::string& tableName, const std::string& columnName, const std::string& role,
430  vtkPlot* plot)
431  {
432  vtkChartXY* chartXY = self->GetChart();
433  const bool visible =
434  this->GetSeriesParameter(self, tableName, columnName, role, this->SeriesVisibilities, false);
435  plot->SetVisible(visible);
436  if (!visible)
437  {
438  return false;
439  }
440 
441  std::string default_label = self->GetDefaultSeriesLabel(tableName, columnName);
442  std::string label =
443  this->GetSeriesParameter(self, tableName, columnName, role, this->Labels, default_label);
444  if (self->GetSeriesLabelPrefix())
445  {
446  label = std::string(self->GetSeriesLabelPrefix()) + label;
447  }
448  plot->SetLabel(label);
449 
451  self, tableName, columnName, role, this->Colors, vtkColor3d(0, 0, 0));
452  plot->SetColor(color.GetRed(), color.GetGreen(), color.GetBlue());
453  plot->GetSelectionPen()->SetColorF(self->SelectionColor);
454 
455  plot->SetWidth(
456  this->GetSeriesParameter(self, tableName, columnName, role, this->LineThicknesses, 2));
457  plot->GetPen()->SetLineType(this->GetSeriesParameter(
458  self, tableName, columnName, role, this->LineStyles, static_cast<int>(vtkPen::SOLID_LINE)));
459 
460  if (vtkPlotPoints* plotPoints = vtkPlotPoints::SafeDownCast(plot))
461  {
462  plotPoints->SetMarkerStyle(this->GetSeriesParameter(self, tableName, columnName, role,
463  this->MarkerStyles, static_cast<int>(vtkPlotPoints::NONE)));
464  plotPoints->SetMarkerSize(
465  this->GetSeriesParameter(self, tableName, columnName, role, this->MarkerSizes, 1.0));
466  // the vtkValidPointMask array is used by some filters (like plot
467  // over line) to indicate invalid points. this instructs the line
468  // plot to not render those points
469  plotPoints->SetValidPointMaskName("vtkValidPointMask");
470  }
471 
472  chartXY->SetPlotCorner(
473  plot, this->GetSeriesParameter(self, tableName, columnName, role, this->AxisCorners, 0));
474 
475  // for now only vtkPlotBar has color mapping
476  vtkPlotBar* plotBar = vtkPlotBar::SafeDownCast(plot);
477  if (plotBar && columnName == "bin_values")
478  {
479  bool colorMapping =
480  this->GetSeriesParameter(self, tableName, columnName, role, this->UseColorMapping, false);
481  plotBar->SetScalarVisibility(colorMapping);
482  plotBar->SelectColorArray("bin_extents");
484  self, tableName, columnName, role, this->Lut, static_cast<vtkScalarsToColors*>(nullptr));
485  if (lut)
486  {
487  plotBar->SetLookupTable(lut);
488  }
489  }
490  return true;
491  }
492 };
493 
494 #endif
495 // VTK-HeaderTest-Exclude: vtkXYChartRepresentationInternals.h
color
std::map< std::string, vtkScalarsToColors * > Lut
order
bool RemovePlot(vtkChartRepresentation *self, const std::string &tableName, const std::string &columnName, const std::string &role=std::string())
static vtkDataArray * SafeDownCast(vtkObjectBase *o)
T GetSeriesParameter(vtkXYChartRepresentation *self, const std::string &tableName, const std::string &columnName, const std::string &vtkNotUsed(role), const std::map< std::string, T > &parameter_map, const T default_value=T()) const
Makes is easy to obtain a value for a series parameter, is set, else the default. ...
virtual void SetWidth(float width)
const double & GetBlue() const
vtkXYChartRepresentation is representation that is used to add vtkPlot subclasses to a vtkChartXY ins...
void AddPlot(vtkChartRepresentation *self, const std::string &tableName, const std::string &columnName, const std::string &role, vtkPlot *plot)
info
vtkXYChartRepresentation::MapOfTables MapOfTables
static vtkPlotFunctionalBag * SafeDownCast(vtkObjectBase *o)
int vtkIdType
virtual void SetColor(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
virtual std::vector< std::string > GetSeriesRoles(const std::string &vtkNotUsed(tableName), const std::string &vtkNotUsed(columnName))
Subclasses can override this method to assign a role for a specific data array in the input dataset...
void SetPlotCorner(vtkPlot *plot, int corner)
std::map< std::string, vtkSmartPointer< vtkTable > > MapOfTables
virtual int GetInputArrayIndex(const std::string &vtkNotUsed(tableName), const std::string &vtkNotUsed(columnName), const std::string &vtkNotUsed(role))
const double & GetRed() const
void AddColumn(vtkAbstractArray *yarray, const char *yarrayname=NULL, vtkDataArray *xarray=NULL)
In STREAM_COLUMNS mode, use this method to add a column (yarray).
virtual bool UpdateSinglePlotProperties(vtkXYChartRepresentation *self, const std::string &tableName, const std::string &columnName, const std::string &role, vtkPlot *plot)
Returns false for in-visible plots.
vtkPen * GetPen()
vtkSmartPointer< vtkPlot > GetPlot(vtkChartRepresentation *self, const std::string &tableName, const std::string &columnName, const std::string &role=std::string()) const
virtual void SetLookupTable(vtkScalarsToColors *lut)
vtkAbstractArray * GetColumnByName(const char *name)
virtual vtkPlot * AddPlot(int type)
void RemoveAllPlots(vtkChartXY *chartXY)
Destroy all vtkPlot instances.
static vtkPlotPoints * SafeDownCast(vtkObjectBase *o)
const char * GetColumnName(vtkIdType col)
virtual bool RemovePlotInstance(vtkPlot *plot)
virtual void UpdatePlotProperties(vtkXYChartRepresentation *self)
Update properties for plots in the chart.
virtual bool IsBag()
virtual vtkTable * GetInput()
virtual void SetInputArray(int index, const vtkStdString &name)
vtkIdType LowerPlot(vtkPlot *plot)
virtual void SetScalarVisibility(bool)
virtual void SetInputData(vtkTable *table)
virtual vtkIdType StackPlotAbove(vtkPlot *plot, vtkPlot *under)
virtual vtkStdString GetLabel()
virtual void UpdatePlots(vtkXYChartRepresentation *self, const MapOfTables &tables)
Update i.e.
void SelectColorArray(vtkIdType arrayNum)
vtkChartRepresentation is the base representation for charting representations.
void SetColorF(double color[3])
virtual void SetUseIndexForXSeries(bool)
exporter used by certain views to export data as CSV.
static vtkPlotBar * SafeDownCast(vtkObjectBase *o)
virtual vtkPlot * NewPlot(vtkXYChartRepresentation *self, const std::string &tableName, const std::string &columnName, const std::string &role)
Add new plot.
vtkPen * GetSelectionPen()
virtual bool Export(vtkXYChartRepresentation *self, vtkCSVExporter *exporter)
Export visible plots to a CSV file.
vtkIdType GetNumberOfColumns()
virtual void SetVisible(bool)
virtual void SetLabel(const vtkStdString &label)
void SetLineType(int type)
virtual bool GetVisible()
void Intersect(const PlotsMap &other, vtkChartXY *chartXY)
const double & GetGreen() const
virtual void SelectableOff()
key