您好,登錄后才能下訂單哦!
小編給大家分享一下C#如何實(shí)現(xiàn)在窗體上的統(tǒng)計(jì)圖,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
具體內(nèi)容如下
忽然要用到C#來制作統(tǒng)計(jì)圖,起初不太清除怎么弄,看了一些代碼實(shí)現(xiàn)統(tǒng)計(jì)圖的文檔。終于實(shí)現(xiàn)了條形圖的設(shè)置,后來又需要餅狀圖。本來還是想自己畫的后來才發(fā)現(xiàn)C#是有這種窗體控件的,控件畫比我強(qiáng)太多了。
這是我自己做的條形圖的函數(shù):
public static int width = 400, height = 400;//聲明寬與高 Bitmap bitmap = new Bitmap(width, height);//創(chuàng)建一個(gè)繪圖對(duì)象 //這四個(gè)參數(shù)分別是表的列名,tp1,tp2,tp3均是條形的長(zhǎng),s1,s2,s3代表每個(gè)條形的數(shù)據(jù) public void createImage(string s, int tp1, int tp2,int tp3, string s1, string s2,string s3) { Graphics g = Graphics.FromImage(bitmap); g.FillRectangle(Brushes.Black, 0, 0, 400, 400);//邊框 try { g.Clear(Color.White); //創(chuàng)建6個(gè)brush,用于填充顏色 Brush brush2 = new SolidBrush(Color.White); Brush brush3 = new SolidBrush(Color.Black); Brush brush4 = new SolidBrush(Color.Red); Brush brush5 = new SolidBrush(Color.Green); Brush brush6 = new SolidBrush(Color.Orange); Brush brush7 = new SolidBrush(Color.DarkBlue); //創(chuàng)建兩個(gè)Font對(duì)象,用于設(shè)置字體 Font font1 = new Font("宋體", 16, FontStyle.Bold); Font font2 = new Font("Courier New", 8); g.FillRectangle(brush2, 0, 0, width, height);//繪制背景圖 g.DrawString(s, font1, brush3, new Point(90, 20));//繪制標(biāo)題 Point p1 = new Point(30, 300); Point p2 = new Point(330, 300); //這里用到的減式是由于畫布的坐標(biāo)系和數(shù)學(xué)中的坐標(biāo)系不同,參考下面的圖 g.DrawString(s1, font2, brush3, new Point(90, 285 - (tp1 * 10))); g.DrawString(s2, font2, brush3, new Point(150, 285 - (tp2 * 10))); g.DrawString(s2, font2, brush3, new Point(210, 285 - (tp3 * 10))); g.FillRectangle(brush4, 90, 300 - (tp1 * 10), 50, tp1 * 10);// g.FillRectangle(brush5, 150, 300 - (tp2 * 10), 50, tp2 * 10);// g.FillRectangle(brush6, 210, 300 - (tp3 * 10), 50, tp3 * 10); g.DrawLine(new Pen(Color.Black), p1, p2);//繪制一條直線 pictureBox1.Image = bitmap; } catch (Exception) { throw; } }
畫布中的坐標(biāo)系:
如果換成C#中的chart控件來做就簡(jiǎn)單多了
1.找到控件的Legends屬性把其中的Enabled改成False。
2.找到控件的Series屬性把其中的ChartType改成你需要的表形式。
3.接下就是為chart控件添加數(shù)據(jù)。
添加數(shù)據(jù)的方式之一:
string[] xData = { "經(jīng)理", "總監(jiān)", "銷售" }; int[] yData = {10, 20, 30}; chart1.Series[0]["PieLabelStyle"] = "Outside";//將文字移到外側(cè) chart1.Series[0]["PieLineColor"] = "Black";//繪制黑色的連線。 chart1.Series[0].Points.DataBindXY(xData, yData);
以上是“C#如何實(shí)現(xiàn)在窗體上的統(tǒng)計(jì)圖”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。