GDI+柱状图
在网上搜寻了一下柱状图的例子,看到这一篇http://www.cnblogs.com/ziyifly/archive/2008/09/24/1297841.html
修改一下后发出来,大家指正.
1
/// <summary>
2
/// 生成柱状图(有比较)
3
/// </summary>
4
public static Bitmap CreateZhuZhuangTu(IList<PReportParam> paraList, IList<PReportParam> paraList2, string name1, string name2)
5
{
6
int height = 500, width = 780;
7
Bitmap image = new Bitmap(width, height);
8
//创建Graphics类对象
9
Graphics g = Graphics.FromImage(image);
10
bool hasPara2 = false; //是否有比较参数
11
if (paraList2 != null && paraList2.Count > 0)
12
{
13
hasPara2 = true;
14
}
15![]()
16
try
17
{
18
//清空图片背景色
19
g.Clear(Color.White);
20![]()
21
Font font = new Font("Arial", 10, FontStyle.Regular);
22
Font font1 = new Font("宋体", 12, FontStyle.Bold);
23![]()
24
LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.BlueViolet, 1.2f, true);
25
g.FillRectangle(Brushes.WhiteSmoke, 0, 0, width, height);
26
// Brush brush1 = new SolidBrush(Color.Blue);
27
StringFormat sf = new StringFormat();
28
sf.FormatFlags = StringFormatFlags.DirectionVertical;
29![]()
30
g.DrawString("月收支分类统计柱状图", font1, brush, new PointF(20, 20), sf);
31
//画图片的边框线
32
g.DrawRectangle(new Pen(Color.Blue), 0, 0, image.Width - 1, image.Height - 1);
33![]()
34
Pen mypen = new Pen(brush, 1);
35
Pen mypen1 = new Pen(Color.Blue, 2);
36![]()
37
int counts = paraList.Count();//显示的项数
38
int zx = 100; //坐标轴原点x坐标
39
int zy = 420; //坐标轴原点y坐标
40
int zwidth = 580; //坐标轴宽
41
int zheight = 400; //坐标轴高
42![]()
43
decimal maxNum = 0.00m;
44![]()
45
List<PReportParam> p1 = paraList.OrderBy<PReportParam, decimal>(tmp => tmp.Num).ToList<PReportParam>();
46
decimal maxNum1 = p1[p1.Count - 1].Num; //最大的一个数字
47
if (hasPara2)
48
{
49
List<PReportParam> p2 = paraList2.OrderBy<PReportParam, decimal>(tmp => tmp.Num).ToList<PReportParam>();
50
decimal maxNum2 = p2[p2.Count - 1].Num;
51
if (maxNum1 > maxNum2)
52
{
53
maxNum = maxNum1;
54
}
55
else
56
{
57
maxNum = maxNum2;
58
}
59
}
60
else
61
{
62
maxNum = maxNum1;
63
}
64
decimal peNum = zheight / maxNum; //每单位代表多少像素
65![]()
66
//绘制线条
67
//绘制竖向线条
68
int x = zx;
69
g.DrawLine(mypen1, zx, zy, zx, zy - zheight);
70![]()
71
//绘制横向线条
72
int y = zy;
73
g.DrawLine(mypen1, zx, zy, zx + zwidth, zy);
74![]()
75
Font font2 = new System.Drawing.Font("Arial", 8);
76![]()
77
for (int i = 0; i < paraList.Count; i++)
78
{
79
string sc = paraList[i].Color;
80
if (sc == null || sc == "")
81
{
82
sc = "ff000000";
83
}
84
int ca = 0, cr = 0, cg = 0, cb = 0;
85
ColorARGBToParam(sc, ref ca, ref cr, ref cg, ref cb);
86
SolidBrush sb = new SolidBrush(Color.FromArgb(ca, cr, cg, cb));
87![]()
88
//画柱状图
89
x = x + 40;
90
y = Convert.ToInt32(paraList[i].Num * peNum);
91
g.FillRectangle(sb, x, zy - y - 1, 18, y);
92
//写字
93
string num = DecimalTrim(paraList[i].Num);
94
g.DrawString(num, font2, Brushes.Black, x, zy - y - 15);
95
g.DrawString(paraList[i].Name, font2, Brushes.Black, x, zy + 1, sf);
96![]()
97
if (hasPara2)
98
{
99
//画竖线
100
g.DrawLine(mypen, x + 18, zy - 1, x + 18, zy - zheight - 1);
101
//画柱状图
102
y = Convert.ToInt32(paraList2[i].Num * peNum);
103
g.FillRectangle(sb, x + 19, zy - y - 1, 18, y);
104
//写字
105
num = DecimalTrim(paraList2[i].Num);
106
g.DrawString(num, font2, Brushes.Black, x + 20, zy - y - 15);
107
}
108
}
109![]()
110
g.DrawString("左边:" + name1, font2, Brushes.Red, 20, 300, sf);
111
g.DrawString("右边:" + name2, font2, Brushes.Green, 40, 300, sf);
112
}
113
catch
114
{ }
115![]()
116
return image;
117
}
/// <summary>2
/// 生成柱状图(有比较)3
/// </summary>4
public static Bitmap CreateZhuZhuangTu(IList<PReportParam> paraList, IList<PReportParam> paraList2, string name1, string name2)5
{6
int height = 500, width = 780;7
Bitmap image = new Bitmap(width, height);8
//创建Graphics类对象9
Graphics g = Graphics.FromImage(image);10
bool hasPara2 = false; //是否有比较参数11
if (paraList2 != null && paraList2.Count > 0)12
{13
hasPara2 = true;14
}15

16
try17
{18
//清空图片背景色19
g.Clear(Color.White);20

21
Font font = new Font("Arial", 10, FontStyle.Regular);22
Font font1 = new Font("宋体", 12, FontStyle.Bold);23

24
LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.BlueViolet, 1.2f, true);25
g.FillRectangle(Brushes.WhiteSmoke, 0, 0, width, height);26
// Brush brush1 = new SolidBrush(Color.Blue);27
StringFormat sf = new StringFormat();28
sf.FormatFlags = StringFormatFlags.DirectionVertical;29

30
g.DrawString("月收支分类统计柱状图", font1, brush, new PointF(20, 20), sf);31
//画图片的边框线32
g.DrawRectangle(new Pen(Color.Blue), 0, 0, image.Width - 1, image.Height - 1);33

34
Pen mypen = new Pen(brush, 1);35
Pen mypen1 = new Pen(Color.Blue, 2);36

37
int counts = paraList.Count();//显示的项数38
int zx = 100; //坐标轴原点x坐标39
int zy = 420; //坐标轴原点y坐标40
int zwidth = 580; //坐标轴宽41
int zheight = 400; //坐标轴高42

43
decimal maxNum = 0.00m;44

45
List<PReportParam> p1 = paraList.OrderBy<PReportParam, decimal>(tmp => tmp.Num).ToList<PReportParam>();46
decimal maxNum1 = p1[p1.Count - 1].Num; //最大的一个数字47
if (hasPara2)48
{49
List<PReportParam> p2 = paraList2.OrderBy<PReportParam, decimal>(tmp => tmp.Num).ToList<PReportParam>();50
decimal maxNum2 = p2[p2.Count - 1].Num;51
if (maxNum1 > maxNum2)52
{53
maxNum = maxNum1;54
}55
else56
{57
maxNum = maxNum2;58
}59
}60
else61
{62
maxNum = maxNum1;63
}64
decimal peNum = zheight / maxNum; //每单位代表多少像素65

66
//绘制线条67
//绘制竖向线条68
int x = zx;69
g.DrawLine(mypen1, zx, zy, zx, zy - zheight);70

71
//绘制横向线条72
int y = zy;73
g.DrawLine(mypen1, zx, zy, zx + zwidth, zy);74

75
Font font2 = new System.Drawing.Font("Arial", 8);76

77
for (int i = 0; i < paraList.Count; i++)78
{79
string sc = paraList[i].Color;80
if (sc == null || sc == "")81
{82
sc = "ff000000";83
}84
int ca = 0, cr = 0, cg = 0, cb = 0;85
ColorARGBToParam(sc, ref ca, ref cr, ref cg, ref cb);86
SolidBrush sb = new SolidBrush(Color.FromArgb(ca, cr, cg, cb));87

88
//画柱状图89
x = x + 40;90
y = Convert.ToInt32(paraList[i].Num * peNum);91
g.FillRectangle(sb, x, zy - y - 1, 18, y);92
//写字93
string num = DecimalTrim(paraList[i].Num);94
g.DrawString(num, font2, Brushes.Black, x, zy - y - 15);95
g.DrawString(paraList[i].Name, font2, Brushes.Black, x, zy + 1, sf);96

97
if (hasPara2)98
{99
//画竖线100
g.DrawLine(mypen, x + 18, zy - 1, x + 18, zy - zheight - 1);101
//画柱状图102
y = Convert.ToInt32(paraList2[i].Num * peNum);103
g.FillRectangle(sb, x + 19, zy - y - 1, 18, y);104
//写字105
num = DecimalTrim(paraList2[i].Num);106
g.DrawString(num, font2, Brushes.Black, x + 20, zy - y - 15);107
}108
}109

110
g.DrawString("左边:" + name1, font2, Brushes.Red, 20, 300, sf);111
g.DrawString("右边:" + name2, font2, Brushes.Green, 40, 300, sf);112
}113
catch114
{ }115

116
return image;117
}
要走的路还很长,很长......
要学习的知识还有很多,很多......



浙公网安备 33010602011771号