问题
I have a bit different kind of requirement for ZedGraph.
I want to create the curves on the ZedGraph pane when user clicks on the ZedGraph pane. Also, I have other graphs plotted on that pane. But i want that whenever user clicks on the zedGraph area, we get the co-ordinates where user have clicked and i draw a straigth line on that clicked co-ordinate.
I have used the MouseCLick event alogn with the FindNearestObject method like the following way:
private void zedGraph_RenderedTrack_MouseClick(object sender, EventArgs e)
{
MouseEventArgs xx = (MouseEventArgs)e;
object nearestObject;
int index;
this.zedGraph_RenderedTrack.GraphPane.FindNearestObject(new PointF(xx.X, xx.Y), this.CreateGraphics(), out nearestObject, out index);
if (nearestObject != null)
{
DrawALine(xx.X, Color.Red, true);
}
}
But using this, ZedGraph search for some curve and find the nearest points and then plot the line but i want that the line be drawn where ever the user clicks. Is there any method to do so?
回答1:
You could try the following code, which will draw a vertical line for a mouse click event.
public Form1()
{
InitializeComponent();
}
PointPairList userClickrList = new PointPairList();
LineItem userClickCurve = new LineItem("userClickCurve");
private void zedGraphControl1_MouseClick(object sender, MouseEventArgs e)
{
// Create an instance of Graph Pane
GraphPane myPane = zedGraphControl1.GraphPane;
// x & y variables to store the axis values
double xVal;
double yVal;
// Clear the previous values if any
userClickrList.Clear();
myPane.Legend.IsVisible = false;
// Use the current mouse locations to get the corresponding
// X & Y CO-Ordinates
myPane.ReverseTransform(e.Location, out xVal, out yVal);
// Create a list using the above x & y values
userClickrList.Add(xVal, myPane.YAxis.Scale.Max);
userClickrList.Add(xVal, myPane.YAxis.Scale.Min);
// Add a curve
userClickCurve = myPane.AddCurve(" ", userClickrList, Color.Red, SymbolType.None);
zedGraphControl1.Refresh();
}
you just have to change the userClickList to draw horizontal line.
Happy Coding.....:)
来源:https://stackoverflow.com/questions/12422398/drawing-line-on-a-click-on-zedgraph-pane