您的位置:首页 > 其它

关于WPF中的饼图

2013-12-17 17:35 218 查看
项目中要用到饼图,之前用Winform中的Chart控件,用一个WindowFormHost装载进来,凑合能用。

最近重新整理代码,看到程序目录下,引用的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,就这么多。

当然,颜色可以随意调整,至于标百分比,按自己的需求,想标哪儿标哪儿吧,还有,别忘了图例。

个人感觉,这样做速度应该比较快。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: