从数据库读取数据总和传送给一个变量(附有画图代码),请大家指教一下,先谢谢了
我想画一个直方图,代码如下:Graphics g = e.Graphics;
int MaxNum = 12;
int start;
int end;
int legendindex, heightindex;
string tempValue = "";
Pen blackpen = new Pen(Color.Black,2);
Brush[] colorarray = new Brush[12];
colorarray[0] = new SolidBrush(Color.Red);
colorarray[1] = new SolidBrush(Color.Green);
colorarray[2] = new SolidBrush(Color.Blue);
colorarray[3] = new SolidBrush(Color.Yellow);
colorarray[4] = new SolidBrush(Color.Gray);
colorarray[5] = new SolidBrush(Color.Aquamarine);
colorarray[6] = new SolidBrush(Color.Turquoise);
colorarray[7] = new SolidBrush(Color.SandyBrown);
colorarray[8] = new SolidBrush(Color.NavajoWhite);
colorarray[9] = new SolidBrush(Color.LavenderBlush);
colorarray[10] = new SolidBrush(Color.Chartreuse);
colorarray[11] = new SolidBrush(Color.LightCyan);
int legendfontsize = 8;
Font LegendArial = new Font("Arial",legendfontsize);
int labelfontsize = 12;
Font LabelArial = new Font("Arial",labelfontsize);
int titlefontsize = 18;
Font TitleArial = new Font("Arial",titlefontsize,FontStyle.Bold);
string legend = initlegend;
start = 0;
end = legend.Length;
legend += ',';
legendindex = 0;
string[] legendarray = new string[20];
while (start < legend.Length)
{
end = legend.IndexOf(',');
tempValue = legend.Substring(start,end);
legendarray[legendindex] = tempValue;
legend = legend.Substring(++end,legend.Length-end);
legendindex++;
}
if (legendindex > MaxNum)
legendindex = MaxNum;
string height = initheight;
start = 0;
end = height.Length;
height += ',';
heightindex = 0;
double[] heightarray = new double[20];
while (start < height.Length)
{
end = height.IndexOf(',');
tempValue = height.Substring(start,end);
heightarray[heightindex] = double.Parse(tempValue);
height = height.Substring(++end,height.Length-end);
heightindex++;
}
if (heightindex > MaxNum)
heightindex = MaxNum;
double barMax = heightarray[0];
for (int i = 1; i < heightindex; i++)
if (barMax < heightarray[i])
barMax = heightarray[i];
double[] scaledheightarray = new double[heightindex];
for (int i = 0; i < heightindex; i++)
scaledheightarray[i] = heightarray[i] * 175 / barMax;
int tpos = 200 - ((TitleStr.Length / 3) * titlefontsize);
g.DrawString(TitleStr,TitleArial,colorarray[0],tpos,15);
int hpos = 200 - ((HorizontalStr.Length / 3) * labelfontsize);
g.DrawString(HorizontalStr,LabelArial,colorarray[2],hpos,240);
int vpos = 120 - ((VerticalStr.Length * 2 / 3) * labelfontsize);
char[] mychar = VerticalStr.ToCharArray();
for (int i = 0; i < VerticalStr.Length; i++)
{
string s1 = mychar[i].ToString();
g.DrawString(s1,LabelArial,colorarray[2],20,vpos+(i*20));
}
g.DrawLine(blackpen,48,50,48,232);
g.DrawLine(blackpen,48,232,450,232);
float x1 = 50;
float barwidth = 350 / heightindex;
for (int i = 0; i < heightindex; i++)
{
g.FillRectangle(colorarray[i],x1,230-Convert.ToInt32(scaledheightarray[i]),barwidth,Convert.ToInt32(scaledheightarray[i]));
x1 += barwidth;
}
float legendXPos = 525;
float legendYPos = 40;
for (int i = 0; i < legendindex; i++)
{
g.FillRectangle(colorarray[i],legendXPos,legendYPos,20,20);
g.DrawString(legendarray[i], LegendArial, colorarray[2], legendXPos + 30, legendYPos);
legendYPos += 30;
}
for (int i = 0; i < 12; i++)
{
colorarray[i].Dispose();
}
TitleArial.Dispose();
LabelArial.Dispose();
LegendArial.Dispose();
blackpen.Dispose();
这样画法,在窗体运行的时候,直方图就直接出来了,但是他的数据信息我是在代码里这样赋值的:
public string initheight = "10,20,30,15,60,45,50";
这些值决定了直方图的高度,我想要从数据库里取数据赋值给initheight ,举个例子:
序号 姓名 成绩
1 张三 80
2 李四 77
3 王五 69
4 张六 85
5 张七 80
6 李二 67
我想把姓张、姓李、姓王的成绩的总和做成直方图,比较一下,就是从数据库中取出第三列中各个姓的成绩总和赋给initheight
public string initheight = "245,144,69";
其实说不定也不用这些代码,另有一种更直接的方法,我现在是没头绪,希望大家指教一下,先谢谢了!