Commit 52901137 authored by Julien Finet's avatar Julien Finet Committed by Marcus D. Hanwell
Browse files

Control mininimum and maximum limits of vtkAxis

Change-Id: Ifebfdc32dc4fb70145ef97693fc464b455943629
parent 81760a4c
......@@ -45,6 +45,7 @@ int TestScalarsToColors(int , char * [])
view->GetRenderWindow()->SetSize(400, 300);
vtkSmartPointer<vtkChartXY> chart = vtkSmartPointer<vtkChartXY>::New();
chart->SetTitle("Chart");
chart->ForceAxesToBoundsOn();
view->GetScene()->AddItem(chart);
vtkSmartPointer<vtkLookupTable> lookupTable =
......
......@@ -24,9 +24,10 @@
#include "vtkStringArray.h"
#include "vtkStdString.h"
#include "vtksys/ios/sstream"
#include "vtkObjectFactory.h"
#include <algorithm>
#include <limits>
#include "math.h"
//-----------------------------------------------------------------------------
......@@ -57,6 +58,8 @@ vtkAxis::vtkAxis()
this->TitleProperties->SetJustificationToCentered();
this->Minimum = 0.0;
this->Maximum = 6.66;
this->LowerLimit = std::numeric_limits<double>::max() * -1.;
this->HigherLimit = std::numeric_limits<double>::max();
this->LogScale = false;
this->GridVisible = true;
this->LabelsVisible = true;
......@@ -368,6 +371,7 @@ bool vtkAxis::Paint(vtkContext2D *painter)
//-----------------------------------------------------------------------------
void vtkAxis::SetMinimum(double minimum)
{
minimum = std::max(minimum, this->LowerLimit);
if (this->Minimum == minimum)
{
return;
......@@ -378,9 +382,24 @@ void vtkAxis::SetMinimum(double minimum)
this->Modified();
}
//-----------------------------------------------------------------------------
void vtkAxis::SetLowerLimit(double lowest)
{
if (this->LowerLimit == lowest)
{
return;
}
if (this->Minimum < lowest )
{
this->SetMinimum(lowest);
}
this->LowerLimit = lowest;
}
//-----------------------------------------------------------------------------
void vtkAxis::SetMaximum(double maximum)
{
maximum = std::min(maximum, this->HigherLimit);
if (this->Maximum == maximum)
{
return;
......@@ -391,6 +410,20 @@ void vtkAxis::SetMaximum(double maximum)
this->Modified();
}
//-----------------------------------------------------------------------------
void vtkAxis::SetHigherLimit(double highest)
{
if (this->HigherLimit == highest)
{
return;
}
if (this->Maximum < highest )
{
this->SetMaximum(highest);
}
this->HigherLimit = highest;
}
//-----------------------------------------------------------------------------
void vtkAxis::SetRange(double minimum, double maximum)
{
......
......@@ -115,6 +115,22 @@ public:
// Get the logical range of the axis, in plot coordinates.
virtual void SetRange(double minimum, double maximum);
// Description:
// Set the logical lowest possible value for \a Minimum, in plot coordinates.
void SetLowerLimit(double lowest);
// Description:
// Get the logical lowest possible value for \a Minimum, in plot coordinates.
vtkGetMacro(LowerLimit, double);
// Description:
// Set the logical highest possible value for \a Maximum, in plot coordinates.
virtual void SetHigherLimit(double highest);
// Description:
// Get the logical highest possible value for \a Maximum, in plot coordinates.
vtkGetMacro(HigherLimit, double);
// Description:
// Get/set the title text of the axis.
virtual void SetTitle(const vtkStdString &title);
......@@ -288,6 +304,8 @@ protected:
vtkTextProperty* LabelProperties; // Text properties for the labels.
double Minimum; // Minimum value of the axis
double Maximum; // Maximum values of the axis
double LowerLimit; // Lowest possible value for Minimum
double HigherLimit; // Highest possible value for Maximum
vtkStdString Title; // The text label drawn on the axis
vtkTextProperty* TitleProperties; // Text properties for the axis title
bool LogScale; // Should the axis use a log scale
......
......@@ -152,6 +152,8 @@ vtkChartXY::vtkChartXY()
this->Tooltip->SetVisible(false);
this->AddItem(this->Tooltip);
this->LayoutChanged = true;
this->ForceAxesToBounds = false;
}
//-----------------------------------------------------------------------------
......@@ -643,6 +645,11 @@ void vtkChartXY::RecalculatePlotBounds()
return;
}
if (this->ForceAxesToBounds)
{
axis->SetLowerLimit(range[0]);
axis->SetHigherLimit(range[1]);
}
if (axis->GetBehavior() == vtkAxis::AUTO && initialized[i])
{
axis->SetRange(range[0], range[1]);
......@@ -1126,6 +1133,12 @@ bool vtkChartXY::MouseMoveEvent(const vtkContextMouseEvent &mouse)
// Now move the axes and recalculate the transform
vtkAxis* xAxis = this->ChartPrivate->axes[vtkAxis::BOTTOM];
vtkAxis* yAxis = this->ChartPrivate->axes[vtkAxis::LEFT];
delta[0] = delta[0] > 0 ?
std::min(delta[0], xAxis->GetHigherLimit() - xAxis->GetMaximum()) :
std::max(delta[0], xAxis->GetLowerLimit() - xAxis->GetMinimum());
delta[1] = delta[1] > 0 ?
std::min(delta[1], yAxis->GetHigherLimit() - yAxis->GetMaximum()) :
std::max(delta[1], yAxis->GetLowerLimit() - yAxis->GetMinimum());
xAxis->SetMinimum(xAxis->GetMinimum() + delta[0]);
xAxis->SetMaximum(xAxis->GetMaximum() + delta[0]);
yAxis->SetMinimum(yAxis->GetMinimum() + delta[1]);
......@@ -1144,6 +1157,12 @@ bool vtkChartXY::MouseMoveEvent(const vtkContextMouseEvent &mouse)
// Now move the axes and recalculate the transform
xAxis = this->ChartPrivate->axes[vtkAxis::TOP];
yAxis = this->ChartPrivate->axes[vtkAxis::RIGHT];
delta[0] = delta[0] > 0 ?
std::min(delta[0], xAxis->GetHigherLimit() - xAxis->GetMaximum()) :
std::max(delta[0], xAxis->GetLowerLimit() - xAxis->GetMinimum());
delta[1] = delta[1] > 0 ?
std::min(delta[1], yAxis->GetHigherLimit() - yAxis->GetMaximum()) :
std::max(delta[1], yAxis->GetLowerLimit() - yAxis->GetMinimum());
xAxis->SetMinimum(xAxis->GetMinimum() + delta[0]);
xAxis->SetMaximum(xAxis->GetMaximum() + delta[0]);
yAxis->SetMinimum(yAxis->GetMinimum() + delta[1]);
......
......@@ -129,6 +129,14 @@ public:
vtkSetMacro(HiddenAxisBorder, int);
vtkGetMacro(HiddenAxisBorder, int);
// Description
// Force the axes to have their Minimum and Maximum properties inside the
// plot boundaries. It constrains pan and zoom interaction.
// False by default.
vtkSetMacro(ForceAxesToBounds, bool);
vtkGetMacro(ForceAxesToBounds, bool);
vtkBooleanMacro(ForceAxesToBounds, bool);
// Description:
// Set the width fraction for any bar charts drawn in this chart. It is
// assumed that all bar plots will use the same array for the X axis, and that
......@@ -257,6 +265,12 @@ protected:
// code to be called.
bool LayoutChanged;
// Description:
// Property to force the axes to have their Minimum and Maximum properties
// inside the plot boundaries. It constrains pan and zoom interaction.
// False by default.
bool ForceAxesToBounds;
private:
vtkChartXY(const vtkChartXY &); // Not implemented.
void operator=(const vtkChartXY &); // 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