Posted on 2006-08-27 13:16 Alvin Ye 叶冬开 阅读(526) 评论(8)   编辑   收藏 所属分类: .Net
/*Title: ZedGraph 在 web 中的应用
 *Author: AlvinYe
 *Date: Aug 27,2006
 *Description: ZedGraph 在 web 中的应用
 *Environment: WinXP sp2,Vs2005 pro,.netFramework 2.0
 *KeyWord: ZedGraph 在 web 中的应用 theme
 */

1.新建一个站点
2.引用ZedGraph.Web.dll 和 ZedGraph.dll 
[url]www.ZedGraph.org[/url] 可以下载到
 
3修改default2.aspx 改为以下内容
<%@ Page Language="C#" CodeFile="Default2.aspx.cs" Inherits="Default2" Theme="" %>
<%@ Register TagPrefix="zgw" Namespace="ZedGraph.Web" Assembly="ZedGraph.Web" %>
 
注意其中的Theme="" 如果你在web.config 中已经设置好theme的话默认所有页都会先加载theme
因为我们这里只要生成图片,如果加载Theme 就会显示错误,看到乱码.
 
4.修改default.aspx.cs 改为
using System;
using System.Drawing;
 
using ZedGraph;
using ZedGraph.Web;
 

public partial class Default2 : System.Web.UI.Page
{
  protected void Page_Load(object sender, System.EventArgs e)
  {
  }
 
  #region Web Form Designer generated code
  override protected void OnInit(EventArgs e)
  {
    //
    // CODEGEN: This call is required by the ASP.NET Web Form Designer.
    //
    InitializeComponent();
    base.OnInit(e);
  }
 
  ///
  /// Required method for Designer support - do not modify
  /// the contents of this method with the code editor.
  ///

  private void InitializeComponent()
  {
    this.ZedGraphWeb1.RenderGraph += new ZedGraph.Web.ZedGraphWebControlEventHandler(this.OnRenderGraph);
 
  }
  #endregion
 
  ///
  /// This method is where you generate your graph.
  ///

  /// You are provided with a MasterPane instance that
  /// contains one GraphPane by default (accessible via masterPane[0]).
  /// A graphics instance so you can easily make the call to AxisChange()
  private void OnRenderGraph(System.Drawing.Graphics g, ZedGraph.MasterPane masterPane)
  {
    // Get the GraphPane so we can work with it
    GraphPane myPane = masterPane[0];
 
    myPane.Title.Text = "学期成绩表";
    myPane.XAxis.Title.Text = "学期";
    myPane.YAxis.Title.Text = "成绩";
 
    PointPairList list = new PointPairList();
    PointPairList list2 = new PointPairList();
    PointPairList list3 = new PointPairList();
 
    DataTable dt = BuildDt();
 
    for (int i = 0; i < dt.Rows.Count; i++)
    {
      if (dt.Rows[i]["Id"].ToString() == "1")
      {
        list.Add(Convert.ToDouble(dt.Rows[i]["XueQi"]), Convert.ToDouble(dt.Rows[i]["FenShu"]), "Alvin");
      }
      else if (dt.Rows[i]["Id"].ToString() == "2")
      {
        list2.Add(Convert.ToDouble(dt.Rows[i]["XueQi"]), Convert.ToDouble(dt.Rows[i]["FenShu"]), "Robin");
      }
      else
      {
        list3.Add(Convert.ToDouble(dt.Rows[i]["XueQi"]), Convert.ToDouble(dt.Rows[i]["FenShu"]), "Jim");
      }
    }
 
    LineItem MyLine = myPane.AddCurve("Alvin", list, Color.Red, SymbolType.HDash);
    LineItem MyLine2 = myPane.AddCurve("Robin", list2, Color.Green);
    LineItem MyLine3 = myPane.AddCurve("Jim", list3, Color.Blue);
    masterPane.AxisChange(g);
  }
 
  private DataTable BuildDt()
  {
    DataTable dt = new DataTable("Students");
    dt.Columns.Add("Id");
    dt.Columns.Add("Name");
    dt.Columns.Add("XueQi");
    dt.Columns.Add("FenShu");
 
    dt.Rows.Add(1, "Alvin", "1", 100);
    dt.Rows.Add(1, "Alvin", "2", 90);
    dt.Rows.Add(1, "Alvin", "3", 80);
    dt.Rows.Add(1, "Alvin", "4", 120);
    dt.Rows.Add(2, "Robin", "1", 120);
    dt.Rows.Add(2, "Robin", "2", 100);
    dt.Rows.Add(2, "Robin", "3", 90);
    dt.Rows.Add(2, "Robin", "4", 110);
    dt.Rows.Add(3, "Jim", "1", 80);
    dt.Rows.Add(3, "Jim", "2", 90);
    dt.Rows.Add(3, "Jim", "3", 130);
    dt.Rows.Add(3, "Jim", "4", 135);
 
    return dt;
 
  }
}
 
运行结果如下