实例介绍
【实例简介】
【实例截图】
【实例截图】
【核心代码】
【实例简介】<br />
【实例截图】<br />
<p>
【核心代码】
</p>
<p>
<pre class="brush:csharp;">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;
namespace Zegraph
{
/**/
/// <summary>
/// 显示统计图形类型
/// </summary>
public enum AnalyticsType
{
Line, //折线图
Bar, //柱状图
Pie //饼图
};
public partial class DrawGrap : 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>
/// 画图51-a-s-px
/// </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;
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
}
}</pre>
</p>
<p>
<br />
</p>
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


网友评论
我要评论