当前位置:文档之家› C#画统计图(ZedGraph)

C#画统计图(ZedGraph)

4.在你的窗体Load方法(如:Form1_Load() )中添加下列代码:

// 在坐标(40,40)处创建一个新图形, 大小为600x400

myPane = new GraphPane( new Rectangle( 40, 40, 600, 400 ),

"My Test Graph\n(For CodeProject Sample)",

"My X Axis",

"My Y Axis" );

// 设置初始数据

double x, y1, y2;

PointPairList list1 = new PointPairList();

PointPairList list2 = new PointPairList();

for( int i=0; i<36; i++ )

{

x = (double) i + 5;

y1 = 1.5 + Math.Sin( (double) i * 0.2 );

y2 = 3.0 * ( 1.5 + Math.Sin( (double) i * 0.2 ) );

list1.Add( x, y1 );

list2.Add( x, y2 );

}

// 创建红色的菱形曲线

// 标记, 图中的"Porsche"

LineItem myCurve = myPane.AddCurve( "Porsche",list1, Color.Red, Symb olType.Diamond );

// 创建蓝色的圆形曲线

// 标记, 图中的"Piper"

LineItem myCurve2 = myPane.AddCurv( "Piper",list2, Color.Blue, SymbolT ype.Circle );

// 在数据变化时绘制图形

myPane.AxisChange( this.CreateGraphics() );

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" %>

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;

///

/// 显示统计图形类型

///

public enum AnalyticsType

{

Line, //折线图

Bar, //柱状图

Pie //饼图

};

public partial class AnalyticsGraph : https://www.doczj.com/doc/929681323.html,erControl

{

#region Private Attribute

///

/// 默认颜色种类

///

private List defaultColors = new List();

///

/// 统计的个数

///

private int Count;

#endregion

#region Public Property

///

/// 统计图的名称

///

public string Title;

///

/// 横轴的名称(饼图不需要)

///

public string XAxisTitle;

///

/// 纵轴的名称(饼图不需要)

///

public string Y AxisTitle;

///

/// 显示的曲线类型:Line,Bar,Pie

///

public AnalyticsType Type;

///

/// 折线图和柱状图的数据源

///

public List DataSource = new List();

///

/// 饼图的数据源

///

public List ScaleData = new List();

///

/// 各段数据的颜色

///

public List Colors = new List();

///

/// 各段数据的名称

///

public List NameList = new List();

///

/// 用于柱状图,每个圆柱体表示的含义

///

public List LabelList = new List();

#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);

}

///

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

///

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 tempIndexList = new List();

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());

}

}

}

///

/// 画图

///

///

///

///

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

///

/// 画折线图

///

///

private void DrawLine(GraphPane graphPane)

{

for (int i = 0; i < Count; i++)

{

graphPane.AddCurve(NameList[i], DataSource[i], Colors[i], SymbolType.None);

}

}

///

/// 画柱状图

///

///

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);

}

///

/// 画饼图

///

///

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]);

}

}

///

/// 如果系统出错,显示错误信息

///

///

///

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.Y AxisTitle = "用户访问数量";

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);

https://www.doczj.com/doc/929681323.html,List.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));

https://www.doczj.com/doc/929681323.html,List.Add(i.ToString());

}

}

}

本文来自CSDN博客,转载请标明出处:https://www.doczj.com/doc/929681323.html,/lanmao100/archive/2007/10/26/1845620.aspx

相关主题
文本预览
相关文档 最新文档