利用ZedGraph制作饼图

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

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

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

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



本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/jlu_juby/archive/2008/06/08/2523245.aspx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
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轴
C知道:C#中的ZedGraph是一个强大的图表绘制库,它提供了丰富的功能来创建各种类型的图表,包括折线图、柱状图、饼图等。使用ZedGraph可以轻松地在C#应用程序中添加图表功能。 要使用ZedGraph,首先需要将ZedGraph库添加到你的项目中。你可以通过NuGet包管理器来安装ZedGraph库。在Visual Studio中,右击你的项目,选择"管理NuGet程序包",然后搜索并安装ZedGraph。 安装完成后,你可以在代码中引用ZedGraph命名空间,开始使用它的功能。以下是一个示例代码,展示了如何使用ZedGraph创建一个简单的折线图: ```csharp using ZedGraph; public partial class Form1 : Form { public Form1() { InitializeComponent(); // 创建ZedGraph控件 ZedGraphControl zedGraphControl = new ZedGraphControl(); zedGraphControl.Dock = DockStyle.Fill; this.Controls.Add(zedGraphControl); // 创建一个图表对象 GraphPane graphPane = zedGraphControl.GraphPane; // 添加一些样本数据 double[] x = { 1, 2, 3, 4, 5 }; double[] y = { 10, 20, 15, 25, 30 }; // 创建一个曲线对象 LineItem curve = graphPane.AddCurve("折线图", x, y, Color.Blue, SymbolType.Circle); // 定义图表标题和轴标签 graphPane.Title.Text = "示例折线图"; graphPane.XAxis.Title.Text = "X轴"; graphPane.YAxis.Title.Text = "Y轴"; // 刷新图表 zedGraphControl.AxisChange(); } } ``` 以上代码演示了如何创建一个简单的折线图,并添加样本数据。你可以根据自己的需求,进一步定制和修改图表的样式和数据。希望对你有所帮助!如果还有其他问题,请继续提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值