ValueScaleBufferAdjuster.xaml.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. using MuchInfo.Chart.WPF.Primitives;
  2. using System.Windows;
  3. using System.Windows.Controls;
  4. using System.Windows.Input;
  5. namespace MuchInfo.Chart.WPF.Controls.ValueScaling
  6. {
  7. /// <summary>
  8. /// ValueScaleBufferAdjuster.xaml 的交互逻辑
  9. /// </summary>
  10. public partial class ValueScaleBufferAdjuster : UserControl
  11. {
  12. #region Fields
  13. private Chart _chart;
  14. //是否是顶部控件
  15. private bool _isTopAdjuster;
  16. /// 鼠标是否位于到控件上面
  17. private bool _mouseEnterFlag;
  18. /// 按住鼠标左键标志
  19. private bool _mouseLeftFlag;
  20. private ValueScaleDisplay _rightValueScaleDisplay;
  21. //private ValueScalerArithmetic _valueScalerArithmetic;
  22. private ValueScaleDisplay _leftValueScaleDisplay;
  23. #endregion Fields
  24. #region Constructors
  25. /// <summary>
  26. /// 构造函数
  27. /// </summary>
  28. public ValueScaleBufferAdjuster()
  29. {
  30. base.SizeChanged -= (new SizeChangedEventHandler(this.ValueScaleBufferAdjuster_SizeChanged));
  31. base.SizeChanged += (new SizeChangedEventHandler(this.ValueScaleBufferAdjuster_SizeChanged));
  32. this._isTopAdjuster = false;
  33. this._mouseLeftFlag = false;
  34. this._mouseEnterFlag = false;
  35. this.InitializeComponent();
  36. this.Cursor = (Cursors.SizeNS);
  37. this.CenterLine.Visibility = Visibility.Collapsed;
  38. }
  39. #endregion Constructors
  40. #region Methods
  41. #region Public Methods
  42. /// <summary>
  43. /// 设置值
  44. /// </summary>
  45. /// <param name="chart">The chart.</param>
  46. /// <param name="leftValueScaleDisplay">The left value scale display.</param>
  47. /// <param name="rightValueScaleDisplay">The right value scale display.</param>
  48. /// <param name="isTop">if set to <c>true</c> [is top].</param>
  49. public void SetValues(Chart chart, ValueScaleDisplay leftValueScaleDisplay, ValueScaleDisplay rightValueScaleDisplay, bool isTop)
  50. {
  51. this._leftValueScaleDisplay = leftValueScaleDisplay;
  52. this._rightValueScaleDisplay = rightValueScaleDisplay;
  53. this._isTopAdjuster = isTop;
  54. this._chart = chart;
  55. }
  56. #endregion Public Methods
  57. #region Private Methods
  58. /// <summary>
  59. /// Handles the LostMouseCapture event of the BorderMain control.
  60. /// </summary>
  61. /// <param name="sender">The source of the event.</param>
  62. /// <param name="e">The <see cref="MouseEventArgs"/> instance containing the event data.</param>
  63. private void Layout_LostMouseCapture(object sender, MouseEventArgs e)
  64. {
  65. this._mouseLeftFlag = false;
  66. this.SetCenterLineVisibility();
  67. }
  68. /// <summary>
  69. /// Handles the MouseEnter event of the BorderMain control.
  70. /// </summary>
  71. /// <param name="sender">The source of the event.</param>
  72. /// <param name="e">The <see cref="MouseEventArgs"/> instance containing the event data.</param>
  73. private void Layout_MouseEnter(object sender, MouseEventArgs e)
  74. {
  75. this._mouseEnterFlag = true;
  76. this.SetCenterLineVisibility();
  77. }
  78. /// <summary>
  79. /// Handles the MouseLeave event of the BorderMain control.
  80. /// </summary>
  81. /// <param name="sender">The source of the event.</param>
  82. /// <param name="e">The <see cref="MouseEventArgs"/> instance containing the event data.</param>
  83. private void Layout_MouseLeave(object sender, MouseEventArgs e)
  84. {
  85. this._mouseEnterFlag = false;
  86. this.SetCenterLineVisibility();
  87. }
  88. /// <summary>
  89. /// Handles the MouseLeftButtonDown event of the BorderMain control.
  90. /// </summary>
  91. /// <param name="sender">The source of the event.</param>
  92. /// <param name="e">The <see cref="MouseButtonEventArgs"/> instance containing the event data.</param>
  93. private void Layout_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  94. {
  95. this._mouseLeftFlag = true;
  96. this.Layout.CaptureMouse();
  97. this.SetCenterLineVisibility();
  98. }
  99. /// <summary>
  100. /// Handles the MouseLeftButtonUp event of the BorderMain control.
  101. /// </summary>
  102. /// <param name="sender">The source of the event.</param>
  103. /// <param name="e">The <see cref="MouseButtonEventArgs"/> instance containing the event data.</param>
  104. private void Layout_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  105. {
  106. if (_mouseLeftFlag)
  107. {
  108. this._mouseLeftFlag = false;
  109. this.Layout.ReleaseMouseCapture();
  110. double y = e.GetPosition(this._rightValueScaleDisplay).Y;
  111. this.RefreshChart(y);
  112. this.SetCenterLineVisibility();
  113. }
  114. }
  115. /// <summary>
  116. /// Handles the MouseMove event of the BorderMain control.
  117. /// </summary>
  118. /// <param name="sender">The source of the event.</param>
  119. /// <param name="e">The <see cref="MouseEventArgs"/> instance containing the event data.</param>
  120. private void Layout_MouseMove(object sender, MouseEventArgs e)
  121. {
  122. if (_mouseLeftFlag)
  123. {
  124. double y = e.GetPosition(this._rightValueScaleDisplay).Y;
  125. this.RefreshChart(y);
  126. }
  127. }
  128. /// <summary>
  129. /// 刷新图表
  130. /// </summary>
  131. /// <param name="y">Y轴值</param>
  132. private void RefreshChart(double y)
  133. {
  134. var canRefresh = default(bool);
  135. if (_isTopAdjuster)
  136. {
  137. if (this._leftValueScaleDisplay != null && this._leftValueScaleDisplay.CurrentValueScaler != null)
  138. {
  139. canRefresh = true;
  140. var leftArithmetic = this._leftValueScaleDisplay.CurrentValueScaler as ValueScalerArithmetic;
  141. if (leftArithmetic != null)
  142. {
  143. leftArithmetic.MaxAddPercent = (float)(100.0 - 100.0 * this._leftValueScaleDisplay.GetValuePercent(y));
  144. }
  145. var rightArithmetic = this._rightValueScaleDisplay.CurrentValueScaler as ValueScalerArithmetic;
  146. if (rightArithmetic != null)
  147. {
  148. rightArithmetic.MaxAddPercent = (float)(100.0 - 100.0 * this._rightValueScaleDisplay.GetValuePercent(y));
  149. }
  150. }
  151. }
  152. else
  153. {
  154. if (this._leftValueScaleDisplay != null && this._leftValueScaleDisplay.CurrentValueScaler != null)
  155. {
  156. canRefresh = true;
  157. var leftArithmetic = this._leftValueScaleDisplay.CurrentValueScaler as ValueScalerArithmetic;
  158. if (leftArithmetic != null)
  159. {
  160. leftArithmetic.MinAddPercent = (float)(100.0 * this._leftValueScaleDisplay.GetValuePercent(y));
  161. }
  162. var rightArithmetic = this._rightValueScaleDisplay.CurrentValueScaler as ValueScalerArithmetic;
  163. if (rightArithmetic != null)
  164. {
  165. rightArithmetic.MinAddPercent = (float)(100.0 * this._rightValueScaleDisplay.GetValuePercent(y));
  166. }
  167. }
  168. }
  169. if (canRefresh)
  170. {
  171. this._chart.Refresh();
  172. }
  173. }
  174. /// <summary>
  175. /// 显示隐藏CenterLine
  176. /// </summary>
  177. private void SetCenterLineVisibility()
  178. {
  179. if (this._mouseEnterFlag || this._mouseLeftFlag)
  180. {
  181. this.CenterLine.Visibility = Visibility.Visible;
  182. }
  183. else
  184. {
  185. this.CenterLine.Visibility = Visibility.Collapsed;
  186. }
  187. }
  188. /// <summary>
  189. /// Handles the SizeChanged event of the ValueScaleBufferAdjuster control.
  190. /// </summary>
  191. /// <param name="sender">The source of the event.</param>
  192. /// <param name="e">The <see cref="SizeChangedEventArgs"/> instance containing the event data.</param>
  193. private void ValueScaleBufferAdjuster_SizeChanged(object sender, SizeChangedEventArgs e)
  194. {
  195. this.CenterLine.X2 = (this.FirstColumn.ActualWidth);
  196. }
  197. #endregion Private Methods
  198. #endregion Methods
  199. }
  200. }