ZedGraph类库之基本教程---PieSampleDemo.cs(画饼)

本文介绍了使用ZedGraph库绘制饼图的方法,包括设置图表标题、背景填充、图例位置及样式等,并展示了如何创建饼图片段及其相关属性。

ZedGraph类库之基本教程

PieSampleDemo.cs介绍

        这节我把饼形图粗略的介绍一下,如下图。

PieSampleDemo.cs(画饼)

 

using System;

using System.Drawing;

using System.Collections;

 

using ZedGraph;

 

namespace ZedGraph.Demo

{

     ///<summary>

     /// Summary description for SimpleDemo.

     ///</summary>

     public class PieSampleDemo : DemoBase

     {

 

           public PieSampleDemo() : base( "Code Project Pie Sample",

                                                "Pie Sample", DemoType.Pie, DemoType.Tutorial )

           {

                GraphPane myPane = base.GraphPane;

 

                // Set the GraphPane title

                myPane.Title = "2004 ZedGraph Sales by Region/n($M)";

                myPane.FontSpec.IsItalic = true;

                myPane.FontSpec.Size = 24f;

                myPane.FontSpec.Family = "Times";

 

                // Fill the pane background with a color gradient

                myPane.PaneFill = new Fill( Color.White, Color.Goldenrod, 45.0f );

                // No fill for the axis background

                myPane.AxisFill.Type = FillType.None;

 

                // Set the legend to an arbitrary location

                myPane.Legend.Position = LegendPos.Float ;

                myPane.Legend.Location = new Location( 0.95f, 0.15f, CoordType.PaneFraction,

                                           AlignH.Right, AlignV.Top );

                myPane.Legend.FontSpec.Size = 10f;

                myPane.Legend.IsHStack = false;

               

                // Add some pie slices

                PieItem segment1 = myPane.AddPieSlice( 20, Color.Navy, Color.White, 45f, 0, "North" );

                PieItem segment3 = myPane.AddPieSlice( 30, Color.Purple, Color.White, 45f, .0, "East" );

                PieItem segment4 = myPane.AddPieSlice( 10.21, Color.LimeGreen, Color.White, 45f, 0, "West" );

                PieItem segment2 = myPane.AddPieSlice( 40, Color.SandyBrown, Color.White, 45f, 0.2, "South" );

                PieItem segment6 = myPane.AddPieSlice( 250, Color.Red, Color.White, 45f, 0, "Europe" );

                PieItem segment7 = myPane.AddPieSlice( 50, Color.Blue, Color.White, 45f, 0.2, "Pac Rim" );

                PieItem segment8 = myPane.AddPieSlice( 400, Color.Green, Color.White, 45f, 0, "South America" );

                PieItem segment9 = myPane.AddPieSlice( 50, Color.Yellow, Color.White, 45f, 0.2, "Africa" );

               

                segment2.LabelDetail.FontSpec.FontColor = Color.Red;

                                                               

                // Sum up the pie values                                                                                                             

                CurveList curves = myPane.CurveList ;

                double total = 0 ;

                for ( int x = 0 ; x < curves.Count ; x++ )

                     total += ((PieItem)curves[x]).Value ;

 

                // Make a text label to highlight the total value

                TextItem text = new TextItem( "Total 2004 Sales/n" + "$" + total.ToString () + "M",

                                           0.18F, 0.40F, 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.FromArgb( 255, 100, 100 ), 45F );

                text.FontSpec.StringAlignment = StringAlignment.Center ;

                myPane.GraphItemList.Add( text );              

 

                // Create a drop shadow for the total value text item

                TextItem text2 = new TextItem( text );

                text2.FontSpec.Fill = new Fill( Color.Black );

                text2.Location.X += 0.008f;

                text2.Location.Y += 0.01f;

                myPane.GraphItemList.Add( text2 );

                 

                base.ZedGraphControl.AxisChange();

           }

     }

}

 

代码分析:

我们先把Legend类的相关属性再补充一下。

// Set the legend to an arbitrary location

                myPane.Legend.Position = LegendPos.Float ;

                myPane.Legend.Location = new Location( 0.95f, 0.15f, CoordType.PaneFraction,

                                           AlignH.Right, AlignV.Top );

                myPane.Legend.FontSpec.Size = 10f;

                myPane.Legend.IsHStack = false;

LegendPos是一个枚举,共有13个枚举值:Top、Left、Right、Bottom、InsideTopLeft、InsideTopRight、InsideBotLeft、InsideBotRight、Float、TopCenter、BottomCenter、TopFlushLeft和BottomFlushLeft。具体含义我就不解释了,都是关于Legend位置的。

Location是指定Legend具体坐标的一个类,要注意的是,只有当LegendPos是Float时,Location才会起作用。

FontSpec类就是一个字体类,里面是关于字体的一些相关设置,这里不再细说。

