Click here to Skip to main content
15,884,904 members
Please Sign up or sign in to vote.
3.71/5 (4 votes)
See more:
Hi.I have a chart in my project.I want the user to see the value of each point in a tooltip ,when hovering the mouse on it.How can I do that?
Edit:
Some One sent me the following codes,I tried but I couldn't improve it.
C#
Point? prevPosition = null; 
ToolTip tooltip = new ToolTip();  
void chart1_MouseMove(object sender, MouseEventArgs e) 
{
     var pos = e.Location;
     if (prevPosition.HasValue && pos == prevPosition.Value)
         return;
     tooltip.RemoveAll();
     prevPosition = pos;
     var results = chart1.HitTest(pos.X, pos.Y, false,                                  
 ChartElementType.PlottingArea);
     foreach (var result in results)
     { 
        if (result.ChartElementType == ChartElementType.PlottingArea)
         {
             var xVal = result.ChartArea.AxisX.PixelPositionToValue(pos.X);
                    var yVal = result.ChartArea.AxisY.PixelPositionToValue(pos.Y); 
                    tp.Show("X=" + xVal + ", Y=" + yVal, this.chart2, pos.X, pos.Y - 15);
         }
     }
 } 

It shows the values where ever I move the mouse.But I want the values be shown only when the pointer is near the series points.
Edit again:
C#
Point? prevPosition = null; 
ToolTip tooltip = new ToolTip();  
void chart1_MouseMove(object sender, MouseEventArgs e) 
{
     var pos = e.Location;
     if (prevPosition.HasValue && pos == prevPosition.Value)
         return;
     tooltip.RemoveAll();
     prevPosition = pos;
     var results = chart1.HitTest(pos.X, pos.Y, false,                                  
 ChartElementType.PlottingArea);
     foreach (var result in results)
     { 
        if (result.ChartElementType == ChartElementType.PlottingArea)
         {
             series2.ToolTip = "X=#VALX, Y=#VALY";
         }
     }
 } 


But I want the values be shown only when the pointer is on the series points.
Posted
Updated 17-May-12 20:48pm
v5
Comments
Sergey Alexandrovich Kryukov 14-May-12 12:42pm    
Good idea, my 5 for the question, but did you try anything?
--SA
Steve Echols 18-May-12 2:54am    
I think you need to determine what Object was hit (such as a datapoint).

http://msdn.microsoft.com/en-us/library/system.windows.forms.datavisualization.charting.hittestresult.aspx

The chart control you're using has to support this feature. So, what chart control are you using?
 
Share this answer
 
Comments
ready to learn 18-May-12 2:10am    
MsChart
Dave Kreskowiak 18-May-12 8:03am    
I believe the feature your want is not supported. You'll have to find another control to do this.
May be this Article could Help :).. See the Tool Tip section or download the source for reference


A Guide to using MSChart for .NET[^]

honeyashu
 
Share this answer
 
C#
void chart1_GetToolTipText(object sender, ToolTipEventArgs e)
        {   HitTestResult hitTestResult = chart1.HitTest(e.X, e.Y);
            
                if (hitTestResult.PointIndex >= 0 )
        if( hitTestResult.ChartElementType == ChartElementType.DataPoint)
        {    tooltip.RemoveAll();
              
                var results = chart1.HitTest(e.X, e.Y, false,
                                                   ChartElementType.DataPoint);
                foreach (var result in results)
                {
                    if (result.ChartElementType == ChartElementType.DataPoint)
                    {
                        var prop = result.Object as DataPoint;
                        if (prop != null)
                        {
                            var pointXPixel = result.ChartArea.AxisX.ValueToPixelPosition(prop.XValue);
                            var pointYPixel = result.ChartArea.AxisY.ValueToPixelPosition(prop.YValues[0]);

                            // check if the cursor is really close to the point (2 pixels around the point)
                            if (Math.Abs(e.X - pointXPixel) < 2 &&
                                Math.Abs(e.Y - pointYPixel) < 2)
                            {
                                tooltip.Show(prop.XValue +
                                    "," + prop.YValues[0], chart1,
                                    e.X, e.Y - 15);
                            }
                        }
                    }
                }
            }
        }
 
Share this answer
 
v3
Comments
Naz_Firdouse 27-May-14 7:29am    
added pre tags for readability

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900