C#實現(xiàn)在窗體上的統(tǒng)計圖效果
本文實例為大家分享了C#實現(xiàn)在窗體上的統(tǒng)計圖,供大家參考,具體內(nèi)容如下
忽然要用到C#來制作統(tǒng)計圖,起初不太清除怎么弄,看了一些代碼實現(xiàn)統(tǒng)計圖的文檔。終于實現(xiàn)了條形圖的設(shè)置,后來又需要餅狀圖。本來還是想自己畫的后來才發(fā)現(xiàn)C#是有這種窗體控件的,控件畫比我強太多了。
這是我自己做的條形圖的函數(shù):
public static int width = 400, height = 400;//聲明寬與高 ? Bitmap bitmap = new Bitmap(width, height);//創(chuàng)建一個繪圖對象 ?? ?//這四個參數(shù)分別是表的列名,tp1,tp2,tp3均是條形的長,s1,s2,s3代表每個條形的數(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個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)建兩個Font對象,用于設(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));//繪制標題 ? ? ? ? ? ? ? ? Point p1 = new Point(30, 300); ? ? ? ? ? ? ? ? Point p2 = new Point(330, 300); ? ? ? ? ? ? ? ? //這里用到的減式是由于畫布的坐標系和數(shù)學中的坐標系不同,參考下面的圖 ? ? ? ? ? ? ? ? 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; ? ? ? ? ? ? } ? ? ? ? }
畫布中的坐標系:
如果換成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)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
c# winform 解決PictureBox 無法打印全部圖片的問題
這篇文章主要介紹了c# winform 解決PictureBox 無法打印全部圖片的問題,幫助大家更好進行c# winform開發(fā),感興趣的朋友可以了解下2020-12-12