PageRenderTime 43ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/Source/OxyPlot/Manipulators/ZoomRectangleManipulator.cs

http://oxyplot.codeplex.com
C# | 154 lines | 70 code | 19 blank | 65 comment | 14 complexity | b9a0ccd8b67030b8d64062feef9ad757 MD5 | raw file
  1. // --------------------------------------------------------------------------------------------------------------------
  2. // <copyright file="ZoomRectangleManipulator.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. // <summary>
  27. // The zoom manipulator.
  28. // </summary>
  29. // --------------------------------------------------------------------------------------------------------------------
  30. namespace OxyPlot
  31. {
  32. using System;
  33. /// <summary>
  34. /// Provides a plot control manipulator for zoom by rectangle functionality.
  35. /// </summary>
  36. public class ZoomRectangleManipulator : ManipulatorBase
  37. {
  38. /// <summary>
  39. /// Initializes a new instance of the <see cref="ZoomRectangleManipulator"/> class.
  40. /// </summary>
  41. /// <param name="plotControl">
  42. /// The plot control.
  43. /// </param>
  44. public ZoomRectangleManipulator(IPlotControl plotControl)
  45. : base(plotControl)
  46. {
  47. }
  48. /// <summary>
  49. /// Gets or sets the zoom rectangle.
  50. /// </summary>
  51. private OxyRect ZoomRectangle { get; set; }
  52. /// <summary>
  53. /// Occurs when a manipulation is complete.
  54. /// </summary>
  55. /// <param name="e">
  56. /// The <see cref="OxyPlot.ManipulationEventArgs"/> instance containing the event data.
  57. /// </param>
  58. public override void Completed(ManipulationEventArgs e)
  59. {
  60. base.Completed(e);
  61. this.PlotControl.HideZoomRectangle();
  62. if (this.ZoomRectangle.Width > 10 && this.ZoomRectangle.Height > 10)
  63. {
  64. DataPoint p0 = this.InverseTransform(this.ZoomRectangle.Left, this.ZoomRectangle.Top);
  65. DataPoint p1 = this.InverseTransform(this.ZoomRectangle.Right, this.ZoomRectangle.Bottom);
  66. if (this.XAxis != null)
  67. {
  68. this.PlotControl.Zoom(this.XAxis, p0.X, p1.X);
  69. }
  70. if (this.YAxis != null)
  71. {
  72. this.PlotControl.Zoom(this.YAxis, p0.Y, p1.Y);
  73. }
  74. this.PlotControl.InvalidatePlot();
  75. }
  76. }
  77. /// <summary>
  78. /// Occurs when the input device changes position during a manipulation.
  79. /// </summary>
  80. /// <param name="e">
  81. /// The <see cref="OxyPlot.ManipulationEventArgs"/> instance containing the event data.
  82. /// </param>
  83. public override void Delta(ManipulationEventArgs e)
  84. {
  85. base.Delta(e);
  86. OxyRect plotArea = this.PlotControl.ActualModel.PlotArea;
  87. double x = Math.Min(this.StartPosition.X, e.CurrentPosition.X);
  88. double w = Math.Abs(this.StartPosition.X - e.CurrentPosition.X);
  89. double y = Math.Min(this.StartPosition.Y, e.CurrentPosition.Y);
  90. double h = Math.Abs(this.StartPosition.Y - e.CurrentPosition.Y);
  91. if (this.XAxis == null)
  92. {
  93. x = plotArea.Left;
  94. w = plotArea.Width;
  95. }
  96. if (this.YAxis == null)
  97. {
  98. y = plotArea.Top;
  99. h = plotArea.Height;
  100. }
  101. this.ZoomRectangle = new OxyRect(x, y, w, h);
  102. this.PlotControl.ShowZoomRectangle(this.ZoomRectangle);
  103. }
  104. /// <summary>
  105. /// Gets the cursor for the manipulation.
  106. /// </summary>
  107. /// <returns>
  108. /// The cursor.
  109. /// </returns>
  110. public override CursorType GetCursorType()
  111. {
  112. if (this.XAxis == null)
  113. {
  114. return CursorType.ZoomVertical;
  115. }
  116. if (this.YAxis == null)
  117. {
  118. return CursorType.ZoomHorizontal;
  119. }
  120. return CursorType.ZoomRectangle;
  121. }
  122. /// <summary>
  123. /// Occurs when an input device begins a manipulation on the plot.
  124. /// </summary>
  125. /// <param name="e">
  126. /// The <see cref="OxyPlot.ManipulationEventArgs"/> instance containing the event data.
  127. /// </param>
  128. public override void Started(ManipulationEventArgs e)
  129. {
  130. base.Started(e);
  131. this.ZoomRectangle = new OxyRect(this.StartPosition.X, this.StartPosition.Y, 0, 0);
  132. this.PlotControl.ShowZoomRectangle(this.ZoomRectangle);
  133. }
  134. }
  135. }