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