IsHStack是一个Legend的属性,是设置Legend中文字和图形的显示方式是水平还是垂直。

 

下面说说本节的主角PieItem类:

PieItem segment6 = myPane.AddPieSlice( 250, Color.Red, Color.White, 45f, 0, "Europe" );

Pie重载了五个构造函数,上面是参数最多的一个构造函数,共有六个,意思分别是:在整个饼形图中占的比重,渐变颜色1,渐变颜色2,渐变颜色角度,远离中心点的距离,饼形图的文字注释。

饼形图也是继承 ZedGraph.CurveItem,与其它继承CurveItem不同的是,PieItem有一个value的属性,可以方便的存取PieItem实例的值,从而可以很方便的动态改变一个饼形在整个饼形区域所占的比重。

 

 

方法二:

首先引用ZedGraph.dll动态库.

然后在工具箱里"添加/移除项"-----"netframework控件中引用该dll"-----加载到工具箱,然后拖动该该工具到你的窗体,即zedGraphControl1.然后

cs代码:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using ZedGraph;

//定义 
  public void CreateChart( ZedGraphControl zgc )
  {
   GraphPane myPane = zgc.GraphPane;

   // Set the GraphPane title
   myPane.Title = "2004 ZedGraph Sales by Region/n($M)";
   myPane.FontSpec.IsItalic = true;
   myPane.FontSpec.Size = 24f;
   myPane.FontSpec.Family = "Times New Roman";

   // Fill the pane background with a color gradient
   myPane.PaneFill  = new Fill( Color.White, Color.Goldenrod, 45.0f );
   // No fill for the chart background
   myPane.AxisFill.Type = FillType.None;

   // Set the legend to an arbitrary location
   myPane.Legend.Position = LegendPos.Float;
   myPane.Legend.Location = new Location( 0.95f, 0.15f, CoordType.PaneFraction,
    AlignH.Right, AlignV.Top );
   myPane.Legend.FontSpec.Size = 10f;
   myPane.Legend.IsHStack = false;

   // Add some pie slices
   PieItem segment1 = myPane.AddPieSlice( 20, Color.Navy, Color.White, 45f, 0, "North" );
   PieItem segment3 = myPane.AddPieSlice( 30, Color.Purple, Color.White, 45f, .0, "East" );
   PieItem segment4 = myPane.AddPieSlice( 10.21, Color.LimeGreen, Color.White, 45f, 0, "West" );
   PieItem segment2 = myPane.AddPieSlice( 40, Color.SandyBrown, Color.White, 45f, 0, "South" );
   PieItem segment6 = myPane.AddPieSlice( 250, Color.Red, Color.White, 45f, 0, "Europe" );
   PieItem segment7 = myPane.AddPieSlice( 150, Color.Blue, Color.White, 45f, 0, "Pac Rim" );
   PieItem segment8 = myPane.AddPieSlice( 400, Color.Green, Color.White, 45f, 0, "South America" );
   PieItem segment9 = myPane.AddPieSlice( 50, Color.Yellow, Color.White, 45f, 0, "Africa" );

   segment2.LabelDetail.FontSpec.FontColor = Color.Red;

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

   // Make a text label to highlight the total value
   TextItem text = new TextItem( "Total 2004 Sales/n" + "$" + total.ToString() + "M",
    0.18F, 0.40F, 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.FromArgb( 255, 100, 100 ), 45F );
   text.FontSpec.StringAlignment = StringAlignment.Center;  
            myPane.GraphItemList.Add( text );    
   // Create a drop shadow for the total value text item
   TextItem text2  = new TextItem( text );
   text2.FontSpec.Fill = new Fill( Color.Black );
   text2.Location.X += 0.008f;
   text2.Location.Y += 0.01f;
   myPane.GraphItemList.Add( text2 );

   // Calculate the Axis Scale Ranges
   zgc.AxisChange();

  }

调用

 CreateChart(zedGraphControl1);

 

