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?
ZedGraph C# bar chart - how to check which bar was clicked by mouse?
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.
© 2022 - 2024 — McMap. All rights reserved.
pt
based onpane.X/YAxisScale
andpane.Chart.Rect
; ReverseTransform reduces my 6 lines into one! Btw you can access the graph pane viaZedGraphControl.GraphPane
, and to avoid casting the sender you can register toZedGraphControl.MouseDown/UpEvent
whose delegate takes aZedGraphControl
as 1st parameter. – Precautious