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());
|
}
|
//
|
}
|
}
|
}
|