using System;
using System.Drawing;
//HttpUtility.UrlEncode /// <summary>
///Curve 的摘要说明
/// </summary>
public class Curve
{
public Curve()
{
//
//TODO: 在此处添加构造函数逻辑
//
} /*******************************************************属性设置**************************************************************/
/// <summary>
/// 房价数组
/// </summary>
private string[] _CurveData;
/// <summary>
/// 房价时间
/// </summary>
//private string[] _CurveDate;
public string[] CurveData
{
get { return _CurveData; }
set { _CurveData = value; }
}
//public string[] CurveDate
//{
// get { return _CurveDate; }
// set { _CurveDate = value; }
//} /// <summary>
/// 是否显示曲线
/// </summary>
private bool _IsDemonstrationCurve=true;
public bool IsDemonstrationCurve
{
get { return _IsDemonstrationCurve;}
set {
if(value==false)
{
_IsDemonstrationCurve = false;
}else
{
_IsDemonstrationCurve = true;
} }
}
/// <summary>
/// 是否显示 矩形图
/// </summary>
private bool _IsDemonstrationRectangle=true;
public bool IsDemonstrationRectangle
{
get { return _IsDemonstrationRectangle; }
set
{
if(value==false)
{
_IsDemonstrationRectangle = false;
}else
{
_IsDemonstrationRectangle = true;
}
} }
/*****************************************属性设置结束***********************************************************************************/ public bool getCurve(string FileName)
{ try
{
/***********************大图的参数*****************************/
/*自写编码*/
int ImgWidth = ; //1.[总宽度] ***图宽度
int ImgHeight = ; //2.[总高度] ***图高度
int ItemNum = ; //3.[项目数量] ***图表划分的块
//int ChildSpace=15; //6.[各块间的间距]
int ChartLeft = ; //7.[图表左边距] ***图表距图的左边距离
int ChartRight = ; //8.[图表右边距] ***图表距图的右边距离
int ChartTop = ; //9.[图表顶边距] ***图表距图顶边距离
int ChartBottom = ; //10.[图表底边距] ***图表距图底边距离 /*************************坐标位置调整******************************************/
int YMaxValue = ; //11.[纵坐标标尺最大值] ***纵坐标标尺的最大值
int YItemNum = ; //12.[纵坐标标尺段数] ***纵坐标标尺的段数
int YTop = ; //13.[距纵轴顶端间隔]
int YStrStart = ; //14.[纵坐标标尺文字起始X坐标]
int XRight = ; //15.[距横轴右端间隔]
int XStrStart = ; //16.[横坐标标尺文字起始Y坐标] /*************************小刻度位置调整*********************************/
int ChildNum = ; //4.[块数] ***大块中划分的子项的数量
float ChildRate = 0.6f; //5.[各块总占空间比率]
int xiaoSpacing = ; //小标示和小刻度之间的距离
int Xxiaospace_between = ; //矩形距离小刻度的位置
int intjiange = ; //顶部名称说明距离 左边的距离 string[] strGIFName = new string[ItemNum]; //大标示 注意和项目个数有联系
//string strGIFName = "2009年价格走势图";
/*因为 最近要换 所以 只有今年的*/
for (int intGIF = ; intGIF <= ItemNum; intGIF++)
{ strGIFName[intGIF - ] = + intGIF + "年价格走势图";
// arrValueNames[1] = "2010年价格走势图";
// arrValueNames[2] = "2011年价格走势图";
} /*****************************各项数值的计算****************************************/
//[图表总宽度]=[总宽度]-[图表左边距]-[图表右边距]-[距横轴右端间隔]
int chartwidth = ImgWidth - ChartLeft - ChartRight - XRight;
//[项目宽度]=[图表总宽度]/[项目数量]
int itemwidth = chartwidth/ItemNum;
//[各块总占空间比率的实际宽度]=[项目宽度]*[各块总占空间比率]
int factwidth = Convert.ToInt32(Math.Floor(itemwidth*ChildRate));
//[各块矩形宽度]=[各块总占空间比率的实际宽度]/[块数]
int rectanglewidth = factwidth/ChildNum;
//[各块间的间距]=([项目宽度]-[各块总占空间比率的实际宽度])/([块数]+1)
int falge = ((itemwidth - factwidth)/(ChildNum + ));
decimal myFale = falge;
int childspace = Convert.ToInt32(Math.Floor(myFale));
//(itemwidth-factwidth)/(ChildNum+1) /***********************************开始画图******************************************************/
Graphics objGps; //建立画板对象
Bitmap objBitMap = new Bitmap(ImgWidth, ImgHeight); //建立位图对象
objGps = Graphics.FromImage(objBitMap); //根据位图对象建立画板对象
objGps.Clear(Color.White); //设置画板对象的背景色
/***************************横坐标设置*************************************************/
string[] strArrValueName = new string[ChildNum]; //标示名称(月份) 注意长度要和ChildNum 相同 //横坐标设置
for (int intquarter = ; intquarter <= strArrValueName.Length; intquarter++)
{
if (intquarter% == )
{
strArrValueName[intquarter - ] = "1~3月";
}
if (intquarter% == )
{
strArrValueName[intquarter - ] = "4~6月";
}
if (intquarter% == )
{
strArrValueName[intquarter - ] = "7~9月";
}
if (intquarter% == )
{
strArrValueName[intquarter - ] = "10~12月";
}
} /****************************数据的赋值*******************************/
/*********************************************************************/
//显示的数据
int[] arrValues = {, , , , , , , , , , , }; //数据数组
//CurveData for (int inttheNum = ; inttheNum <ChildNum; inttheNum++)
{ /* (ImgHeight - ChartBottom - ChartTop - YTop)/YItemNum*i 纵坐标的计算方法 */
//ImgHeight - ChartBottom - ChartTop - YTop 总高度-距离顶部高度-距离底部高度-y坐标距离顶部高度/标示个数个数=单个坐标的高度
// ImgHeight - ChartBottom-单个坐标的高度 总高度-距离底部高度-单个项目的高度
double dange = (ImgHeight - ChartBottom - ChartTop - YTop)/YItemNum; //单个高度的计算 double floatfalge = (double.Parse(CurveData[inttheNum])) * dange;
// double floatfalge = (ImgHeight - ChartBottom - ChartTop - YTop)*(inttheNum + 1)*400/YMaxValue;
arrValues[inttheNum] = Convert.ToInt32(Math.Floor(floatfalge));
} /***************************************纵坐标显示标示设置*******************************************/
//string[] arrValueNames = {"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0"};
string[] arrValueNames = new string[YItemNum]; //这里表示所有的纵坐标的标示度名称
for (int intValueName = ; intValueName < arrValueNames.Length; intValueName++)
{
arrValueNames[intValueName] = ((intValueName*) + ).ToString();
} //得出矩形宽度,和画图X轴位置 //[项目宽度]=[总宽度]/[项目数量]
//======[各块总占空间比率]=([各块矩形宽度]+[各块间的间距])/[项目宽度]
//[各块总占空间比率的实际宽度]=[项目宽度]*[各块总占空间比率]
//[各块矩形宽度]=([各块总占空间比率的实际宽度]-[各块间的间距]*([块数]))/[块数]
//[一边空余空间宽度]=([项目宽度]-[各块所占空间比率的总宽度])/2 System.Drawing.Point[] pi = new Point[arrValues.Length]; //定义折线点的对象数组
System.Drawing.Point[] pit = new Point[]; //定义坐标三角点的对象数组
System.Drawing.Pen pe = new Pen(new SolidBrush(GetColor()), 1f); //定义画直线的对象
//画纵轴
objGps.DrawLine(pe, new Point(ChartLeft, ImgHeight - ChartBottom), new Point(ChartLeft, ChartTop));
//画纵轴终点箭头
pit[].X = ImgWidth - ChartRight; //确定三角形三点的位置
pit[].Y = ImgHeight - ChartBottom - ;
pit[].X = ImgWidth - ChartRight;
pit[].Y = ImgHeight - ChartBottom + ;
pit[].X = ImgWidth - ChartRight + ;
pit[].Y = ImgHeight - ChartBottom;
objGps.FillPolygon(new SolidBrush(GetColor()), pit);
//画纵轴标尺和标尺描述
for (int i = ; i <= YItemNum; i++)
{
//画标尺
objGps.DrawLine(pe,
new PointF(ChartLeft,
ImgHeight - ChartBottom -
(ImgHeight - ChartBottom - ChartTop - YTop)/YItemNum*i),
new PointF(ChartLeft - ,
ImgHeight - ChartBottom -
(ImgHeight - ChartBottom - ChartTop - YTop)/YItemNum*i));
//画描述
objGps.DrawString(arrValueNames[i - ].ToString(), new Font("宋体", ), Brushes.Black,
new Point(YStrStart,
ImgHeight - ChartBottom -
(ImgHeight - ChartBottom - ChartTop - YTop)/YItemNum*i - ));
}
//画横轴
objGps.DrawLine(pe, new Point(ChartLeft, ImgHeight - ChartBottom),
new Point(ImgWidth - ChartRight, ImgHeight - ChartBottom));
//画横轴终点箭头
pit[].X = ChartLeft - ; //确定三角形三点的位置
pit[].Y = ChartTop;
pit[].X = ChartLeft + ;
pit[].Y = ChartTop;
pit[].X = ChartLeft;
pit[].Y = ChartTop - ;
objGps.FillPolygon(new SolidBrush(GetColor()), pit);
//画横轴标尺和标尺描述 arrValueNames = strGIFName; for (int i = ; i <= ItemNum; i++)
{
//i表示画的 第几个 ImgHeight - ChartBottom + 5 表示线的长度
//pe 表示pen 定义的一只笔
// ChartLeft + itemwidth * i, ImgHeight - ChartBottom 横坐标=左边距+显示个数 ImgHeight - ChartBottom纵坐标=图片总高度-底边距离 objGps.DrawLine(pe, new PointF(ChartLeft + itemwidth*i, ImgHeight - ChartBottom),
new PointF(ChartLeft + itemwidth*i, ImgHeight - ChartBottom + )); //画横坐标大刻度 //画小刻度和标示
for (int j = ; j <= ChildNum; j++)
{
//
float widthX; //定义起点和重点的横坐标
widthX = ChartLeft + itemwidth*j/ChildNum + (i - )*itemwidth;
//画小的刻度
objGps.DrawLine(pe, new PointF(widthX, ImgHeight - ChartBottom),
new PointF(widthX, ImgHeight - ChartBottom + ));
//画小标示 //arrValueNames[]
// ChartLeft + childspace + itemwidth * (i - 1) 横坐标=左边距+间隔+个数
int widthxiao;
widthxiao = ChartLeft + xiaoSpacing + itemwidth*(j - )/ChildNum + itemwidth*(i - );
objGps.DrawString(strArrValueName[j - ].ToString(), new Font("宋体", ), Brushes.Blue,
new Point(widthxiao, ImgHeight - ChartBottom + XStrStart - ));
} objGps.DrawString(arrValueNames[i - ].ToString(), new Font("宋体", ), Brushes.Black,
new Point(ChartLeft + intjiange + itemwidth*(i - ),
ImgHeight - ChartBottom + XStrStart));
} //框架完成 开始画图
for (int intfalge = ; intfalge <= ItemNum; intfalge++)
{ for (int j = ; j < ChildNum; j++) //画矩形图和折线图
{
int theintwidth = ;
//左边距+各块间距
theintwidth = ChartLeft + itemwidth * j / ChildNum + Xxiaospace_between + (intfalge - ) * itemwidth; if(IsDemonstrationRectangle==true)
{
//画矩形
//5表示距离小刻度的位置
//j * (childspace + rectanglewidth)) + childspace + ChartLeft
//画矩形里面的颜色
objGps.FillRectangle(new SolidBrush(GetColor(j)), theintwidth,
ImgHeight - ChartBottom - arrValues[j],
rectanglewidth, arrValues[j]);
//画矩形外框
objGps.DrawRectangle(Pens.Black, theintwidth, ImgHeight - ChartBottom - arrValues[j], rectanglewidth,
arrValues[j]);
} //********************************画折线图****************************************************
if(IsDemonstrationCurve==true)
{
// pi[j].X = (j * (childspace + rectanglewidth)) + childspace + ChartLeft;
pi[j].X = theintwidth; //定义折线的横坐标
pi[j].Y = ImgHeight - ChartBottom - arrValues[j]; //纵坐标=图片总高度-底部高度-实际数值 pe.SetLineCap(System.Drawing.Drawing2D.LineCap.RoundAnchor,
System.Drawing.Drawing2D.LineCap.RoundAnchor,
System.Drawing.Drawing2D.DashCap.Round);
if (j > ) //进行连线
{
objGps.DrawLine(pe, pi[j - ], pi[j]);
}
}
}
} //objBitMap.Save(Response.OutputStream, ImageFormat.Gif);//该位图对象以"GIF"格式输出
//objBitMap.Save();//进行保存
System.Web.HttpServerUtility myServer = System.Web.HttpContext.Current.Server;
string strPath= myServer.MapPath("~/CurveGIF/"); //string strPath = System.Web.HttpContext.Current.Request.PhysicalApplicationPath.ToString() +
//"/CurveGIF/"+FileName+".gif"; objBitMap.Save(strPath+FileName+".gif");
pe.Dispose();
objBitMap.Dispose();
GC.Collect(); //资源释放 垃圾回收
return true;
}catch(Exception ex)
{
return false;
}
} /**/
/// <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 :
objColor = Color.Blue;
break;
case :
objColor = Color.Yellow;
break;
case :
objColor = Color.Red;
break;
case :
objColor = Color.Orange;
break;
case :
objColor = Color.Purple;
break;
case :
objColor = Color.Brown;
break;
case :
objColor = Color.Pink;
break;
case :
objColor = Color.Peru;
break;
case :
objColor = Color.PowderBlue;
break;
case :
objColor = Color.RosyBrown;
break;
case :
objColor = Color.RoyalBlue;
break;
case :
objColor = Color.SaddleBrown;
break;
case :
objColor = Color.SeaGreen;
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;
} }
然后这里是 调用的方法:
Curve myCurve = new Curve(); /**设置参数**/
myCurve.IsDemonstrationCurve = true; //是否显示曲折线
myCurve.IsDemonstrationRectangle = false;//是否显示矩形
string[] strValue = {"","","","1.5"};//显示的数据 现在只能显示4 个
myCurve.CurveData = strValue; //赋值
myCurve.getCurve(Guid.NewGuid().ToString());//创建GIF 保存
Response.Write("GIF生成成功!");
不过要注意的是 需要建立文件夹
出处:http://blog.****.net/jacky4955/article/details/4061658