Click here to Skip to main content
15,886,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone!

The question is if it's possible to show a point's value of a c# chart when i'm with the cursor line over it.

For example this:

https://www.dropbox.com/s/ruynlkah2n259id/cursor%201.png[^]

This point is on the 53% of the scale but i don't know his real value(if, for example was from 0 to 999).

Thanks beforehead.


Regards.
Posted

Well, if scale is known, then it is very easy to compute the point actual value, e.g.
53% on 0-999 scale is 529 (int((999-0)/100*53)).
 
Share this answer
 
Comments
Acusu 12-Aug-13 5:28am    
Thanks for the response, but what i want to do is show this value in a tooltip, for example, when the mouse is over that point
CPallini 12-Aug-13 5:33am    
Then you have to get mouse position, transform it into position in graph and eventually transform the latter into actual (X,Y) values. The two transformations are really simple (translation and scale).
Thanks CPallini, the solution was in the coordinates. I found this, if it's useful for anyone else.

Regards!
----------------------------------------------------------------------------------------------------

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.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(pos.X - pointXPixel) < 2 &&
                    Math.Abs(pos.Y - pointYPixel) < 2)
                {
                    tooltip.Show("X=" + prop.XValue + ", Y=" + prop.YValues[0], this.chart1,
                                    pos.X, pos.Y - 15);
                }
            }
        }
    }
}
 
Share this answer
 

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