现在的位置: 首页 > 综合 > 正文

用ZedGraph控件画统计分析图.

2013年10月09日 ⁄ 综合 ⁄ 共 3972字 ⁄ 字号 评论关闭

由于朋友需要把C1WebChart.替换掉,改用开源的ZedGraph控件.以下做一个示例,供大家参考:

步骤如下:

1、添加ZedGraph控件。如下图:
 

2、添加到控制面版。如下图:

3、制作用户控件。

   a>  建立一个命名为: DrawGrap.ascx 用户控件。
   b>  通过控制面版,把ZedGraphWeb拖到默认页面。 如下图:

    c>   生成代码(DrawGrap.ascx)如下:
     

     <%@ Control Language="C#" AutoEventWireup="true" CodeFile="DrawGrap.ascx.cs" Inherits="DrawGrap" %>
<%@ Register TagPrefix="zgw" Namespace="ZedGraph.Web" Assembly="ZedGraph.Web" %>
<ZGW:ZEDGRAPHWEB id="zedGraphControl" runat="server" width="500" Height="375" RenderMode="ImageTag"/>

 

  d>  生成代码(DrawGrap.ascx.cs)如下:

 

using System;
using System.Data;
using System.Configuration;
using System.Collections;
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 ZedGraph;
using ZedGraph.Web;
using System.Collections.Generic;

/// <summary>
/// 显示统计图形类型
/// </summary>

public enum AnalyticsType
{
    Line,   
//折线图
    Bar,    //柱状图
    Pie     //饼图
}
;
public partial class DrawGrap : System.Web.UI.UserControl
{
    
Private Attribute

    
Public Property
    
protected void Page_Load(object sender, EventArgs e)
    
{
        zedGraphControl.RenderGraph 
+= new ZedGraph.Web.ZedGraphWebControlEventHandler(zedGraphControl_RenderGraph);
    }



   
private void InitDefaultColors()
        
{
            defaultColors.Add(Color.Red);
            defaultColors.Add(Color.Green);
            defaultColors.Add(Color.Blue);
            defaultColors.Add(Color.Yellow);
            defaultColors.Add(Color.YellowGreen);
            defaultColors.Add(Color.Brown);
            defaultColors.Add(Color.Aqua);
            defaultColors.Add(Color.Cyan);
            defaultColors.Add(Color.DarkSeaGreen);
            defaultColors.Add(Color.Indigo);
        }

        
/// <summary>
        
/// 如果属性为空则初始化属性数据
        
/// </summary>

        private void InitProperty()
        
{
            InitDefaultColors();
            
if (string.IsNullOrEmpty(Title))
            
{
                Title 
= "未命名统计图";
            }

            
if (string.IsNullOrEmpty(XAxisTitle))
            
{
                XAxisTitle 
= "横轴";
            }

            
if (string.IsNullOrEmpty(YAxisTitle))
            
{
                YAxisTitle 
= "纵轴";
            }

            
if (Type == AnalyticsType.Pie)
            
{
                Count 
= ScaleData.Count;
            }

            
else
            
{
                Count 
= DataSource.Count;
            }

            
if (Colors.Count == 0 || Colors.Count != Count)
            
{
                Random r 
= new Random();
                
int tempIndex = 0;
                List
<int> tempIndexList = new List<int>();
                
for (int i = 0; i < Count; i++)
                
{
                    tempIndex 
= r.Next(defaultColors.Count);
                    

抱歉!评论已关闭.