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

article/2025/10/6 21:25:03

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

 


http://chatgpt.dhexx.cn/article/k71KgyC5.shtml

相关文章

Winforn中设置ZedGraph曲线图的属性、坐标轴属性、刻度属性

场景 C#窗体应用中使用ZedGraph曲线插件绘制图表&#xff1a; https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/99716066 在上面已经实现基本的曲线图之后&#xff0c;效果如下&#xff1a; 当然这不是我们的效果&#xff0c;还要对其属性进行设置。 但是毕竟其…

转载:ZedGraph使用帮助

译文&#xff1a; 序言 ZedGraph是用于创建任意数据的二维线型、条型、饼型图表的一个类库&#xff0c;也可以作为Windows窗体用户控件和ASP网页控件&#xff08;这里有个web-accessible 不知道该怎么翻译&#xff09;。这个类库具有高度的适应性&#xff0c;几乎所有式样的图…

ZedGraph如何去掉外边框?并设置背景颜色

ZedGraph的在显示曲线图的时候&#xff0c;有外边框一直在显示&#xff0c;会显得特别碍眼&#xff0c;如何去掉它呢&#xff1f; 这个是原始的现象 1 去掉外边框的做法如下&#xff1a; //去掉外边框this.zedGraph.GraphPane.Border.IsVisible false;2 设置一下背景颜色 //设…

用ZedGraph控件画统计分析图.

由于朋友需要把C1WebChart.替换掉,改用开源的ZedGraph控件.以下做一个示例,供大家参考: 步骤如下: 1、添加ZedGraph控件。如下图&#xff1a; 2、添加到控制面版。如下图&#xff1a; 3、制作用户控件。 a> 建立一个命名为&#xff1a; DrawGrap.ascx 用户控件。 b…

C# 利用ZedGraph控件画简单折线图示例

下载ZedGraph 官网下载地址 http://sourceforge.net/projects/zedgraph/files/ 添加 ZedGraph.dll 和ZedGraph.Web.dll的引用 在控件库中添加ZedGraph控件 右键点击工具箱 - 选择项 - .Net Framework 组件 - 浏览 - 找到ZedGraph.dll 和ZedGraph.Web.dll添加…

zed graph使用经验

转自&#xff1a;http://www.cnblogs.com/gaizai/archive/2010/02/22/1671154.html ZedGraph使用经验 开源的统计图控件中基本常用的是OpenFlashChar和ZedGraph&#xff0c;今天就先来讲讲ZedGraph的使用。 ZedGraph资源 ZedGraph来源&#xff1a;http://sourceforge.net/proj…

ZedGraph

ZedGraph使用经验 2010-02-22 14:28 by 听风吹雨, 8194 阅读, 3 评论, 收藏, 编辑 开源的统计图控件中基本常用的是OpenFlashChar和ZedGraph&#xff0c;今天就先来讲讲ZedGraph的使用。 ZedGraph资源 ZedGraph来源&#xff1a;http://sourceforge.net/project/showfiles.php…

zedGraph 绘制实时曲线 卡顿

zedGraph是一个开源的曲线绘制插件&#xff0c;由于要制作上位机显示实时数据&#xff0c;百度搜索到了zedgraph&#xff0c;优点是开源&#xff0c;功能也够使用。 缺点是看网址08年后就没有更新过。尝试使用了一下&#xff0c;感觉还可以。 曲线绘制直接看下图。 原理很简单…

ZedGraph 官网下载和帮助文档ZedGraph.chm

ZedGraph介绍 ZedGraph是用C#编写的.NET的类库控件&#xff0c;可用于绘制曲线图、饼图、柱状图、股票K线图等&#xff0c;功能非常强大。 1、ZedGraph的官网是&#xff1a; https://sourceforge.net/projects/zedgraph/ 2、ZedGraph.CHM帮助文档 官网的链接&#xff1a; htt…

graphviz简介

前几天去杨瑞那儿玩&#xff0c;他向我介绍了一个叫graphviz的工具&#xff0c;回来之后试用了一下&#xff0c;觉得真的是很好很强大。下面简单的介绍一下这个工具。 graphviz是贝尔实验室设计的一个开源的画图工具&#xff0c;它的强大主要体现在“所思即所得"&#xff…

