先建个 Chart.csusing System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Drawing.Imaging;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Data;
///
///Chart 的摘要说明
///
public class Chart
{
public Chart()
{
// //TODO: 在此处添加构造函数逻辑 //
}
///
/// 绘制走势图
///
/// 承载的页面
/// 数据源
/// 图的标题
public static void DrawChart(Page page, DataTable dt, string title)
{
#region file
int count = dt.Rows.Count;
//记算图表宽度
int wd = 30 * (count );
Bitmap img = new Bitmap(wd, 400);
Graphics g = Graphics.FromImage(img);
//黑色、红色、银灰色画笔
Pen Bp = new Pen(Color.Black);
Pen Rp = new Pen(Color.Red);
Pen Sp = new Pen(Color.Silver);
//定义字体
Font Bfont = new Font("Arial", 12, FontStyle.Bold);
Font font = new Font("Arial", 6);
Font Tfont = new Font("Arial", 9);
//绘制图片底色
g.DrawRectangle(new Pen(Color.White, 400), 0, 0, img.Width, img.Height); LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, img.Width, img.Height), Color.YellowGreen, Color.Black, 1.2F, true); LinearGradientBrush Bluebrush = new LinearGradientBrush(new Rectangle(0, 0, img.Width, img.Height), Color.Blue, Color.Blue, 1.2F, true);
#endregion #region 绘制图形的辅助数据
////绘制标题
g.DrawString(title, Bfont, brush, 40, 5);
////绘制图片边框
g.DrawRectangle(Bp, 0, 0, img.Width - 1, img.Height - 1);
//绘制竖坐标线 (表格纵坐标)
for (int i = 0; i < count; i++)
{
g.DrawLine(Sp, 40 + 20 * i, 60, 40 + 20 * i, 360);
}
//绘制时间轴坐标值(横坐标值)
for (int i = 0; i < count; i += 2)
{
//取日期的day
string datetime = dt.Rows[i]["createdtm"].ToString(); g.DrawString(datetime, font, brush, 30 + 20 * i, 370);
}
//绘制横坐标线
for (int i = 0; i < 10; i++)
{ //绘制横向表格线
g.DrawLine(Sp, 40, 60 + 30 * i, 40 + 20 * (count - 1), 60 + 30 * i);
//绘制纵坐标的刻度
int s = 10000 - 200 * i * 5;
g.DrawString(s.ToString(), font, brush, 10, 60 + 30 * i);
}
////绘制竖坐标标题
g.DrawString("价格(¥)", Tfont, brush, 5, 40);
////绘制横坐标标题
string Xtitle = DateTime.Now.Year.ToString() + "年" + DateTime.Now.Month.ToString() + "月";
g.DrawString(Xtitle, Tfont, brush, 40, 385);
#region 绘制两条坐标轴的轴线放在表格绘制的后面,以免覆盖
//绘制图竖坐标轴线
g.DrawLine(Bp, 40, 55, 40, 360);
//绘制图横坐标轴线
g.DrawLine(Bp, 40, 360, 45 + 20 * (count - 1), 360); #endregion
#endregion
#region 绘制图形中曲线及数据
//定义曲线转折点
Point[] p = new Point[count];
for (int i = 0; i < count; i++)
{
p[i].X = 40 + 20 * i; p[i].Y = 360 - Convert.ToInt32(dt.Rows[i]["price"]) / 27 * 4 / 5;
}
//绘制曲线
g.DrawLines(Rp, p);
for (int i = 0; i < count; i++)
{
//绘制价格点数字
g.DrawString(dt.Rows[i]["price"].ToString(), font, Bluebrush, p[i].X, p[i].Y - 10);
//绘制价格点
g.DrawRectangle(Rp, p[i].X - 1, p[i].Y - 1, 2, 2);
}
#endregion #region 保存与输出
//保存绘制的图片
MemoryStream stream = new MemoryStream();
img.Save(stream, ImageFormat.Jpeg);
//图片输出
page.Response.Clear(); page.Response.ContentType = "image/jpeg"; page.Response.BinaryWrite(stream.ToArray());
#endregion
}
}
然后在页面后台调用protected void Page_Load(object sender, EventArgs e)
{
string st = " name='冷轧卷板'";
string title = "冷轧卷板走势图";
DataTable dt = new DataTable();
dt.Columns.Add("createdtm");
dt.Columns.Add("price"); DataRow dr = dt.NewRow();
object[] objs = { "10-20", "3000" };
object[] objs1 = { "10-21", "2000" };
dr.ItemArray = objs;
dr.ItemArray = objs1;
dt.Rows.Add(dr);
Chart.DrawChart(Page, dt, title);
}调试运行后,//绘制曲线
这句会报错! g.DrawLines(Rp, p);
Rp参数无效。求高手帮忙下了。
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Drawing.Imaging;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Data;
///
///Chart 的摘要说明
///
public class Chart
{
public Chart()
{
// //TODO: 在此处添加构造函数逻辑 //
}
///
/// 绘制走势图
///
/// 承载的页面
/// 数据源
/// 图的标题
public static void DrawChart(Page page, DataTable dt, string title)
{
#region file
int count = dt.Rows.Count;
//记算图表宽度
int wd = 30 * (count );
Bitmap img = new Bitmap(wd, 400);
Graphics g = Graphics.FromImage(img);
//黑色、红色、银灰色画笔
Pen Bp = new Pen(Color.Black);
Pen Rp = new Pen(Color.Red);
Pen Sp = new Pen(Color.Silver);
//定义字体
Font Bfont = new Font("Arial", 12, FontStyle.Bold);
Font font = new Font("Arial", 6);
Font Tfont = new Font("Arial", 9);
//绘制图片底色
g.DrawRectangle(new Pen(Color.White, 400), 0, 0, img.Width, img.Height); LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, img.Width, img.Height), Color.YellowGreen, Color.Black, 1.2F, true); LinearGradientBrush Bluebrush = new LinearGradientBrush(new Rectangle(0, 0, img.Width, img.Height), Color.Blue, Color.Blue, 1.2F, true);
#endregion #region 绘制图形的辅助数据
////绘制标题
g.DrawString(title, Bfont, brush, 40, 5);
////绘制图片边框
g.DrawRectangle(Bp, 0, 0, img.Width - 1, img.Height - 1);
//绘制竖坐标线 (表格纵坐标)
for (int i = 0; i < count; i++)
{
g.DrawLine(Sp, 40 + 20 * i, 60, 40 + 20 * i, 360);
}
//绘制时间轴坐标值(横坐标值)
for (int i = 0; i < count; i += 2)
{
//取日期的day
string datetime = dt.Rows[i]["createdtm"].ToString(); g.DrawString(datetime, font, brush, 30 + 20 * i, 370);
}
//绘制横坐标线
for (int i = 0; i < 10; i++)
{ //绘制横向表格线
g.DrawLine(Sp, 40, 60 + 30 * i, 40 + 20 * (count - 1), 60 + 30 * i);
//绘制纵坐标的刻度
int s = 10000 - 200 * i * 5;
g.DrawString(s.ToString(), font, brush, 10, 60 + 30 * i);
}
////绘制竖坐标标题
g.DrawString("价格(¥)", Tfont, brush, 5, 40);
////绘制横坐标标题
string Xtitle = DateTime.Now.Year.ToString() + "年" + DateTime.Now.Month.ToString() + "月";
g.DrawString(Xtitle, Tfont, brush, 40, 385);
#region 绘制两条坐标轴的轴线放在表格绘制的后面,以免覆盖
//绘制图竖坐标轴线
g.DrawLine(Bp, 40, 55, 40, 360);
//绘制图横坐标轴线
g.DrawLine(Bp, 40, 360, 45 + 20 * (count - 1), 360); #endregion
#endregion
#region 绘制图形中曲线及数据
//定义曲线转折点
Point[] p = new Point[count];
for (int i = 0; i < count; i++)
{
p[i].X = 40 + 20 * i; p[i].Y = 360 - Convert.ToInt32(dt.Rows[i]["price"]) / 27 * 4 / 5;
}
//绘制曲线
g.DrawLines(Rp, p);
for (int i = 0; i < count; i++)
{
//绘制价格点数字
g.DrawString(dt.Rows[i]["price"].ToString(), font, Bluebrush, p[i].X, p[i].Y - 10);
//绘制价格点
g.DrawRectangle(Rp, p[i].X - 1, p[i].Y - 1, 2, 2);
}
#endregion #region 保存与输出
//保存绘制的图片
MemoryStream stream = new MemoryStream();
img.Save(stream, ImageFormat.Jpeg);
//图片输出
page.Response.Clear(); page.Response.ContentType = "image/jpeg"; page.Response.BinaryWrite(stream.ToArray());
#endregion
}
}
然后在页面后台调用protected void Page_Load(object sender, EventArgs e)
{
string st = " name='冷轧卷板'";
string title = "冷轧卷板走势图";
DataTable dt = new DataTable();
dt.Columns.Add("createdtm");
dt.Columns.Add("price"); DataRow dr = dt.NewRow();
object[] objs = { "10-20", "3000" };
object[] objs1 = { "10-21", "2000" };
dr.ItemArray = objs;
dr.ItemArray = objs1;
dt.Rows.Add(dr);
Chart.DrawChart(Page, dt, title);
}调试运行后,//绘制曲线
这句会报错! g.DrawLines(Rp, p);
Rp参数无效。求高手帮忙下了。
http://www.fusioncharts.com/