日期:2014-05-18  浏览次数:20416 次

网上找的代码,有个地方出错,高手帮看下。
先建个 Chart.cs

using 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 <