利用ZedGraph制作饼图

本文介绍如何使用ZedGraph库在C#项目中绘制饼图,展示年度商品销售前十名的数据。通过实例代码详细解释了从数据库获取数据到图表生成的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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

注:我在制作的过程中baidu搜索了不少相关信息,谢谢大家的无私奉献精神。特别感谢王旭博客上的文章http://www.cnblogs.com/wxukie/archive/2007/05/16/748922.html给我的启示。

//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
  defaultColors = new List
 ();
       
       /// 初始化颜色数组
        
        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);//注册事件
        }

 
        /// This method is where you generate your graph. 
        /// You are provided with a MasterPane instance that
        /// contains one GraphPane by default (accessible via masterPane[0]).
        /// 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);
        }
    }
}
    新申请的博客,第一次发帖,有不好之处希望大家指正,望大家多多交流,常来我的博客逛逛。^_^
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轴
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值