Controlling tooltip display in a Flex PlotChart

Tooltips are a great feature of the Flex charting components but it's an all or nothing affair either all your points display them or none of them do. Here a way so only the points you decide show tooltips when you mouse over them.

One of the issues with the Flex charting component is that the source code is not included with the Flex SDK. However you can extract the source code by following the steps at this blog post by the Flex documentation team. Seeing what is going on inside the Charting classes is a great help.

Once you have the course code you can see that the tooltips are cached in the ChartBase class and that the cached array is private and without monkey patching the code there's going to be no way you can modify if or overwrite any of the protected functions that are involved. Could of been a better design but that's what we have to work with.

So after playing about with custom tooltip renderers, visibility of tooltips and quite a few other dead ends, I realised there was a far simpler solution and that was to overwrite the findDataPoints public method as this is the method that is used to find which points display tooltips.

From there it's a simple matter to get the list of points and simply removing the ones you don't want tooltips to display on. In the code below I've removed any point with a blank tooltip label. (Note: this code assumes that a dataTipFunction has been set up on the PlatChart.)

package com.classsoftware.charts
{
   import mx.charts.HitData;
   import mx.charts.PlotChart;

   public class PlotChartDataTips extends PlotChart
   {
      public function PlotChartDataTips()
      {
         super();
      }
      
      override public function findDataPoints(x:Number, y:Number):Array {
         var dataPoints:Array = super.findDataPoints(x,y);
         var nopoints:int = dataPoints.length;
         
         // remove points that have an empty label
         for (var i:int = nopoints-1; i > 0; i--) {
            var hitData:HitData = dataPoints[i] as HitData;
            var label:String = this.dataTipFunction(hitData);
            if (label == '' || label == null) {
               dataPoints.splice(i,1);
            }
         }
         
         return dataPoints;
      }
      
   }
}

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)