Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

zedgraph

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?

like image 254
Michał Kowalczyk Avatar asked Jul 19 '10 22:07

Michał Kowalczyk


1 Answers

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.

like image 51
Gacek Avatar answered Oct 27 '22 01:10

Gacek