Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
201 views
in Technique[技术] by (71.8m points)

c# - finding the value of the points in a chart

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...