Shuxia Ning
2024-12-10 cecd5e98941ff09c2e00b50d1c3e5733d2d9309c
WinFrmUI/Yw.WinFrmUI.Phart.Core/01-pump/01-single/04-run-view/PumpRunViewChart.cs
@@ -1,5 +1,6 @@
using DevExpress.Utils;
using DevExpress.Utils.Drawing;
using DevExpress.Utils.Html.Internal;
using DevExpress.XtraCharts;
namespace Yw.WinFrmUI.Phart
@@ -14,6 +15,8 @@
            InitializeComponent();
            InitialChart();
            this.chartControl1.RuntimeHitTesting = true;
            this.chartControl1.SelectionMode = ElementSelectionMode.Extended;
            this.chartControl1.SeriesSelectionMode = SeriesSelectionMode.Point;
        }
        #region Private Variable 
@@ -32,11 +35,55 @@
        private bool _qe_visible = true;
        private bool _qp_visible = true;
        private bool _run_visible = true;
        private bool _run_point_visible = true;
        private bool _initial_data = false;
        private PumpRunViewViewModel _vm = null;
        #endregion
        #region Public Variable
        /// <summary>
        /// 效率显示
        /// </summary>
        public bool QEVisible
        {
            get => _qe_visible;
            set
            {
                _qe_visible = value;
                SetQEVisible(_qe_visible);
            }
        }
        /// <summary>
        /// 功率显示
        /// </summary>
        public bool QPVisble
        {
            get => _qp_visible;
            set
            {
                _qp_visible = value;
                SetQPVisible(_qp_visible);
            }
        }
        /// <summary>
        /// 运行点显示
        /// </summary>
        public bool RunPointVisible
        {
            get => _run_point_visible;
            set
            {
                _run_point_visible = value;
            }
        }
        #endregion
@@ -46,6 +93,13 @@
        /// 坐标变换事件
        /// </summary>
        public event Action<PumpCoordinate> CoordinateChangedEvent = null;
        /// <summary>
        /// 运行点选择事件
        /// </summary>
        public event Action<PumpRunViewItemViewModel> RunPointSelectedEvent = null;
        #endregion
@@ -64,7 +118,7 @@
            _bottom_pane = (XYDiagramPane)_diagram.FindPaneByName("BottomPanel");
            _axis_x_flow = _diagram.AxisX;
            _axis_x_flow.SetAxisXQDisplay();
            _axis_x_flow.SetAxisXQDisplay();
            _axis_y_head = _diagram.AxisY;
            _axis_y_head.SetAxisYQHDisplay();
            _axis_y_eff = _diagram.SecondaryAxesY.GetAxisByName("AxisYQE");
@@ -73,7 +127,7 @@
            _axis_y_power = _diagram.SecondaryAxesY.GetAxisByName("AxisYQP");
            _axis_y_power.SetSecondaryAxisYQPDisplay();
            this.chartControl1.SetChartMonoColorDisplay();
            this.chartControl1.SetChartMonoColorDisplay();
            _axis_x_flow.Visibility = DefaultBoolean.False;
            _axis_x_flow.GridLines.Visible = false;
