ZedGraph用法

我们现在的项目是.NET1.1开发的.我在网上找了一天也没有发现一个例子,下面我将介绍下其在ASP.NET下的用做WEB控件的用法
step 1.先将它提供的两个DLL文件添加引用

 

step 2:在Project中引用控件

一般情况下,此控件应该是使用在表现层(UI层),所以你可以直接在你的UI层直接引用,当然,为了你方便使用,你可以先把他加到控件箱里头(ToolBox)

方法: 对着控件箱右键点击->选择Choose Item -> 浏览 ->找到你下载的控件-> 选择 就可以了

这样你就可以在控件箱里看到ZedGraph的图标了,你就可以像其它控件一样使用拖拽了

按此在新窗口打开图片


按此在新窗口打开图片

 

Step3:设置属性

先把控件拖拉到页面中或用户控件中,ZedGraph的属性很多,我这里只说明一下常用的几个属性哈,其实大多数是默认的就OK的,大多数都是对图表的外表进行相关设置,如果你想做出非常精美的图表,那就要对这些属性下一番功夫了。


ID:控件ID
BarBase: 设计图表的基准轴,默认为X
BarType:图表的类型(饼图,柱状图,曲线图等等)
Title:图表标题
OutPutFormat:输出的图表文件类型(Png,Gif,Jpeg,Ico)
width:图表宽度
height:图表高度
ChartFill: 图表背景直充(俺根据字面理解,可能不太准确)
ChartBoder: 设计图表边框样式
lineType:线条类型
...... ....... 
(老实说,我一般在提供的属性里面只是去整宽度跟高度,其它的就很少动了)


Step4:引用控件

这几个东西,你就不要丢了,记得搞上去

using ZedGraph;
using ZedGraph.Web;
using System.Drawing;
using System.Drawing.Imaging;


Step5:初始化


不管你在页面中(aspx)直接使用,还是在用户控件中(ascx),均要对ZedGraph进行初始化操作,其实很简单,你就把下面这段COPY就到你的CS里面就可以了

 private void InitializeComponent()
    {
        this.ZedGraphWeb1.RenderGraph += new ZedGraph.Web.ZedGraphWebControlEventHandler(this.OnRenderGraph);//注册事件
        this.Load += new System.EventHandler(this.Page_Load);

    }


注意:这只是放置一张ZedGraph中生成的图片,那假如是放两个呢?两个就要加一个喔

   private void InitializeComponent()
    {
         this.ZedGraphWeb1.RenderGraph += new ZedGraph.Web.ZedGraphWebControlEventHandler(this.OnRenderGraph1);//注册事件
        this.ZedGraphWeb2.RenderGraph += new ZedGraph.Web.ZedGraphWebControlEventHandler(this.OnRenderGraph2);//注册事件
        this.Load += new System.EventHandler(this.Page_Load);

    }

当你COPY了这段代码后,还要记得在page_load()中加载

protected void Page_Load(object sender, EventArgs e)
    {
        

        InitializeComponent();

    }

Step6:绑定数据

 

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 datagrid_study
{
 /// <summary>
 /// ZedGraph 的摘要说明。
 /// </summary>
 public class ZedGraph : System.Web.UI.Page
 {
  protected ZedGraphWeb ZedGraphWeb2;
 
  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.ZedGraphWeb2.RenderGraph += new ZedGraphWebControlEventHandler(this.OnRenderGraph);
   this.Load += new System.EventHandler(this.Page_Load);

  }
  #endregion

  private void OnRenderGraph(ZedGraphWeb zgw, Graphics g, MasterPane masterPane)     
  {
   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);          
   }   
   //线条
   LineItem myCuve=myPane.AddCurve("My Curve",list, Color.DarkGreen, SymbolType.None);
   LineItem myCuve1=myPane.AddCurve("My Curve1",list2, Color.Blue, SymbolType.Circle);
   LineItem myCuve2=myPane.AddCurve("My Curve2",list3, Color.Red, SymbolType.Triangle);
   masterPane.AxisChange();

   //柱状图
   /*
   //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);   */
  }
 }
}

 

 

