用ZedGraph控件画统计分析图.

由于朋友需要把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);
                    
if (tempIndexList.Contains(tempIndex))
                    
{
                        i
--;
                    }

                    
else
                    
{
                        tempIndexList.Add(tempIndex);
                        Colors.Add(defaultColors[tempIndex]);
                    }

                }

            }

            
if (NameList.Count == 0)
            
{
                
if (Type == AnalyticsType.Bar)
                
{
                    
for (int i = 0; i < DataSource[0].Count; i++)
                    
{
                        NameList.Add(
"" + i.ToString() + "");
                    }

                }

                
else
                
{
                    
for (int i = 0; i < Count; i++)
                    
{
                        NameList.Add(
"" + i.ToString() + "");
                    }

                }

            }

            
if (LabelList.Count == 0)
            
{
                
for (int i = 0; i < Count; i++)
                
{
                    LabelList.Add(
"含义" + i.ToString());
                }

            }

        }

        
/// <summary>
        
/// 画图
        
/// </summary>
        
/// <param name="webObject"></param>
        
/// <param name="g"></param>
        
/// <param name="pane"></param>

        private void zedGraphControl_RenderGraph(ZedGraph.Web.ZedGraphWeb webObject, System.Drawing.Graphics g, ZedGraph.MasterPane pane)
        
{
            InitProperty();

            GraphPane myPane 
= pane[0];

            myPane.Title.Text 
= Title;
            myPane.XAxis.Title.Text 
= XAxisTitle;
            myPane.YAxis.Title.Text 
= YAxisTitle;

            
//if (true)
            
//{
            
//    DrawMessage(myPane, "yiafdhaskjhfasfksahfasdlhfaslf lasgfasglgsadi");
            
//    pane.AxisChange(g);
            
//    return;
            
//}

            
switch (Type)
            
{
                
case AnalyticsType.Line:
                    DrawLine(myPane);
                    
break;
                
case AnalyticsType.Bar:
                    DrawBar(myPane);
                    
break;
                
case AnalyticsType.Pie:
                    DrawPie(myPane);
                    
break;
                
default:
                    
break;
            }

            pane.AxisChange(g);
        }


        
Draw
    }

  e>  用户控件制作完成。


4、对控件的使用。

  a>  创建测试页面(DrawGrap.aspx)

  b> 把用户控件DrawGrap.ascx 拖到默认的测试页面上(DrawGrap.aspx)

  c> 后台代码如下:

 

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;

public   partial   class  DrawGrap : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
    
{
        
//柱状图
        DrawBar();
        
//饼图
        
//DrawPie();
        
//曲线图
        
//DrawLine();
    }

    
private void DrawBar()
    
{
        DrawGrap1.Type 
= AnalyticsType.Bar;
        DrawGrap1.Title 
= "用户访问柱状图";
        DrawGrap1.XAxisTitle 
= "月份";
        DrawGrap1.YAxisTitle 
= "用户访问数量";
        Random rand 
= new Random();
        
for (int i = 0; i < 6; i++)
        
{
            ZedGraph.PointPairList ppl 
= new ZedGraph.PointPairList();
            
for (int j = 0; j < 3; j++)
            
{
                
double x = rand.Next(10);
                
double y = rand.NextDouble() * 1000;
                ppl.Add(x, y);
            }

            DrawGrap1.DataSource.Add(ppl);
        }

    }


    
private void DrawPie()
    
{
        DrawGrap1.Type 
= AnalyticsType.Pie;
        DrawGrap1.Title 
= "用户访问饼图";
        Random rand 
= new Random();
        
for (int i = 0; i < 3; i++)
        
{
            DrawGrap1.ScaleData.Add((i 
+ 2* rand.Next(100));
            DrawGrap1.NameList.Add(i.ToString());
        }

    }


    
private void DrawLine()
    
{
        DrawGrap1.Type 
= AnalyticsType.Line;
        DrawGrap1.Title 
= "用户访问曲线图";
        DrawGrap1.XAxisTitle 
= "月份";
        DrawGrap1.YAxisTitle 
= "用户访问数量";
        Random rand 
= new Random();
        
for (int i = 0; i < 3; i++)
        
{
            ZedGraph.PointPairList ppl 
= new ZedGraph.PointPairList();

            
for (double x = 0; x < 5; x += 1.0)
            
{
                
double y = rand.NextDouble() * 1000;
                ppl.Add(x, y);
            }

            DrawGrap1.DataSource.Add(ppl);
            DrawGrap1.NameList.Add(i.ToString());
        }

    }

}

d>  生成的柱状图如下:

 e>  生成的曲线图如下:

 f>  生成的饼图如下:

5、补充说明

a>  成功部署该项目需要在虚拟网站建临时文件夹(ZedGraphImages),便于存放生成的临时文件。

b> 据说该控件支持3D图效果,具体我没测试。希望有兴趣的同学可以测试。

     (这些图一般的项目应用就足够了。)

  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 14
    评论
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轴
### 回答1: ZedGraph是一个用于.NET平台的绘控件,它可以帮助开发人员轻松地创建各种表和绘,包括2D和3D表。在开发项目中,通常需要呈现数据和趋势,这时ZedGraph控件就能派上用场。 ZedGraph控件的优点是非常易于使用,开发人员可以轻松地使用控件自带的方法和属性,创建各种类型的表和绘,包括柱状图、线、区域等。此外,ZedGraph还具有非常出色的可定制性,开发人员可以访问控件的底层代码,以实现更高级的自定义功能。 示例工程.zip里提供了许多实际应用场景下的代码。它提供了不同的示例和文档,帮助用户更快地理解和使用控件。开发人员可以根据需要使用这些代码,进行二次开发和定制,以满足自己的业务需求。 总体而言,ZedGraph控件是一个非常优秀的绘工具,可以帮助开发人员提高数据可视化的效果和质量。示例工程.zip中提供的代码和文档可以帮助开发人员更好地学习和使用这个控件。 ### 回答2: zedgraph控件及示例工程.zip是一个基于.NET平台的形绘制控件和示例工程文件。该控件可以在Windows Forms应用程序中使用,支持绘制多种类型的形,包括线柱状图、饼等。该控件提供了多种自定义选项,能够满足不同场景下的需求。 示例工程文件中提供了多个使用该控件的实例,展示了其在不同场景下的使用方式,供用户参考。同时,该控件也提供了丰富的文档和API文档,帮助用户快速上手并了解其详细信息。 需要注意的是,该控件需要用户具备一定的.NET编程相关知识,能够理解其相关API和使用方法。同时,该控件只能在Windows Forms应用程序中使用,不能在其他类型的应用中使用。 ### 回答3: zedgraph控件是一个基于.NET平台的开源绘控件,可以用于绘制各种类型的表,如柱状图、折线、饼等。它提供了丰富的功能和灵活的配置选项,使得开发人员可以轻松地创建出具有交互性和美观性的表。 示例工程.zip是一个包含了多个使用zedgraph控件绘制表的示例工程的压缩包。其中包含了柱状图、折线、饼等多种类型的表,每个示例工程都有详细的注释和说明,方便开发人员进行学习和使用。 除了示例工程之外,zedgraph控件还提供了完整的API文档和大量的在线教程和示例,帮助开发人员快速掌握该控件的使用方法和技巧。 总之,zedgraph控件是一个功能强大、易用性高的绘控件,可以满足开发人员各种不同类型的数据可视化需求,示例工程则为学习和使用该控件提供了非常好的参考。
评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值