WinForm使用ZedGraph控件画 饼状图

using ZedGraph;  // 必须
private void Form1_Load(object sender, EventArgs e)
{
	CreatePie(mypie);  //mypie是你起的ZedGraphControl名字
}
public void CreatePie(ZedGraphControl zgc)
{
	// 创建图表对象
	GraphPane myPane = zgc.GraphPane;
	// 标题Set the GraphPane title
	myPane.Title.Text = "2020区域销售情况\n($M)";
	myPane.Title.FontSpec.IsItalic = true;
	myPane.Title.FontSpec.Size = 24f;
	myPane.Title.FontSpec.Family = "Times New Roman";
	
	// 背景色(渐变色)Fill the pane background with a color gradient
	myPane.Fill = new Fill(Color.White, Color.Goldenrod, 45.0f);
	// 设置图表的颜色填充,如果设置为FillType.None,则填充色和背景色相同 No fill for the chart background
	myPane.Chart.Fill.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
	// 设置饼图的各个部分:AddPieSlice方法的参数是 (value值,颜色,渐变色,渐变大小,离开中心点的距离,名称)
	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(1500, 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字体颜色为红色
	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
	TextObj text = new TextObj("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.GraphObjList.Add(text);
	// 为text创建一个投影
	TextObj text2 = new TextObj(text);
	text2.FontSpec.Fill = new Fill(Color.Black);
	text2.Location.X += 0.008f;
	text2.Location.Y += 0.01f;
	myPane.GraphObjList.Add(text2);
	
	// 计算轴的刻度范围Calculate the Axis Scale Ranges
	zgc.AxisChange();
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
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轴

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值