Some modifications to the OxyPlot library to back-port to .NET 2.0. Added the LINQBridge library for the LINQ based code in OxyPlot (the original .NET LINQ is not available in .NET 2.0).
1 // --------------------------------------------------------------------------------------------------------------------
2 // <copyright file="PlotControl.cs" company="OxyPlot">
3 // The MIT License (MIT)
5 // Copyright (c) 2012 Oystein Bjorke
7 // Permission is hereby granted, free of charge, to any person obtaining a
8 // copy of this software and associated documentation files (the
9 // "Software"), to deal in the Software without restriction, including
10 // without limitation the rights to use, copy, modify, merge, publish,
11 // distribute, sublicense, and/or sell copies of the Software, and to
12 // permit persons to whom the Software is furnished to do so, subject to
13 // the following conditions:
15 // The above copyright notice and this permission notice shall be included
16 // in all copies or substantial portions of the Software.
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21 // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
22 // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 // --------------------------------------------------------------------------------------------------------------------
28 using System.Collections.Generic;
29 using System.ComponentModel;
31 using System.Windows.Forms;
34 namespace Oxyplot.WindowsForms
36 public class PlotControl : Control, IPlotControl
38 public List<MouseAction> MouseActions { get; private set; }
40 private readonly PanAction panAction;
41 private readonly SliderAction sliderAction;
42 private readonly ZoomAction zoomAction;
43 private Rectangle zoomRectangle;
47 // InitializeComponent();
48 DoubleBuffered = true;
49 Model = new PlotModel();
51 panAction = new PanAction(this);
52 zoomAction = new ZoomAction(this);
53 sliderAction = new SliderAction(this);
55 MouseActions = new List<MouseAction>();
56 MouseActions.Add(panAction);
57 MouseActions.Add(zoomAction);
58 MouseActions.Add(sliderAction);
61 private PlotModel model;
63 [Browsable(false), DefaultValue(null)]
64 public PlotModel Model
74 public override void Refresh()
81 protected override void OnResize(EventArgs e)
87 protected override void OnPaint(PaintEventArgs e)
91 var rc = new GraphicsRenderContext(this, e.Graphics, e.ClipRectangle);
94 if (zoomRectangle != Rectangle.Empty)
96 using (var zoomBrush = new SolidBrush(Color.FromArgb(0x40, 0xFF, 0xFF, 0x00)))
97 using (var zoomPen = new Pen(Color.Black))
99 zoomPen.DashPattern = new float[] { 3, 1 };
100 e.Graphics.FillRectangle(zoomBrush, zoomRectangle);
101 e.Graphics.DrawRectangle(zoomPen, zoomRectangle);
106 protected override void OnKeyDown(KeyEventArgs e)
109 if (e.KeyCode == Keys.A)
115 public void ZoomAll()
117 foreach (var a in Model.Axes)
122 protected override void OnMouseDown(MouseEventArgs e)
129 bool control = Control.ModifierKeys == Keys.Control;
130 bool shift = Control.ModifierKeys == Keys.Shift;
132 var button = OxyMouseButton.Left;
133 if (e.Button == MouseButtons.Middle)
134 button = OxyMouseButton.Middle;
135 if (e.Button == MouseButtons.Right)
136 button = OxyMouseButton.Right;
137 if (e.Button == MouseButtons.XButton1)
138 button = OxyMouseButton.XButton1;
139 if (e.Button == MouseButtons.XButton2)
140 button = OxyMouseButton.XButton2;
142 var p = new ScreenPoint(e.X, e.Y);
143 foreach (var a in MouseActions)
144 a.OnMouseDown(p, button, e.Clicks, control, shift);
147 protected override void OnMouseMove(MouseEventArgs e)
150 bool control = Control.ModifierKeys == Keys.Control;
151 bool shift = Control.ModifierKeys == Keys.Shift;
152 var p = new ScreenPoint(e.X, e.Y);
153 foreach (var a in MouseActions)
154 a.OnMouseMove(p, control, shift);
157 protected override void OnMouseUp(MouseEventArgs e)
160 foreach (var a in MouseActions)
165 protected override void OnMouseWheel(MouseEventArgs e)
167 base.OnMouseWheel(e);
168 bool control = Control.ModifierKeys == Keys.Control;
169 bool shift = Control.ModifierKeys == Keys.Shift;
170 var p = new ScreenPoint(e.X, e.Y);
171 foreach (var a in MouseActions)
172 a.OnMouseWheel(p, e.Delta, control, shift);
175 public void GetAxesFromPoint(ScreenPoint pt, out AxisBase xaxis, out AxisBase yaxis)
177 Model.GetAxesFromPoint(pt, out xaxis, out yaxis);
180 public DataSeries GetSeriesFromPoint(ScreenPoint pt, double limit)
182 return Model.GetSeriesFromPoint(pt, limit);
185 public void Refresh(bool refreshData)
192 public void Pan(AxisBase axis, double dx)
197 public void Reset(AxisBase axis)
202 public void Zoom(AxisBase axis, double p1, double p2)
207 public void ZoomAt(AxisBase axis, double factor, double x)
209 axis.ZoomAt(factor, x);
212 public OxyRect GetPlotArea()
214 return Model.PlotArea;
217 public void ShowSlider(DataSeries s, DataPoint dp)
221 public void HideSlider()
225 public void ShowZoomRectangle(OxyRect r)
227 zoomRectangle = new Rectangle((int)r.Left, (int)r.Top, (int)r.Width, (int)r.Height);
231 public void HideZoomRectangle()
233 zoomRectangle = Rectangle.Empty;