External/OxyPlot/OxyPlot.WindowsForms/PlotControl.cs
author moel.mich
Sat, 08 Jun 2013 17:06:00 +0000
changeset 392 4b43228a9894
permissions -rw-r--r--
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)
     4 //
     5 //   Copyright (c) 2012 Oystein Bjorke
     6 //
     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:
    14 //
    15 //   The above copyright notice and this permission notice shall be included
    16 //   in all copies or substantial portions of the Software.
    17 //
    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.
    25 // </copyright>
    26 // --------------------------------------------------------------------------------------------------------------------
    27 using System;
    28 using System.Collections.Generic;
    29 using System.ComponentModel;
    30 using System.Drawing;
    31 using System.Windows.Forms;
    32 using OxyPlot;
    33 
    34 namespace Oxyplot.WindowsForms
    35 {
    36     public class PlotControl : Control, IPlotControl
    37     {
    38         public List<MouseAction> MouseActions { get; private set; }
    39 
    40         private readonly PanAction panAction;
    41         private readonly SliderAction sliderAction;
    42         private readonly ZoomAction zoomAction;
    43         private Rectangle zoomRectangle;
    44 
    45         public PlotControl()
    46         {
    47             //    InitializeComponent();
    48             DoubleBuffered = true;
    49             Model = new PlotModel();
    50 
    51             panAction = new PanAction(this);
    52             zoomAction = new ZoomAction(this);
    53             sliderAction = new SliderAction(this);
    54 
    55             MouseActions = new List<MouseAction>();
    56             MouseActions.Add(panAction);
    57             MouseActions.Add(zoomAction);
    58             MouseActions.Add(sliderAction);
    59         }
    60 
    61         private PlotModel model;
    62 
    63         [Browsable(false), DefaultValue(null)]
    64         public PlotModel Model
    65         {
    66             get { return model; }
    67             set
    68             {
    69                 model = value;
    70                 Refresh();
    71             }
    72         }
    73 
    74         public override void Refresh()
    75         {
    76             if (model != null)
    77                 model.UpdateData();
    78             base.Refresh();
    79         }
    80 
    81         protected override void OnResize(EventArgs e)
    82         {
    83             base.OnResize(e);
    84             Invalidate();
    85         }
    86 
    87         protected override void OnPaint(PaintEventArgs e)
    88         {
    89             base.OnPaint(e);
    90 
    91             var rc = new GraphicsRenderContext(this, e.Graphics, e.ClipRectangle);
    92             if (model != null)
    93                 model.Render(rc);
    94             if (zoomRectangle != Rectangle.Empty)
    95             {
    96                 using (var zoomBrush = new SolidBrush(Color.FromArgb(0x40, 0xFF, 0xFF, 0x00)))
    97                 using (var zoomPen = new Pen(Color.Black))
    98                 {
    99                     zoomPen.DashPattern = new float[] { 3, 1 };
   100                     e.Graphics.FillRectangle(zoomBrush, zoomRectangle);
   101                     e.Graphics.DrawRectangle(zoomPen, zoomRectangle);
   102                 }
   103             }
   104         }
   105 
   106         protected override void OnKeyDown(KeyEventArgs e)
   107         {
   108             base.OnKeyDown(e);
   109             if (e.KeyCode == Keys.A)
   110             {
   111                 ZoomAll();
   112             }
   113         }
   114 
   115         public void ZoomAll()
   116         {
   117             foreach (var a in Model.Axes)
   118                 a.Reset();
   119             Refresh();
   120         }
   121 
   122         protected override void OnMouseDown(MouseEventArgs e)
   123         {
   124             base.OnMouseDown(e);
   125 
   126             Focus();
   127             Capture = true;
   128 
   129             bool control = Control.ModifierKeys == Keys.Control;
   130             bool shift = Control.ModifierKeys == Keys.Shift;
   131 
   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;
   141 
   142             var p = new ScreenPoint(e.X, e.Y);
   143             foreach (var a in MouseActions)
   144                 a.OnMouseDown(p, button, e.Clicks, control, shift);
   145         }
   146 
   147         protected override void OnMouseMove(MouseEventArgs e)
   148         {
   149             base.OnMouseMove(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);
   155         }
   156 
   157         protected override void OnMouseUp(MouseEventArgs e)
   158         {
   159             base.OnMouseUp(e);
   160             foreach (var a in MouseActions)
   161                 a.OnMouseUp();
   162             Capture = false;
   163         }
   164 
   165         protected override void OnMouseWheel(MouseEventArgs e)
   166         {
   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);
   173         }
   174 
   175         public void GetAxesFromPoint(ScreenPoint pt, out AxisBase xaxis, out AxisBase yaxis)
   176         {
   177             Model.GetAxesFromPoint(pt, out xaxis, out yaxis);
   178         }
   179 
   180         public DataSeries GetSeriesFromPoint(ScreenPoint pt, double limit)
   181         {
   182             return Model.GetSeriesFromPoint(pt, limit);
   183         }
   184 
   185         public void Refresh(bool refreshData)
   186         {
   187             if (refreshData)
   188                 Model.UpdateData();
   189             Invalidate();
   190         }
   191 
   192         public void Pan(AxisBase axis, double dx)
   193         {
   194             axis.Pan(dx);
   195         }
   196 
   197         public void Reset(AxisBase axis)
   198         {
   199             axis.Reset();
   200         }
   201 
   202         public void Zoom(AxisBase axis, double p1, double p2)
   203         {
   204             axis.Zoom(p1, p2);
   205         }
   206 
   207         public void ZoomAt(AxisBase axis, double factor, double x)
   208         {
   209             axis.ZoomAt(factor, x);
   210         }
   211 
   212         public OxyRect GetPlotArea()
   213         {
   214             return Model.PlotArea;
   215         }
   216 
   217         public void ShowSlider(DataSeries s, DataPoint dp)
   218         {
   219         }
   220 
   221         public void HideSlider()
   222         {
   223         }
   224 
   225         public void ShowZoomRectangle(OxyRect r)
   226         {
   227             zoomRectangle = new Rectangle((int)r.Left, (int)r.Top, (int)r.Width, (int)r.Height);
   228             Invalidate();
   229         }
   230 
   231         public void HideZoomRectangle()
   232         {
   233             zoomRectangle = Rectangle.Empty;
   234             Invalidate();
   235         }
   236     }
   237 }