[vtk-developers] Accessible tooltip info method in vtkChartXY

Eric E. Monson emonson at cs.duke.edu
Mon Aug 16 14:52:47 EDT 2010


Hey Marcus,

Here's another problem I'm running into as I'm trying to make my customized charts code more maintainable. Right now the code to pass data to the tooltip in vtkChartXY is buried in LocatePointInPlots(), which is a private method that needs to access the ChartPrivate data, so if I want a customized tooltip it seems like I need to re-implement too much of the vtkChartXY code. So, I was wondering if we could break out a method that can be overridden in a subclass more easily?

I'll paste in a diff of what I've been using. I'm afraid the signature for my new SetTooltipInfo method is a bit ugly, but I wanted to pass any data I thought people might use to make it generic. Personally, I am just passing the mouse position and "seriesIndex" to my tooltip, so for now anything that allows that will work for me.

Talk to you later,
-Eric

------------------------------------------------------
Eric E Monson
Duke Visualization Technology Group

===============================

diff --git a/Charts/vtkChartXY.cxx b/Charts/vtkChartXY.cxx
index 31ac0e2..d762d07 100644
--- a/Charts/vtkChartXY.cxx
+++ b/Charts/vtkChartXY.cxx
@@ -1064,13 +1064,8 @@ bool vtkChartXY::LocatePointInPlots(const vtkContextMouseEvent &mouse)
             int seriesIndex = plot->GetNearestPoint(position, tolerance, &plotPos);
             if (seriesIndex >= 0)
               {
-              const char *label = plot->GetLabel(seriesIndex);
-              // We found a point, set up the tooltip and return
-              vtksys_ios::ostringstream ostr;
-              ostr << label << ": " << plotPos.X() << ", " << plotPos.Y();
-              this->Tooltip->SetText(ostr.str().c_str());
-              this->Tooltip->SetPosition(mouse.ScreenPos[0]+2,
-                                         mouse.ScreenPos[1]+2);
+	            // We found a point, set up the tooltip and return
+              this->SetTooltipInfo(mouse, plotPos, seriesIndex, plot);
               return true;
               }
             }
@@ -1081,6 +1076,17 @@ bool vtkChartXY::LocatePointInPlots(const vtkContextMouseEvent &mouse)
   return false;
 }
 
+void vtkChartXY::SetTooltipInfo(const vtkContextMouseEvent& mouse, vtkVector2f plotPos, 
+                                int seriesIndex, vtkPlot* plot)
+{
+	const char *label = plot->GetLabel(seriesIndex);
+	vtksys_ios::ostringstream ostr;
+	ostr << label << ": " << plotPos.X() << ", " << plotPos.Y();
+	this->Tooltip->SetText(ostr.str().c_str());
+	this->Tooltip->SetPosition(mouse.ScreenPos[0]+2,
+														 mouse.ScreenPos[1]+2);
+}
+
 //-----------------------------------------------------------------------------
 bool vtkChartXY::MouseLeaveEvent(const vtkContextMouseEvent &)
 {
diff --git a/Charts/vtkChartXY.h b/Charts/vtkChartXY.h
index 7efd0a4..528dc72 100644
--- a/Charts/vtkChartXY.h
+++ b/Charts/vtkChartXY.h
@@ -22,6 +22,7 @@
 #define __vtkChartXY_h
 
 #include "vtkChart.h"
+#include "vtkVector.h"
 
 class vtkPlot;
 class vtkAxis;
@@ -32,6 +33,7 @@ class vtkChartLegend;
 class vtkTooltipItem;
 class vtkContextMouseEvent;
 class vtkDataArray;
+class vtkVecto2f;
 class vtkChartXYPrivate; // Private class to keep my STL vector in...
 
 class VTK_CHARTS_EXPORT vtkChartXY : public vtkChart
@@ -226,6 +228,10 @@ protected:
   // Indicate if the layout has changed in some way that would require layout
   // code to be called.
   bool LayoutChanged;
+  
+  // Description:
+  // Set the information passed to the tooltip
+  virtual void SetTooltipInfo(const vtkContextMouseEvent&, vtkVector2f, int, vtkPlot*);
 
 private:
   vtkChartXY(const vtkChartXY &); // Not implemented.




More information about the vtk-developers mailing list