| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229 |
- using MuchInfo.Chart.WPF.Primitives;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Input;
- namespace MuchInfo.Chart.WPF.Controls.ValueScaling
- {
- /// <summary>
- /// ValueScaleBufferAdjuster.xaml 的交互逻辑
- /// </summary>
- 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
- /// <summary>
- /// 构造函数
- /// </summary>
- 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
- /// <summary>
- /// 设置值
- /// </summary>
- /// <param name="chart">The chart.</param>
- /// <param name="leftValueScaleDisplay">The left value scale display.</param>
- /// <param name="rightValueScaleDisplay">The right value scale display.</param>
- /// <param name="isTop">if set to <c>true</c> [is top].</param>
- 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
- /// <summary>
- /// Handles the LostMouseCapture event of the BorderMain control.
- /// </summary>
- /// <param name="sender">The source of the event.</param>
- /// <param name="e">The <see cref="MouseEventArgs"/> instance containing the event data.</param>
- private void Layout_LostMouseCapture(object sender, MouseEventArgs e)
- {
- this._mouseLeftFlag = false;
- this.SetCenterLineVisibility();
- }
- /// <summary>
- /// Handles the MouseEnter event of the BorderMain control.
- /// </summary>
- /// <param name="sender">The source of the event.</param>
- /// <param name="e">The <see cref="MouseEventArgs"/> instance containing the event data.</param>
- private void Layout_MouseEnter(object sender, MouseEventArgs e)
- {
- this._mouseEnterFlag = true;
- this.SetCenterLineVisibility();
- }
- /// <summary>
- /// Handles the MouseLeave event of the BorderMain control.
- /// </summary>
- /// <param name="sender">The source of the event.</param>
- /// <param name="e">The <see cref="MouseEventArgs"/> instance containing the event data.</param>
- private void Layout_MouseLeave(object sender, MouseEventArgs e)
- {
- this._mouseEnterFlag = false;
- this.SetCenterLineVisibility();
- }
- /// <summary>
- /// Handles the MouseLeftButtonDown event of the BorderMain control.
- /// </summary>
- /// <param name="sender">The source of the event.</param>
- /// <param name="e">The <see cref="MouseButtonEventArgs"/> instance containing the event data.</param>
- private void Layout_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
- {
- this._mouseLeftFlag = true;
- this.Layout.CaptureMouse();
- this.SetCenterLineVisibility();
- }
- /// <summary>
- /// Handles the MouseLeftButtonUp event of the BorderMain control.
- /// </summary>
- /// <param name="sender">The source of the event.</param>
- /// <param name="e">The <see cref="MouseButtonEventArgs"/> instance containing the event data.</param>
- 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();
- }
- }
- /// <summary>
- /// Handles the MouseMove event of the BorderMain control.
- /// </summary>
- /// <param name="sender">The source of the event.</param>
- /// <param name="e">The <see cref="MouseEventArgs"/> instance containing the event data.</param>
- private void Layout_MouseMove(object sender, MouseEventArgs e)
- {
- if (_mouseLeftFlag)
- {
- double y = e.GetPosition(this._rightValueScaleDisplay).Y;
- this.RefreshChart(y);
- }
- }
- /// <summary>
- /// 刷新图表
- /// </summary>
- /// <param name="y">Y轴值</param>
- 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();
- }
- }
- /// <summary>
- /// 显示隐藏CenterLine
- /// </summary>
- private void SetCenterLineVisibility()
- {
- if (this._mouseEnterFlag || this._mouseLeftFlag)
- {
- this.CenterLine.Visibility = Visibility.Visible;
- }
- else
- {
- this.CenterLine.Visibility = Visibility.Collapsed;
- }
- }
- /// <summary>
- /// Handles the SizeChanged event of the ValueScaleBufferAdjuster control.
- /// </summary>
- /// <param name="sender">The source of the event.</param>
- /// <param name="e">The <see cref="SizeChangedEventArgs"/> instance containing the event data.</param>
- private void ValueScaleBufferAdjuster_SizeChanged(object sender, SizeChangedEventArgs e)
- {
- this.CenterLine.X2 = (this.FirstColumn.ActualWidth);
- }
- #endregion Private Methods
- #endregion Methods
- }
- }
|