C#實(shí)現(xiàn)在窗體上的統(tǒng)計(jì)圖效果
本文實(shí)例為大家分享了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均是條形的長,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 brush1 = new SolidBrush(Color.White); ? ? ? ? ? ? ? ? Brush brush2 = new SolidBrush(Color.Black); ? ? ? ? ? ? ? ? Brush brush3 = new SolidBrush(Color.Red); ? ? ? ? ? ? ? ? Brush brush4 = new SolidBrush(Color.Green); ? ? ? ? ? ? ? ? Brush brush5 = new SolidBrush(Color.Orange); ? ? ? ? ? ? ? ? Brush brush6 = 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(brush1, 0, 0, width, height);//繪制背景圖 ? ? ? ? ? ? ? ? g.DrawString(s, font1, brush2, 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, brush2, new Point(90, 285 - (tp1 * 10))); ? ? ? ? ? ? ? ? g.DrawString(s2, font2, brush2, new Point(150, 285 - (tp2 * 10))); ? ? ? ? ? ? ? ? g.DrawString(s2, font2, brush2, new Point(210, 285 - (tp3 * 10))); ? ? ? ? ? ? ? ? g.FillRectangle(brush3, 90, 300 - (tp1 * 10), 50, tp1 * 10);// ? ? ? ? ? ? ? ? g.FillRectangle(brush4, 150, 300 - (tp2 * 10), 50, tp2 * 10);// ? ? ? ? ? ? ? ? g.FillRectangle(brush5, 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控件來做就簡單多了
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);
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#實(shí)現(xiàn)Array添加擴(kuò)展實(shí)例
這篇文章主要介紹了C#實(shí)現(xiàn)Array添加擴(kuò)展,對(duì)C#初學(xué)者有不錯(cuò)的參考價(jià)值,需要的朋友可以參考下2014-08-08C#日期格式強(qiáng)制轉(zhuǎn)換方法(推薦)
下面小編就為大家分享一C#日期格式強(qiáng)制轉(zhuǎn)換的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2017-11-11c# winform 解決PictureBox 無法打印全部圖片的問題
這篇文章主要介紹了c# winform 解決PictureBox 無法打印全部圖片的問題,幫助大家更好進(jìn)行c# winform開發(fā),感興趣的朋友可以了解下2020-12-12Unity實(shí)現(xiàn)3D貪吃蛇的移動(dòng)代碼
這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)3D貪吃蛇的移動(dòng)代碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-04-04C#中參數(shù)個(gè)數(shù)可變的方法實(shí)例分析
這篇文章主要介紹了C#中參數(shù)個(gè)數(shù)可變的方法,以一個(gè)簡單實(shí)例分析了C#中參數(shù)個(gè)數(shù)可變的方法,主要是使用params關(guān)鍵字來實(shí)現(xiàn)的,是C#編程中比較實(shí)用的技巧,需要的朋友可以參考下2014-11-11C#實(shí)現(xiàn)客戶端彈出消息框封裝類實(shí)例
這篇文章主要介紹了C#實(shí)現(xiàn)客戶端彈出消息框封裝類,實(shí)例分析了C#彈出窗口的實(shí)現(xiàn)技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-03-03