C#画统计图(ZedGraph)

C#画统计图(ZedGraph
    最近要做一个统计分析系统,需要画统计图,听说 OWC 画图特别慢,而且不好看, ReportView 效果不好而且生成图也很慢。于是就找了一个开源的画统计图的系统 ------ZedGraph.
 
    选 ZedGraph 的另一个主要目的是它的开源代码有 .NET2.0 的版本,我现在在 2005 上开发,有 2.0 的程序当然最好了!
 
ZedGraph 支持折线图、柱状图、饼图。
 
由于开发的需要,我把 ZedGraph 封装起来,作为一个专门的用户控件。
 
下面是相关的代码,可以做个参考。
 
//AnalyticsGraph.ascx
 
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="AnalyticsGraph.ascx.cs" Inherits="AnalyticsGraph" %>
<%@ Register TagPrefix="zgw" Namespace="ZedGraph.Web" Assembly="ZedGraph.Web" %>
<ZGW:ZEDGRAPHWEB id="zedGraphControl" runat="server" width="500" Height="375" RenderMode="ImageTag"/>
RenderMode  只有设置为 ImageTag 的时候 ZedGraph 才可以被封装到用户控件中(这个值表示把统计图生成为一个图片文件,再链接到页面上,图片文件在 IE 关闭后会自动删除)
 
//AnalyticsGraph.cs
 
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
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;
 
/// <summary>
///  显示统计图形类型
/// </summary>
public enum AnalyticsType
{
    Line,   // 折线图
    Bar,    // 柱状图
    Pie     // 饼图
};
public partial class AnalyticsGraph : System.Web.UI.UserControl
{
    #region Private Attribute
    /// <summary>
    ///  默认颜色种类
    /// </summary>
    private List<Color> defaultColors = new List<Color>();
    /// <summary>
    ///  统计的个数
    /// </summary>
    private int Count;
    #endregion
 
    #region Public Property
    /// <summary>
    ///  统计图的名称
    /// </summary>
    public string Title;
    /// <summary>
    ///  横轴的名称(饼图不需要)
    /// </summary>
    public string XAxisTitle;
    /// <summary>
    ///  纵轴的名称(饼图不需要)
    /// </summary>
    public string YAxisTitle;
    /// <summary>
    ///  显示的曲线类型: Line,Bar,Pie
    /// </summary>
    public AnalyticsType Type;
    /// <summary>
    ///  折线图和柱状图的数据源
    /// </summary>
    public List<PointPairList> DataSource = new List<PointPairList>();
    /// <summary>
    ///  饼图的数据源
    /// </summary>
    public List<double> ScaleData = new List<double>();
    /// <summary>
    ///  各段数据的颜色
    /// </summary>
    public List<Color> Colors = new List<Color>();
    /// <summary>
    ///  各段数据的名称
    /// </summary>
    public List<string> NameList = new List<string>();
    /// <summary>
    ///  用于柱状图,每个圆柱体表示的含义
    /// </summary>
    public List<string> LabelList = new List<string>();
    #endregion
 
    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);
    }
 
    #region Draw
    /// <summary>
    ///  画折线图
    /// </summary>
    /// <param name="graphPane"></param>
    private void DrawLine(GraphPane graphPane)
    {
        for (int i = 0; i < Count; i++)
        {
            graphPane.AddCurve(NameList[i], DataSource[i], Colors[i], SymbolType.None);
        }
    }
    /// <summary>
    ///  画柱状图
    /// </summary>
    /// <param name="graphPane"></param>
    private void DrawBar(GraphPane graphPane)
    {
        for (int i = 0; i < Count; i++)
        {
            graphPane.AddBar(LabelList[i], DataSource[i], Colors[i]).Bar.Fill = new Fill(Colors[i], Color.White, Colors[i]);
        }
        graphPane.XAxis.MajorTic.IsBetweenLabels = true;
        string[] labels = NameList.ToArray();
        graphPane.XAxis.Scale.TextLabels = labels;
        graphPane.XAxis.Type = AxisType.Text;
        graphPane.Fill = new Fill(Color.White, Color.FromArgb(200, 200, 255), 45.0f);
        graphPane.Chart.Fill = new Fill(Color.White, Color.LightGoldenrodYellow, 45.0f);
    }
    /// <summary>
    ///  画饼图
    /// </summary>
    /// <param name="graphPane"></param>
    private void DrawPie(GraphPane graphPane)
    {
        graphPane.Fill = new Fill(Color.White,Color.Silver, 45.0f);
 
        graphPane.Legend.Position = LegendPos.Float;
        graphPane.Legend.Location = new Location(0.95f, 0.15f, CoordType.PaneFraction, AlignH.Right, AlignV.Top);
        graphPane.Legend.FontSpec.Size = 20f;
        graphPane.Legend.IsHStack = false;
 
        for (int i = 0; i < Count; i++)
        {
            graphPane.AddPieSlice(ScaleData[i], Colors[i], Color.White, 45f, 0, NameList[i]);
        }
    }
    /// <summary>
    ///  如果系统出错,显示错误信息
    /// </summary>
    /// <param name="graphPane"></param>
    /// <param name="message"></param>
    private void DrawMessage(GraphPane graphPane,string message)
    {
        TextObj text = new TextObj( message, 200, 200);
        text.Text = message;
        graphPane.GraphObjList.Add(text);
 
    }
    #endregion
}
 
 
调用方法:
 
using System;
using System.Data;
using System.Configuration;
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 _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DrawPie();
    }
 
    private void DrawLine()
    {
        AnalyticsGraph1.Type = AnalyticsType.Line;
        AnalyticsGraph1.Title = " 用户访问曲线图 ";
        AnalyticsGraph1.XAxisTitle = " 月份 ";
        AnalyticsGraph1.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);
            }
            AnalyticsGraph1.DataSource.Add(ppl);
            AnalyticsGraph1.NameList.Add(i.ToString());
        }
    }
    private void DrawBar()
    {
        AnalyticsGraph1.Type = AnalyticsType.Bar;
        AnalyticsGraph1.Title = " 用户访问柱状图 ";
        AnalyticsGraph1.XAxisTitle = " 月份 ";
        AnalyticsGraph1.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);
             }
            AnalyticsGraph1.DataSource.Add(ppl);
        }
    }
    private void DrawPie()
    {
        AnalyticsGraph1.Type = AnalyticsType.Pie;
        AnalyticsGraph1.Title = " 用户访问饼图 ";
        Random rand = new Random();
        for (int i = 0; i < 3; i++)
        {
            AnalyticsGraph1.ScaleData.Add((i+2) * rand.Next(100));
            AnalyticsGraph1.NameList.Add(i.ToString());
        }
    }
}




本文转自 qianshao 51CTO博客,原文链接:http://blog.51cto.com/qianshao/202150,如需转载请自行联系原作者

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值