using MuchInfo.Chart.WPF.Primitives; using System.Windows; using System.Windows.Controls; using System.Windows.Input; namespace MuchInfo.Chart.WPF.Controls.ValueScaling { /// /// ValueScaleBufferAdjuster.xaml 的交互逻辑 /// public partial class ValueScaleBufferAdjuster : UserControl { #region Fields private Chart _chart; //是否是顶部控件 private bool _isTopAdjuster; /// 鼠标是否位于到控件上面 private bool _mouseEnterFlag; /// 按住鼠标左键标志 private bool _mouseLeftFlag; private ValueScaleDisplay _rightValueScaleDisplay; //private ValueScalerArithmetic _valueScalerArithmetic; private ValueScaleDisplay _leftValueScaleDisplay; #endregion Fields #region Constructors /// /// 构造函数 /// public ValueScaleBufferAdjuster() { base.SizeChanged -= (new SizeChangedEventHandler(this.ValueScaleBufferAdjuster_SizeChanged)); base.SizeChanged += (new SizeChangedEventHandler(this.ValueScaleBufferAdjuster_SizeChanged)); this._isTopAdjuster = false; this._mouseLeftFlag = false; this._mouseEnterFlag = false; this.InitializeComponent(); this.Cursor = (Cursors.SizeNS); this.CenterLine.Visibility = Visibility.Collapsed; } #endregion Constructors #region Methods #region Public Methods /// /// 设置值 /// /// The chart. /// The left value scale display. /// The right value scale display. /// if set to true [is top]. public void SetValues(Chart chart, ValueScaleDisplay leftValueScaleDisplay, ValueScaleDisplay rightValueScaleDisplay, bool isTop) { this._leftValueScaleDisplay = leftValueScaleDisplay; this._rightValueScaleDisplay = rightValueScaleDisplay; this._isTopAdjuster = isTop; this._chart = chart; } #endregion Public Methods #region Private Methods /// /// Handles the LostMouseCapture event of the BorderMain control. /// /// The source of the event. /// The instance containing the event data. private void Layout_LostMouseCapture(object sender, MouseEventArgs e) { this._mouseLeftFlag = false; this.SetCenterLineVisibility(); } /// /// Handles the MouseEnter event of the BorderMain control. /// /// The source of the event. /// The instance containing the event data. private void Layout_MouseEnter(object sender, MouseEventArgs e) { this._mouseEnterFlag = true; this.SetCenterLineVisibility(); } /// /// Handles the MouseLeave event of the BorderMain control. /// /// The source of the event. /// The instance containing the event data. private void Layout_MouseLeave(object sender, MouseEventArgs e) { this._mouseEnterFlag = false; this.SetCenterLineVisibility(); } /// /// Handles the MouseLeftButtonDown event of the BorderMain control. /// /// The source of the event. /// The instance containing the event data. private void Layout_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { this._mouseLeftFlag = true; this.Layout.CaptureMouse(); this.SetCenterLineVisibility(); } /// /// Handles the MouseLeftButtonUp event of the BorderMain control. /// /// The source of the event. /// The instance containing the event data. private void Layout_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { if (_mouseLeftFlag) { this._mouseLeftFlag = false; this.Layout.ReleaseMouseCapture(); double y = e.GetPosition(this._rightValueScaleDisplay).Y; this.RefreshChart(y); this.SetCenterLineVisibility(); } } /// /// Handles the MouseMove event of the BorderMain control. /// /// The source of the event. /// The instance containing the event data. private void Layout_MouseMove(object sender, MouseEventArgs e) { if (_mouseLeftFlag) { double y = e.GetPosition(this._rightValueScaleDisplay).Y; this.RefreshChart(y); } } /// /// 刷新图表 /// /// Y轴值 private void RefreshChart(double y) { var canRefresh = default(bool); if (_isTopAdjuster) { if (this._leftValueScaleDisplay != null && this._leftValueScaleDisplay.CurrentValueScaler != null) { canRefresh = true; var leftArithmetic = this._leftValueScaleDisplay.CurrentValueScaler as ValueScalerArithmetic; if (leftArithmetic != null) { leftArithmetic.MaxAddPercent = (float)(100.0 - 100.0 * this._leftValueScaleDisplay.GetValuePercent(y)); } var rightArithmetic = this._rightValueScaleDisplay.CurrentValueScaler as ValueScalerArithmetic; if (rightArithmetic != null) { rightArithmetic.MaxAddPercent = (float)(100.0 - 100.0 * this._rightValueScaleDisplay.GetValuePercent(y)); } } } else { if (this._leftValueScaleDisplay != null && this._leftValueScaleDisplay.CurrentValueScaler != null) { canRefresh = true; var leftArithmetic = this._leftValueScaleDisplay.CurrentValueScaler as ValueScalerArithmetic; if (leftArithmetic != null) { leftArithmetic.MinAddPercent = (float)(100.0 * this._leftValueScaleDisplay.GetValuePercent(y)); } var rightArithmetic = this._rightValueScaleDisplay.CurrentValueScaler as ValueScalerArithmetic; if (rightArithmetic != null) { rightArithmetic.MinAddPercent = (float)(100.0 * this._rightValueScaleDisplay.GetValuePercent(y)); } } } if (canRefresh) { this._chart.Refresh(); } } /// /// 显示隐藏CenterLine /// private void SetCenterLineVisibility() { if (this._mouseEnterFlag || this._mouseLeftFlag) { this.CenterLine.Visibility = Visibility.Visible; } else { this.CenterLine.Visibility = Visibility.Collapsed; } } /// /// Handles the SizeChanged event of the ValueScaleBufferAdjuster control. /// /// The source of the event. /// The instance containing the event data. private void ValueScaleBufferAdjuster_SizeChanged(object sender, SizeChangedEventArgs e) { this.CenterLine.X2 = (this.FirstColumn.ActualWidth); } #endregion Private Methods #endregion Methods } }