tangxu
2024-11-25 0a2c59670b82d61d3fa79f51a54e82e7bd0134c4
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
 
namespace DPumpHydr.WinFrmUI.Volute
{
    public partial class ChartSectArea : UserControl
    {
        public ChartSectArea()
        {
            Section sect1 = new Section() { SectIndex = 1, Name = "1", Area = 300 };
            Section sect2 = new Section() { SectIndex = 2, Name = "2", Area = 200 };
            Section sect3 = new Section() { SectIndex = 3, Name = "3", Area = 320 };
            Section sect4 = new Section() { SectIndex = 4, Name = "4", Area = 460 };
            Section sect5 = new Section() { SectIndex = 5 , Name = "5", Area = 110 };
            Section sect6 = new Section() { SectIndex = 6, Name = "6", Area = 608 };
            Section sect7 = new Section() { SectIndex = 7 , Name = "7", Area = 708 };
            Section sect8 = new Section() { SectIndex = 8, Name = "8", Area = 819 };
 
            _allSection = new List<Section> { sect1, sect2, sect3, sect4, sect5, sect6, sect7, sect8 };
            var areaMaxValue = (from x in _allSection select x.Area).Max();
            var areaMinValue = (from x in _allSection select x.Area).Min();
            _areaAxisRange.CoordMin = areaMinValue/2;
            _areaAxisRange.CoordMax = areaMaxValue * 1.2;
        }
        class Section
        {
            public int SectIndex { get;set; }
            public string Name { get; set; }
            public double Area { get; set; }
        }
        class AreaAxisRange
        { 
            public double CoordMax { get; set; }//坐标上最大值
            public double CoordMin { get; set; }//坐标上最小值 
 
        }
        internal float CalcAreaPixelPt(double area)
        {
            var y = this._diagram_padding_top + this._diagram_padding_top + (_areaAxisRange.CoordMax - area) * (this._diagram_height - _diagram_padding_top - _diagram_padding_bottom) /(_areaAxisRange.CoordMax - _areaAxisRange.CoordMin);
 
            return Convert.ToSingle( y );
        }
        List<Section> _allSection;
        AreaAxisRange _areaAxisRange = new AreaAxisRange();
        /// <summary>
        /// 
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
 
 
            _diagram_width = this.Width - this._diagram_margin_left- this._diagram_margin_right;
            _diagram_height = this.Height - this._diagram_margin_top - this._diagram_margin_bottom;
            if(_diagram_height<=0 || _diagram_width <= 10)
            {
                return;
            }
            _diagram_leftTopPoint.X = this._diagram_margin_left;
            _diagram_leftTopPoint.Y = this._diagram_margin_top;
 
            _diagram_leftBottomPoint.X = this._diagram_margin_left;
            _diagram_leftBottomPoint.Y = this._diagram_margin_top + _diagram_height;
 
            _diagram_rightTopPoint.X = this._diagram_margin_left + _diagram_width;
            _diagram_rightTopPoint.Y = this._diagram_margin_top;
 
            _diagram_rightBottomPoint.X = this._diagram_margin_left + _diagram_width;
            _diagram_rightBottomPoint.Y = this._diagram_margin_top + _diagram_height;
 
 
            Graphics g = e.Graphics;
 
            DrawAxisX(g);
            DrawAxisY(g);
            DrawDiagram(g);
 
 
 
 
        }
       
        
        
        
        
        
        
        
        
        PointF _diagram_leftTopPoint = new PointF() ;
        PointF _diagram_leftBottomPoint = new PointF();
        PointF _diagram_rightTopPoint = new PointF();
        PointF _diagram_rightBottomPoint = new PointF();
 
        float _diagram_width = 0;
        float _diagram_height = 0;
        float _diagram_margin_left = 40;
        float _diagram_margin_right = 40;
        float _diagram_margin_top = 20;
        float _diagram_margin_bottom = 30;
 
        float _diagram_padding_left = 5;
        float _diagram_padding_right = 5;
        float _diagram_padding_top = 2 ;
        float _diagram_padding_bottom = 2 ;
 
        Color _tickmarkColorX = Color.Blue;//主刻度
        float _tickmarkLengthX = 5;
 
        private void DrawAxisX(Graphics g)
        { 
            StringFormat sf = new StringFormat();
            sf.Alignment = StringAlignment.Center;
            sf.LineAlignment = StringAlignment.Near;
 
            var sect_count = _allSection.Count;
            float space_width = (this._diagram_width - _diagram_padding_left - _diagram_padding_right) / (sect_count - 1);
 
            //刻度 tickmark
            using (SolidBrush brushText = new SolidBrush(Color.Red))
            using (Font fontText = new Font("Courier New", 11))
            using (Pen penAxisBase = new Pen(_tickmarkColorX, 2f))
            {
                for (int i = 0; i < sect_count; i++)
                {
                    var sect = _allSection[i];
                    var x = this._diagram_leftBottomPoint.X + _diagram_padding_left + i * space_width;
                    g.DrawLine(penAxisBase, new PointF(x, _diagram_leftBottomPoint.Y), new PointF(x, _diagram_leftBottomPoint.Y + _tickmarkLengthX));
               
                    g.DrawString(sect.Name, fontText, brushText, new PointF(x, _diagram_leftBottomPoint.Y + _tickmarkLengthX + 5), sf);
                }
 
                //基线
                g.DrawLine(penAxisBase, _diagram_leftBottomPoint, _diagram_rightBottomPoint);
            } 
        }
        private void DrawAxisY(Graphics g)
        {
            using (Pen penCurve = new Pen(Color.Blue, 2f))
            {
                g.DrawLine(penCurve, _diagram_leftBottomPoint, _diagram_leftTopPoint);
            }
        }
        private void DrawDiagram(Graphics g)
        {
            using (Pen penCurve = new Pen(Color.Red, 2f))
            using (SolidBrush penBrush = new SolidBrush(Color.Black))
            { 
                var sect_count = _allSection.Count;
                float space_width = (this._diagram_width - _diagram_padding_left - _diagram_padding_right) / (sect_count - 1);
                List<PointF> lines = new List<PointF>();
                for (int i = 0; i < sect_count; i++)
                {
                    var sect = _allSection[i];
                    PointF pt = new PointF();
                    pt.X = this._diagram_leftBottomPoint.X + _diagram_padding_left + i * space_width;
                    pt.Y = this.CalcAreaPixelPt(sect.Area);
                    g.FillEllipse(penBrush, pt.X - 4f, pt.Y - 4f, 8, 8);
                    lines.Add(pt);
                }
 
                g.DrawCurve(penCurve, lines.ToArray());
            }
            //
        }
    }
}