内容概要:本文围绕“新型电力系统下多分布式电源接入配电网承载力评估方法”的研究,系统性地介绍了基于Matlab的仿真建模与代码实现方案,旨在评估高比例分布式电源(如光伏、风电等)接入背景下配电网的接纳能力。研究融合了智能优化算法(如蜣螂优化、灰狼优化、遗传算法)、多目标优化、鲁棒优化及双层优化模型,结合潮流计算、稳定性分析与故障仿真,构建了完整的承载力评估体系。文档不仅提供核心算法实现,还拓展至微电网调度、储能配置、电氢耦合系统、电动汽车协同等前沿方向,强调“复现+创新”相结合的科研路径,助力研究者快速掌握高水平论文复现技巧并激发原创思路。; 适合人群:具备电力系统、自动化或相关专业背景,熟悉Matlab/Simulink仿真环境,正在从事科研或工程应用的研究生及初级科研人员(工作1-3年);; 使用场景及目标:①复现高水平期刊中关于配电网承载力的优化模型;②开展高比例可再生能源接入下的配电网规划与运行研究;③学习并应用智能优化算法解决复杂电力系统问题;④获取完整科研资源包以加速课题进展与论文撰写; 阅读建议:建议读者关注公众号“荔枝科研社”获取网盘资源,下载全套代码与模型文件,按照文档结构循序渐进学习,重点理解算法设计逻辑与仿真建模细节,结合所提供的复现案例深化对优化模型与工程应用场景的理解,提升科研效率与创新能力。
内容概要:本文系统研究了综合能源系统中的容量配置与运行调度问题,采用双层优化方法构建模型并通过Matlab代码实现求解。上层优化侧重于设备容量的科学配置,以降低投资成本并提升系统经济性;下层优化聚焦于多能源协同运行调度,综合考虑光伏、储能、电动汽车等多种能源形式的动态特性,旨在实现系统在不同运行工况下的能效最大化、运行可靠性与低碳化目标。研究融合智能优化算法(如遗传算法、粒子群算法)与电力系统建模技术,深入探讨了多能耦合、不确定性处理及复杂约束下的优化机制,并提供了完整的仿真案例与代码资源,涵盖微电网调度、风光储协同、电动汽车接入等典型应用场景,形成了具有较强实用价值的科研技术体系。; 适合人群:具备电力系统分析、优化算法理论及Matlab编程基础的研究生、科研人员和工程技术人员,特别适用于从事综合能源系统规划、微电网运行、智能调度与能源互联网等领域研究的专业人士。; 使用场景及目标:① 掌握双层优化在综合能源系统中的建模方法与求解流程;② 利用所提供Matlab代码进行科研复现、算法改进与系统仿真验证;③ 拓展应用于电动汽车集群调度、可再生能源消纳、多能互补系统优化等实际工程与学术研究场景; 阅读建议:建议结合文档中列出的相关研究方向与配套代码资源,按照主题分类循序渐进地学习,优先理解双层架构的设计逻辑与上下层耦合机制,并借助提供的网盘资料开展仿真实验与参数调试,以深化对优化模型与算法实现的理解,提升科研创新能力。
【重要提示】本资源设置为0积分下载,若非0积分请勿轻易下载 亲爱的CSDN用户: 首先感谢你点进这个资源页面。我需要提前说明一个重要情况: 本资源原本已设置为“0积分下载”,即作者希望完全免费共享。但CSDN平台有时会根据文件的下载热度、文件大小、用户权限等因素,自动将部分资源的积分调整为非0数值(如1积分、2积分、5积分等)。这是平台系统的自动行为,而非作者本人的设定。 因此,如果你当前看到该资源的下载所需积分不是0(例如显示为1、2、3……),请谨慎决定是否下载。 如果你按照非0积分支付并下载后发现资源内容不符合预期、链接失效,或者实际上该资源本应是免费的,作者无法为此承担积分损失或退还操作。强烈建议:仅在页面显示为0积分时进行下载。 另外,本资源描述中并未直接提供具体的下载地址或外部链接,因为它本身是一个通过CSDN官方上传通道提交的文件/内容包。如果你看到描述中没有外部网盘地址,这是正常的——资源文件应通过CSDN内置的“下载”按钮获取。若因平台积分显示异常导致你支付了积分,请优先联系CSDN客服咨询积分退还政策,作者没有权限修改平台自动设定的积分值。 感谢你的理解与支持。技术分享本应开放,但受限于平台规则,特此提醒如上。祝学习进步!
【重要提示】本资源设置为0积分下载,若非0积分请勿轻易下载 亲爱的CSDN用户: 首先感谢你点进这个资源页面。我需要提前说明一个重要情况: 本资源原本已设置为“0积分下载”,即作者希望完全免费共享。但CSDN平台有时会根据文件的下载热度、文件大小、用户权限等因素,自动将部分资源的积分调整为非0数值(如1积分、2积分、5积分等)。这是平台系统的自动行为,而非作者本人的设定。 因此,如果你当前看到该资源的下载所需积分不是0(例如显示为1、2、3……),请谨慎决定是否下载。 如果你按照非0积分支付并下载后发现资源内容不符合预期、链接失效,或者实际上该资源本应是免费的,作者无法为此承担积分损失或退还操作。强烈建议:仅在页面显示为0积分时进行下载。 另外,本资源描述中并未直接提供具体的下载地址或外部链接,因为它本身是一个通过CSDN官方上传通道提交的文件/内容包。如果你看到描述中没有外部网盘地址,这是正常的——资源文件应通过CSDN内置的“下载”按钮获取。若因平台积分显示异常导致你支付了积分,请优先联系CSDN客服咨询积分退还政策,作者没有权限修改平台自动设定的积分值。 感谢你的理解与支持。技术分享本应开放,但受限于平台规则,特此提醒如上。祝学习进步!
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值