Commit 6d38a728 authored by Marcus D. Hanwell's avatar Marcus D. Hanwell

ENH: Added a new ChartMatrix class.

This class provides a matrix of charts where the width and the height
can be set. It takes care of instantiating the charts, and setting their
size to occupy individual elements in the matrix.

Change-Id: I9fa215c551a684ea2de1780b50e8e52c68c0b5c5
parent e4ff954b
......@@ -22,6 +22,7 @@ SET(Kit_SRCS
vtkChart.cxx
vtkChartLegend.cxx
vtkChartHistogram2D.cxx
vtkChartMatrix.cxx
vtkChartParallelCoordinates.cxx
vtkChartXY.cxx
vtkChartPie.cxx
......
......@@ -15,6 +15,7 @@ IF(VTK_USE_RENDERING AND VTK_USE_VIEWS)
TestBarGraph.cxx
TestBarGraphHorizontal.cxx
TestColorTransferFunction.cxx
TestChartMatrix.cxx
TestChartsOn3D.cxx
TestContext.cxx
TestContextImage.cxx
......
/*=========================================================================
Program: Visualization Toolkit
Module: TestLinePlot.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm 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.
=========================================================================*/
#include "vtkChartMatrix.h"
#include "vtkRenderWindow.h"
#include "vtkChartXY.h"
#include "vtkPlot.h"
#include "vtkTable.h"
#include "vtkFloatArray.h"
#include "vtkContextView.h"
#include "vtkContextScene.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkNew.h"
//----------------------------------------------------------------------------
int TestChartMatrix( int, char * [] )
{
// Set up a 2D scene, add an XY chart to it
vtkNew<vtkContextView> view;
view->GetRenderWindow()->SetSize(400, 300);
vtkNew<vtkChartMatrix> matrix;
view->GetScene()->AddItem(matrix.GetPointer());
matrix->SetSize(vtkVector2i(2, 2));
vtkChart *chart = matrix->GetChart(vtkVector2i(0, 0));
// Create a table with some points in it...
vtkNew<vtkTable> table;
vtkNew<vtkFloatArray> arrX;
arrX->SetName("X Axis");
table->AddColumn(arrX.GetPointer());
vtkNew<vtkFloatArray> arrC;
arrC->SetName("Cosine");
table->AddColumn(arrC.GetPointer());
vtkNew<vtkFloatArray> arrS;
arrS->SetName("Sine");
table->AddColumn(arrS.GetPointer());
vtkNew<vtkFloatArray> arrS2;
arrS2->SetName("Sine2");
table->AddColumn(arrS2.GetPointer());
vtkNew<vtkFloatArray> tangent;
tangent->SetName("Tangent");
table->AddColumn(tangent.GetPointer());
// Test charting with a few more points...
int numPoints = 69;
float inc = 7.5 / (numPoints-1);
table->SetNumberOfRows(numPoints);
for (int i = 0; i < numPoints; ++i)
{
table->SetValue(i, 0, i * inc);
table->SetValue(i, 1, cos(i * inc));
table->SetValue(i, 2, sin(i * inc));
table->SetValue(i, 3, sin(i * inc) + 0.5);
table->SetValue(i, 4, tan(i * inc));
}
// Add multiple line plots, setting the colors etc
vtkPlot *line = chart->AddPlot(vtkChart::POINTS);
line->SetInput(table.GetPointer(), 0, 1);
line->SetColor(0, 255, 0, 255);
chart = matrix->GetChart(vtkVector2i(0, 1));
line = chart->AddPlot(vtkChart::POINTS);
line->SetInput(table.GetPointer(), 0, 2);
line->SetColor(255, 0, 0, 255);
chart = matrix->GetChart(vtkVector2i(1, 0));
line = chart->AddPlot(vtkChart::LINE);
line->SetInput(table.GetPointer(), 0, 3);
line->SetColor(0, 0, 255, 255);
chart = matrix->GetChart(vtkVector2i(1, 1));
line = chart->AddPlot(vtkChart::POINTS);
line->SetInput(table.GetPointer(), 0, 4);
//Finally render the scene and compare the image to a reference image
view->GetRenderWindow()->SetMultiSamples(0);
view->GetInteractor()->Initialize();
view->GetInteractor()->Start();
return EXIT_SUCCESS;
}
/*=========================================================================
Program: Visualization Toolkit
Module: vtkChartMatrix.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm 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.
=========================================================================*/
#include "vtkChartMatrix.h"
#include "vtkChartXY.h"
#include "vtkSmartPointer.h"
#include "vtkContext2D.h"
#include "vtkContextScene.h"
#include "vtkObjectFactory.h"
#include <vector>
class vtkChartMatrix::PIMPL
{
public:
PIMPL() {}
~PIMPL() {}
std::vector< vtkSmartPointer<vtkChart> > Charts;
vtkVector2i Geometry;
};
vtkStandardNewMacro(vtkChartMatrix)
vtkChartMatrix::vtkChartMatrix()
{
this->Private = new PIMPL;
}
vtkChartMatrix::~vtkChartMatrix()
{
delete this->Private;
}
void vtkChartMatrix::Update()
{
}
bool vtkChartMatrix::Paint(vtkContext2D *painter)
{
if (this->GetScene()->GetSceneWidth() != this->Private->Geometry.X() ||
this->GetScene()->GetSceneHeight() != this->Private->Geometry.Y())
{
// Update the chart element positions
this->Private->Geometry.Set(this->GetScene()->GetSceneWidth(),
this->GetScene()->GetSceneHeight());
vtkVector2f increments(this->Private->Geometry.X() / this->Size.X(),
this->Private->Geometry.Y() / this->Size.Y());
for (int i = 0; i < this->Size.X(); ++i)
{
for (int j = 0; j < this->Size.Y(); ++j)
{
size_t index = j * this->Size.X() + i;
if (this->Private->Charts[index])
{
vtkChart *chart = this->Private->Charts[index];
chart->SetSize(vtkRectf(i * increments.X(),
j * increments.Y(),
increments.X(),
increments.Y()));
}
}
}
}
return Superclass::Paint(painter);
}
void vtkChartMatrix::SetSize(const vtkVector2i &size)
{
if (this->Size.X() != size.X() || this->Size.Y() != size.Y())
{
this->Size = size;
this->Private->Charts.resize(size.X() * size.Y());
}
}
void vtkChartMatrix::Allocate()
{
// Force allocation of all objects as vtkChartXY.
}
bool vtkChartMatrix::SetChart(const vtkVector2i &position, vtkChart *chart)
{
if (position.X() < this->Size.X() && position.Y() < this->Size.Y())
{
size_t index = position.Y() * this->Size.X() + position.X();
if (this->Private->Charts[index])
{
this->RemoveItem(this->Private->Charts[index]);
}
this->Private->Charts[index] = chart;
this->AddItem(chart);
chart->SetAutoSize(false);
return true;
}
else
{
return false;
}
}
vtkChart* vtkChartMatrix::GetChart(const vtkVector2i &position)
{
if (position.X() < this->Size.X() && position.Y() < this->Size.Y())
{
size_t index = position.Y() * this->Size.X() + position.X();
if (this->Private->Charts[index] == NULL)
{
vtkChart *chart = vtkChartXY::New();
this->Private->Charts[index] = chart;
this->AddItem(chart);
chart->SetAutoSize(false);
}
return this->Private->Charts[index];
}
else
{
return NULL;
}
}
void vtkChartMatrix::PrintSelf(ostream &os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
}
/*=========================================================================
Program: Visualization Toolkit
Module: vtkChartMatrix.h
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm 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 vtkChartMatrix - container for a matrix of charts.
//
// .SECTION Description
// This class contains a matrix of charts. These charts will be of type
// vtkChartXY by default, but this can be overridden. The class will manage
// their layout and object lifetime.
#ifndef __vtkChartMatrix_h
#define __vtkChartMatrix_h
#include "vtkAbstractContextItem.h"
#include "vtkVector.h" // For ivars
class vtkChart;
class VTK_CHARTS_EXPORT vtkChartMatrix : public vtkAbstractContextItem
{
public:
vtkTypeMacro(vtkChartMatrix, vtkAbstractContextItem);
virtual void PrintSelf(ostream &os, vtkIndent indent);
// Description:
// Creates a new object.
static vtkChartMatrix *New();
// Description:
// Perform any updates to the item that may be necessary before rendering.
virtual void Update();
// Description:
// Paint event for the chart matrix.
virtual bool Paint(vtkContext2D *painter);
// Description:
// Set the width and height of the chart matrix. This will cause an immediate
// resize of the chart matrix, the default size is 0x0 (no charts). No chart
// objects are created until Allocate is called.
virtual void SetSize(const vtkVector2i& size);
// Description:
// Get the width and height of the chart matrix.
virtual vtkVector2i GetSize() const { return this->Size; }
// Description:
// Allocate the charts, this will cause any null chart to be allocated.
virtual void Allocate();
// Description:
// Set the chart element, note that the chart matrix must be large enough to
// accommodate the element being set. Note that this class will take ownership
// of the chart object.
// \return false if the element cannot be set.
virtual bool SetChart(const vtkVector2i& position, vtkChart* chart);
// Description:
// Get the specified chart element, if the element does not exist NULL will be
// returned. If the chart element has not yet been allocated it will be at
// this point.
virtual vtkChart* GetChart(const vtkVector2i& position);
protected:
vtkChartMatrix();
~vtkChartMatrix();
class PIMPL;
PIMPL *Private;
vtkVector2i Size;
private:
vtkChartMatrix(const vtkChartMatrix &); // Not implemented.
void operator=(const vtkChartMatrix &); // Not implemented.
};
#endif //__vtkChartMatrix_h
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