当前位置:首页 > 利用ZedGraph制作饼图

利用ZedGraph制作饼图

点击次数:1779  更新日期:2010-12-30
\n

前一阵的一个项目用到了开源项目ZedGraph制作了一个饼图,现把源代码贴出来供大家交流,也方便自己日后查看。


\n

这个项目用的是VS2008,C#代码,zedgraph的版本是:zedgraph_dll_v5.1.4,另外要在项目根目录下面建立一个以ZedGraphImages命名的文件夹,供生成的图片临时存放处。


\n

注:我在制作的过程中baidu搜索了不少相关信息,谢谢大家的无私奉献精神。特别感谢王旭博客上的文章http://www.dwww.cn/news/2008-6/2008691849197372.shtml给我的启示。


\n

view plaincopy to clipboardprint?
//yearTop10CommoditySell.aspx.cs
using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using DHEECommoditySellSystem.ClassFiles;
using ZedGraph;
using ZedGraph.Web;
using System.Drawing;
using System.Drawing.Imaging;
using DHEECommoditySellSystem.DataAccess;
using System.Data.Common;
using System.Data.SqlClient;


namespace DHEECommoditySellSystem.SearchManagement
{
public partial class Taxis : System.Web.UI.Page
{
/// 默认颜色种类
private List<COLOR></COLOR> defaultColors = new List<COLOR></COLOR>();
<SUMMARY></SUMMARY>
/// 初始化颜色数组

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


/// PieItem.LabelType数组
PieLabelType[] lt = new PieLabelType[]{
PieLabelType.Name_Percent,
PieLabelType.Name_Value,
PieLabelType.Percent,
PieLabelType.Value,
PieLabelType.Name_Value
};


protected void Page_Load(object sender, EventArgs e)
{
InitializeComponent();
}

private void InitializeComponent()
{
this.ZedGraphWeb1.RenderGraph += new ZedGraph.Web.ZedGraphWebControlEventHandler(this.OnRenderGraph);//注册事件
}

<SUMMARY></SUMMARY>
/// This method is where you generate your graph.
/// <PARAM name=”masterPane” />You are provided with a MasterPane instance that
/// contains one GraphPane by default (accessible via masterPane[0]).
/// <PARAM name=”g” />A graphics instance so you can easily make the call to AxisChange()
private void OnRenderGraph(ZedGraphWeb cc1, System.Drawing.Graphics g, ZedGraph.MasterPane masterPane)
{
InitDefaultColors();
// Get the GraphPane so we can work with it
GraphPane myPane = masterPane[0];

// Set the pane title
myPane.Title.Text = “本年商品销售Top10″;

// Fill the pane and axis background with solid color
myPane.Fill = new Fill(Color.Cornsilk);
myPane.Chart.Fill = new Fill(Color.Cornsilk);
myPane.Legend.Position = LegendPos.Right;

DatabaseHelper db=new DatabaseHelper();
DataSet ds = db.ExecuteDataSet(“GetTop10CommoditySelledInThisWeek”, CommandType.StoredProcedure);
int i = 0;
foreach (DataRowView view in ds.Tables[0].DefaultView)
{
double salesCount = double.Parse(view[0].ToString());
string commodityName = view["商品名称"].ToString();

PieItem segment = myPane.AddPieSlice(salesCount, defaultColors[i], Color.White, 45f, 0, commodityName);
i++;
segment.LabelType = PieLabelType.Value;
}

// Sum up the values
CurveList curves = myPane.CurveList;
double total = 0;
for (int x = 0; x < curves.Count; x++)
total += ((PieItem)curves[x]).Value;

// Add a text item to highlight total sales
TextObj text = new TextObj(“Total Week Sales – ” + “___FCKpd___0quot; + total.ToString() + “M”, 0.85F, 0.80F, CoordType.PaneFraction);
text.Location.AlignH = AlignH.Center;
text.Location.AlignV = AlignV.Bottom;
text.FontSpec.Border.IsVisible = false;
text.FontSpec.Fill = new Fill(Color.White, Color.PowderBlue, 45F);
text.FontSpec.StringAlignment = StringAlignment.Center;
myPane.GraphObjList.Add(text);

// Add a colored background behind the pie
BoxObj box = new BoxObj(0, 0, 1, 1, Color.Empty, Color.PeachPuff);
box.Location.CoordinateFrame = CoordType.ChartFraction;
box.Border.IsVisible = false;
box.Location.AlignH = AlignH.Left;
box.Location.AlignV = AlignV.Top;
box.ZOrder = ZOrder.E_BehindCurves;
myPane.GraphObjList.Add(box);

masterPane.AxisChange(g);
}
}
}


\n

来源:http://blog.csdn.net/jlu_juby

\n