using
System;
using
System.Data;
using
System.Configuration;
using
System.Web;
using
System.Web.Security;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
using
System.Web.UI.HtmlControls;
using
System.Collections;
using
System.ComponentModel;
using
System.Drawing;
using
System.Web.SessionState;
using
System.Drawing.Imaging;
using
System.Data.OleDb;
public
partial
class
TGtuCon : System.Web.UI.Page
{
public
void
Page_Load(
object
sender, EventArgs e)
{
System.Drawing.Bitmap image
=
new
System.Drawing.Bitmap(
1024
,
800
);
Graphics g
=
Graphics.FromImage(image);
try
{
//
公司1 01/01~02/28
//
公司2 02/01~03/21
//
公司3 01/13~02/02 02/12~03/04
int
hang
=
20
;
//
行距
int
shu
=
100
;
//
最大日期,最小日期
string
maxdate
=
"
2009-03-04
"
, mindate
=
"
2009-01-01
"
;
//
清空图片背景色
g.Clear(Color.Snow);
//
在绘画图面的指定位置,以指定的字体、指定的颜色绘制指定的字符串。
g.DrawString(
"
××公司甘特图
"
,
new
Font(
"
黑体
"
,
14
), Brushes.Black,
new
Point(
5
,
5
));
//
定点
Point myRec
=
new
Point(
10
,
70
); Point myDec
=
new
Point(
10
+
shu,
40
);
//
竖列字
g.DrawString(
"
公司1
"
,
new
Font(
"
黑体
"
,
10
), Brushes.Black, myRec.X, myRec.Y);
g.DrawString(
"
公司2
"
,
new
Font(
"
黑体
"
,
10
), Brushes.Black, myRec.X, myRec.Y
+
hang);
g.DrawString(
"
公司3
"
,
new
Font(
"
黑体
"
,
10
), Brushes.Black, myRec.X, myRec.Y
+
hang
*
2
);
//
横列标题
int
x
=
0
, nextX
=
myDec.X;
string
nextdate
=
mindate;
for
(
int
i
=
1
;i
<
5
;i
++
)
{
g.DrawString(i.ToString()
+
"
月
"
,
new
Font(
"
黑体
"
,
10
), Brushes.Black, nextX
+
x, myDec.Y);
nextX
=
nextX
+
x;
TimeSpan ts
=
new
TimeSpan(); ts
=
Convert.ToDateTime(nextdate).AddMonths(
1
)
-
Convert.ToDateTime(nextdate);
x
=
Convert.ToInt32 (ts.Days)
*
10
;
nextdate
=
Convert.ToDateTime(nextdate).AddMonths(
1
).ToString() ;
}
//
公司1 01/01~02/28
int
width
=
0
,startx
=
0
;TimeSpan tspan
=
new
TimeSpan();
tspan
=
Convert.ToDateTime(
"
2009-02-28
"
)
-
Convert.ToDateTime(mindate);
width
=
Convert.ToInt32 (tspan.Days)
*
10
;
g.DrawRectangle(Pens.Black, myDec.X
+
startx , myRec.Y, width,
10
);
//
绘制Bar图
g.FillRectangle(
new
SolidBrush(GetColor(
0
)), myDec.X
+
startx , myRec.Y, width,
10
);
//
以指定的色彩填充Bar图
g.DrawString(
"
01/01~02/28
"
,
new
Font(
"
黑体
"
,
10
), Brushes.Black, myDec.X
+
startx, myRec.Y);
//
公司2 02/01~03/21
tspan
=
Convert.ToDateTime(
"
2009-02-01
"
)
-
Convert.ToDateTime(mindate);
startx
=
Convert.ToInt32(tspan.Days)
*
10
;
tspan
=
Convert.ToDateTime(
"
2009-03-21
"
)
-
Convert.ToDateTime(mindate);
width
=
Convert.ToInt32(tspan.Days)
*
10
-
startx;
g.DrawRectangle(Pens.Black, myDec.X
+
startx, myRec.Y
+
hang, width,
10
);
//
绘制Bar图
g.FillRectangle(
new
SolidBrush(GetColor(
1
)), myDec.X
+
startx, myRec.Y
+
hang, width,
10
);
//
以指定的色彩填充Bar图
g.DrawString(
"
02/01~03/21
"
,
new
Font(
"
黑体
"
,
10
), Brushes.Black, myDec.X
+
startx, myRec.Y
+
hang);
//
公司3 01/13~02/02 02/12~03/04
tspan
=
Convert.ToDateTime(
"
2009-01-13
"
)
-
Convert.ToDateTime(mindate);
startx
=
Convert.ToInt32(tspan.Days)
*
10
;
tspan
=
Convert.ToDateTime(
"
2009-02-02
"
)
-
Convert.ToDateTime(mindate);
width
=
Convert.ToInt32(tspan.Days)
*
10
-
startx;
g.DrawRectangle(Pens.Black, myDec.X
+
startx, myRec.Y
+
hang
*
2
, width,
10
);
//
绘制Bar图
g.FillRectangle(
new
SolidBrush(GetColor(
2
)), myDec.X
+
startx, myRec.Y
+
hang
*
2
, width,
10
);
//
以指定的色彩填充Bar图
g.DrawString(
"
01/13~02/02
"
,
new
Font(
"
黑体
"
,
10
), Brushes.Black, myDec.X
+
startx, myRec.Y
+
hang
*
2
);
tspan
=
Convert.ToDateTime(
"
2009-02-12
"
)
-
Convert.ToDateTime(mindate);
startx
=
Convert.ToInt32(tspan.Days)
*
10
;
tspan
=
Convert.ToDateTime(
"
2009-03-04
"
)
-
Convert.ToDateTime(mindate);
width
=
Convert.ToInt32(tspan.Days)
*
10
-
startx;
g.DrawRectangle(Pens.Black, myDec.X
+
startx, myRec.Y
+
hang
*
2
, width,
10
);
//
绘制Bar图
g.FillRectangle(
new
SolidBrush(GetColor(
2
)), myDec.X
+
startx, myRec.Y
+
hang
*
2
, width,
10
);
//
以指定的色彩填充Bar图
g.DrawString(
"
02/12~03/04
"
,
new
Font(
"
黑体
"
,
10
), Brushes.Black, myDec.X
+
startx, myRec.Y
+
hang
*
2
);
//
画图片的边框线
g.DrawRectangle(
new
Pen(Color.Red),
0
,
0
, image.Width
-
1
, image.Height
-
1
);
System.IO.MemoryStream ms
=
new
System.IO.MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
Response.ClearContent();
Response.ContentType
=
"
image/Gif
"
;
Response.BinaryWrite(ms.ToArray());
}
finally
{
g.Dispose();
image.Dispose();
}
}
///
<summary>
///
获取颜色
///
</summary>
///
<param name="itemIndex">
数组的索引
</param>
///
<returns></returns>
private
Color GetColor(
int
itemIndex)
{
Color myColor;
int
i
=
itemIndex;
switch
(i)
{
case
0
:
myColor
=
Color.LightGreen ;
return
myColor;
case
1
:
myColor
=
Color.Pink ;
return
myColor;
case
2
:
myColor
=
Color.LightYellow;
return
myColor;
case
3
:
myColor
=
Color.LightBlue;
return
myColor;
case
4
:
myColor
=
Color.Orange;
return
myColor;
case
5
:
myColor
=
Color.Aqua;
return
myColor;
case
6
:
myColor
=
Color.SkyBlue;
return
myColor;
case
7
:
myColor
=
Color.DeepPink;
return
myColor;
case
8
:
myColor
=
Color.Azure;
return
myColor;
case
9
:
myColor
=
Color.Brown;
return
myColor;
case
10
:
myColor
=
Color.Pink;
return
myColor;
case
11
:
myColor
=
Color.BurlyWood;
return
myColor;
case
12
:
myColor
=
Color.Chartreuse;
return
myColor;
default
:
myColor
=
Color.Pink;
return
myColor;
}
}
}