C語(yǔ)言繪制曲線圖的示例代碼
統(tǒng)計(jì)圖曲線圖繪制方法
常用的統(tǒng)計(jì)圖有條形圖、柱形圖、折線圖、曲線圖、餅圖、環(huán)形圖、扇形圖。
前幾類(lèi)圖比較容易繪制,餅圖環(huán)形圖繪制較難。
曲線圖的曲線繪制較難,今提供曲線圖的繪制方法供參考。
本方法采用C語(yǔ)言的最基本功能:
( 1) 繪圖功能畫(huà)線,畫(huà)圓,畫(huà)長(zhǎng)方形。
(2) 界面美工設(shè)計(jì),界面文字打印輸出。
代碼中有詳細(xì)的注釋?zhuān)ㄋ滓锥豢淳蜁?huì)。
效果圖
實(shí)現(xiàn)代碼
下面提供2個(gè)繪制曲線圖的代碼:
//變量: 可設(shè)置成全局變量或私有變量 Canvas cs ; //畫(huà)布,繪制圖表載體 float pi=3.1415926535 ; float a ; //三角函數(shù) sin (a), cos (a), float r ; //圓半徑 radius int i, j, n ; float x0,y0,x1,y1 ; //作圖坐標(biāo) float dx,dy ; //中心坐標(biāo) string ss, ss1, ss2 ; //打印文字 int p[6] ; //set data or input data double pn ; //顯示數(shù)據(jù) //************************* CurveGraph6 (){ //曲線圖 6 cs.ClearDraw (0,src); //清屏 clearOutput(); selectStyle () ; //圖例樣式選項(xiàng)設(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); //畫(huà)標(biāo)尺 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 標(biāo)尺x軸 0 位, bs*5 步長(zhǎng) //x0 曲線幅寬, + -pn y軸峰谷弧高, y+300 x軸y定位 // pn= 0 -- 200 高,bs=50*2 int bs ; //步長(zhǎng) //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 曲線點(diǎn)密度 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 曲線點(diǎn)密度 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)計(jì)圖 - 曲線圖" ; 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 () ; //圖例樣式選項(xiàng)設(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); //畫(huà)標(biāo)尺 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 標(biāo)尺x軸 0 位, bs 幅寬 //x0 X軸 0位, + -pn y軸峰谷弧高, y+250 x軸y定位 // pn= 0 -- 200,+ - pn> 正弦余弦。bs=幅寬 int bs ; //步長(zhǎng) 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)計(jì)圖 - 曲線圖" ; 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語(yǔ)言繪制曲線圖的示例代碼的文章就介紹到這了,更多相關(guān)C語(yǔ)言曲線圖內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
VC MFC非模態(tài)對(duì)話框的實(shí)現(xiàn)方法
這篇文章主要介紹了VC MFC非模態(tài)對(duì)話框的實(shí)現(xiàn)方法,有助于讀者加深對(duì)于模態(tài)對(duì)話框與非模態(tài)對(duì)話框的理解與運(yùn)用,需要的朋友可以參考下2014-07-07C語(yǔ)言中sizeof函數(shù)的基本使用總結(jié)
這篇文章主要給大家介紹了關(guān)于C語(yǔ)言中sizeof函數(shù)的基本使用的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2018-03-03C語(yǔ)言函數(shù)棧幀的創(chuàng)建和銷(xiāo)毀詳解
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言函數(shù)棧幀的創(chuàng)建和銷(xiāo)毀,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助2022-02-02C語(yǔ)言進(jìn)階教程之字符串&內(nèi)存函數(shù)
對(duì)于字符,在計(jì)算機(jī)內(nèi)部都是用數(shù)字(字符編碼)來(lái)表示的,而字符串是“字符連續(xù)排列”的一種表現(xiàn),這篇文章主要給大家介紹了關(guān)于C語(yǔ)言進(jìn)階教程之字符串&內(nèi)存函數(shù)的相關(guān)資料,需要的朋友可以參考下2021-09-09一起來(lái)學(xué)習(xí)C++的動(dòng)態(tài)內(nèi)存管理
這篇文章主要為大家詳細(xì)介紹了C++的動(dòng)態(tài)內(nèi)存管理,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助2022-03-03C++實(shí)現(xiàn)簡(jiǎn)易的通訊錄管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)簡(jiǎn)易的通訊錄管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06