以下是Default.aspx.cs源码.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.Drawing;
using System.Drawing.Imaging;public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string[] years = new string[11] { "1998", "1999", "2000", "2001", "2002", "2003", "2004", "2005", "2006", "2007", "2008" };
float[] vals = new float[11] { 575, 584.8f, 598f, 575, 565.9f, 589.3f, 600.7f, 590, 570.4f, 565.8f, 580 };
HttpContext we = new HttpContext(Request,Response); CreateGraphic(we, 800, 600, "你好", "长", "宽", 10, 100, 9, years, vals);
}
/// <param name="context">请求上下文</param>
/// <param name="width">图片宽度</param>
/// <param name="height">图片高度</param>
/// <param name="title">图片标题</param>
/// <param name="unitX">X轴单位名称</param>
/// <param name="unitY">Y轴单位名称</param>
/// <param name="minVal">Y轴最小刻度值</param>
/// <param name="maxVal">Y轴最大刻度值</param>
/// <param name="density">密度【Y轴刻度数】</param>
/// <param name="tagsX">X轴项目数组</param>
/// <param name="vals">Y值数组</param> public static void CreateGraphic(System.Web.HttpContext context, int width, int height, string title, string unitX, string unitY, int minVal, int maxVal, int density, string[] tagsX, float[] vals)
{
#region 画图初始化
Bitmap bmap = new Bitmap(width, height);
Graphics gph = Graphics.FromImage(bmap);
gph.Clear(Color.White);
PointF cpt = new PointF(40, height - 60);//中心点
float xl = width - cpt.X;//800-40=760
float xlUnit = (xl - 60) / tagsX.Length; //X轴单位刻度长度 760-60/11=60
float ylUnit = (height - 90) / density; //Y轴单位刻度长度600-90/9
int yUnit = (maxVal - minVal) / density; //Y轴单位刻度代表的值
PointF[] xpt = new PointF[3] { new PointF(xl + 15, cpt.Y), new PointF(xl, cpt.Y - 8), new PointF(xl, cpt.Y + 8) }; //x轴三角形
PointF[] ypt = new PointF[3] { new PointF(cpt.X, 0), new PointF(cpt.X - 8, 15), new PointF(cpt.X + 8, 15) }; //y轴三角形
gph.DrawString(title, new Font("宋体", 14), Brushes.Blue, new PointF(cpt.X + 85, 10));//图表标题
#endregion #region 画x轴
gph.DrawLine(Pens.Black, cpt.X, cpt.Y, xl, cpt.Y);
gph.DrawPolygon(Pens.Black, xpt);
gph.FillPolygon(new SolidBrush(Color.Black), xpt);
gph.DrawString(unitX, new Font("宋体", 12), Brushes.Black, new PointF(xl, cpt.Y + 20));
#endregion #region 画y轴
gph.DrawLine(Pens.Black, cpt.X, cpt.Y, cpt.X, 15);
gph.DrawPolygon(Pens.Black, ypt);
gph.FillPolygon(new SolidBrush(Color.Black), ypt);
gph.DrawString(unitY, new Font("宋体", 12), Brushes.Black, new PointF(50, 7));
#endregion #region 画图
for (int i = 1; i <= tagsX.Length; i++)
{
//画y轴刻度
if (i <= density)
{
gph.DrawString(Convert.ToString(minVal + yUnit * i - 10), new Font("宋体", 11), Brushes.Black, new PointF(cpt.X - ((minVal + yUnit * i).ToString().Length) * 12, cpt.Y - i * ylUnit - 6));
gph.DrawLine(Pens.Black, cpt.X - 3, cpt.Y - i * ylUnit, cpt.X, cpt.Y - i * ylUnit);
}
//画x轴项目
for (int j = 0; j < tagsX[i - 1].Length; j++)
{
gph.DrawString(tagsX[i - 1].Substring(j, 1), new Font("宋体", 11), Brushes.Black, new PointF(cpt.X + i * xlUnit - 6, cpt.Y + 5 + 12 * j));
gph.DrawLine(Pens.Black, cpt.X + i * xlUnit, cpt.Y, cpt.X + i * xlUnit, cpt.Y + 3);
}
//画点
gph.DrawEllipse(Pens.Black, cpt.X + i * xlUnit - 1.5f, cpt.Y - ((vals[i - 1] - minVal) / yUnit) * ylUnit - 1.5f, 3, 3);
gph.FillEllipse(new SolidBrush(Color.Black), cpt.X + i * xlUnit - 1.5f, cpt.Y - ((vals[i - 1] - minVal) / yUnit) * ylUnit - 1.5f, 3, 3);
//画数值
gph.DrawString(vals[i - 1].ToString(), new Font("宋体", 11), Brushes.Black, new PointF(cpt.X + i * xlUnit, cpt.Y - ((vals[i - 1] - minVal) / yUnit) * ylUnit - 1.5f));
//画折线
if (i > 1)
{
gph.DrawLine(Pens.Red, cpt.X + (i - 1) * xlUnit, cpt.Y - ((vals[i - 2] - minVal) / yUnit) * ylUnit, cpt.X + i * xlUnit, cpt.Y - ((vals[i - 1] - minVal) / yUnit) * ylUnit);
}
}
if (density > tagsX.Length)
{
for (int i = tagsX.Length; i <= density; i++)
{
gph.DrawString(Convert.ToString(minVal + yUnit * i), new Font("宋体", 11), Brushes.Black, new PointF(cpt.X - ((minVal + yUnit * i).ToString().Length) * 12, cpt.Y - i * ylUnit - 6));
gph.DrawLine(Pens.Black, cpt.X - 3, cpt.Y - i * ylUnit, cpt.X, cpt.Y - i * ylUnit);
}
}
#endregion #region 输出到浏览器
System.IO.MemoryStream ms = new System.IO.MemoryStream();
bmap.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
context.Response.ClearContent();
context.Response.ContentType = "image/Gif";
context.Response.BinaryWrite(ms.ToArray());
// gph.Dispose();
// bmap.Dispose();
#endregion
}
}运行后,坐标轴是画出来了.折线却没画出来.请高手看看啊
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.Drawing;
using System.Drawing.Imaging;public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string[] years = new string[11] { "1998", "1999", "2000", "2001", "2002", "2003", "2004", "2005", "2006", "2007", "2008" };
float[] vals = new float[11] { 575, 584.8f, 598f, 575, 565.9f, 589.3f, 600.7f, 590, 570.4f, 565.8f, 580 };
HttpContext we = new HttpContext(Request,Response); CreateGraphic(we, 800, 600, "你好", "长", "宽", 10, 100, 9, years, vals);
}
/// <param name="context">请求上下文</param>
/// <param name="width">图片宽度</param>
/// <param name="height">图片高度</param>
/// <param name="title">图片标题</param>
/// <param name="unitX">X轴单位名称</param>
/// <param name="unitY">Y轴单位名称</param>
/// <param name="minVal">Y轴最小刻度值</param>
/// <param name="maxVal">Y轴最大刻度值</param>
/// <param name="density">密度【Y轴刻度数】</param>
/// <param name="tagsX">X轴项目数组</param>
/// <param name="vals">Y值数组</param> public static void CreateGraphic(System.Web.HttpContext context, int width, int height, string title, string unitX, string unitY, int minVal, int maxVal, int density, string[] tagsX, float[] vals)
{
#region 画图初始化
Bitmap bmap = new Bitmap(width, height);
Graphics gph = Graphics.FromImage(bmap);
gph.Clear(Color.White);
PointF cpt = new PointF(40, height - 60);//中心点
float xl = width - cpt.X;//800-40=760
float xlUnit = (xl - 60) / tagsX.Length; //X轴单位刻度长度 760-60/11=60
float ylUnit = (height - 90) / density; //Y轴单位刻度长度600-90/9
int yUnit = (maxVal - minVal) / density; //Y轴单位刻度代表的值
PointF[] xpt = new PointF[3] { new PointF(xl + 15, cpt.Y), new PointF(xl, cpt.Y - 8), new PointF(xl, cpt.Y + 8) }; //x轴三角形
PointF[] ypt = new PointF[3] { new PointF(cpt.X, 0), new PointF(cpt.X - 8, 15), new PointF(cpt.X + 8, 15) }; //y轴三角形
gph.DrawString(title, new Font("宋体", 14), Brushes.Blue, new PointF(cpt.X + 85, 10));//图表标题
#endregion #region 画x轴
gph.DrawLine(Pens.Black, cpt.X, cpt.Y, xl, cpt.Y);
gph.DrawPolygon(Pens.Black, xpt);
gph.FillPolygon(new SolidBrush(Color.Black), xpt);
gph.DrawString(unitX, new Font("宋体", 12), Brushes.Black, new PointF(xl, cpt.Y + 20));
#endregion #region 画y轴
gph.DrawLine(Pens.Black, cpt.X, cpt.Y, cpt.X, 15);
gph.DrawPolygon(Pens.Black, ypt);
gph.FillPolygon(new SolidBrush(Color.Black), ypt);
gph.DrawString(unitY, new Font("宋体", 12), Brushes.Black, new PointF(50, 7));
#endregion #region 画图
for (int i = 1; i <= tagsX.Length; i++)
{
//画y轴刻度
if (i <= density)
{
gph.DrawString(Convert.ToString(minVal + yUnit * i - 10), new Font("宋体", 11), Brushes.Black, new PointF(cpt.X - ((minVal + yUnit * i).ToString().Length) * 12, cpt.Y - i * ylUnit - 6));
gph.DrawLine(Pens.Black, cpt.X - 3, cpt.Y - i * ylUnit, cpt.X, cpt.Y - i * ylUnit);
}
//画x轴项目
for (int j = 0; j < tagsX[i - 1].Length; j++)
{
gph.DrawString(tagsX[i - 1].Substring(j, 1), new Font("宋体", 11), Brushes.Black, new PointF(cpt.X + i * xlUnit - 6, cpt.Y + 5 + 12 * j));
gph.DrawLine(Pens.Black, cpt.X + i * xlUnit, cpt.Y, cpt.X + i * xlUnit, cpt.Y + 3);
}
//画点
gph.DrawEllipse(Pens.Black, cpt.X + i * xlUnit - 1.5f, cpt.Y - ((vals[i - 1] - minVal) / yUnit) * ylUnit - 1.5f, 3, 3);
gph.FillEllipse(new SolidBrush(Color.Black), cpt.X + i * xlUnit - 1.5f, cpt.Y - ((vals[i - 1] - minVal) / yUnit) * ylUnit - 1.5f, 3, 3);
//画数值
gph.DrawString(vals[i - 1].ToString(), new Font("宋体", 11), Brushes.Black, new PointF(cpt.X + i * xlUnit, cpt.Y - ((vals[i - 1] - minVal) / yUnit) * ylUnit - 1.5f));
//画折线
if (i > 1)
{
gph.DrawLine(Pens.Red, cpt.X + (i - 1) * xlUnit, cpt.Y - ((vals[i - 2] - minVal) / yUnit) * ylUnit, cpt.X + i * xlUnit, cpt.Y - ((vals[i - 1] - minVal) / yUnit) * ylUnit);
}
}
if (density > tagsX.Length)
{
for (int i = tagsX.Length; i <= density; i++)
{
gph.DrawString(Convert.ToString(minVal + yUnit * i), new Font("宋体", 11), Brushes.Black, new PointF(cpt.X - ((minVal + yUnit * i).ToString().Length) * 12, cpt.Y - i * ylUnit - 6));
gph.DrawLine(Pens.Black, cpt.X - 3, cpt.Y - i * ylUnit, cpt.X, cpt.Y - i * ylUnit);
}
}
#endregion #region 输出到浏览器
System.IO.MemoryStream ms = new System.IO.MemoryStream();
bmap.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
context.Response.ClearContent();
context.Response.ContentType = "image/Gif";
context.Response.BinaryWrite(ms.ToArray());
// gph.Dispose();
// bmap.Dispose();
#endregion
}
}运行后,坐标轴是画出来了.折线却没画出来.请高手看看啊
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货