数据绑定:

Use the DataSourcePointList to access database data

From ZedGraphWiki

Jump to: navigation, search

It is often desirable to access database data directly by ZedGraph. This saves the trouble of having to copy and convert the data into a PointPairList class. This is done using the DataSourcePointList class, which is an IPointList interface type. This means that you can use a DataSourcePointList in place of a PointPairList when you call the GraphPane.AddCurve(), GraphPane.AddBar(), etc. methods.

The DataSourcePointList is intended to be very simple to use. All you do is create an instance of a DataSourcePointList, specify the data source name, and then you specify the column names in the data table that will be used for each property. The following are the pertinent properties in the DataSourcePointList:

DataSource 
typically the name of a DataTable containing the data of interest. This can also be an ArrayList or other data collection type.
XDataMember 
the name of the column in DataSource that will contain the PointPair.X data values
YDataMember 
the name of the column in DataSource that will contain the PointPair.Y data values
ZDataMember 
the name of the column in DataSource that will contain the PointPair.Z data values
TagDataMember 
the name of the column in DataSource that will contain the PointPair.Tag data values

The sample source code below demonstrates the use of these properties.

For an example, we will use the Northwind database, which is a sample MS access database. First, you need to link the database into your Visual Studio 2005 project with the following steps:

  1. Right-click on your project in the solution explorer and select "Add Existing Item..."
  2. Set the file type to "Data Files", navigate to the folder that contains the Northwind.mdb file, select the file and click "Add"
  3. Open up the Tables tree, and check-mark the "Orders" table
  4. Leave the dataset name as the default ("NorthwindDataSet")
  5. Click "Finish"

You now have a "NorthwindDataSet" class added to your project, along with a table adapter that will fill a data table with data from the Orders table.

The following demonstates how to generate a graph from the database data using the DataSourcePointList. Once the graph is displayed, you will be able to see a scatter plot showing the freight cost versus order date. If you mouse-over the individual points, you will see that the tooltips are sourced from the "ShipName" column of the data table.

private void CreateGraph_DataSource( ZedGraphControl z1 )
{
   GraphPane myPane = z1.GraphPane;

   // Set the titles
   myPane.Title.Text = "DataSourcePointList Test";
   myPane.XAxis.Title.Text = "Date";
   myPane.YAxis.Title.Text = "Freight Charges ($US)";

   // Create a new DataSourcePointList to handle the database connection
   DataSourcePointList dspl = new DataSourcePointList();
   // Create a TableAdapter instance to access the database
   NorthwindDataSetTableAdapters.OrdersTableAdapter adapter =
            new NorthwindDataSetTableAdapters.OrdersTableAdapter();
   // Create a DataTable and fill it with data from the database
   NorthwindDataSet.OrdersDataTable table = adapter.GetData();

   // Specify the table as the data source
   dspl.DataSource = table;
   // The X data will come from the "OrderDate" column
   dspl.XDataMember = "OrderDate";
   // The Y data will come from the "Freight" column
   dspl.YDataMember = "Freight";
   // The Z data are not used
   dspl.ZDataMember = null;
   // The Tag data will come from the "ShipName" column
   // (note that this will just set PointPair.Tag = ShipName)
   dspl.TagDataMember = "ShipName";

   // X axis will be dates
   z1.GraphPane.XAxis.Type = AxisType.Date;

   // Make a curve
   LineItem myCurve = z1.GraphPane.AddCurve( "Orders", dspl, Color.Blue );
   // Turn off the line so it's a scatter plot
   myCurve.Line.IsVisible = false;

   // Show the point values.  These are derived from the "ShipName" database column,
   // which is set as the "Tag" property.
   z1.IsShowPointValues = true;

   // Auto set the scale ranges
   z1.AxisChange();
}

 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
