C ා drawing real-time curve

The curve display on the terminal machine this afternoon was intended to use a control, but the control has been tossed for a long time and has not been straightened out. It's better to write it by yourself, and then improve it after recording it.
//Definition of drawing part
Int32 Draw_Top; / / draw Y starting point
Int32 Draw_Left; / / draw X start point
Int32 draw? Edgewidth; / / X edge width
Int32 Draw_EdgeHeight;//Y edge height
Int32 draw? Rangewidth; / / drawing range width
Int32 draw? Rangeheight; / / drawing range height
Double[] XTDYData = new Double[21];
Int32 ICountDraw=0;

    Int32 IActualWidth, IActualHeight;
    Int32 Draw_AdjustX = 20;
    Int32 Draw_AdjustY = 20;
    Point StartPoint = new Point();
    Point EndPoint = new Point();
    Point ZeroPoint = new Point();
    Int32 IScalesY = 11;
    Int32 IScalesX = 21;
    Int32 IIncrementX;

    Pen pSpecial = new Pen(Color.Black,1);
    Pen pCommon = new Pen(Color.Black,2);
    Font FontTitle = new Font("Microsoft YaHei", 12);//Font to write title
    Font FontText = new Font("Microsoft YaHei", 9);//Font for body writing           
    SolidBrush SBTitle = new SolidBrush(Color.Red);
    SolidBrush SBText = new SolidBrush(Color.Black);

 
Draw the curve in the event of data receiving:
//Show trend chart
ICountDraw = ICountDraw + 1;
if (ICountDraw ==21)
{
for (int i=1;i<21;i++)
{
XTDYData[i - 1] = XTDYData[i];
}
XTDYData[20]= YBDWDealWithData.TYNDianYa.dValue;
ICountDraw = 20;
}
else
{
XTDYData[ICountDraw] = YBDWDealWithData.TYNDianYa.dValue;
}
FuncDrawYBDW();
Here are the basic functions:
public void FuncDrawYBDW() {
Bitmap BMP = new bitmap (draw'rangewidth, draw'rangeheight); / / defines the size of the canvas
Graphics graphics = Graphics.FromImage(bmp);
FuncDrawBasePic(ref graphics);
FuncDrawDynamicPic(ref graphics);
Graphics g = Graphics.FromHwnd(pictureBox1.Handle);
g.DrawImage(bmp, new Point(0, 0)); / / after drawing in memory, it will be displayed on pictureBox1 to avoid flickering
}

    public void FuncDrawBasePic(ref Graphics YBDWCanvas)
    {
        //Drawing foundation
        //Draw a box
        YBDWCanvas.FillRectangle(Brushes.WhiteSmoke, Draw_Top, Draw_Left, Draw_RangeWidth, Draw_RangeHeight);
        //Draw axis X
        pCommon.Width = 2;
        pCommon.Color = Color.Black;
        StartPoint.X = Draw_Left + Draw_AdjustX;
        StartPoint.Y = Draw_Top + Draw_RangeHeight - Draw_AdjustY * 2;
        EndPoint.X = Draw_Left + Draw_RangeWidth - Draw_AdjustX * 2;
        EndPoint.Y = Draw_Top + Draw_RangeHeight - Draw_AdjustY * 2;
        YBDWCanvas.DrawLine(pCommon, StartPoint, EndPoint);
        IActualWidth = EndPoint.X - StartPoint.X;
        YBDWCanvas.DrawString("X", FontText, SBText, EndPoint.X, EndPoint.Y - 8);
        //Arrow drawing
        StartPoint.X = EndPoint.X - 8;
        StartPoint.Y = EndPoint.Y - 4;
        YBDWCanvas.DrawLine(pCommon, StartPoint, EndPoint);
        StartPoint.X = EndPoint.X - 8;
        StartPoint.Y = EndPoint.Y + 4;
        YBDWCanvas.DrawLine(pCommon, StartPoint, EndPoint);
        //Draw axis Y
        pCommon.Width = 2;
        pCommon.Color = Color.Black;
        StartPoint.X = Draw_Left + Draw_AdjustX;
        StartPoint.Y = Draw_Top + Draw_AdjustY;
        EndPoint.X = Draw_Left + Draw_AdjustX;
        EndPoint.Y = Draw_Top + Draw_RangeHeight - Draw_AdjustY * 2;
        YBDWCanvas.DrawLine(pCommon, StartPoint, EndPoint);
        IActualHeight = EndPoint.Y - StartPoint.Y;
        ZeroPoint = EndPoint;
        //Painting arrow
        EndPoint.X = StartPoint.X - 4;
        EndPoint.Y = StartPoint.Y + 8;
        YBDWCanvas.DrawLine(pCommon, StartPoint, EndPoint);
        EndPoint.X = StartPoint.X + 4;
        EndPoint.Y = StartPoint.Y + 8;
        YBDWCanvas.DrawLine(pCommon, StartPoint, EndPoint);
        YBDWCanvas.DrawString("Y", FontText, SBText, EndPoint.X, EndPoint.Y - 8);
        //Draw X scale
        pSpecial.Width = 1;
        pSpecial.DashStyle = DashStyle.Custom;
        pSpecial.DashPattern = new float[] { 1f, 1f };
        IIncrementX = IActualWidth / IScalesX;
        for (int i = 1; i < IScalesX; i = i + 1)
        {
            StartPoint.X = ZeroPoint.X + IIncrementX * i;
            StartPoint.Y = ZeroPoint.Y - 2;
            EndPoint.X = StartPoint.X;
            EndPoint.Y = StartPoint.Y - IActualHeight - 2;
            YBDWCanvas.DrawLine(pSpecial, StartPoint, EndPoint);
            //Write identification word
            if (i % 2 == 0)
            {
                YBDWCanvas.DrawString(i.ToString(), FontText, SBText, StartPoint.X - 6, StartPoint.Y + 4);
            }
        }

        //Draw Y scale
        Int32 IIncrementY = IActualHeight / IScalesY;
        for (int i = 1; i < IScalesY; i = i + 1)
        {
            StartPoint.X = ZeroPoint.X;
            StartPoint.Y = ZeroPoint.Y - IIncrementY * i;
            EndPoint.X = StartPoint.X + IActualWidth;
            EndPoint.Y = StartPoint.Y;
            YBDWCanvas.DrawLine(pSpecial, StartPoint, EndPoint);
            //Write identification word
            if (i % 2 == 0)
            {
                YBDWCanvas.DrawString((i - 1).ToString(), FontText, SBText, StartPoint.X - 12, StartPoint.Y + 4);
            }
        }
        //Write title
        //Rectangle ybdwme * * ect = new rectangle (SX, sy, messagedotradius * 2, messagedotradius * 2); / / circle size
        //YBDWCanvas.FillEllipse(SBTitle, YBDWMe***ect);
        //Draw a connecting line
        YBDWCanvas.DrawString(SDrawTitle, FontTitle, SBTitle, 100,30);

    }

    public void FuncDrawDynamicPic(ref Graphics YBDWCanvas)
        {
        //Prepare data for actual line drawing
        Point[] XTDYPoints =new Point[21];//System voltage data
        for (int i = 0; i < IScalesX; i = i + 1)
        {
                XTDYPoints[i].X = ZeroPoint.X + IIncrementX * i;
                XTDYPoints[i].Y = ZeroPoint.Y - (int)((XTDYData[i]/IActualHeight)*6000);
        }
        pSpecial.Width = 3;
        pSpecial.Color = Color.Red;
        YBDWCanvas.DrawCurve(pSpecial, XTDYPoints, 3.0F);
    }

Actual rendering:

Click different text to display the corresponding real-time dynamic graph.

Tags: ASP.NET

Posted on Fri, 29 Nov 2019 22:05:48 -0800 by gabeanderson