zedgraph控件使用

最近做一个上位机的项目&#xff0c;要求实时显示温度曲线&#xff0c;开始用.net自带的 chart控做的&#xff0c;在动态显示那块&#xff0c;在删除一个数据点、加入一个新的数据点的时候&#xff0c;新的数据点显示不出来&#xff1b;纠结好久&#xff0c;解决不了这个问题。…

ZedGraph控件常用方法和属性总结

最近在WPF使用ZedGraph控件&#xff0c;发现这个控件的功能很强大&#xff0c;据说采用了双缓冲机制来绘制图&#xff0c;只要控制好显示的帧速&#xff0c;能够显示速度较快的动态图。 参考&#xff1a;https://blog.csdn.net/qq_26093511/article/details/51329059 1、常用…

ZedGraph设置刻度轴的颜色、刻度文本颜色以及网格线的颜色

1 刻度轴的设置 本小节介绍 如何设置刻度的颜色、长度、生长方向以及将正上方的刻度隐藏掉&#xff0c;还有设置刻度文本的颜色Scale.FontSpec.FontColor。 Color axisColor Color.FromArgb(150, 150, 150);#region X轴//设置主刻度的长度this.zedGraph.GraphPane.XAxis.Majo…

ZedGraph 总论

ZedGraph 总论 ZedGraph 是一个开源的.NET图表类库&#xff0c; 并且全部代码都是用C#开发的。它可以利用任意的数据集合创建2D的线性和柱形图表。 ZedGraph的类库具有很高的灵活性。几乎图表的每个层面都可以被用户修改。同时&#xff0c;为了保证类库的易用性&#xff0c;所…

ZedGraph做统计

下载ZedGraph.dll,在工具箱添加 &#xff0c;然后将空间拖入界面&#xff0c;&#xff0c; ZedGraph.dll下载地址 &#xff1a; http://download.csdn.net/detail/happy09li/4276410 参考资料 点击打开链接 private void Form9_Load(object sender, EventArgs e){DataTable …

ZedGraph使用(一) 柱形图

ZedGraph使用&#xff08;一&#xff09; 柱形图 作者&#xff1a;Kevin 日期&#xff1a; 2008-12-04 QQ:475762235 关键字&#xff1a;ZedGraph、柱形图、统计图、C#、绘图、绘图控件、WebFrom 内容摘要&#xff1a; ZedGraph是一个开源的控件&#xff0c;提供了用户控件和…

ZedGraph绘图

一、下载及配置 下载ZedGraph 官网下载地址 http://sourceforge.net/projects/zedgraph/files/ 添加 ZedGraph.dll 和ZedGraph.Web.dll的引用 在控件库中添加ZedGraph控件 右键点击工具箱 - 选择项 - .Net Framework 组件 - 浏览 - 找到ZedGraph.dll 和ZedGraph.Web.d…

ZedGraph类库之基本教程篇

第一部分&#xff1a;基本教程篇 ZedGraphDemo中一共有9个基本教程的例子。其中大部分都类似&#xff0c;我会讲解其中一些比较典型的例子。把ZedGraph类库的使用逐步展现给大家。 第一节&#xff1a; InitialSampleDemo.cs这个文件 http://blog.csdn.net/tjvictor/archive/20…

ZedGraph使用经验

ZedGraph资源 ZedGraph来源&#xff1a;http://sourceforge.net/project/showfiles.php?group_id114675 ZedGraph 相关例子资源&#xff1a;http://zedgraph.org/wiki/index.php?titleSample_Graphs ZedGraph的特点&#xff1a; 第一&#xff0c;可以先生成图片后再显示&…

C#zegraph用法

一、引用using ZedGraph; 在资源管理器中点击引用&#xff0c;将ZedGraph.dll添加到工程项目中。 二、添加zedGraphControl控件 在工具箱的常规项目右击添加选择项&#xff0c;浏览zedgraph.dll 二、基本图、直方图、折线图、圆饼图 三、zedgraph相关属性 classDescrip…