C#繪制實(shí)時(shí)曲線圖的方法詳解
在終端機(jī)器上的曲線顯示本打算用控件,可控件折騰好長(zhǎng)時(shí)間也沒弄順,還是自己寫的好使,記錄下來后面再改進(jìn)。
//繪圖部分的定義
Int32 Draw_Top;//繪畫Y起點(diǎn)
Int32 Draw_Left;//繪畫X起點(diǎn)
Int32 Draw_EdgeWidth;//X邊緣寬度
Int32 Draw_EdgeHeight;//Y邊緣高度
Int32 Draw_RangeWidth;//繪畫范圍寬度
Int32 Draw_RangeHeight;//繪畫范圍高度
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("微軟雅黑", 12);//寫標(biāo)題的字體
Font FontText = new Font("微軟雅黑", 9);//寫正文的字體
SolidBrush SBTitle = new SolidBrush(Color.Red);
SolidBrush SBText = new SolidBrush(Color.Black);在數(shù)據(jù)接收的事件中進(jìn)行曲線繪制:
//顯示趨勢(shì)圖
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();下面是基礎(chǔ)函數(shù):
public void FuncDrawYBDW() {
Bitmap bmp = new Bitmap(Draw_RangeWidth, Draw_RangeHeight);//定義畫布的大小
Graphics graphics = Graphics.FromImage(bmp);
FuncDrawBasePic(ref graphics);
FuncDrawDynamicPic(ref graphics);
Graphics g = Graphics.FromHwnd(pictureBox1.Handle);
g.DrawImage(bmp, new Point(0, 0));//在內(nèi)存中畫完后顯示在pictureBox1上,避免閃爍
}
public void FuncDrawBasePic(ref Graphics YBDWCanvas)
{
//繪制基礎(chǔ)
//畫一個(gè)方框
YBDWCanvas.FillRectangle(Brushes.WhiteSmoke, Draw_Top, Draw_Left, Draw_RangeWidth, Draw_RangeHeight);
//畫坐標(biāo)軸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);
//畫箭頭
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);
//畫坐標(biāo)軸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;
//畫畫箭頭
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);
//畫X刻度
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);
//寫標(biāo)識(shí)字
if (i % 2 == 0)
{
YBDWCanvas.DrawString(i.ToString(), FontText, SBText, StartPoint.X - 6, StartPoint.Y + 4);
}
}
//畫Y刻度
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);
//寫標(biāo)識(shí)字
if (i % 2 == 0)
{
YBDWCanvas.DrawString((i - 1).ToString(), FontText, SBText, StartPoint.X - 12, StartPoint.Y + 4);
}
}
//寫標(biāo)題
//Rectangle YBDWMessRect = new Rectangle(SX, SY, MessageDotRadius * 2, MessageDotRadius * 2);//圓的大小
//YBDWCanvas.FillEllipse(SBTitle, YBDWMessRect);
//畫連接線
YBDWCanvas.DrawString(SDrawTitle, FontTitle, SBTitle, 100,30);
}
public void FuncDrawDynamicPic(ref Graphics YBDWCanvas)
{
//準(zhǔn)備實(shí)際畫線的數(shù)據(jù)
Point[] XTDYPoints =new Point[21];//系統(tǒng)電壓數(shù)據(jù)
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);
}實(shí)際效果圖:

點(diǎn)選不同的文字進(jìn)行相應(yīng)的實(shí)時(shí)動(dòng)態(tài)圖的顯示。
以上就是C#繪制實(shí)時(shí)曲線圖的方法詳解的詳細(xì)內(nèi)容,更多關(guān)于C#實(shí)時(shí)曲線圖的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C# 計(jì)算標(biāo)準(zhǔn)偏差相當(dāng)于Excel中的STDEV函數(shù)實(shí)例
下面小編就為大家?guī)硪黄狢# 計(jì)算標(biāo)準(zhǔn)偏差相當(dāng)于Excel中的STDEV函數(shù)實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-01-01
P/Invoke之C#調(diào)用動(dòng)態(tài)鏈接庫DLL示例詳解
這篇文章主要為大家介紹了P/Invoke之C#調(diào)用動(dòng)態(tài)鏈接庫DLL示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
基于C#實(shí)現(xiàn)語音識(shí)別功能詳解
在.NET4.0中,可以借助System.Speech組件讓電腦來識(shí)別我們的聲音。本文將利用該組件實(shí)現(xiàn)語音識(shí)別功能,文中實(shí)現(xiàn)過程講解詳細(xì),需要的可以參考一下2022-04-04
C#實(shí)現(xiàn)讀取Word表格到DataSet
在應(yīng)用項(xiàng)目里,多數(shù)情況下我們會(huì)遇到導(dǎo)入 Excel 文件數(shù)據(jù)到數(shù)據(jù)庫的功能需求,但某些情況下,也存在使用 Word 進(jìn)行表格數(shù)據(jù)編輯的情況,這其中也包括導(dǎo)入Word內(nèi)容的功能,比如表格數(shù)據(jù)導(dǎo)出到DataSet數(shù)據(jù)集,本文將給大家介紹了C#讀取Word表格到DataSet2023-12-12

