Commit 9f7d433c authored by Shawn Waldon's avatar Shawn Waldon

Add option to sort data by x-axis for vtkPVXYChartView

The sorted order of the data arrays determines the connectivity of the
line graphs.  And many line graphs want the line to be connected along
the points in order of their x-coordinate.  This option allows this type
of graph even when the original data is not sorted in that order.
parent e402b9d4
......@@ -239,6 +239,7 @@ int vtkChartRepresentation::RequestData(
reductionFilter->Update();
data = reductionFilter->GetOutputDataObject(0);
data = this->TransformTable(data);
if (this->EnableServerSideRendering && numProcs > 1)
{
......@@ -282,6 +283,12 @@ vtkDataObject* vtkChartRepresentation::TransformInputData(
return data;
}
//----------------------------------------------------------------------------
vtkSmartPointer<vtkDataObject> vtkChartRepresentation::TransformTable(
vtkSmartPointer<vtkDataObject> data)
{
return data;
}
//----------------------------------------------------------------------------
bool vtkChartRepresentation::IsCached(double cache_key)
{
......
......@@ -220,6 +220,14 @@ protected:
virtual vtkDataObject* TransformInputData(
vtkInformationVector** inputVector, vtkDataObject* data);
/**
* Method to be overridden to apply an operation of the table after it is
* gathered to the first rank for rendering the chart. This allows subclasses
* to operate on the final table. The default implementation just returns the
* input.
*/
virtual vtkSmartPointer<vtkDataObject> TransformTable(vtkSmartPointer<vtkDataObject> table);
typedef std::map<std::string, vtkSmartPointer<vtkTable> > MapOfTables;
/**
* Convenience method to get all vtkTable instances with their associated
......
......@@ -69,6 +69,7 @@ vtkPVXYChartView::vtkPVXYChartView()
this->InternalTitle = NULL;
this->PlotTime = vtkPVPlotTime::New();
this->HideTimeMarker = false;
this->SortByXAxis = false;
// Use the buffer id - performance issues are fixed.
this->ContextView->GetScene()->SetUseBufferId(true);
......
......@@ -423,6 +423,15 @@ public:
vtkGetMacro(HideTimeMarker, bool);
//@}
//@{
/**
* Set whether to sort the data in the chart by the x-axis array so that line
* plot connectivity is from left to right rather than semi-randomly by index.
*/
vtkGetMacro(SortByXAxis, bool);
vtkSetMacro(SortByXAxis, bool);
//@}
/**
* Provides access to the chart view.
*/
......@@ -483,6 +492,7 @@ protected:
void SelectionChanged();
bool HideTimeMarker;
bool SortByXAxis;
private:
vtkPVXYChartView(const vtkPVXYChartView&) VTK_DELETE_FUNCTION;
......
......@@ -16,16 +16,58 @@
#include "vtkXYChartRepresentationInternals.h"
#include "vtkCommand.h"
#include "vtkCompositeDataIterator.h"
#include "vtkContextView.h"
#include "vtkDataSetAttributes.h"
#include "vtkInformation.h"
#include "vtkMultiBlockDataSet.h"
#include "vtkNew.h"
#include "vtkObjectFactory.h"
#include "vtkPVContextView.h"
#include "vtkPVXYChartView.h"
#include "vtkScalarsToColors.h"
#include "vtkSmartPointer.h"
#include "vtkSortFieldData.h"
#include "vtkTableAlgorithm.h"
#include "vtkWeakPointer.h"
#include <map>
#include <string>
class vtkXYChartRepresentation::SortTableFilter : public vtkTableAlgorithm
{
private:
char* ArrayToSortBy;
protected:
SortTableFilter()
: ArrayToSortBy(NULL)
{
}
~SortTableFilter() {}
public:
static SortTableFilter* New();
int RequestData(
vtkInformation*, vtkInformationVector** inVector, vtkInformationVector* outVector) VTK_OVERRIDE
{
vtkTable* in = vtkTable::GetData(inVector[0], 0);
vtkTable* out = vtkTable::GetData(outVector, 0);
if (!this->ArrayToSortBy)
{
vtkErrorMacro(<< "The array name to sort by must be set.");
return 0;
}
out->DeepCopy(in);
vtkSortFieldData::Sort(out->GetRowData(), this->ArrayToSortBy, 0, 0);
return 1;
}
vtkGetStringMacro(ArrayToSortBy);
vtkSetStringMacro(ArrayToSortBy);
};
vtkStandardNewMacro(vtkXYChartRepresentation::SortTableFilter);
//-----------------------------------------------------------------------------
#define vtkCxxSetChartTypeMacro(_name, _value) \
void vtkXYChartRepresentation::SetChartTypeTo##_name() { this->SetChartType(_value); }
......@@ -43,6 +85,7 @@ vtkXYChartRepresentation::vtkXYChartRepresentation()
, ChartType(vtkChart::LINE)
, XAxisSeriesName(NULL)
, UseIndexForXAxis(true)
, SortDataByXAxis(false)
, PlotDataHasChanged(false)
, SeriesLabelPrefix(NULL)
{
......@@ -80,6 +123,16 @@ void vtkXYChartRepresentation::PrintSelf(ostream& os, vtkIndent indent)
this->Superclass::PrintSelf(os, indent);
}
//----------------------------------------------------------------------------
void vtkXYChartRepresentation::SetSortDataByXAxis(bool val)
{
if (this->SortDataByXAxis == val)
{
return;
}
this->SortDataByXAxis = val;
this->MarkModified();
}
//----------------------------------------------------------------------------
void vtkXYChartRepresentation::SetVisibility(bool visible)
{
......@@ -234,6 +287,21 @@ void vtkXYChartRepresentation::ClearLabels()
this->Modified();
}
//----------------------------------------------------------------------------
int vtkXYChartRepresentation::ProcessViewRequest(
vtkInformationRequestKey* request_type, vtkInformation* inInfo, vtkInformation* outInfo)
{
if (request_type == vtkPVView::REQUEST_UPDATE())
{
vtkPVXYChartView* view = vtkPVXYChartView::SafeDownCast(inInfo->Get(vtkPVView::VIEW()));
if (view)
{
this->SetSortDataByXAxis(view->GetSortByXAxis());
}
}
return Superclass::ProcessViewRequest(request_type, inInfo, outInfo);
}
//----------------------------------------------------------------------------
int vtkXYChartRepresentation::RequestData(
vtkInformation* request, vtkInformationVector** inputVector, vtkInformationVector* outputVector)
......@@ -252,6 +320,22 @@ int vtkXYChartRepresentation::RequestData(
return 1;
}
//----------------------------------------------------------------------------
vtkSmartPointer<vtkDataObject> vtkXYChartRepresentation::TransformTable(
vtkSmartPointer<vtkDataObject> data)
{
if (!(this->SortDataByXAxis && this->XAxisSeriesName))
{
return Superclass::TransformTable(data);
}
vtkNew<SortTableFilter> sorter;
sorter->SetInputDataObject(data);
sorter->SetArrayToSortBy(this->XAxisSeriesName);
sorter->Update();
vtkSmartPointer<vtkDataObject> sortedTable = sorter->GetOutputDataObject(0);
return sortedTable;
}
//----------------------------------------------------------------------------
void vtkXYChartRepresentation::PrepareForRendering()
{
......
......@@ -92,6 +92,16 @@ public:
vtkGetMacro(UseIndexForXAxis, bool);
//@}
//@{
/**
* Get/set whether the points in the chart should be sorted by their x-axis value.
* Points are connected in line plots in the order they are in the table. Sorting
* by the x-axis allows the line to have no cycles.
*/
void SetSortDataByXAxis(bool val);
vtkGetMacro(SortDataByXAxis, bool);
//@}
//@{
/**
* Set/Clear the properties for Y series/columns.
......@@ -141,16 +151,23 @@ protected:
/**
* Overridden to remove all plots from the view.
*/
virtual bool RemoveFromView(vtkView* view) VTK_OVERRIDE;
bool RemoveFromView(vtkView* view) VTK_OVERRIDE;
int ProcessViewRequest(vtkInformationRequestKey* request_type, vtkInformation* inInfo,
vtkInformation* outInfo) VTK_OVERRIDE;
virtual int RequestData(vtkInformation*, vtkInformationVector**, vtkInformationVector*) VTK_OVERRIDE;
int RequestData(vtkInformation*, vtkInformationVector**, vtkInformationVector*) VTK_OVERRIDE;
virtual void PrepareForRendering() VTK_OVERRIDE;
vtkSmartPointer<vtkDataObject> TransformTable(vtkSmartPointer<vtkDataObject>) VTK_OVERRIDE;
void PrepareForRendering() VTK_OVERRIDE;
class vtkInternals;
friend class vtkInternals;
vtkInternals* Internals;
class SortTableFilter;
private:
vtkXYChartRepresentation(const vtkXYChartRepresentation&) VTK_DELETE_FUNCTION;
void operator=(const vtkXYChartRepresentation&) VTK_DELETE_FUNCTION;
......@@ -158,6 +175,7 @@ private:
int ChartType;
char* XAxisSeriesName;
bool UseIndexForXAxis;
bool SortDataByXAxis;
bool PlotDataHasChanged;
double SelectionColor[3];
char* SeriesLabelPrefix;
......
......@@ -194,6 +194,14 @@
value="8" />
</EnumerationDomain>
</IntVectorProperty>
<IntVectorProperty command="SetSortByXAxis"
default_values="0"
name="SortByXAxis"
label="Sort By X-Axis"
number_of_elements="1">
<BooleanDomain name="bool" />
<Documentation>Sort the chart data by the x-axis before deciding line plot connectivity.</Documentation>
</IntVectorProperty>
<IntVectorProperty command="SetLegendPosition"
default_values="0 0"
name="LegendPosition"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment