public static PlotModel ArrowAnnotation() { var model = new PlotModel("ArrowAnnotation", "Click and drag the arrow."); model.Axes.Add(new LinearAxis(AxisPosition.Bottom, -40, 60)); model.Axes.Add(new LinearAxis(AxisPosition.Left, -10, 10)); var arrow = new ArrowAnnotation { StartPoint = new DataPoint(8, 4), EndPoint = new DataPoint(0, 0), Text = "Move me!" }; var lastPoint = DataPoint.Undefined; bool moveStartPoint = false; bool moveEndPoint = false; var originalColor = OxyColors.White; // Handle left mouse clicks arrow.MouseDown += (s, e) => { if (e.ChangedButton != OxyMouseButton.Left) { return; } lastPoint = arrow.InverseTransform(e.Position); moveStartPoint = e.HitTestResult.Index != 2; moveEndPoint = e.HitTestResult.Index != 1; originalColor = arrow.Color; arrow.Color = OxyColors.Red; model.InvalidatePlot(false); e.Handled = true; }; // Handle mouse movements (note: this is only called when the mousedown event was handled) arrow.MouseMove += (s, e) => { var thisPoint = arrow.InverseTransform(e.Position); double dx = thisPoint.X - lastPoint.X; double dy = thisPoint.Y - lastPoint.Y; if (moveStartPoint) { arrow.StartPoint = new DataPoint(arrow.StartPoint.X + dx, arrow.StartPoint.Y + dy); } if (moveEndPoint) { arrow.EndPoint = new DataPoint(arrow.EndPoint.X + dx, arrow.EndPoint.Y + dy); } lastPoint = thisPoint; model.InvalidatePlot(false); e.Handled = true; }; // Handle mouse up (note: this is only called when the mousedown event was handled) arrow.MouseUp += (s, e) => { arrow.Color = originalColor; }; model.Annotations.Add(arrow); return model; }
public static PlotModel AddAnnotations() { var model = new PlotModel("Add arrow annotations", "Press and drag the left mouse button"); var xaxis = new LinearAxis(AxisPosition.Bottom); var yaxis = new LinearAxis(AxisPosition.Left); model.Axes.Add(xaxis); model.Axes.Add(yaxis); model.Series.Add(new FunctionSeries(x => Math.Sin(x / 4) * Math.Acos(Math.Sin(x)), 0, Math.PI * 8, 2000, "sin(x/4)*acos(sin(x))")); ArrowAnnotation tmp = null; // Add handlers to the PlotModel's mouse events model.MouseDown += (s, e) => { if (e.ChangedButton == OxyMouseButton.Left) { // Create a new arrow annotation tmp = new ArrowAnnotation(); tmp.StartPoint = tmp.EndPoint = Axis.InverseTransform(e.Position, xaxis, yaxis); model.Annotations.Add(tmp); e.Handled = true; } }; // Handle mouse movements (note: this is only called when the mousedown event was handled) model.MouseMove += (s, e) => { if (tmp != null) { // Modify the end point tmp.EndPoint = Axis.InverseTransform(e.Position, xaxis, yaxis); tmp.Text = string.Format("Y = {0:0.###}", tmp.EndPoint.Y); // Redraw the plot model.RefreshPlot(false); e.Handled = true; } }; model.MouseUp += (s, e) => { if (tmp != null) { tmp = null; e.Handled = true; } }; return model; }