ZedGraph控件属性设置 ZedGraph是一个非常优秀的开源的作图控件 ZedGraph来源:http://sourceforge.net/project/showfiles.php?group_id=114675 ZedGraph相关例子资源:http://zedgraph.org/wiki/index.php?title=Sample_Graphs 1、在vs中使用ZedGraph 2、一些基本概念 几个注意点: 图片的保存路径设置:RenderedImagePath属性中设置,程序对该文件夹应该是有写和修改权限的 图片的输出格式:OutputFormat属性中设置,Png的推荐,比较清晰。 Chart ChartBorder 图表区域的边框设置 ChartFill 图表区域的背景填充 Legend 图表的注释标签显示设置项目,一组数据对应一种颜色的注释 IsHStack 当有多个显示项的时候设置Y轴数据是叠加的还是分开的 Xaxis 图表区域的X轴相关信息设置 AxisColor 坐标轴颜色 Cross 坐标的原点,可以设置坐标的偏移程度 CrossAuto 原点自动设置:True的话Cross的设置就无效了。 FontSpec X轴标题字体相关信息 Angle X轴标题字体显示时候的角度,0为水平 90为垂直 Fill X轴标题字体填充信息 ColorOpacity 透明度 IsScaled 设置X轴标题字体显示大小是否根据图的比例放大缩小 RangeMax 填充时候的最大倾斜度(有过渡色,没试过) RangeMin 填充时候的最小倾斜度(有过渡色,没试过) StringAlignment X轴标题字体排列(不清楚,没试过) IsOmitMag 是否显示指数幂(10次方,没试过,似乎与IsUseTenPower有关系) IsPreventLabelOverlap 坐标值显示是否允许重叠,如果False的话,控件会根据坐标值长度自动消除部分坐标值的显示状态 IsShowTitle X轴标题是否显示 IsTicsBetweenLabels 两个坐标值之间是否自动显示分隔标志 IsUseTenPower 是否使用10次幂指数 IsVisible 是否显示X轴 IsZeroLine 当数据为0时候是否显示(在饼状图显示的时候有用) MajorGrid 大跨度的X轴表格虚线线显示信息 DashOff 虚线中孔间距 DashOn 虚线单位长度 MajorTic 大跨度的X轴刻度信息 IsInside 在Chart内部是否显示 IsOutSide 在Chart外部是否显示 IsOpposite
ZedGraph饼图、条形图和饼图Demo源码 ZedGraphV515是C#编写的.NET类库,提供了用户控件和web控件。它可以创建2D的线性图、条形图和饼图。 它功能完整且有详细的功能自定义。 基于LGPL协议开源,.NET 2.0 C#源代码)它的思路清淅,所以非常容易就上手. 几个注意点: 图片的保存路径设置:RenderedImagePath属性中设置,程序对该文件夹应该是有写和修改权限的 图片的输出格式:OutputFormat属性中设置,Png的推荐,比较清晰。 Chart ChartBorder 图表区域的边框设置 ChartFill 图表区域的背景填充 Legend 图表的注释标签显示设置项目,一组数据对应一种颜色的注释 IsHStack 当有多个显示项的时候设置Y轴数据是叠加的还是分开的 Xaxis 图表区域的X轴相关信息设置 AxisColor 坐标轴颜色 Cross 坐标的原点,可以设置坐标的偏移程度 CrossAuto 原点自动设置:True的话Cross的设置就无效了。 FontSpec X轴标题字体相关信息 Angle X轴标题字体显示时候的角度,0为水平 90为垂直 Fill X轴标题字体填充信息 ColorOpacity 透明度 IsScaled 设置X轴标题字体显示大小是否根据图的比例放大缩小 RangeMax 填充时候的最大倾斜度(有过渡色,没试过) RangeMin 填充时候的最小倾斜度(有过渡色,没试过) StringAlignment X轴标题字体排列(不清楚,没试过) IsOmitMag 是否显示指数幂(10次方,没试过,似乎与IsUseTenPower有关系) IsPreventLabelOverlap 坐标值显示是否允许重叠,如果False的话,控件会根据坐标值长度自动消除部分坐标值的显示状态 IsShowTitle X轴标题是否显示 IsTicsBetweenLabels 两个坐标值之间是否自动显示分隔标志 IsUseTenPower 是否使用10次幂指数 IsVisible 是否显示X轴

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值