Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# WinForm 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 6:53am
Edited 17-May-12 21:48pm
v5
Comments
SAKryukov at 14-May-12 12:42pm
   
Good idea, my 5 for the question, but did you try anything?
--SA
Steve Echols at 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 at 18-May-12 2:10am
   
MsChart
Dave Kreskowiak at 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 at 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
0 Marcin Kozub 310
1 OriginalGriff 248
2 Sergey Alexandrovich Kryukov 215
3 Richard MacCutchan 202
4 Praneet Nadkar 197
0 OriginalGriff 8,048
1 Sergey Alexandrovich Kryukov 7,287
2 DamithSL 5,614
3 Manas Bhardwaj 4,986
4 Maciej Los 4,910


Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 27 May 2014
Copyright © CodeProject, 1999-2014
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