@@ -84,21 +138,41 @@
            _axis_y_power.Visibility = DefaultBoolean.False;
            _axis_y_power.GridLines.Visible = false;
            this.chartControl1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.chartControl1_MouseDown);
            this.chartControl1.CustomPaint += ChartControl1_CustomPaint;
        }
        private List<Tuple<Rectangle, Color, PumpRunViewItemViewModel>> _rect_list = new();
        private Point? _rect_point = null;
        private void chartControl1_MouseDown(object sender, MouseEventArgs e)
        {
            if (!_initial_data)
                return;
            if (!_run_point_visible)
                return;
            if (_rect_list == null || !_rect_list.Any())
                return;
            foreach (var rect in _rect_list)
            {
                if (rect.Item1.Contains(e.Location))
                {
                    _rect_point = new Point(rect.Item1.X + 5, rect.Item1.Y + 5);
                    this.RunPointSelectedEvent?.Invoke(rect.Item3);
                    break;
                }
            }
        }
        private void ChartControl1_CustomPaint(object sender, CustomPaintEventArgs e)
        {
            if (e is not DXCustomPaintEventArgs dxArgs)
                return;
            if (_vm == null)
                return;
            if (_vm.Items!=null&&_vm.Items.Any()&& _run_visible)
            _rect_list.Clear();
            if (_vm.Items != null && _vm.Items.Any() && _run_point_visible)
            {
                using Pen pen = new(Color.Black, 2);
                pen.DashStyle = System.Drawing.Drawing2D.DashStyle.DashDotDot;
                foreach (var vm in _vm.Items)
                {
                    var color = vm.Color;
@@ -106,21 +180,43 @@
                    var head = vm.H;
                    var eff = vm.E;
                    var power = vm.P;
                    DrawWorkPoint(dxArgs.Cache, color, _axis_y_head, flow, head);
                    if (eff.HasValue && _qe_visible)
                        DrawWorkPoint(dxArgs.Cache, color, _axis_y_eff, flow, eff.Value);
                    if (power.HasValue && _qp_visible)
                        DrawWorkPoint(dxArgs.Cache, color, _axis_y_power, flow, power.Value);
                }
            }
        }
                    {
                        var dg_pt = _diagram.DiagramToPoint(flow, head, _axis_x_flow, _axis_y_head);
                        var dg_x = dg_pt.Point.X - 5;
                        var dg_y = dg_pt.Point.Y - 5;
        private void DrawWorkPoint(GraphicsCache cache, Color color, AxisYBase axis_y, double x, double y)
        {
            var dg_pt = _diagram.DiagramToPoint(x, y, _axis_x_flow, axis_y);
            cache.FillEllipse((int)(dg_pt.Point.X - 5), (int)(dg_pt.Point.Y - 5), 10, 10, color);
                        dxArgs.Cache.FillEllipse(dg_x, dg_y, 10, 10, color);
                        _rect_list.Add(new(new Rectangle(dg_x, dg_y, 10, 10), color, vm));
                    }
                    if (eff.HasValue && _qe_visible)
                    {
                        var dg_pt = _diagram.DiagramToPoint(flow, eff.Value, _axis_x_flow, _axis_y_eff);
                        var dg_x = dg_pt.Point.X - 5;
                        var dg_y = dg_pt.Point.Y - 5;
                        dxArgs.Cache.FillEllipse(dg_x, dg_y, 10, 10, color);
                        _rect_list.Add(new(new Rectangle(dg_x, dg_y, 10, 10), color, vm));
                    }
                    if (power.HasValue && _qp_visible)
                    {
                        var dg_pt = _diagram.DiagramToPoint(flow, power.Value, _axis_x_flow, _axis_y_power);
                        var dg_x = dg_pt.Point.X - 5;
                        var dg_y = dg_pt.Point.Y - 5;
                        dxArgs.Cache.FillEllipse(dg_x, dg_y, 10, 10, color);
                        _rect_list.Add(new(new Rectangle(dg_x, dg_y, 10, 10), color, vm));
                    }
                }
                if (_rect_point.HasValue)
                {
                    var x = _rect_point.Value.X;
                    var y = _rect_point.Value.Y;
                    var length = 20;
                    using Pen pen = new Pen(Color.Magenta, 2);
                    dxArgs.Cache.DrawLine(pen, new Point(x - length, y), new Point(x + length, y));
                    dxArgs.Cache.DrawLine(pen, new Point(x, y - length), new Point(x, y + length));
                    dxArgs.Cache.DrawEllipse(pen, new Rectangle(x - 10, y - 10, length, length));
                }
            }
        }
         
        /// <summary>
@@ -129,8 +225,7 @@
        public void InitialChartData()
        {
            _initial_data = false;
            _coordinate = null;
            _coordinate = null;
            UpdateChart(false);
        }
@@ -164,6 +259,8 @@
        /// </summary>
        public void UpdateChart(bool calc_coordinate = false)
        {
            _rect_point = null;
            _rect_list = new ();
            if (calc_coordinate || _coordinate == null)
            {
                //不强迫计算,就用上次更新的坐标系
@@ -438,8 +535,7 @@
            this.chartControl1.EndInit();
        }
        /// <summary>
        /// 创建线系列
        /// </summary> 
@@ -574,8 +670,7 @@
            }
        }
        /// <summary>
        /// 创建线系列
        /// </summary> 
@@ -710,37 +805,9 @@
        #endregion
        #region Right Click Menu 
        #region Event
        private void barBtnSetChartAxis_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            SetChartAxis();
        }
        private void barCekCurveQEVisible_CheckedChanged(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            _qe_visible = this.barCekCurveQEVisible.Checked;
            UpdateChart();
        }
        private void barCekCurveQPVisible_CheckedChanged(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            _bottom_pane.Visibility = _bottom_pane.Visibility == ChartElementVisibility.Visible ? ChartElementVisibility.Hidden : ChartElementVisibility.Visible;
            _qp_visible = _bottom_pane.Visibility == ChartElementVisibility.Visible ? true : false;
            UpdateChart();
        }
        private void barCekCurveEQVisible_CheckedChanged(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            _run_visible = this.barCekCurveEQVisible.Checked;
        }
        #endregion
        /// <summary>
        /// 设置坐标轴
        /// </summary>
@@ -757,6 +824,36 @@
            dlg.ShowDialog();
        }
        /// <summary>
        /// 设置运行点显示
        /// </summary>
        public void SetEQVisible(bool visible)
        {
            _run_point_visible = visible;
        }
        /// <summary>
        /// 设置效率显示
        /// </summary>
        public void SetQEVisible(bool visible)
        {
            _qe_visible = visible;
            UpdateChart();
        }
        /// <summary>
        /// 设置功率显示
        /// </summary>
        public void SetQPVisible(bool visible)
        {
            _qp_visible = visible;
            _bottom_pane.Visibility = _qp_visible ? ChartElementVisibility.Visible : ChartElementVisibility.Hidden;
            UpdateChart();
        }
        #endregion