ZedGraph C# bar chart - how to check which bar was clicked by mouse?
Asked Answered
D

1

6

I am using ZedGraph to draw my plots in C#. I need to know which bar (in bar chart) was clicked by a mouse. How can I do that? Is there any way to get a bar by a point and for example change bar`s color?

Darrel answered 19/7, 2010 at 22:24 Comment(0)
F
4

Use MouseClick event and find the X and Y coordinates of the point where you clicked:

    zg1.MouseClick+=new MouseEventHandler(zg1_MouseClick3);



    private void zg1_MouseClick3(object sender, MouseEventArgs e)
    {
        PointF pt = (PointF)e.Location;
        double x,y;
        ((ZedGraphControl)sender).MasterPane[0].ReverseTransform(pt, out x, out y);

        // Do something with X and Y
    }

Note, that I assumed we are operating on first pane (index 0) but if it is not your case, then you'll have to find which pane was clicked (see this example).

When you have X and Y position you should easily be able to guess which bar was clicked and do whatever you need with that information.

Felton answered 20/7, 2010 at 11:16 Comment(1)
Wow, I used to compute this using an affine transformation of pt based on pane.X/YAxisScale and pane.Chart.Rect; ReverseTransform reduces my 6 lines into one! Btw you can access the graph pane via ZedGraphControl.GraphPane, and to avoid casting the sender you can register to ZedGraphControl.MouseDown/UpEvent whose delegate takes a ZedGraphControl as 1st parameter.Precautious

© 2022 - 2024 — McMap. All rights reserved.