Click here to Skip to main content
12,402,241 members (39,054 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# MSChart
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.
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:
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 14-May-12 5:53am
Updated 17-May-12 20:48pm
v5
Comments
SAKryukov 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
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

The chart control you're using has to support this feature. So, what chart control are you using?
  Permalink  
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.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

May be this Article could Help Smile | :) .. See the Tool Tip section or download the source for reference


A Guide to using MSChart for .NET[^]

honeyashu
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

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);
                            }
                        }
                    }
                }
            }
        }
  Permalink  
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160721.1 | Last Updated 27 May 2014
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100