画出的图片如下:
下面是代码:
/// <copyright>天志(六子) 1989-2006</copyright>
/// <version>1.0</version>
/// <author>天志</author>
/// <email></email>
/// <log date="2006-11-10">创建</log>
namespace MyBank.UI
{
using System;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using DOTNET.Utils;
using Data;
using Business.Interface;
/// <summary>
/// 天志(六子)的曲线走势图。
/// </summary>
/// <author>天志</author>
/// <log date="2006-11-10">创建</log>
public class Curve : System.Web.UI.UserControl
{
private void Page_Load(object sender, System.EventArgs e)
{
// 绘制曲线走势图
InitializeGraph();
}
/// <summary>
/// 画图板的宽度。
/// </summary>
/// <author>天志</author>
/// <log date="2006-11-10">创建</log>
int Width
{
get
{
return (5*widthNum) + 200;
}
}
/// <summary>
/// 画图板的高度。
/// </summary>
/// <author>天志</author>
/// <log date="2006-11-10">创建</log>
int Height = 520;
/// <summary>
/// 曲线图的节点数。
/// </summary>
/// <author>天志</author>
/// <log date="2006-11-10">创建</log>
int widthNum
{
get
{
return 164;
}
}
/// <summary>
/// 画图板中的纵轴刻度数。
/// </summary>
/// <author>天志</author>
/// <log date="2006-11-10">创建</log>
int heigthNum = 300;
/// <summary>
/// 绘制曲线走势图。
/// </summary>
/// <author>天志</author>
/// <log date="2006-11-10">创建</log>
private void InitializeGraph()
{
// 根据给定的高度和宽度创建一个位图图像
Bitmap objBitmap = new Bitmap(Width, Height);
// 从指定的 objBitmap 对象创建 objGraphics 对象 (即在objBitmap对象中画图)
Graphics objGraphics = Graphics.FromImage(objBitmap);
// 设置画图板边框颜色
objGraphics.DrawRectangle
(
new Pen(Color.Green, 1),
0,
0,
Width,
Height
);
// 填充画图板
objGraphics.FillRectangle
(new SolidBrush(Color.LightSteelBlue),
1,
1,
(Width - 2),
(Height - 2)
);
// 在某一高度画一条横线
objGraphics.DrawLine
(
new Pen(new SolidBrush(Color.Aqua), 1),
100,
(Height - 100),
(Width - 75),
(Height - 100)
);
// 在某一处画一条直立的直线
objGraphics.DrawLine
(
new Pen(new SolidBrush(Color.Aqua), 1),
100,
(Height - 100),
100,
75);
初始化轴线说明文字
// 初始化X轴上的刻度和文字
SetXAxis(ref objGraphics);
// 初始化Y轴上的刻度和文字
SetYAxis(ref objGraphics);
// 初始化标题
CreateTitle(ref objGraphics);
DrawContent(ref objGraphics);
// 将objGraphics对象以指定的图形格式(这里是Gif)保存到指定的Stream对象,并输出到客户端。
objBitmap.Save(HttpContext.Current.Response.OutputStream, ImageFormat.Gif);
}
/// <summary>
/// 设置画图板横轴的刻度。
/// </summary>
/// <param name="objGraphics"></param>
/// <author>天志</author>
/// <log date="2006-11-10">创建</log>
private void SetXAxis(ref Graphics objGraphics)
{
int x1 = 100;
int y1 = Height - 110;
int x2 = 100;
int y2 = Height - 90;
// 判断是否为7的标记
int iCount = 0;
// 刻度尺的纵轴坐标点
float Scale = 0;
// 取得每个刻度的大小
int iWidth = Width - 200;
// 画出横轴开始点
objGraphics.DrawString
(
"开始",
new Font("宋体", 8),
new SolidBrush(Color.Blue),
85,
(Height-90)
);
// 设置横轴的刻度
for(int i = 0; i <= widthNum; i++)
{
// 如果横轴等于7(间隔一周),则画一个长刻度
if(iCount == 7)
{
// 画出刻度
objGraphics.DrawLine
(
new Pen(new SolidBrush(Color.Red)),
(x1 + Scale),
y1,
(x2 +Scale),
y2
);
// 画上刻度标记
objGraphics.DrawString
(
i.ToString(),
new Font("宋体", 8),
new SolidBrush(Color.Black),
(x1 + Scale - 15),
y2
);
iCount = 0;
}
else
{
// 画一个短刻度
objGraphics.DrawLine
(
new Pen(new SolidBrush(Color.Red)),
(x1 + Scale),
(y1 + 5),
(x2 + Scale),
(y2 - 5)
);
}
iCount++;
Scale += 5;
}
}
/// <summary>
/// 设置画图板纵轴的刻度。
/// </summary>
/// <param name="objGraphics"></param>
/// <author>天志</author>
/// <log date="2006-11-10">创建</log>
private void SetYAxis(ref Graphics objGraphics)
{
int x1 = 95;
int y1 = Height - 100;
int x2 = 105;
int y2 = Height - 100;
int iCount = 1;
float Scale = 0;
// 画出纵轴的开始点
objGraphics.DrawString
(
"开始",
new Font("宋体", 10),
new SolidBrush(Color.Blue),
60,
(Height - 110)
);
for(int i = 0; i < heigthNum; i++)
{
// 如果纵轴等于10,则画一个长刻度
if (iCount == 10)
{
// 画出刻度
objGraphics.DrawLine
(
new Pen(new SolidBrush(Color.Red)),
(x1 - 5),
(y1 - Scale),
(x2 + 5),
(y2 - Scale)
);
iCount = 0;
}
else
{
// 画出刻度
objGraphics.DrawLine
(
new Pen(new SolidBrush(Color.Red)),
(x1 + 5),
(y1-Scale),
(x2 - 5),
(y2 - Scale)
);
if (iCount == 1)
{
// 画上刻度标记
objGraphics.DrawString
(
Convert.ToString((i + "")),
new Font("宋体", 8),
new SolidBrush(Color.Black),
60,
(y1 - Scale)
);
}
}
iCount++;
Scale += 1;
}
}
/// <summary>
/// 设置图片标题。
/// </summary>
/// <param name="objGraphics"></param>
/// <author>天志</author>
/// <log date="2006-11-10">创建</log>
private void CreateTitle(ref Graphics objGraphics)
{
// 设置图片标题
objGraphics.DrawString(
"天志(六子)的曲线走势图",
new Font("宋体",16),
new SolidBrush(Color.Blue),
new Point(5,5));
}
/// <summary>
/// 绘制曲线图。
/// </summary>
/// <param name="objGraphics"></param>
/// <author>天志</author>
/// <log date="2006-11-10">创建</log>
private void DrawContent(ref Graphics objGraphics)
{
// 存储曲线上的节点数组
float[] floatArr = new float[164];
// 取得一个随机数
Random Seed = new Random();
int randomNum = Seed.Next(1, 100);
// 给节点数组赋值
for (int i = 0; i < 164; i++)
{
randomNum = Seed.Next(1, 10);
floatArr[i] = (randomNum*10) + 150;
}
// 设置绘制画笔
Pen CurvePen = new Pen(Color.Red, 1);
PointF[] CurvePointF = new PointF[widthNum];
// 开始的画板横轴
float keys = 0;
// 节点纵轴位置
float values = 0;
// 开始的画板纵轴
float Offset1 = Height - 100;
for(int i = 0;i < widthNum; i++)
{
// 设置画板横轴位置
keys = (5*i) + 100;
// 取得节点纵轴位置
values = Offset1 - floatArr[i];
// 设置绘制节点
CurvePointF[i] = new PointF(keys, values);
}
// 绘制曲线
objGraphics.DrawCurve
(
CurvePen,
CurvePointF,
1.0F
);
}
Web 窗体设计器生成的代码
}
}
/// <version>1.0</version>
/// <author>天志</author>
/// <email></email>
/// <log date="2006-11-10">创建</log>
namespace MyBank.UI
{
using System;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using DOTNET.Utils;
using Data;
using Business.Interface;
/// <summary>
/// 天志(六子)的曲线走势图。
/// </summary>
/// <author>天志</author>
/// <log date="2006-11-10">创建</log>
public class Curve : System.Web.UI.UserControl
{
private void Page_Load(object sender, System.EventArgs e)
{
// 绘制曲线走势图
InitializeGraph();
}
/// <summary>
/// 画图板的宽度。
/// </summary>
/// <author>天志</author>
/// <log date="2006-11-10">创建</log>
int Width
{
get
{
return (5*widthNum) + 200;
}
}
/// <summary>
/// 画图板的高度。
/// </summary>
/// <author>天志</author>
/// <log date="2006-11-10">创建</log>
int Height = 520;
/// <summary>
/// 曲线图的节点数。
/// </summary>
/// <author>天志</author>
/// <log date="2006-11-10">创建</log>
int widthNum
{
get
{
return 164;
}
}
/// <summary>
/// 画图板中的纵轴刻度数。
/// </summary>
/// <author>天志</author>
/// <log date="2006-11-10">创建</log>
int heigthNum = 300;
/// <summary>
/// 绘制曲线走势图。
/// </summary>
/// <author>天志</author>
/// <log date="2006-11-10">创建</log>
private void InitializeGraph()
{
// 根据给定的高度和宽度创建一个位图图像
Bitmap objBitmap = new Bitmap(Width, Height);
// 从指定的 objBitmap 对象创建 objGraphics 对象 (即在objBitmap对象中画图)
Graphics objGraphics = Graphics.FromImage(objBitmap);
// 设置画图板边框颜色
objGraphics.DrawRectangle
(
new Pen(Color.Green, 1),
0,
0,
Width,
Height
);
// 填充画图板
objGraphics.FillRectangle
(new SolidBrush(Color.LightSteelBlue),
1,
1,
(Width - 2),
(Height - 2)
);
// 在某一高度画一条横线
objGraphics.DrawLine
(
new Pen(new SolidBrush(Color.Aqua), 1),
100,
(Height - 100),
(Width - 75),
(Height - 100)
);
// 在某一处画一条直立的直线
objGraphics.DrawLine
(
new Pen(new SolidBrush(Color.Aqua), 1),
100,
(Height - 100),
100,
75);
初始化轴线说明文字
// 初始化X轴上的刻度和文字
SetXAxis(ref objGraphics);
// 初始化Y轴上的刻度和文字
SetYAxis(ref objGraphics);
// 初始化标题
CreateTitle(ref objGraphics);
DrawContent(ref objGraphics);
// 将objGraphics对象以指定的图形格式(这里是Gif)保存到指定的Stream对象,并输出到客户端。
objBitmap.Save(HttpContext.Current.Response.OutputStream, ImageFormat.Gif);
}
/// <summary>
/// 设置画图板横轴的刻度。
/// </summary>
/// <param name="objGraphics"></param>
/// <author>天志</author>
/// <log date="2006-11-10">创建</log>
private void SetXAxis(ref Graphics objGraphics)
{
int x1 = 100;
int y1 = Height - 110;
int x2 = 100;
int y2 = Height - 90;
// 判断是否为7的标记
int iCount = 0;
// 刻度尺的纵轴坐标点
float Scale = 0;
// 取得每个刻度的大小
int iWidth = Width - 200;
// 画出横轴开始点
objGraphics.DrawString
(
"开始",
new Font("宋体", 8),
new SolidBrush(Color.Blue),
85,
(Height-90)
);
// 设置横轴的刻度
for(int i = 0; i <= widthNum; i++)
{
// 如果横轴等于7(间隔一周),则画一个长刻度
if(iCount == 7)
{
// 画出刻度
objGraphics.DrawLine
(
new Pen(new SolidBrush(Color.Red)),
(x1 + Scale),
y1,
(x2 +Scale),
y2
);
// 画上刻度标记
objGraphics.DrawString
(
i.ToString(),
new Font("宋体", 8),
new SolidBrush(Color.Black),
(x1 + Scale - 15),
y2
);
iCount = 0;
}
else
{
// 画一个短刻度
objGraphics.DrawLine
(
new Pen(new SolidBrush(Color.Red)),
(x1 + Scale),
(y1 + 5),
(x2 + Scale),
(y2 - 5)
);
}
iCount++;
Scale += 5;
}
}
/// <summary>
/// 设置画图板纵轴的刻度。
/// </summary>
/// <param name="objGraphics"></param>
/// <author>天志</author>
/// <log date="2006-11-10">创建</log>
private void SetYAxis(ref Graphics objGraphics)
{
int x1 = 95;
int y1 = Height - 100;
int x2 = 105;
int y2 = Height - 100;
int iCount = 1;
float Scale = 0;
// 画出纵轴的开始点
objGraphics.DrawString
(
"开始",
new Font("宋体", 10),
new SolidBrush(Color.Blue),
60,
(Height - 110)
);
for(int i = 0; i < heigthNum; i++)
{
// 如果纵轴等于10,则画一个长刻度
if (iCount == 10)
{
// 画出刻度
objGraphics.DrawLine
(
new Pen(new SolidBrush(Color.Red)),
(x1 - 5),
(y1 - Scale),
(x2 + 5),
(y2 - Scale)
);
iCount = 0;
}
else
{
// 画出刻度
objGraphics.DrawLine
(
new Pen(new SolidBrush(Color.Red)),
(x1 + 5),
(y1-Scale),
(x2 - 5),
(y2 - Scale)
);
if (iCount == 1)
{
// 画上刻度标记
objGraphics.DrawString
(
Convert.ToString((i + "")),
new Font("宋体", 8),
new SolidBrush(Color.Black),
60,
(y1 - Scale)
);
}
}
iCount++;
Scale += 1;
}
}
/// <summary>
/// 设置图片标题。
/// </summary>
/// <param name="objGraphics"></param>
/// <author>天志</author>
/// <log date="2006-11-10">创建</log>
private void CreateTitle(ref Graphics objGraphics)
{
// 设置图片标题
objGraphics.DrawString(
"天志(六子)的曲线走势图",
new Font("宋体",16),
new SolidBrush(Color.Blue),
new Point(5,5));
}
/// <summary>
/// 绘制曲线图。
/// </summary>
/// <param name="objGraphics"></param>
/// <author>天志</author>
/// <log date="2006-11-10">创建</log>
private void DrawContent(ref Graphics objGraphics)
{
// 存储曲线上的节点数组
float[] floatArr = new float[164];
// 取得一个随机数
Random Seed = new Random();
int randomNum = Seed.Next(1, 100);
// 给节点数组赋值
for (int i = 0; i < 164; i++)
{
randomNum = Seed.Next(1, 10);
floatArr[i] = (randomNum*10) + 150;
}
// 设置绘制画笔
Pen CurvePen = new Pen(Color.Red, 1);
PointF[] CurvePointF = new PointF[widthNum];
// 开始的画板横轴
float keys = 0;
// 节点纵轴位置
float values = 0;
// 开始的画板纵轴
float Offset1 = Height - 100;
for(int i = 0;i < widthNum; i++)
{
// 设置画板横轴位置
keys = (5*i) + 100;
// 取得节点纵轴位置
values = Offset1 - floatArr[i];
// 设置绘制节点
CurvePointF[i] = new PointF(keys, values);
}
// 绘制曲线
objGraphics.DrawCurve
(
CurvePen,
CurvePointF,
1.0F
);
}
Web 窗体设计器生成的代码
}
}
本源代码参照种玉堂的《先发个绘制曲线图的:用ASP.NET with C# 绘制曲线图(Curve图) 》[URL:http://www.cnblogs.com/KenBlove/archive/2006/10/23/461082.html#537662]