Commit c3415541 authored by Marcus D. Hanwell's avatar Marcus D. Hanwell
Browse files

ENH: Inherit from vtkChartXY for 2D histogram charts.

This gets rid of some duplicated code, and makes interaction etc work
from the start. I think some refactoring of the vtkChartXY class may be
necessary, as it is not as flexible as I had first hoped.
parent 6e4bb50c
......@@ -24,33 +24,8 @@
#include "vtkAxis.h"
#include "vtk2DHistogramItem.h"
#include "vtkColorLegend.h"
#include "vtkContextTransform.h"
#include "vtkSmartPointer.h"
#include "vtkTransform2D.h"
#include "vtkObjectFactory.h"
#include "vtkCommand.h"
#include <vector>
// Minimal storage class for STL containers etc.
class vtkChart2DHistogram::Private
{
public:
Private()
{
this->Transform = vtkSmartPointer<vtkContextTransform>::New();
}
~Private()
{
for(std::vector<vtkAxis *>::iterator it = this->Axes.begin();
it != this->Axes.end(); ++it)
{
(*it)->Delete();
}
}
vtkSmartPointer<vtkContextTransform> Transform;
std::vector<vtkAxis *> Axes;
};
//-----------------------------------------------------------------------------
vtkStandardNewMacro(vtkChart2DHistogram);
......@@ -58,20 +33,9 @@ vtkStandardNewMacro(vtkChart2DHistogram);
//-----------------------------------------------------------------------------
vtkChart2DHistogram::vtkChart2DHistogram()
{
this->Storage = new vtkChart2DHistogram::Private;
// Now for the 2D histogram
this->AddItem(this->Storage->Transform);
this->Histogram = vtkSmartPointer<vtk2DHistogramItem>::New();
this->Storage->Transform->AddItem(this->Histogram);
// Setup the axes
for (int i = 0; i < 2; ++i)
{
this->Storage->Axes.push_back(vtkAxis::New());
this->AddItem(this->Storage->Axes.back());
this->Storage->Axes[i]->SetPosition(i);
}
this->AddPlot(this->Histogram);
this->Legend = vtkSmartPointer<vtkColorLegend>::New();
this->AddItem(this->Legend);
......@@ -80,46 +44,14 @@ vtkChart2DHistogram::vtkChart2DHistogram()
//-----------------------------------------------------------------------------
vtkChart2DHistogram::~vtkChart2DHistogram()
{
delete this->Storage;
}
//-----------------------------------------------------------------------------
void vtkChart2DHistogram::Update()
{
for(std::vector<vtkAxis *>::iterator it = this->Storage->Axes.begin();
it != this->Storage->Axes.end(); ++it)
{
(*it)->Update();
}
this->Histogram->Update();
this->Legend->Update();
}
//-----------------------------------------------------------------------------
bool vtkChart2DHistogram::Paint(vtkContext2D *painter)
{
int geometry[] = { this->GetScene()->GetSceneWidth(),
this->GetScene()->GetSceneHeight() };
if (geometry[0] == 0 || geometry[1] == 0 || !this->Visible)
{
// The geometry of the chart must be valid before anything can be drawn
return false;
}
this->Update();
if (geometry[0] != this->Geometry[0] || geometry[1] != this->Geometry[1] ||
this->MTime > this->Storage->Axes[0]->GetMTime())
{
// Take up the entire window right now, this could be made configurable
this->SetGeometry(geometry);
}
this->UpdateGeometry();
this->PaintChildren(painter);
return true;
this->vtkChartXY::Update();
}
void vtkChart2DHistogram::SetInput(vtkImageData *data, vtkIdType z)
......@@ -134,69 +66,13 @@ void vtkChart2DHistogram::SetTransferFunction(vtkScalarsToColors *function)
}
//-----------------------------------------------------------------------------
vtkPlot* vtkChart2DHistogram::GetPlot(vtkIdType)
{
return NULL;
}
//-----------------------------------------------------------------------------
vtkIdType vtkChart2DHistogram::GetNumberOfPlots()
{
return 1;
}
//-----------------------------------------------------------------------------
vtkAxis* vtkChart2DHistogram::GetAxis(int index)
bool vtkChart2DHistogram::UpdateLayout(vtkContext2D *painter)
{
if (index < this->GetNumberOfAxes())
{
return this->Storage->Axes[index];
}
else
{
return NULL;
}
}
//-----------------------------------------------------------------------------
vtkIdType vtkChart2DHistogram::GetNumberOfAxes()
{
return this->Storage->Axes.size();
}
//-----------------------------------------------------------------------------
void vtkChart2DHistogram::UpdateGeometry()
{
this->SetBorders(20, 20, 60, 20);
double bounds[4];
this->Histogram->GetBounds(bounds);
vtkAxis *axis = this->Storage->Axes[vtkAxis::LEFT];
axis->SetRange(bounds[2], bounds[3]);
axis->SetPoint1(this->Point1[0], this->Point1[1]);
axis->SetPoint2(this->Point1[0], this->Point2[1]);
axis->AutoScale();
axis->Update();
axis = this->Storage->Axes[vtkAxis::BOTTOM];
axis->SetRange(bounds[0], bounds[1]);
axis->SetPoint1(this->Point1[0], this->Point1[1]);
axis->SetPoint2(this->Point2[0], this->Point1[1]);
axis->AutoScale();
axis->Update();
this->vtkChartXY::UpdateLayout(painter);
this->Legend->SetPosition(vtkRectf(this->Point2[0], this->Point1[1],
10, this->Point2[1] - this->Point1[1]));
this->CalculatePlotTransform(this->Storage->Axes[vtkAxis::BOTTOM],
this->Storage->Axes[vtkAxis::LEFT],
this->Storage->Transform->GetTransform());
}
//-----------------------------------------------------------------------------
void vtkChart2DHistogram::RecalculateBounds()
{
return;
this->Legend->Update();
return true;
}
//-----------------------------------------------------------------------------
......@@ -215,42 +91,6 @@ bool vtkChart2DHistogram::Hit(const vtkContextMouseEvent &mouse)
}
}
//-----------------------------------------------------------------------------
bool vtkChart2DHistogram::MouseEnterEvent(const vtkContextMouseEvent &)
{
return true;
}
//-----------------------------------------------------------------------------
bool vtkChart2DHistogram::MouseMoveEvent(const vtkContextMouseEvent &)
{
return true;
}
//-----------------------------------------------------------------------------
bool vtkChart2DHistogram::MouseLeaveEvent(const vtkContextMouseEvent &)
{
return true;
}
//-----------------------------------------------------------------------------
bool vtkChart2DHistogram::MouseButtonPressEvent(const vtkContextMouseEvent &)
{
return false;
}
//-----------------------------------------------------------------------------
bool vtkChart2DHistogram::MouseButtonReleaseEvent(const vtkContextMouseEvent &)
{
return false;
}
//-----------------------------------------------------------------------------
bool vtkChart2DHistogram::MouseWheelEvent(const vtkContextMouseEvent &, int)
{
return true;
}
//-----------------------------------------------------------------------------
void vtkChart2DHistogram::PrintSelf(ostream &os, vtkIndent indent)
{
......
......@@ -21,7 +21,7 @@
#ifndef __vtkChart2DHistogram_h
#define __vtkChart2DHistogram_h
#include "vtkChart.h"
#include "vtkChartXY.h"
#include "vtkSmartPointer.h" // For SP ivars
class vtkColorLegend;
......@@ -29,10 +29,10 @@ class vtk2DHistogramItem;
class vtkImageData;
class vtkScalarsToColors;
class VTK_CHARTS_EXPORT vtkChart2DHistogram : public vtkChart
class VTK_CHARTS_EXPORT vtkChart2DHistogram : public vtkChartXY
{
public:
vtkTypeMacro(vtkChart2DHistogram, vtkChart);
vtkTypeMacro(vtkChart2DHistogram, vtkChartXY);
virtual void PrintSelf(ostream &os, vtkIndent indent);
// Description:
......@@ -45,62 +45,13 @@ public:
// Paint function is invoked.
virtual void Update();
// Description:
// Paint event for the chart, called whenever the chart needs to be drawn
virtual bool Paint(vtkContext2D *painter);
virtual void SetInput(vtkImageData *data, vtkIdType z = 0);
virtual void SetTransferFunction(vtkScalarsToColors *function);
// Description:
// Get the plot at the specified index, returns null if the index is invalid.
virtual vtkPlot* GetPlot(vtkIdType index);
// Description:
// Get the number of plots the chart contains.
virtual vtkIdType GetNumberOfPlots();
// Description:
// Get the axis specified by axisIndex.
virtual vtkAxis* GetAxis(int axisIndex);
// Description:
// Get the number of axes in the current chart.
virtual vtkIdType GetNumberOfAxes();
// Description:
// Request that the chart recalculates the range of its axes. Especially
// useful in applications after the parameters of plots have been modified.
virtual void RecalculateBounds();
// Description:
// Return true if the supplied x, y coordinate is inside the item.
virtual bool Hit(const vtkContextMouseEvent &mouse);
// Description:
// Mouse enter event.
virtual bool MouseEnterEvent(const vtkContextMouseEvent &mouse);
// Description:
// Mouse move event.
virtual bool MouseMoveEvent(const vtkContextMouseEvent &mouse);
// Description:
// Mouse leave event.
virtual bool MouseLeaveEvent(const vtkContextMouseEvent &mouse);
// Description:
// Mouse button down event
virtual bool MouseButtonPressEvent(const vtkContextMouseEvent &mouse);
// Description:
// Mouse button release event.
virtual bool MouseButtonReleaseEvent(const vtkContextMouseEvent &mouse);
// Description:
// Mouse wheel event, positive delta indicates forward movement of the wheel.
virtual bool MouseWheelEvent(const vtkContextMouseEvent &mouse, int delta);
protected:
vtkChart2DHistogram();
~vtkChart2DHistogram();
......@@ -115,7 +66,7 @@ protected:
class Private;
Private* Storage;
void UpdateGeometry();
virtual bool UpdateLayout(vtkContext2D *painter);
private:
vtkChart2DHistogram(const vtkChart2DHistogram &); // Not implemented.
......
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