【求助】折线图的程序设计
书上的例子,[color=Lime]在VS2005里调试private Color GetColor(int itemIndex)处却总是有一错误,不知道怎么改,请高手帮忙指出哪地方有错[/color]using System;
using System.Collections;
using
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
using System.Drawing.Imaging;
private Color GetColor(int itemIndex)
{
Color MyColor;
int l = itemIndex;
switch(l)
{
case 1:
MyColor = Color.Red;
return MyColor;
case 2:
MyColor = Color.Blue;
return MyColor;
case 3;
MyColor = Color.Green;
return MyColor;
case 4:
MyColor = Color.Yellow;
return MyColor;
Default;
MyColor = Color.Black;
return MyColor;
}
private void Page_Load(object sender,System.EventArgs e)
{
this.oleDbConnection1.Open();
String commm = "SELECT 年份,DELL,IBM,HP,SONY FROM SALES ORDER BY 年份";
OleDbCommand myCommand = new OleDbCommand(comm,oleDbConnection1);
OleDbDataReader myOleDbDataReader = myCommand.ExecuteReader();
int[,] val = new int [10,5];
string [] com = new string [4]{"DELL","IBM","HP","SONY"};
int l = 0;
int k = 0;
int jd = 0;
while(myOleDbDataReader.Read())
{
val[k,0] = myOleDbDataReader.GetInt32(1);
val[k,1] = myOleDbDataReader.GetInt32(2);
val[k,2] = myOleDbDataReader.GetInt32(3);
val[k,3] = myOleDbDataReader.GetInt32(4);
val[k,4] = myOleDbDataReader.GetInt32(0);
k++;
jd=k-1;
}
this.oleDbConnection1.Close();
myOleDbDataReader.Close();
Bitmap bm = new Bitmap(850,400);
Graphics g;
g = Graphics.FromImage(bm);
g.Clear(Color.LightBlue);
g.DrawString("电脑销量折线图",new Font("黑体",30),Brushes.Black,new Point(150,20));
Point myRec = new Point(700,70);
Point myDec = new Point(720,70);
g.DrawString("单位:万台",new Font("黑体",15),Brushes.Black,new Point(700,20));
for(l=0;l<4;l++)
{
g.DrawRectangle(Pens.Black,myRec.X,myRec.Y,20,10);
g.FillRectangle(new SolidBrush(GetColor(l+1)),myRec.X,myRec.Y,20,30);
g.DrawString(com[1].ToString(),new Font("黑体",15),Brushes.Black,myDec);
myRec.Y+=50;
myDec.Y+=50;
}
int iBarWidth = 80;
for(l=0;l<=3;l++)
{
Pen t = new Pen(GetColor(l+1),2);
for(k=0;k<jd;k++)
{
g.DrawString(val[k,4].ToString()+"年",new Font("黑体",15),Brushes.Black,((k)*iBarWidth + 65),380);
g.DrawLine(t,(k*iBarWidth + 70),(400-(val[k,l]/500)),((k+l)*iBarWidth+70),(400-(val[k+1,1]/500));
g.DrawString(val[k,l].ToString(),new Font(" 宋体",8),Brushes.Black,(k*iBarWidth+70),(380-(val[k,l]/500)));
}
Pen p=new Pen(Color.Black,1);
g.DrawLine(p,1,100,1,373);
g.DrawLine(p,1,373,698,373);
g.DrawString("15万",new Font("黑体",15),Brushes.Black,5,85);
g.DrawString("10万",new Font("黑体",15),Brushes.Black,5,185);
g.DrawString("5万",new Font("黑体",15),Brushes.Black,5,285);
bm.Save(Response.OutputStream,ImageFormat.Gif);
}
}
SQLSever数据库建一表名SALES
年份 DELL IBM HP SONY
1990 16580 56540 89560 55860
1991 32565 58968 95650 48620
1992 65487 75482 65458 56845
1993 26588 54876 36548 15487
1994 35646 69874 54687 25487
1995 45576 76565 85465 74457
连接数据库运行程序画出折线图
[[it] 本帖最后由 ruanyap 于 2008-10-21 20:14 编辑 [/it]]