C語言繪制曲線圖的示例代碼
更新時間:2024年02月25日 09:38:58 作者:老花眼貓
這篇文章主要介為大家詳細紹了如何使用C語言繪制統(tǒng)計圖中的曲線圖,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
統(tǒng)計圖曲線圖繪制方法
常用的統(tǒng)計圖有條形圖、柱形圖、折線圖、曲線圖、餅圖、環(huán)形圖、扇形圖。
前幾類圖比較容易繪制,餅圖環(huán)形圖繪制較難。
曲線圖的曲線繪制較難,今提供曲線圖的繪制方法供參考。
本方法采用C語言的最基本功能:
( 1) 繪圖功能畫線,畫圓,畫長方形。
(2) 界面美工設(shè)計,界面文字打印輸出。
代碼中有詳細的注釋,通俗易懂,一看就會。
效果圖


實現(xiàn)代碼
下面提供2個繪制曲線圖的代碼:
//變量: 可設(shè)置成全局變量或私有變量
Canvas cs ; //畫布,繪制圖表載體
float pi=3.1415926535 ;
float a ; //三角函數(shù) sin (a), cos (a),
float r ; //圓半徑 radius
int i, j, n ;
float x0,y0,x1,y1 ; //作圖坐標
float dx,dy ; //中心坐標
string ss, ss1, ss2 ; //打印文字
int p[6] ; //set data or input data
double pn ; //顯示數(shù)據(jù)
//*************************
CurveGraph6 (){ //曲線圖 6
cs.ClearDraw (0,src); //清屏
clearOutput();
selectStyle () ; //圖例樣式選項設(shè)置
cs.SetFillMode (1);//0不填色,1填色
cs.SetColor (255,250,250,250);
cs.DrawRect (0,4,720,600); //back board
cs.SetColor (255,140,140,140);
cs.DrawRect (24,24,706,586); //back
cs.SetColor (255,250,250,250);
cs.DrawRect (20,20,700,580); //back
cs.SetFillMode (0);//0不填色,1填色
cs.SetColor (255,0,0,240);
cs.DrawRect (20,20,700,580); //框線
cs.DrawRect (24,24,696,576); //框線
cs.SetFillMode (1);//0不填色,1填色
cs.SetStrokeWidth(1);
cs.SetColor(255,220,220,220);
for (j=0; j<=10 ; j++){
for (i=0; i<=7 ; i++){
dx=j*50+100 ;
dy=i*50+50 ;
cs.DrawRect(dx,dy,dx+49,dy+49); } }//grid
cs.SetTextStyle (0);
cs.SetColor(255,0,0,250);
cs.DrawRect (50,450,670,452); //X線
cs.DrawRect (99,48,101,450); //Y線
cs.SetColor(255,250,0,0);
cs.SetTextSize (18);
cs.DrawRect (90,249,670,251); //standard線
cs.DrawText ("0.75", 52 ,109) ;
cs.DrawText ("0.50", 52 ,159) ;
cs.DrawText ("0.25", 52 ,209) ;
cs.DrawText ("0", 65 ,259) ;
cs.DrawText ("-0.25", 50 ,309) ;
cs.DrawText ("-0.50", 50 ,359) ;
cs.DrawText ("-0.75", 50 ,409) ;
cs.SetTextStyle (0); //畫標尺
cs.SetTextSize (18);
cs.SetColor(255,50,80,80);
ss=" 0 30 60 90 120 150 " ;
ss1=" 180 210 240 270 300 330" ;
cs.DrawText (ss,90,475) ;
cs.DrawText (ss1,380,475) ; //standard
cs.SetTextSize (24);
cs.DrawText ("Sec >",30, 475) ;
//draw Curve graphics 繪制曲線
//* curve : x1 +5 標尺x軸 0 位, bs*5 步長
//x0 曲線幅寬, + -pn y軸峰谷弧高, y+300 x軸y定位
// pn= 0 -- 200 高,bs=50*2
int bs ; //步長
//p[1]=150; p[2]=80; p[3]=100; p[4]=50; p[5]=140;
cs.SetColor (255,0,120,0);
for (n=1; n<=5; n++){
bs=n*100-100 ;
//pn=p[n] ;
Rn=random ()*90+50 ;
pn=(int) Rn ;
for (i=120;i<240;i++){
a=pi/360*i*6 ; //360 曲線點密度
x0=50;
// pn=170 ;
x1=(float)((x0/pi*a)+bs);
y1=(float)(- pn*sin(a)+250);
cs.DrawCircle(x1,y1,2); } }
cs.Update ();
sleep (300) ;
for (n=1; n<=5; n++){
bs=n*100-100 ;
Rn=random ()*40+40 ;
pn=(int) Rn ;
for (i=120;i<240;i++){
a=pi/360*i*6 ; //360 曲線點密度
x0=50;
x1=(float)((x0/pi*a)+bs);
y1=(float)(- pn*sin(a)+250);
cs.SetColor (255,250,100,160);
cs.DrawCircle(x1,y1,1);
cs.SetColor (255,250,220,150);
cs.DrawLine (x1,y1,x1,250) ; } }
//draw title
cs.SetFillMode (1);//0不填色,1填色
cs.SetTextStyle (1);
cs.SetStrokeWidth(1);
cs.SetTextSize (26);
cs.SetColor (255,0,220,150);
cs.DrawText ("Curve Chart ??",470,80) ;
cs.SetTextSize (50);
ss="統(tǒng)計圖 - 曲線圖" ;
cs.SetColor(255,50,120,20); //立體字
cs.DrawText (ss,164,534); //陰影
cs.SetColor(255,0,250,0);
cs.DrawText (ss,160,530); //本字
cs.SetFillMode (0);//0不填色,1填色
cs.SetColor(255,250,150,0);
cs.DrawText (ss,160,530); //框線
cs.Update ();
}//CurveGraph6 ()
CurveGraph7 (){ //曲線圖 7:填充區(qū)域
cs.ClearDraw (0,src); //清屏
clearOutput();
selectStyle () ; //圖例樣式選項設(shè)置
cs.SetFillMode (1);//0不填色,1填色
cs.SetColor (255,250,250,250);
cs.DrawRect (0,4,720,600); //back board
cs.SetColor (255,140,140,140);
cs.DrawRect (24,24,706,586); //back
cs.SetColor (255,250,250,250);
cs.DrawRect (20,20,700,580); //back
cs.SetFillMode (0);//0不填色,1填色
cs.SetColor (255,0,0,240);
cs.DrawRect (20,20,700,580); //框線
cs.DrawRect (24,24,696,576); //框線
cs.SetFillMode (1);//0不填色,1填色
cs.SetStrokeWidth(1);
cs.SetColor(255,220,220,220);
for (j=0; j<=10 ; j++){
for (i=0; i<=7 ; i++){
dx=j*50+100 ;
dy=i*50+50 ;
cs.DrawRect(dx,dy,dx+49,dy+49); } }//grid
cs.SetTextStyle (0);
cs.SetTextSize (18);
cs.SetColor(255,0,0,250);
cs.DrawRect (50,450,670,452); //X線
cs.DrawRect (99,48,101,450); //Y線
cs.SetColor(255,250,0,0);
cs.DrawRect (90,249,670,251); //standard線
cs.DrawText ("0.75", 52 ,107) ;
cs.DrawText ("0.50", 52 ,157) ;
cs.DrawText ("0.25", 52 ,207) ;
cs.DrawText ("0", 65 ,257) ;
cs.DrawText ("-0.25", 50 ,307) ;
cs.DrawText ("-0.50", 50 ,357) ;
cs.DrawText ("-0.75", 50 ,407) ;
cs.SetTextStyle (1); //畫標尺
cs.SetTextSize (18);
cs.SetColor(255,0,0,0);
ss=" 0 30 60 90 120 150 " ;
ss1=" 180 210 240 270 300 330" ;
cs.DrawText (ss,90,475) ;
cs.DrawText (ss1,380,475) ; //standard
cs.SetTextSize (24);
cs.DrawText ("Sec >",30, 475) ;
//draw Curve graphics 繪制曲線
//* curve : x1-bs 標尺x軸 0 位, bs 幅寬
//x0 X軸 0位, + -pn y軸峰谷弧高, y+250 x軸y定位
// pn= 0 -- 200,+ - pn> 正弦余弦。bs=幅寬
int bs ; //步長
cs.SetStrokeWidth(1);
cs.SetColor (255,255,0,0);
for (i=100;i<283;i++){
a=pi/360*i*6 ; //360 完整雙曲線,小于為局部段
// x0=240 ; bs=300 ; pn=150 ; //i=100-285 (1)
x0=180 ; bs=200 ; pn=80 ; //i=100-283 (2)
x1=(float)((x0/pi*a)-bs); //bs=50--300
y1=(float)(-pn*sin(a)+250); //dy=250 Y軸center
cs.SetColor (255,255,0,0);
cs.DrawCircle(x1,y1,2);
cs.SetColor (255,0,0,250);
cs.DrawLine (x1,y1,x1,250) ; }
for (n=1; n<=8; n++){ //draw 多條曲線
Rn=random ()*60+100 ;
pn=(int) Rn ;
for (i=100;i<283;i++){
a=pi/360*i*6 ; //360 完整雙曲線,小于為局部段
x0=180 ; bs=200 ; //pn=80 ;
x1=(float)((x0/pi*a)-bs); //bs=50--300
y1=(float)(-pn*sin(a)+250); //dy=250 Y軸center
if (n==1||n==5) cs.SetColor (255,200,0,100);
if (n==2||n==6) cs.SetColor (255,250,150,100);
if (n==3||n==7) cs.SetColor (255,0,0,250);
if (n==4||n==8) cs.SetColor (255,250,100,130);
cs.DrawCircle(x1,y1,1.5); } }
//draw title
cs.SetFillMode (1);//0不填色,1填色
cs.SetTextStyle (1);
cs.SetStrokeWidth(1);
cs.SetTextSize (26);
cs.DrawText ("Curve Chart ??",470,80) ;
cs.SetTextSize (50);
ss="統(tǒng)計圖 - 曲線圖" ;
cs.SetColor(255,50,120,20); //立體字
cs.DrawText (ss,164,534); //陰影
cs.SetColor(255,0,250,0);
cs.DrawText (ss,160,530); //本字
cs.SetFillMode (0);//0不填色,1填色
cs.SetColor(255,250,150,0);
cs.DrawText (ss,160,530); //框線
cs.Update ();
}//CurveGraph7 ()到此這篇關(guān)于C語言繪制曲線圖的示例代碼的文章就介紹到這了,更多相關(guān)C語言曲線圖內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
一起來學(xué)習(xí)C++的動態(tài)內(nèi)存管理
這篇文章主要為大家詳細介紹了C++的動態(tài)內(nèi)存管理,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-03-03

