关于WPF中的饼图
2013-12-17 17:35
218 查看
项目中要用到饼图,之前用Winform中的Chart控件,用一个WindowFormHost装载进来,凑合能用。
最近重新整理代码,看到程序目录下,引用的DLL不少,想用WPF自身的控件,可没找到(笨啊)
于是生出一想法:自己动手画一个吧。
给出xaml代码先:
View Code
调用方法:
DrawPartOfEllipseService.Draw(80, Ell1);/*80就是百分比,Ell1是上面的xaml中的Ellipse.Clip中PathGeometry的PathFigure*/
OK,就这么多。
当然,颜色可以随意调整,至于标百分比,按自己的需求,想标哪儿标哪儿吧,还有,别忘了图例。
个人感觉,这样做速度应该比较快。
最近重新整理代码,看到程序目录下,引用的DLL不少,想用WPF自身的控件,可没找到(笨啊)
于是生出一想法:自己动手画一个吧。
给出xaml代码先:
class DrawPartOfEllipseService { private const int ELLIPSERADIUS = 100; /// <summary> /// 画扇形 /// </summary> /// <param name="per">百分比</param> /// <param name="ell">椭圆的PathFigure</param> /// <param name="Radius">椭圆半径</param> public static void Draw(double per, PathFigure ell,int Radius) { if (per > 100) { return; } ell.Segments.Clear(); double AAA = per * 3.6 / 180 * Math.PI; Point st = new Point(Radius, Radius); Point st2 = new Point(Radius * 2, Radius);/*这两个点确定一个x轴正向*/ ell.Segments.Add(new LineSegment { Point = st }); ell.Segments.Add(new LineSegment { Point = st2 }); if (per <= 12.5) { Point p1 = new Point(); p1.X = Radius*2; p1.Y = Radius - Radius * Math.Tan(AAA); ell.Segments.Add(new LineSegment { Point = p1 }); } else if (per <= 37.5) { Point p1 = new Point(Radius*2, 0); ell.Segments.Add(new LineSegment { Point = p1 }); Point p2 = new Point(); p2.Y = 0; p2.X = Radius + Radius / Math.Tan(AAA); ell.Segments.Add(new LineSegment { Point = p2 }); } else if (per <= 62.5) { Point p1 = new Point(Radius*2, 0); Point p2 = new Point(0, 0); ell.Segments.Add(new LineSegment { Point = p1 }); ell.Segments.Add(new LineSegment { Point = p2 }); Point p3 = new Point(); p3.X = 0; p3.Y = Radius + Radius * Math.Tan(AAA); ell.Segments.Add(new LineSegment { Point = p3 }); } else if (per <= 87.5) { Point p1 = new Point(Radius*2, 0); Point p2 = new Point(0, 0); Point p3 = new Point(0, Radius*2); ell.Segments.Add(new LineSegment { Point = p1 }); ell.Segments.Add(new LineSegment { Point = p2 }); ell.Segments.Add(new LineSegment { Point = p3 }); Point p4 = new Point(); p4.Y = Radius*2; p4.X = Radius - Radius / Math.Tan(AAA); ell.Segments.Add(new LineSegment { Point = p4 }); } else { Point p1 = new Point(Radius*2, 0); Point p2 = new Point(0, 0); Point p3 = new Point(0, Radius*2); ell.Segments.Add(new LineSegment { Point = p1 }); ell.Segments.Add(new LineSegment { Point = p2 }); ell.Segments.Add(new LineSegment { Point = p3 }); Point p4 = new Point(); p4.Y = Radius*2; p4.X = Radius - Radius / Math.Tan(AAA); ell.Segments.Add(new LineSegment { Point = p4 }); } } public static void Draw(double per, PathFigure ell) { Draw(per, ell, ELLIPSERADIUS); } }
View Code
调用方法:
DrawPartOfEllipseService.Draw(80, Ell1);/*80就是百分比,Ell1是上面的xaml中的Ellipse.Clip中PathGeometry的PathFigure*/
OK,就这么多。
当然,颜色可以随意调整,至于标百分比,按自己的需求,想标哪儿标哪儿吧,还有,别忘了图例。
个人感觉,这样做速度应该比较快。
相关文章推荐
- WPF:关于Slider控件的一些说明
- WPF关于“在“System.Windows.Markup.StaticResourceHolder”上提供值时引发了异常。”问题解决办法
- 关于wpf设置属性的几种方式
- WPF 关于圆角的制作
- WPF关于WindowInteropHelper的一个BUG
- 关于WPF后台触发键盘按键
- 关于WPF的两个窗口之间简单的数据传递【附源代码】
- 关于.Net中的计时器及WPF中最适合的计时器问题
- 也来一篇关于Infragistics WPF Report的使用教程 (二)
- 关于WPF中菜单等图标的路径问题
- 关于WPF和Silverlight的参考文献 不定期更新中....
- 关于WPF下ComBox的SelectionChanged事件
- wpf 关于图片加载后变模糊的原因
- [WPF]幽默的事情关于wpf的contextmenu,触发
- WPF 关于C#代码实现ControlTemplate
- 关于.NET/C#/WCF/WPF 打造IP网络智能视频监控系统的介绍
- 关于WPF动画的简单例子
- 关于WPF自定义的求助贴
- WPF 关于控件背景色的设置和穿透效果
- 关于未捕获异常的处理(WPF)