Commit 000878ab authored by Brian Helba's avatar Brian Helba
Browse files

ENH: Add customizable tooltips to vtkPlotHistogram2D

Note that the histogram TooltipDefaultLabelFormat is intended to be
different from that in a vtkPlot, as a histogram is a
"location -> scalar value" mapping, while a typical plot maps
"point name -> location".

Change-Id: I2c48070f44f1d8b3578dcc8ad1802401bc6a8acf
parent 630fab72
......@@ -55,11 +55,13 @@ void vtkChartHistogram2D::Update()
this->vtkChartXY::Update();
}
//-----------------------------------------------------------------------------
void vtkChartHistogram2D::SetInput(vtkImageData *data, vtkIdType z)
{
this->Histogram->SetInput(data, z);
}
//-----------------------------------------------------------------------------
void vtkChartHistogram2D::SetTransferFunction(vtkScalarsToColors *function)
{
this->Histogram->SetTransferFunction(function);
......@@ -101,6 +103,17 @@ bool vtkChartHistogram2D::Hit(const vtkContextMouseEvent &mouse)
}
}
//-----------------------------------------------------------------------------
vtkPlot* vtkChartHistogram2D::GetPlot(vtkIdType index)
{
if (index == 0)
{
return this->Histogram;
}
return 0;
}
//-----------------------------------------------------------------------------
void vtkChartHistogram2D::PrintSelf(ostream &os, vtkIndent indent)
{
......
......@@ -52,6 +52,10 @@ public:
// Return true if the supplied x, y coordinate is inside the item.
virtual bool Hit(const vtkContextMouseEvent &mouse);
// Description:
// Get the plot at the specified index, returns null if the index is invalid.
virtual vtkPlot* GetPlot(vtkIdType index);
protected:
vtkChartHistogram2D();
~vtkChartHistogram2D();
......
......@@ -14,9 +14,14 @@
=========================================================================*/
#include "vtkPlotHistogram2D.h"
#include "vtkAxis.h"
#include "vtkContext2D.h"
#include "vtkDoubleArray.h"
#include "vtkImageData.h"
#include "vtkMath.h"
#include "vtkScalarsToColors.h"
#include "vtkContext2D.h"
#include "vtkStringArray.h"
#include "vtkObjectFactory.h"
......@@ -26,6 +31,7 @@ vtkStandardNewMacro(vtkPlotHistogram2D);
//-----------------------------------------------------------------------------
vtkPlotHistogram2D::vtkPlotHistogram2D()
{
this->TooltipDefaultLabelFormat = "%x, %y: %v";
}
//-----------------------------------------------------------------------------
......@@ -118,6 +124,112 @@ vtkRectf vtkPlotHistogram2D::GetPosition()
return this->Position;
}
//-----------------------------------------------------------------------------
vtkIdType vtkPlotHistogram2D::GetNearestPoint(const vtkVector2f& point,
const vtkVector2f& tolerance,
vtkVector2f* location)
{
double bounds[4];
this->GetBounds(bounds);
double spacing[3];
this->Input->GetSpacing(spacing);
if (point.GetX() < bounds[0] ||
point.GetX() > bounds[1]+spacing[0] ||
point.GetY() < bounds[2] ||
point.GetY() > bounds[3]+spacing[1])
{
return -1;
}
// Can't use vtkImageData::FindPoint() / GetPoint(), as ImageData points are
// rendered as the bottom left corner of a histogram cell, not the center
int locX = vtkMath::Floor( (point.GetX() - bounds[0]) / spacing[0] );
int locY = vtkMath::Floor( (point.GetY() - bounds[2]) / spacing[1] );
int width = vtkMath::Ceil( (bounds[1] - bounds[0]) / spacing[0] ) + 1.0;
// Discretize to ImageData point values
location->SetX(locX * spacing[0] + bounds[0]);
location->SetY(locY * spacing[1] + bounds[2]);
return (locX + (locY * width));
}
//-----------------------------------------------------------------------------
vtkStdString vtkPlotHistogram2D::GetTooltipLabel(const vtkVector2f &plotPos,
vtkIdType seriesIndex,
vtkIdType)
{
// This does not call the Superclass vtkPlot::GetTooltipLabel(), since the
// format tags internally refer to different values
vtkStdString tooltipLabel;
vtkStdString &format = this->TooltipLabelFormat.empty() ?
this->TooltipDefaultLabelFormat : this->TooltipLabelFormat;
double bounds[4];
this->GetBounds(bounds);
int width = vtkMath::Ceil( (bounds[1] - bounds[0]) /
this->Input->GetSpacing()[0] ) + 1.0;
int pointX = seriesIndex % width;
int pointY = seriesIndex / width;
// Parse TooltipLabelFormat and build tooltipLabel
bool escapeNext = false;
for (size_t i = 0; i < format.length(); ++i)
{
if (escapeNext)
{
switch (format[i])
{
case 'x':
tooltipLabel += this->GetNumber(plotPos.X(), this->XAxis);
break;
case 'y':
tooltipLabel += this->GetNumber(plotPos.Y(), this->YAxis);
break;
case 'i':
if (this->XAxis->GetTickLabels() &&
pointX >= 0 &&
pointX < this->XAxis->GetTickLabels()->GetNumberOfTuples())
{
tooltipLabel += this->XAxis->GetTickLabels()->GetValue(pointX);
}
break;
case 'j':
if (this->YAxis->GetTickLabels() &&
pointY >= 0 &&
pointY < this->YAxis->GetTickLabels()->GetNumberOfTuples())
{
tooltipLabel += this->YAxis->GetTickLabels()->GetValue(pointY);
}
break;
case 'v':
tooltipLabel += this->GetNumber(this->Input->GetScalarComponentAsDouble(
pointX, pointY, 0, 0),
NULL);
break;
default: // If no match, insert the entire format tag
tooltipLabel += "%";
tooltipLabel += format[i];
break;
}
escapeNext = false;
}
else
{
if (format[i] == '%')
{
escapeNext = true;
}
else
{
tooltipLabel += format[i];
}
}
}
return tooltipLabel;
}
//-----------------------------------------------------------------------------
void vtkPlotHistogram2D::GenerateHistogram()
{
......
......@@ -75,6 +75,38 @@ public:
virtual vtkRectf GetPosition();
//BTX
// Description:
// Generate and return the tooltip label string for this plot
// The segmentIndex parameter is ignored.
// The member variable TooltipLabelFormat can be set as a
// printf-style string to build custom tooltip labels from,
// and may contain:
// An empty string generates the default tooltip labels.
// The following case-sensitive format tags (without quotes) are recognized:
// '%x' The X position of the histogram cell
// '%y' The Y position of the histogram cell
// '%v' The scalar value of the histogram cell
// Note: the %i and %j tags are valid only if there is a 1:1 correspondence
// between individual histogram cells and axis tick marks
// '%i' The X axis tick label for the histogram cell
// '%j' The Y axis tick label for the histogram cell
// Any other characters or unrecognized format tags are printed in the
// tooltip label verbatim.
virtual vtkStdString GetTooltipLabel(const vtkVector2f &plotPos,
vtkIdType seriesIndex,
vtkIdType segmentIndex);
// Description:
// Function to query a plot for the nearest point to the specified coordinate.
// Returns an index between 0 and (number of histogram cells - 1), or -1.
// The index 0 is at cell x=0, y=0 of the histogram, and the index increases
// in a minor fashon with x and in a major fashon with y.
// The referent of "location" is set to the x and y integer indices of the
// histogram cell.
virtual vtkIdType GetNearestPoint(const vtkVector2f& point,
const vtkVector2f& tolerance,
vtkVector2f* location);
protected:
vtkPlotHistogram2D();
~vtkPlotHistogram2D();
......
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