谭玉琼

天行健,君子以自强不息,地势坤,君子以厚得载物
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

ZedGraph 在项目中的简单应用

Posted on 2007-12-13 22:17  谭玉琼  阅读(3598)  评论(0编辑  收藏  举报
 Step1 :   下载ZedGraph 4.6 (.net 1.1)
 Step2 :   在vs2003.net 中项目中添加引用   ZedGraph.dll  和  ZedGraph.Web.dll
 Step3 :   在工具箱中添加   ZedGraph.Web.dll 
 Step4 :   拖拽刚添加的可视化组件 
 Step5 :   aspx页面生成代码   

<%@ Register TagPrefix="cc1" Namespace="ZedGraph.Web" Assembly="ZedGraph.Web" %>
<%@ Page language="c#" Codebehind="netpictest.aspx.cs" AutoEventWireup="false" Inherits="WS.Webs.netpictest" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
 <HEAD>
  <title>netpictest</title>
  <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
  <meta name="CODE_LANGUAGE" Content="C#">
  <meta name="vs_defaultClientScript" content="JavaScript">
  <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
 </HEAD>
 <body MS_POSITIONING="GridLayout">
  <form id="Form1" method="post" runat="server">
   <cc1:ZedGraphWeb id="ZedGraphWeb1" runat="server"></cc1:ZedGraphWeb>
  </form>
 </body>
</HTML>

step6  :  aspx.cs 代码

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using ZedGraph;
using ZedGraph.Web;

namespace WS.Webs
{
 /// <summary>
 /// netpictest 的摘要说明。
 /// </summary>
 public class netpictest : System.Web.UI.Page
 {
  protected ZedGraph.Web.ZedGraphWeb ZedGraphWeb1;
 
  private void Page_Load(object sender, System.EventArgs e)
  {
   // 在此处放置用户代码以初始化页面
  }

  #region Web 窗体设计器生成的代码
  override protected void OnInit(EventArgs e)
  {
   //
   // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
   //
   InitializeComponent();
   base.OnInit(e);
  }
  
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {   
   this.Load += new System.EventHandler(this.Page_Load);
   this.ZedGraphWeb1.RenderGraph +=new ZedGraphWebControlEventHandler(this.OnRenderGraph);


  }
  #endregion


  private void OnRenderGraph(ZedGraphWeb zgw, Graphics g, 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();
   Random rand = new Random();

   for (double x = 0; x < 5; x += 1.0)
   {
    double y = rand.NextDouble() * 100;
    double y2 = rand.NextDouble() * 100;
    double y3 = rand.NextDouble() * 100;
    list.Add(x, y);
    list2.Add(x, y2);
    list3.Add(x, y3);
   }
 
   BarItem myCurve = myPane.AddBar("购买", list, Color.Blue);
   myCurve.Bar.Fill = new Fill(Color.Blue, Color.White, Color.Blue);
   BarItem myCurve2 = myPane.AddBar("续费", list2, Color.Red);
   myCurve2.Bar.Fill = new Fill(Color.Red, Color.White, Color.Red);
   BarItem myCurve3 = myPane.AddBar("升级", list3, Color.Green);
   myCurve3.Bar.Fill = new Fill(Color.Green, Color.White, Color.Green);
 
   myPane.XAxis.MajorTic.IsBetweenLabels = true;
   string[] labels = { "域名", "主机", "数据库", "邮局", "套餐" };
   myPane.XAxis.Scale.TextLabels = labels;
   myPane.XAxis.Type = AxisType.Text;
   myPane.Fill = new Fill(Color.White, Color.FromArgb(200, 200, 255), 45.0f);
   myPane.Chart.Fill = new Fill(Color.White, Color.LightGoldenrodYellow, 45.0f);

   masterPane.AxisChange(g);
  }

 }
}

Step7 : 在项目中创建默认的图片保存路径文件夹  ZedGraphImages

最后效果如下: