8

I have made a chart on my form.

I want the user to see the value, x_value and y_value of each part in a balloon by clicking on that part.

The ballon shoud disappear when the user moves the mouse.

How can I do that?

Asma Good
  • 421
  • 3
  • 9
  • 19

1 Answers1

12

You could do something like this:

    ToolTip tooltip = new ToolTip();
    Point? clickPosition = null;

    void chart1_MouseMove(object sender, MouseEventArgs e)
    {
        if (clickPosition.HasValue && e.Location != clickPosition)
        {
            tooltip.RemoveAll();
            clickPosition = null;
        }
    }

    void chart1_MouseClick(object sender, MouseEventArgs e)
    {
        var pos = e.Location;
        clickPosition = 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);

                tooltip.Show("X=" + xVal + ", Y=" + yVal, 
                             this.chart1, e.Location.X,e.Location.Y - 15);
            }
        }
    }

Result:

enter image description here

EDIT :

to show the tooltip whenever the mouse move, you can use the following code:

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

            tooltip.Show("X=" + xVal + ", Y=" + yVal, this.chart1, 
                         pos.X, pos.Y - 15);
        }
    }
}

Note that this shows the tooltip on any position of the chart. If you want to show it only when the mouse is near to a series point, you can use a mschart functionality e.g. :

yourSeries.ToolTip = "X=#VALX, Y=#VALY";

(further examples here)

Community
  • 1
  • 1
digEmAll
  • 56,430
  • 9
  • 115
  • 140
  • Thanks a lot.When running the project nothing occures for me . The methods mouse_Move and mouse_Click aren't running.Should I add any thing to the chart? for excample"chart1.Mouseclick....." . Note that I have made the chart dinamically. – Asma Good Mar 10 '12 at 18:42
  • My last problem is solved.It worked well.another question:What if I want to use mouse_hover.Can you change the code so that the user can see the values by hovering the mouse? – Asma Good Mar 10 '12 at 19:16
  • Hi digEmAll again.How can I changew the code so that the user see the values only when the pointer id on the point? – Asma Good May 18 '12 at 06:41
  • @AsmaGood: well the code is basically the same, you just need to be more restrictive and don't show the tooltip if the mouse is not on a point. If you absolute can't, open a new question because we're going far from the original question here... – digEmAll May 18 '12 at 06:58
  • I'm getting a definition error on `var pos = e.Location;` – user2924019 Oct 02 '16 at 14:30
  • This code should work... If you have specific problems please open another question – digEmAll Oct 03 '16 at 07:48
  • Thanks for the code, all other various accepted answers from SO was not worked, but this one works fine! – sailfish009 Jul 31 '18 at 05:58