C#繪制柱狀圖和折線圖的方法

本文實例為大傢分享瞭C#繪制柱狀圖和折線圖的具體代碼,供大傢參考,具體內容如下

運行效果如下圖:

設計上面的柱狀圖和折線圖其實並沒有什麼難度,主要是各個坐標的計算,完全是精細活。首先在窗體在添加瞭一個tabControl控件來切換柱狀圖和折線圖的顯示,在tabPage1中顯示柱狀圖,在tabPage2中顯示折線圖。然後在各自的Page頁屬性中定義Paint事件,具體實現過程可以從下面的代碼中看到。

代碼如下:

添加頭文件:

using System.Drawing.Drawing2D;

tabPage1的Paint事件(畫柱狀圖):

private void tabPage1_Paint(object sender, PaintEventArgs e)
        {
            BackColor = Color.White;
            //標題
            Graphics g = tabPage1.CreateGraphics();
            Font f = new Font("宋體", 24, FontStyle.Regular);
            Pen p = new Pen(Color.Blue);
            g.DrawString("報名及考試統計柱狀圖", f, p.Brush, 200, 20);
 
            //畫表格
            for (int i = 0; i <= 9; i++)
            {
                g.DrawLine(p, 30, 90 + 31 * i, 620, 90 + 31 * i);
            }
            for (int i = 1; i <= 14; i++)
            {
                g.DrawLine(p, 30 + 42 * i, 60, 30 + 42 * i, 370);
            }
 
            Pen MyPen = new Pen(Color.Blue, 2);
            Point p1 = new Point(30, 60);
            Point p2 = new Point(30, 370);
            Point p3 = new Point(30, 370);
            Point p4 = new Point(620, 370);
            g.DrawLine(MyPen, p1, p2);
            g.DrawLine(MyPen, p3, p4);
 
            //紅色圖形部分
            Pen drawPen = new Pen(Color.Red, 1);
            SolidBrush mybrush = new SolidBrush(Color.Red);
 
            e.Graphics.DrawRectangle(drawPen, 30 + 21, 370 - 41, 21, 41);
            e.Graphics.FillRectangle(mybrush, 30 + 21, 370 - 41, 21, 41);
 
            e.Graphics.DrawRectangle(drawPen, 30 + 42 * 2 + 21, 370 - 31 * 4 - 10, 21, 31 * 4 + 10);
            e.Graphics.FillRectangle(mybrush, 30 + 42 * 2 + 21, 370 - 31 * 4 - 10, 21, 31 * 4 + 10);
 
            e.Graphics.DrawRectangle(drawPen, 30 + 42 * 4 + 21, 370 - 31 * 2 - 20, 21, 31 * 2 + 20);
            e.Graphics.FillRectangle(mybrush, 30 + 42 * 4 + 21, 370 - 31 * 2 - 20, 21, 31 * 2 + 20);
 
            e.Graphics.DrawRectangle(drawPen, 30 + 42 * 6 + 21, 370 - 31 * 1 - 20, 21, 31 * 1 + 20);
            e.Graphics.FillRectangle(mybrush, 30 + 42 * 6 + 21, 370 - 31 * 1 - 20, 21, 31 * 1 + 20);
 
            e.Graphics.DrawRectangle(drawPen, 30 + 42 * 8 + 21, 370 - 31 * 5 - 25, 21, 31 * 5 + 25);
            e.Graphics.FillRectangle(mybrush, 30 + 42 * 8 + 21, 370 - 31 * 5 - 25, 21, 31 * 5 + 25);
 
            e.Graphics.DrawRectangle(drawPen, 30 + 42 * 10 + 21, 370 - 31 * 4 - 7, 21, 31 * 4 + 7);
            e.Graphics.FillRectangle(mybrush, 30 + 42 * 10 + 21, 370 - 31 * 4 - 7, 21, 31 * 4 + 7);
 
            e.Graphics.DrawRectangle(drawPen, 30 + 42 * 12 + 21, 60, 21, 370 - 60);
            e.Graphics.FillRectangle(mybrush, 30 + 42 * 12 + 21, 60, 21, 370 - 60);
 
 
            //綠色圖形部分
            Pen drawPen2 = new Pen(Color.Green, 1);
            SolidBrush brush = new SolidBrush(Color.Green);
            e.Graphics.DrawRectangle(drawPen2, 30 + 42, 370 - 31, 21, 31);
            e.Graphics.FillRectangle(brush, 30 + 42, 370 - 31, 21, 31);
 
            e.Graphics.DrawRectangle(drawPen2, 30 + 42 * 3, 370 - 31 * 2 - 15, 21, 31 * 2 + 15);
            e.Graphics.FillRectangle(brush, 30 + 42 * 3, 370 - 31 * 2 - 15, 21, 31 * 2 + 15);
 
            e.Graphics.DrawRectangle(drawPen2, 30 + 42 * 5, 370 - 31 - 10, 21, 41);
            e.Graphics.FillRectangle(brush, 30 + 42 * 5, 370 - 31 - 10, 21, 41);
 
            e.Graphics.DrawRectangle(drawPen2, 30 + 42 * 7, 370 - 16, 21, 16);
            e.Graphics.FillRectangle(brush, 30 + 42 * 7, 370 - 16, 21, 16);
 
            e.Graphics.DrawRectangle(drawPen2, 30 + 42 * 9, 370 - 31 * 3 - 20, 21, 31 * 3 + 20);
            e.Graphics.FillRectangle(brush, 30 + 42 * 9, 370 - 31 * 3 - 20, 21, 31 * 3 + 20);
 
            e.Graphics.DrawRectangle(drawPen2, 30 + 42 * 11, 370 - 31 * 1 - 28, 21, 31 * 1 + 28);
            e.Graphics.FillRectangle(brush, 30 + 42 * 11, 370 - 31 * 1 - 28, 21, 31 * 1 + 28);
 
            e.Graphics.DrawRectangle(drawPen2, 30 + 42 * 13, 370 - 31 * 5 - 15, 21, 31 * 5 + 15);
            e.Graphics.FillRectangle(brush, 30 + 42 * 13, 370 - 31 * 5 - 15, 21, 31 * 5 + 15);
 
            //圖上的文字部分
            Font font2 = new Font("宋體", 10, FontStyle.Regular);
            g.DrawString("第一期", font2, p.Brush, 30 + 21, 375);
            g.DrawString("第二期", font2, p.Brush, 30 + 42 * 2 + 21, 375);
            g.DrawString("第三期", font2, p.Brush, 30 + 42 * 4 + 21, 375);
            g.DrawString("第四期", font2, p.Brush, 30 + 42 * 6 + 21, 375);
            g.DrawString("上半年", font2, p.Brush, 30 + 42 * 8 + 21, 375);
            g.DrawString("下半年", font2, p.Brush, 30 + 42 * 10 + 21, 375);
            g.DrawString("全年統計", font2, p.Brush, 30 + 42 * 12 + 21, 375);
 
            //圖上數字部分
            g.DrawString("25", font2, p.Brush, 10, 370 - 35);
            g.DrawString("50", font2, p.Brush, 10, 370 - 35 * 2);
            g.DrawString("75", font2, p.Brush, 10, 370 - 34 * 3);
            g.DrawString("100", font2, p.Brush, 5, 370 - 33 * 4);
            g.DrawString("125", font2, p.Brush, 5, 370 - 33 * 5);
            g.DrawString("150", font2, p.Brush, 5, 370 - 32 * 6);
            g.DrawString("175", font2, p.Brush, 5, 370 - 32 * 7);
            g.DrawString("200", font2, p.Brush, 5, 370 - 32 * 8);
            g.DrawString("225", font2, p.Brush, 5, 370 - 32 * 9);
            g.DrawString("250", font2, p.Brush, 5, 370 - 32 * 10);
 
            //紅色數
            Pen pen2 = new Pen(Color.Red);
            g.DrawString("39", font2, pen2.Brush, 30 + 21, 370 - 41 - 15);
            g.DrawString("111", font2, pen2.Brush, 30 + 42 * 2 + 21, 370 - 31 * 4 - 10 - 15);
            g.DrawString("71", font2, pen2.Brush, 30 + 42 * 4 + 21, 370 - 31 * 2 - 20 - 15);
            g.DrawString("40", font2, pen2.Brush, 30 + 42 * 6 + 21, 370 - 31 * 1 - 20 - 15);
            g.DrawString("150", font2, pen2.Brush, 30 + 42 * 8 + 21, 370 - 31 * 5 - 25 - 15);
            g.DrawString("111", font2, pen2.Brush, 30 + 42 * 10 + 21, 370 - 31 * 4 - 7 - 15);
            g.DrawString("261", font2, pen2.Brush, 30 + 42 * 12 + 21, 60 - 15);
 
 
            //綠色數
            Pen pen3 = new Pen(Color.Green);
            g.DrawString("39", font2, pen2.Brush, 30 + 21, 370 - 41 - 15);
            g.DrawString("111", font2, pen2.Brush, 30 + 42 * 2 + 21, 370 - 31 * 4 - 10 - 15);
            g.DrawString("71", font2, pen2.Brush, 30 + 42 * 4 + 21, 370 - 31 * 2 - 20 - 15);
            g.DrawString("40", font2, pen2.Brush, 30 + 42 * 6 + 21, 370 - 31 * 1 - 20 - 15);
            g.DrawString("150", font2, pen2.Brush, 30 + 42 * 8 + 21, 370 - 31 * 5 - 25 - 15);
            g.DrawString("111", font2, pen2.Brush, 30 + 42 * 10 + 21, 370 - 31 * 4 - 7 - 15);
            g.DrawString("261", font2, pen2.Brush, 30 + 42 * 12 + 21, 60 - 15);
 
 
            //最下面的矩形框
            e.Graphics.DrawRectangle(p, 30 + 42 * 2 + 30, 400, 42 * 7, 31 * 2);
 
            e.Graphics.DrawRectangle(drawPen, 30 + 42 * 5, 410, 21, 10);
            e.Graphics.FillRectangle(mybrush, 30 + 42 * 5, 410, 21, 10);
            g.DrawString("報名人數", font2, pen2.Brush, 30 + 42 * 6, 410);
 
            e.Graphics.DrawRectangle(drawPen2, 30 + 42 * 5, 440, 21, 10);
            e.Graphics.FillRectangle(brush, 30 + 42 * 5, 440, 21, 10);
            g.DrawString("通過人數", font2, pen3.Brush, 30 + 42 * 6, 440);
 }

