之前的gdi+绘图,整理了一下,清晰了很多
柱状图
饼状图
工具
调用柱状图
DAL是使用的微软的DAAB
1using System;
2using System.IO;
3using System.Data;
4using System.Drawing;
5using System.Drawing.Text;
6using System.Drawing.Drawing2D;
7using System.Drawing.Imaging;
8
9namespace WanFangData.Chart
10{
11 public class Bar
12 {
13 public void Render(string title, string subTitle, int width, int height, DataSet chartData, Stream target)
14 {
15 const int SIDE_LENGTH = 400;
16 const int CHART_TOP = 75;
17 const int CHART_HEIGHT = 200;
18 const int CHART_LEFT = 50;
19 const int CHART_WIDTH = 300;
20 DataTable dt = chartData.Tables[0];
21
22 //计算最高的点
23 float highPoint = 0;
24 foreach (DataRow dr in dt.Rows)
25 {
26 if (highPoint < Convert.ToSingle(dr[1]))
27 {
28 highPoint = Convert.ToSingle(dr[1]);
29 }
30 }
31 //建立一个Graphics对象实例
32 Bitmap bm = new Bitmap(width, height);
33 Graphics g = Graphics.FromImage(bm);
34 //设置条图图形和文字属性
35 g.ScaleTransform((Convert.ToSingle(width)) / SIDE_LENGTH, (Convert.ToSingle(height)) / SIDE_LENGTH);
36 g.SmoothingMode = SmoothingMode.Default;
37 g.TextRenderingHint = TextRenderingHint.AntiAlias;
38
39 //设定画布和边
40 g.Clear(Color.White);
41 g.DrawRectangle(Pens.Black, 0, 0, SIDE_LENGTH - 1, SIDE_LENGTH - 1);
42 //画大标题
43 g.DrawString(title, new Font("Tahoma", 24), Brushes.Black, new PointF(5, 5));
44 //画小标题
45 g.DrawString(subTitle, new Font("Tahoma", 14), Brushes.Black, new PointF(7, 35));
46 //画条形图
47 float barWidth = CHART_WIDTH / (dt.Rows.Count * 2);
48 PointF barOrigin = new PointF(CHART_LEFT + (barWidth / 2), 0);
49 float barHeight = dt.Rows.Count;
50 for (int i = 0; i < dt.Rows.Count; i++)
51 {
52 barHeight = Convert.ToSingle(dt.Rows[i][1]) * 200 / highPoint;
53 barOrigin.Y = CHART_TOP + CHART_HEIGHT - barHeight;
54 g.FillRectangle(new SolidBrush(Utils.GetChartItemColor(i)), barOrigin.X, barOrigin.Y, barWidth, barHeight);
55 barOrigin.X = barOrigin.X + (barWidth * 2);
56 }
57 //设置边
58 g.DrawLine(new Pen(Color.Black, 2), new Point(CHART_LEFT, CHART_TOP), new Point(CHART_LEFT, CHART_TOP + CHART_HEIGHT));
59 g.DrawLine(new Pen(Color.Black, 2), new Point(CHART_LEFT, CHART_TOP + CHART_HEIGHT), new Point(CHART_LEFT + CHART_WIDTH, CHART_TOP + CHART_HEIGHT));
60 //画图例框和文字
61 g.DrawRectangle(new Pen(Color.Black, 1), 200, 300, 199, 99);
62 g.DrawString("Legend", new Font("Tahoma", 12, FontStyle.Bold), Brushes.Black, new PointF(200, 300));
63
64 //画图例
65 PointF boxOrigin = new PointF(210, 330);
66 PointF textOrigin = new PointF(235, 326);
67 for (int i = 0; i < dt.Rows.Count; i++)
68 {
69 g.FillRectangle(new SolidBrush(Utils.GetChartItemColor(i)), boxOrigin.X, boxOrigin.Y, 20, 10);
70 g.DrawRectangle(Pens.Black, boxOrigin.X, boxOrigin.Y, 20, 10);
71 g.DrawString(dt.Rows[i][0].ToString() + " - " + dt.Rows[i][1].ToString(), new Font("Tahoma", 10), Brushes.Black, textOrigin);
72 boxOrigin.Y += 15;
73 textOrigin.Y += 15;
74 }
75 //输出图形
76 bm.Save(target, ImageFormat.Gif);
77
78 //资源回收
79 bm.Dispose();
80 g.Dispose();
81 }
82 }
83}
84
2using System.IO;
3using System.Data;
4using System.Drawing;
5using System.Drawing.Text;
6using System.Drawing.Drawing2D;
7using System.Drawing.Imaging;
8
9namespace WanFangData.Chart
10{
11 public class Bar
12 {
13 public void Render(string title, string subTitle, int width, int height, DataSet chartData, Stream target)
14 {
15 const int SIDE_LENGTH = 400;
16 const int CHART_TOP = 75;
17 const int CHART_HEIGHT = 200;
18 const int CHART_LEFT = 50;
19 const int CHART_WIDTH = 300;
20 DataTable dt = chartData.Tables[0];
21
22 //计算最高的点
23 float highPoint = 0;
24 foreach (DataRow dr in dt.Rows)
25 {
26 if (highPoint < Convert.ToSingle(dr[1]))
27 {
28 highPoint = Convert.ToSingle(dr[1]);
29 }
30 }
31 //建立一个Graphics对象实例
32 Bitmap bm = new Bitmap(width, height);
33 Graphics g = Graphics.FromImage(bm);
34 //设置条图图形和文字属性
35 g.ScaleTransform((Convert.ToSingle(width)) / SIDE_LENGTH, (Convert.ToSingle(height)) / SIDE_LENGTH);
36 g.SmoothingMode = SmoothingMode.Default;
37 g.TextRenderingHint = TextRenderingHint.AntiAlias;
38
39 //设定画布和边
40 g.Clear(Color.White);
41 g.DrawRectangle(Pens.Black, 0, 0, SIDE_LENGTH - 1, SIDE_LENGTH - 1);
42 //画大标题
43 g.DrawString(title, new Font("Tahoma", 24), Brushes.Black, new PointF(5, 5));
44 //画小标题
45 g.DrawString(subTitle, new Font("Tahoma", 14), Brushes.Black, new PointF(7, 35));
46 //画条形图
47 float barWidth = CHART_WIDTH / (dt.Rows.Count * 2);
48 PointF barOrigin = new PointF(CHART_LEFT + (barWidth / 2), 0);
49 float barHeight = dt.Rows.Count;
50 for (int i = 0; i < dt.Rows.Count; i++)
51 {
52 barHeight = Convert.ToSingle(dt.Rows[i][1]) * 200 / highPoint;
53 barOrigin.Y = CHART_TOP + CHART_HEIGHT - barHeight;
54 g.FillRectangle(new SolidBrush(Utils.GetChartItemColor(i)), barOrigin.X, barOrigin.Y, barWidth, barHeight);
55 barOrigin.X = barOrigin.X + (barWidth * 2);
56 }
57 //设置边
58 g.DrawLine(new Pen(Color.Black, 2), new Point(CHART_LEFT, CHART_TOP), new Point(CHART_LEFT, CHART_TOP + CHART_HEIGHT));
59 g.DrawLine(new Pen(Color.Black, 2), new Point(CHART_LEFT, CHART_TOP + CHART_HEIGHT), new Point(CHART_LEFT + CHART_WIDTH, CHART_TOP + CHART_HEIGHT));
60 //画图例框和文字
61 g.DrawRectangle(new Pen(Color.Black, 1), 200, 300, 199, 99);
62 g.DrawString("Legend", new Font("Tahoma", 12, FontStyle.Bold), Brushes.Black, new PointF(200, 300));
63
64 //画图例
65 PointF boxOrigin = new PointF(210, 330);
66 PointF textOrigin = new PointF(235, 326);
67 for (int i = 0; i < dt.Rows.Count; i++)
68 {
69 g.FillRectangle(new SolidBrush(Utils.GetChartItemColor(i)), boxOrigin.X, boxOrigin.Y, 20, 10);
70 g.DrawRectangle(Pens.Black, boxOrigin.X, boxOrigin.Y, 20, 10);
71 g.DrawString(dt.Rows[i][0].ToString() + " - " + dt.Rows[i][1].ToString(), new Font("Tahoma", 10), Brushes.Black, textOrigin);
72 boxOrigin.Y += 15;
73 textOrigin.Y += 15;
74 }
75 //输出图形
76 bm.Save(target, ImageFormat.Gif);
77
78 //资源回收
79 bm.Dispose();
80 g.Dispose();
81 }
82 }
83}
84
饼状图
1using System;
2using System.IO;
3using System.Data;
4using System.Drawing;
5using System.Drawing.Text;
6using System.Drawing.Drawing2D;
7using System.Drawing.Imaging;
8
9namespace WanFangData.Chart
10{
11 public class Pie
12 {
13 public void Render(string title, string subTitle, int width, int height, DataSet chartData, Stream target)
14 {
15 const int SIDE_LENGTH = 400;
16 const int PIE_DIAMETER = 200;
17 DataTable dt = chartData.Tables[0];
18
19 //通过输入参数,取得饼图中的总基数
20 float sumData = 0;
21 foreach (DataRow dr in dt.Rows)
22 {
23 sumData += Convert.ToSingle(dr[1]);
24 }
25 //产生一个image对象,并由此产生一个Graphics对象
26 Bitmap bm = new Bitmap(width, height);
27 Graphics g = Graphics.FromImage(bm);
28 //设置对象g的属性
29 g.ScaleTransform((Convert.ToSingle(width)) / SIDE_LENGTH, (Convert.ToSingle(height)) / SIDE_LENGTH);
30 g.SmoothingMode = SmoothingMode.Default;
31 g.TextRenderingHint = TextRenderingHint.AntiAlias;
32
33 //画布和边的设定
34 g.Clear(Color.White);
35 g.DrawRectangle(Pens.Black, 0, 0, SIDE_LENGTH - 1, SIDE_LENGTH - 1);
36 //画饼图标题
37 g.DrawString(title, new Font("Tahoma", 24), Brushes.Black, new PointF(5, 5));
38 //画饼图的图例
39 g.DrawString(subTitle, new Font("Tahoma", 14), Brushes.Black, new PointF(7, 35));
40 //画饼图
41 float curAngle = 0;
42 float totalAngle = 0;
43 for (int i = 0; i < dt.Rows.Count; i++)
44 {
45 curAngle = Convert.ToSingle(dt.Rows[i][1]) / sumData * 360;
46
47 g.FillPie(new SolidBrush(Utils.GetChartItemColor(i)), 100, 65, PIE_DIAMETER, PIE_DIAMETER, totalAngle, curAngle);
48 g.DrawPie(Pens.Black, 100, 65, PIE_DIAMETER, PIE_DIAMETER, totalAngle, curAngle);
49 totalAngle += curAngle;
50 }
51 //画图例框及其文字
52 g.DrawRectangle(Pens.Black, 200, 300, 199, 99);
53 g.DrawString("Legend", new Font("Tahoma", 12, FontStyle.Bold), Brushes.Black, new PointF(200, 300));
54
55 //画图例各项
56 PointF boxOrigin = new PointF(210, 330);
57 PointF textOrigin = new PointF(235, 326);
58 float percent = 0;
59 for (int i = 0; i < dt.Rows.Count; i++)
60 {
61 g.FillRectangle(new SolidBrush(Utils.GetChartItemColor(i)), boxOrigin.X, boxOrigin.Y, 20, 10);
62 g.DrawRectangle(Pens.Black, boxOrigin.X, boxOrigin.Y, 20, 10);
63 percent = Convert.ToSingle(dt.Rows[i][1]) / sumData * 100;
64 g.DrawString(dt.Rows[i][0].ToString() + " - " + dt.Rows[i][1].ToString() + " (" + percent.ToString("0") + "%)", new Font("Tahoma", 10), Brushes.Black, textOrigin);
65 boxOrigin.Y += 15;
66 textOrigin.Y += 15;
67 }
68 //通过Response.OutputStream,将图形的内容发送到浏览器
69 bm.Save(target, ImageFormat.Gif);
70 //回收资源
71 bm.Dispose();
72 g.Dispose();
73 }
74 }
75}
2using System.IO;
3using System.Data;
4using System.Drawing;
5using System.Drawing.Text;
6using System.Drawing.Drawing2D;
7using System.Drawing.Imaging;
8
9namespace WanFangData.Chart
10{
11 public class Pie
12 {
13 public void Render(string title, string subTitle, int width, int height, DataSet chartData, Stream target)
14 {
15 const int SIDE_LENGTH = 400;
16 const int PIE_DIAMETER = 200;
17 DataTable dt = chartData.Tables[0];
18
19 //通过输入参数,取得饼图中的总基数
20 float sumData = 0;
21 foreach (DataRow dr in dt.Rows)
22 {
23 sumData += Convert.ToSingle(dr[1]);
24 }
25 //产生一个image对象,并由此产生一个Graphics对象
26 Bitmap bm = new Bitmap(width, height);
27 Graphics g = Graphics.FromImage(bm);
28 //设置对象g的属性
29 g.ScaleTransform((Convert.ToSingle(width)) / SIDE_LENGTH, (Convert.ToSingle(height)) / SIDE_LENGTH);
30 g.SmoothingMode = SmoothingMode.Default;
31 g.TextRenderingHint = TextRenderingHint.AntiAlias;
32
33 //画布和边的设定
34 g.Clear(Color.White);
35 g.DrawRectangle(Pens.Black, 0, 0, SIDE_LENGTH - 1, SIDE_LENGTH - 1);
36 //画饼图标题
37 g.DrawString(title, new Font("Tahoma", 24), Brushes.Black, new PointF(5, 5));
38 //画饼图的图例
39 g.DrawString(subTitle, new Font("Tahoma", 14), Brushes.Black, new PointF(7, 35));
40 //画饼图
41 float curAngle = 0;
42 float totalAngle = 0;
43 for (int i = 0; i < dt.Rows.Count; i++)
44 {
45 curAngle = Convert.ToSingle(dt.Rows[i][1]) / sumData * 360;
46
47 g.FillPie(new SolidBrush(Utils.GetChartItemColor(i)), 100, 65, PIE_DIAMETER, PIE_DIAMETER, totalAngle, curAngle);
48 g.DrawPie(Pens.Black, 100, 65, PIE_DIAMETER, PIE_DIAMETER, totalAngle, curAngle);
49 totalAngle += curAngle;
50 }
51 //画图例框及其文字
52 g.DrawRectangle(Pens.Black, 200, 300, 199, 99);
53 g.DrawString("Legend", new Font("Tahoma", 12, FontStyle.Bold), Brushes.Black, new PointF(200, 300));
54
55 //画图例各项
56 PointF boxOrigin = new PointF(210, 330);
57 PointF textOrigin = new PointF(235, 326);
58 float percent = 0;
59 for (int i = 0; i < dt.Rows.Count; i++)
60 {
61 g.FillRectangle(new SolidBrush(Utils.GetChartItemColor(i)), boxOrigin.X, boxOrigin.Y, 20, 10);
62 g.DrawRectangle(Pens.Black, boxOrigin.X, boxOrigin.Y, 20, 10);
63 percent = Convert.ToSingle(dt.Rows[i][1]) / sumData * 100;
64 g.DrawString(dt.Rows[i][0].ToString() + " - " + dt.Rows[i][1].ToString() + " (" + percent.ToString("0") + "%)", new Font("Tahoma", 10), Brushes.Black, textOrigin);
65 boxOrigin.Y += 15;
66 textOrigin.Y += 15;
67 }
68 //通过Response.OutputStream,将图形的内容发送到浏览器
69 bm.Save(target, ImageFormat.Gif);
70 //回收资源
71 bm.Dispose();
72 g.Dispose();
73 }
74 }
75}
工具
1using System;
2using System.Drawing;
3
4namespace WanFangData.Chart
5{
6 class Utils
7 {
8 public static Color GetChartItemColor(int itemIndex)
9 {
10 Color selectedColor;
11 switch (itemIndex)
12 {
13 case 0:
14 selectedColor = Color.Blue;
15 break;
16 case 1:
17 selectedColor = Color.Red;
18 break;
19 case 2:
20 selectedColor = Color.Yellow;
21 break;
22 case 3:
23 selectedColor = Color.Purple;
24 break;
25 default:
26 selectedColor = Color.Green;
27 break;
28 }
29 return selectedColor;
30 }
31 }
32}
2using System.Drawing;
3
4namespace WanFangData.Chart
5{
6 class Utils
7 {
8 public static Color GetChartItemColor(int itemIndex)
9 {
10 Color selectedColor;
11 switch (itemIndex)
12 {
13 case 0:
14 selectedColor = Color.Blue;
15 break;
16 case 1:
17 selectedColor = Color.Red;
18 break;
19 case 2:
20 selectedColor = Color.Yellow;
21 break;
22 case 3:
23 selectedColor = Color.Purple;
24 break;
25 default:
26 selectedColor = Color.Green;
27 break;
28 }
29 return selectedColor;
30 }
31 }
32}
调用柱状图
1using System;
2using System.Data;
3using WanFangData.Chart;
4using WanFangData.Common;
5
6namespace WanFangData.Web
7{
8 public partial class CBar : WanFangData.Page.BasePage
9 {
10 protected void Page_Load(object sender, EventArgs e)
11 {
12 Bar p = new Bar();
13 p.Render("大标题", "小标题", 300, 300,WanFangData.Common.Database.ExecuteDataset(CommandType.Text, "select * from a"), Response.OutputStream);
14 }
15 }
16}
17
2using System.Data;
3using WanFangData.Chart;
4using WanFangData.Common;
5
6namespace WanFangData.Web
7{
8 public partial class CBar : WanFangData.Page.BasePage
9 {
10 protected void Page_Load(object sender, EventArgs e)
11 {
12 Bar p = new Bar();
13 p.Render("大标题", "小标题", 300, 300,WanFangData.Common.Database.ExecuteDataset(CommandType.Text, "select * from a"), Response.OutputStream);
14 }
15 }
16}
17
调用饼状图
1using System;
2using System.Data;
3using WanFangData.Chart;
4using WanFangData.Common;
5
6namespace WanFangData.Web
7{
8 public partial class CPie : WanFangData.Page.BasePage
9 {
10 protected void Page_Load(object sender, EventArgs e)
11 {
12 Pie p = new Pie();
13 p.Render("大标题", "小标题", 400, 400, Database.ExecuteDataset(CommandType.Text, "select * from a"), Response.OutputStream);
14 }
15 }
16}
17
2using System.Data;
3using WanFangData.Chart;
4using WanFangData.Common;
5
6namespace WanFangData.Web
7{
8 public partial class CPie : WanFangData.Page.BasePage
9 {
10 protected void Page_Load(object sender, EventArgs e)
11 {
12 Pie p = new Pie();
13 p.Render("大标题", "小标题", 400, 400, Database.ExecuteDataset(CommandType.Text, "select * from a"), Response.OutputStream);
14 }
15 }
16}
17
DAL是使用的微软的DAAB