//需引用的命名空间
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
private int[] arrValuesSJ={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};//数据数组
private int[] arrValuesFJ={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};//数据数组
private string[] arrValueNames={"0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0"};
private void Page_Load(object sender, System.EventArgs e)
{
FillArr();
DrawMap();
}
private void FillArr()
{
for(int i = 0; i < 6; i++)
{
arrValueNames[i] = i+1 + "日";
arrValuesSJ[i] = i*5;
arrValuesFJ[i] = i*8;
}
}
private void DrawMap()
{
/**//*自写编码*/
int ImgWidth=610; //1.[总宽度] ***图宽度
int ImgHeight=400; //2.[总高度] ***图高度
int ItemNum=20; //3.[项目数量] ***图表划分的块
int ChildNum=2; //4.[块数] ***大块中划分的子项的数量
float ChildRate=0.7f; //5.[各块总占空间比率]
int ChartLeft=50; //7.[图表左边距] ***图表距图的左边距离
int ChartRight=15; //8.[图表右边距] ***图表距图的右边距离
int ChartTop=20; //9.[图表顶边距] ***图表距图顶边距离
int ChartBottom=80; //10.[图表底边距] ***图表距图底边距离
double YMaxValue=50.0; //11.[纵坐标标尺最大值] ***纵坐标标尺的最大值
int YItemNum=10; //12.[纵坐标标尺段数] ***纵坐标标尺的段数
int YTop=15; //13.[距纵轴顶端间隔]
int YStrStart=15; //14.[纵坐标标尺文字起始X坐标]
int XRight=25; //15.[距横轴右端间隔]
int XStrStart=5; //16.[横坐标标尺文字起始Y坐标]
StringFormat sf;
//[图表总宽度]=[总宽度]-[图表左边距]-[图表右边距]-[距横轴右端间隔]
int chartwidth=ImgWidth-ChartLeft-ChartRight-XRight;
//[项目宽度]=[图表总宽度]/[项目数量]
int itemwidth=chartwidth/ItemNum;
//[各块总占空间比率的实际宽度]=[项目宽度]*[各块总占空间比率]
int factwidth=Convert.ToInt32(Math.Floor(itemwidth*ChildRate));
//[各块矩形宽度]=[各块总占空间比率的实际宽度]/[块数]
int rectanglewidth=factwidth/ChildNum;
//[各块间的间距]=([项目宽度]-[各块总占空间比率的实际宽度])/([块数]+1)
//int childspace=Convert.ToInt32(Math.Floor((itemwidth-factwidth)/(ChildNum+1)));
int chartheight = ImgHeight-ChartTop-ChartBottom-YTop;
double rectangleheight = (ImgHeight-ChartTop-ChartBottom-YTop)/YMaxValue;
int childspace=Convert.ToInt32(Math.Floor(itemwidth-factwidth));
Graphics objGps;//建立画板对象
Bitmap objBitMap = new Bitmap(ImgWidth,ImgHeight);//建立位图对象
objGps = Graphics.FromImage(objBitMap);//根据位图对象建立画板对象B
objGps.Clear(Color.White);//设置画板对象的背景色
//得出矩形宽度,和画图X轴位置
//[项目宽度]=[总宽度]/[项目数量]
//======[各块总占空间比率]=([各块矩形宽度]+[各块间的间距])/[项目宽度]
//[各块总占空间比率的实际宽度]=[项目宽度]*[各块总占空间比率]
//[各块矩形宽度]=([各块总占空间比率的实际宽度]-[各块间的间距]*([块数]))/[块数]
//[一边空余空间宽度]=([项目宽度]-[各块所占空间比率的总宽度])/2
System.Drawing.Point[] pit=new Point[3]; //定义坐标三角点的对象数组
System.Drawing.Pen pe=new Pen(new SolidBrush(GetColor(7)),2f); //定义画直线的对象
//画纵轴
objGps.DrawLine(pe,new Point(ChartLeft,ImgHeight-ChartBottom),new Point(ChartLeft,ChartTop));
//画纵轴终点箭头
pit[0].X=ImgWidth-ChartRight; //确定三角形三点的位置
pit[0].Y=ImgHeight-ChartBottom-4;
pit[1].X=ImgWidth-ChartRight;
pit[1].Y=ImgHeight-ChartBottom+4;
pit[2].X=ImgWidth-ChartRight+10;
pit[2].Y=ImgHeight-ChartBottom;
objGps.FillPolygon(new SolidBrush(GetColor(7)),pit);
//画纵轴标尺和标尺描述
int ys;
for(int i=1;i<=YItemNum;i++)
{
//画标尺
objGps.DrawLine(pe,new PointF(ChartLeft,ImgHeight-ChartBottom-(ImgHeight-ChartBottom-ChartTop-YTop)/YItemNum*i),new PointF(ChartLeft-5,ImgHeight-ChartBottom-(ImgHeight-ChartBottom-ChartTop-YTop)/YItemNum*i));
//画描述
ys = Convert.ToInt32(YMaxValue)/YItemNum*i;
objGps.DrawString(ys.ToString(),new Font("宋体",10),Brushes.Black,new Point(YStrStart,ImgHeight-ChartBottom-(ImgHeight-ChartBottom-ChartTop-YTop)/YItemNum*i-5));
}
objGps.DrawString("收发量",new Font("宋体",10),Brushes.Black,new Point(YStrStart-15,ChartTop+YTop-25));
//画横轴
objGps.DrawLine(pe,new Point(ChartLeft,ImgHeight-ChartBottom),new Point(ImgWidth-ChartRight,ImgHeight-ChartBottom));
//画横轴终点箭头
pit[0].X=ChartLeft-4; //确定三角形三点的位置
pit[0].Y=ChartTop;
pit[1].X=ChartLeft+4;
pit[1].Y=ChartTop;
pit[2].X=ChartLeft;
pit[2].Y=ChartTop-10;
objGps.FillPolygon(new SolidBrush(GetColor(7)),pit);
//画横轴标尺和标尺描述
for(int i=1;i<=ItemNum;i++)
{
sf = new StringFormat();
sf.FormatFlags = StringFormatFlags.DirectionVertical;
objGps.DrawLine(pe,new PointF(ChartLeft+itemwidth*i,ImgHeight-ChartBottom),new PointF(ChartLeft+itemwidth*i,ImgHeight-ChartBottom+5));
objGps.DrawString(arrValueNames[i-1].ToString(),new Font("宋体",9),Brushes.Black,new Point(ChartLeft+childspace*ChildNum/2+itemwidth*(i-1)+10,ImgHeight-ChartBottom+XStrStart),sf);
}
for(int j = 0;j<arrValuesSJ.Length;j++)
{
//画矩形图
objGps.FillRectangle(new SolidBrush(GetColor(0)),(j*itemwidth)+rectanglewidth+childspace+ChartLeft,ImgHeight-ChartBottom-Convert.ToInt32(arrValuesSJ[j]*rectangleheight),rectanglewidth,Convert.ToInt32(arrValuesSJ[j]*rectangleheight));
objGps.FillRectangle(new SolidBrush(GetColor(2)),(j*itemwidth)+rectanglewidth*2+childspace+ChartLeft,ImgHeight-ChartBottom-Convert.ToInt32(arrValuesFJ[j]*rectangleheight),rectanglewidth,Convert.ToInt32(arrValuesFJ[j]*rectangleheight));
objGps.DrawRectangle(Pens.Black,(j*itemwidth)+rectanglewidth+childspace+ChartLeft,ImgHeight-ChartBottom-Convert.ToInt32(arrValuesSJ[j]*rectangleheight),rectanglewidth,Convert.ToInt32(arrValuesSJ[j]*rectangleheight));
objGps.DrawRectangle(Pens.Black,(j*itemwidth)+rectanglewidth*2+childspace+ChartLeft,ImgHeight-ChartBottom-Convert.ToInt32(arrValuesFJ[j]*rectangleheight),rectanglewidth,Convert.ToInt32(arrValuesFJ[j]*rectangleheight));
}
objGps.FillRectangle(new SolidBrush(GetColor(0)),chartwidth-20,ChartTop+YTop+10,rectanglewidth,20);
objGps.DrawRectangle(Pens.Black,chartwidth-20,ChartTop+YTop+10,rectanglewidth,20);
objGps.FillRectangle(new SolidBrush(GetColor(2)),chartwidth-20+rectanglewidth,ChartTop+YTop+20,rectanglewidth,10);
objGps.DrawRectangle(Pens.Black,chartwidth-20+rectanglewidth,ChartTop+YTop+20,rectanglewidth,10);
sf = new StringFormat();
sf.FormatFlags = StringFormatFlags.DirectionVertical;
objGps.DrawString("收件量",new Font("宋体",8),Brushes.Black,new Point(chartwidth-25,ChartTop+YTop+30),sf);
objGps.DrawString("发件量",new Font("宋体",8),Brushes.Black,new Point(chartwidth-20+rectanglewidth,ChartTop+YTop+30),sf);
objBitMap.Save(Response.OutputStream,ImageFormat.Gif);//该位图对象以"GIF"格式输出
}
/// <param name="itemIndex">系统定义的颜色,有效值0到7,分别为(Blue,Yellow,Red,Orange,Purple,Brown,Pink,Black)</param>
/// <returns></returns>
public static Color GetColor(int itemIndex)
{
Color objColor = new Color();
switch(itemIndex)
{
case 0:
objColor = Color.Blue;
break;
case 1:
objColor = Color.Yellow;
break;
case 2:
objColor = Color.Red;
break;
case 3:
objColor = Color.Orange;
break;
case 4:
objColor = Color.Purple;
break;
case 5:
objColor = Color.Brown;
break;
case 6:
objColor = Color.Pink;
break;
default:
objColor = Color.Black;
break;
}
return objColor;
}
/// <param name="red">自定义颜色红色分量值,有效值0到255</param>
/// <param name="green">自定义颜色绿色分量值,有效值0到255</param>
/// <param name="blue">自定义颜色蓝色分量值,有效值0到255</param>
/// <returns></returns>
public static Color GetColor(int red,int green,int blue)
{
Color objColor = new Color();
objColor = Color.FromArgb(red,green,blue);
return objColor;
}