Shuxia Ning
2025-01-15 7d97c427cdd1893e09d9819463c6c13844e62a3a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
using Yw.Ahart;
 
namespace Yw.WinFrmUI.Phart
{
    public partial class UniversalChartExcelEditCtrl : DevExpress.XtraEditors.XtraUserControl
    {
        public UniversalChartExcelEditCtrl()
        {
            InitializeComponent();
            this.gridView1.SetDefaultEditView();
            this.gridView1.BorderStyle = DevExpress.XtraEditors.Controls.BorderStyles.NoBorder;
 
            this.repImgCmbFeatType.Items.Add("穿过点", Yw.Ahart.eFeatType.Through, -1); 
            this.repImgCmbFeatType.Items.Add("二次拟合", Yw.Ahart.eFeatType.Quadratic, -1);
            this.repImgCmbFeatType.Items.Add("三次拟合", Yw.Ahart.eFeatType.Cubic, -1);
            this.repImgCmbFeatType.Items.Add("四次拟合", Yw.Ahart.eFeatType.Quartic, -1);
 
            this.repImgCmbEditModel.Items.Add("鼠标", 0, -1);
            this.repImgCmbEditModel.Items.Add("键盘", 1, -1);
 
            this.universalEditChart1.DefinePointChangedEvent += UniversalEditChart1_DefinePointChangedEvent;
            this.universalEditChart1.SelectedPointIndexChangedEvent += (index) =>
            {
                this.gridView1.FocusedRowHandle = index;
            };
        }
 
 
 
        private Yw.Ahart.eFeatType _feat_type;
        private List<Yw.Geometry.Point2d> _def_pt_list = null;
 
        /// <summary>
        /// 绑定数据
        /// </summary> 
        public void SetBindingData(Yw.Ahart.eCurveType curve_type, List<Yw.Geometry.Point2d> def_pt_list, Yw.Ahart.eFeatType feat_type = Yw.Ahart.eFeatType.Cubic)
        {
            _def_pt_list = def_pt_list?.Select(t =>
            {
                var x = Math.Round(t.X, 2);
                var y = Math.Round(t.Y, 2);
                return new Yw.Geometry.Point2d(x, y);
            }).ToList();
 
            _feat_type = feat_type;
            _def_pt_list ??= new List<Geometry.Point2d>();
            SetChart(_feat_type, _def_pt_list);
 
            var (axis_x_title, axis_y_title) = PhartAxisTitleHelper.Get(curve_type);
            this.colX.Caption = axis_x_title;
            this.colY.Caption = axis_y_title;
 
             this.universalEditChart1.AxisXTitle = axis_x_title;
             this.universalEditChart1.AxisYTitle = axis_y_title;
 
            this.barEditModel.EditValue = 0;
            this.barFeatType.EditValue = _feat_type;
            this.bindingSource1.DataSource = _def_pt_list;
            this.bindingSource1.ResetBindings(false);
        }
 
 
        private void UniversalEditChart1_DefinePointChangedEvent(List<Geometry.Point2d> pt_list)
        {
            _def_pt_list.Clear();
            if (pt_list != null && pt_list.Any())
                _def_pt_list.AddRange(pt_list);
            this.bindingSource1.ResetBindings(false);
            SetChart(_feat_type, _def_pt_list);
        }
 
        //设置图表
        private void SetChart(Yw.Ahart.eFeatType feat_type, List<Yw.Geometry.Point2d> def_pt_list)
        {
            if (def_pt_list == null || def_pt_list.Count < 4)
            {
               this.universalEditChart1.Clear();
                return;
            }
            var fit_pt_list = def_pt_list.GetPointList(feat_type);
            this.universalEditChart1.SetBindingData(def_pt_list, fit_pt_list);
        }
 
 
        //值变换
        private void gridView1_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            SetChart(_feat_type, _def_pt_list);
        }
 
        //拟合类型
        private void barFeatType_EditValueChanged(object sender, EventArgs e)
        {
            _feat_type = (Yw.Ahart.eFeatType)this.barFeatType.EditValue;
            SetChart(_feat_type, _def_pt_list);
        }
 
        //鼠标模式
        private void barEditModel_EditValueChanged(object sender, EventArgs e)
        {
            var index = (int)this.barEditModel.EditValue;
           this.universalEditChart1.MouseModel = index == 0;
        }
 
        //添加点
        private void barAdd_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            double x = 0;
            double y = 1;
            if (_def_pt_list.Count > 0)
            {
                x = _def_pt_list.Last().X;
                y = _def_pt_list.Last().Y;
            }
 
            _def_pt_list.Add(new Geometry.Point2d(x, y));
            this.bindingSource1.ResetBindings(false);
            this.gridView1.FocusedRowHandle = _def_pt_list.Count - 1;
            SetChart(_feat_type, _def_pt_list);
        }
 
 
        //删除点
        private void btnDelete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (_def_pt_list == null || !_def_pt_list.Any())
                return; 
            var row = this.gridView1.GetCurrentViewModel(_def_pt_list);
            if (row == null)
                return; 
            var count = _def_pt_list.Count - 1;
            switch (_feat_type)
            {
                case Ahart.eFeatType.Cubic:
                    {
                        if (count < 4)
                        {
                            TipFormHelper.ShowInfo("点数少于4个点");
                            return;
                        }
                    }
                    break;
                case Ahart.eFeatType.Through:
                    {
                        if (count < 1)
                        {
                            TipFormHelper.ShowInfo("点数少于1个点");
                            return;
                        }
                    }
                    break;
                case Ahart.eFeatType.Quadratic:
                    {
                        if (count < 3)
                        {
                            TipFormHelper.ShowInfo("点数少于3个点");
                            return;
                        }
                    }
                    break;
                case Ahart.eFeatType.Quartic:
                    {
                        if (count < 5)
                        {
                            TipFormHelper.ShowInfo("点数少于5个点");
                            return;
                        }
                    }
                    break;
            }
 
            _def_pt_list.Remove(row);
            this.bindingSource1.ResetBindings(false);
            SetChart(_feat_type, _def_pt_list);
        }
 
        /// <summary>
        /// 获取数据
        /// </summary> 
        public bool Get(out Yw.Ahart.eFeatType feat_type, out List<Yw.Geometry.Point2d> pt_list)
        {
            feat_type = _feat_type;
            pt_list = _def_pt_list;
            if (_def_pt_list == null || !_def_pt_list.Any())
                return false;
            return true;
        }
 
 
    }
 
}