tabPage2的Paint事件(畫折線圖):

private void tabPage2_Paint(object sender, PaintEventArgs e)
        {
            BackColor = Color.White;
            //標題
            Graphics g = tabPage2.CreateGraphics();
            Font f = new Font("宋體", 24, FontStyle.Regular);
            Pen p = new Pen(Color.Blue);
            g.DrawString("報名及考試統計折線圖", f, p.Brush, 200, 20);
 
            //畫表格
            for (int i = 0; i <= 9; i++)
            {
                g.DrawLine(p, 30, 90 + 31 * i, 620, 90 + 31 * i);
            }
            for (int i = 1; i <= 7; i++)
            {
                g.DrawLine(p, 30 + 84 * i, 60, 30 + 84 * i, 370);
            }
            Pen MyPen = new Pen(Color.Blue, 2);
            Point p1 = new Point(30, 60);
            Point p2 = new Point(30, 370);
            Point p3 = new Point(30, 370);
            Point p4 = new Point(620, 370);
            g.DrawLine(MyPen, p1, p2);
            g.DrawLine(MyPen, p3, p4);
 
 
            //繪制折線
            Pen pen1 = new Pen(Color.Red, 2);
            Pen pen2 = new Pen(Color.Green,2);
 
            //紅色折線
            Point a1, a2, a3, a4, a5, a6, a7;
            a1 = new Point(30,370-31-20);
            a2 = new Point(30+84*1,370-(31*4+9));
            a3 = new Point(30 + 84 * 2,370-(31*2+28));
            a4 = new Point(30 + 84 * 3, 370 - (31 * 1 + 20));
            a5 = new Point(30 + 84 * 4, 370 - (31 * 5 + 21));
            a6 = new Point(30 + 84 * 5, 370 - (31 * 4 + 10));
            a7 = new Point(30 + 84 * 6, 60);
            Point[] points = { a1,a2,a3,a4,a5,a6,a7};
            g.DrawLines(pen1, points);
 
            //綠色折線
            Point b1, b2, b3, b4, b5, b6, b7;
            b1 = new Point(30,370-(31*1+1));
            b2 = new Point(30+84*1,370-(31*2+15));
            b3 = new Point(30 + 84 * 2, 370 - (31 * 1 + 10));
            b4 = new Point(30 + 84 * 3, 370 - (31 * 0 + 15));
            b5 = new Point(30 + 84 * 4, 370 - (31 * 3 + 15));
            b6 = new Point(30 + 84 * 5, 370 - (31 * 1 + 29));
            b7 = new Point(30 + 84 * 6, 370 - (31 * 5 + 14));
            Point[] points2 = { b1, b2, b3, b4, b5, b6, b7 };
            g.DrawLines(pen2,points2);
 
            //圖上數字部分
            Font font2 = new Font("宋體", 10, FontStyle.Regular);
            g.DrawString("25", font2, pen1.Brush, 10, 370 - 35);
            g.DrawString("50", font2, pen1.Brush, 10, 370 - 35 * 2);
            g.DrawString("75", font2, pen1.Brush, 10, 370 - 34 * 3);
            g.DrawString("100", font2, pen1.Brush, 5, 370 - 33 * 4);
            g.DrawString("125", font2, pen1.Brush, 5, 370 - 33 * 5);
            g.DrawString("150", font2, pen1.Brush, 5, 370 - 32 * 6);
            g.DrawString("175", font2, pen1.Brush, 5, 370 - 32 * 7);
            g.DrawString("200", font2, pen1.Brush, 5, 370 - 32 * 8);
            g.DrawString("225", font2, pen1.Brush, 5, 370 - 32 * 9);
            g.DrawString("250", font2, pen1.Brush, 5, 370 - 32 * 10);
 
            //文字
            g.DrawString("第一期", font2, pen1.Brush, 15, 375);
            g.DrawString("第二期", font2, pen1.Brush, 15 + 84 * 1, 375);
            g.DrawString("第三期", font2, pen1.Brush, 15 + 84 * 2, 375);
            g.DrawString("第四期", font2, pen1.Brush, 15 + 84 * 3, 375);
            g.DrawString("上半年", font2, pen1.Brush, 15 + 84 * 4, 375);
            g.DrawString("下半年", font2, pen1.Brush, 15 + 84 * 5, 375);
            g.DrawString("全年統計", font2, pen1.Brush, 15 + 84 * 6, 375);
 
 
            //折線圖上的數字
            g.DrawString("39", font2, pen1.Brush, 30, 370 - 31 - 20 - 15);
            g.DrawString("111", font2, pen1.Brush, 30 + 84 * 1, 370 - (31 * 4 + 9) - 15);
            g.DrawString("71", font2, pen1.Brush, 30 + 84 * 2, 370 - (31 * 2 + 28) - 15);
            g.DrawString("40", font2, pen1.Brush, 30 + 84 * 3, 370 - (31 * 1 + 20) - 15);
            g.DrawString("150", font2, pen1.Brush, 30 + 84 * 4, 370 - (31 * 5 + 21) - 15);
            g.DrawString("111", font2, pen1.Brush, 30 + 84 * 5, 370 - (31 * 4 + 10) - 15);
            g.DrawString("261", font2, pen1.Brush, 30 + 84 * 6, 60 - 15);
 
            g.DrawString("26", font2, pen2.Brush, 30, 370 - (31 * 1 + 1) - 15);
            g.DrawString("68", font2, pen2.Brush, 30 + 84 * 1, 370 - (31 * 2 + 15) - 15);
            g.DrawString("35", font2, pen2.Brush, 30 + 84 * 2, 370 - (31 * 1 + 10) - 15);
            g.DrawString("14", font2, pen2.Brush, 30 + 84 * 3, 370 - (31 * 0 + 15) - 15);
            g.DrawString("94", font2, pen2.Brush, 30 + 84 * 4, 370 - (31 * 3 + 15) - 15);
            g.DrawString("49", font2, pen2.Brush, 30 + 84 * 5, 370 - (31 * 1 + 29) - 15);
            g.DrawString("143", font2, pen2.Brush, 30 + 84 * 6, 370 - (31 * 5 + 14) - 15);

            //最下面的矩形框
 
            SolidBrush mybrush = new SolidBrush(Color.Red);
            SolidBrush brush = new SolidBrush(Color.Green);
 
            e.Graphics.DrawRectangle(pen1, 30 + 42 * 2 + 30, 400, 42 * 7, 31 * 2);
 
            e.Graphics.DrawRectangle(pen1, 30 + 42 * 5, 410, 21, 10);
            e.Graphics.FillRectangle(mybrush, 30 + 42 * 5, 410, 21, 10);
            g.DrawString("報名人數", font2, pen1.Brush, 30 + 42 * 6, 410);
 
            e.Graphics.DrawRectangle(pen2, 30 + 42 * 5, 440, 21, 10);
            e.Graphics.FillRectangle(brush, 30 + 42 * 5, 440, 21, 10);
            g.DrawString("通過人數", font2, pen2.Brush, 30 + 42 * 6, 440);
}

以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。

推薦閱讀: