2016-02-23 105 views
2

我正嘗試使用WPF編寫自己的餅圖控件。我創建了一個函數,它返回表示餅圖的一部分的Path對象。當我使用它來生成一個圖,其中最大的切片是小於或等於它呈現微細圖形的50%,並且看起來像這樣: Working Chart如何使用ArcSegment繪製具有一致半徑的半圓的線段

但當arcsegments中的一個具有更然後PI的角度弧度半徑不等於所有的方式輪,它看起來像這樣:

Broken Chart

Segment over PI radians

這裏是我寫繪製圖表的每一段代碼:

 public Path CreateSlice(Point centerPoint, double radius, ref Point initialPoint, ref double totalAngle, double sliceAngle, Color sliceColor) 
 
     { 
 
      //A set of connected simple graphics objects that makes up the Slice graphic 
 
      Path slicePath = new Path(); 
 
      slicePath.Fill = new SolidColorBrush(sliceColor); 
 
      slicePath.Stroke = new SolidColorBrush(Colors.White); 
 
      slicePath.StrokeThickness = 2; 
 

 
      PathGeometry pathGeometry = new PathGeometry(); 
 
      PathFigureCollection pathFigureCollection = new PathFigureCollection(); 
 
      PathSegmentCollection sliceSegmentCollection = new PathSegmentCollection(); 
 

 
      //The path figure describes the shape in the slicePath object using geometry 
 
      PathFigure pathFigure = new PathFigure(); 
 
      pathFigure.StartPoint = centerPoint;//Set the start point to the center of the pie chart 
 

 
      //Representing the first line of the slice from the centerpoint to the initial point 
 
      //I.E one length of the slice 
 
      LineSegment firstLineSegment = new LineSegment(); 
 
      firstLineSegment.Point = initialPoint; 
 
      sliceSegmentCollection.Add(firstLineSegment); 
 

 
      //Calculate the next point along the circle that the slice should arc too. 
 
      //This point is calculated using the total angle used of the pie chart including this slice 
 
      totalAngle += sliceAngle; 
 
      //Calculate the X and Y coordinates of the next point 
 
      double x = centerPoint.X + radius * Math.Cos(totalAngle); 
 
      double y = centerPoint.Y + radius * Math.Sin(totalAngle); 
 
      initialPoint = new Point(x, y); 
 

 
      //Represents the arc segment of the slice 
 
      ArcSegment sliceArcSegment = new ArcSegment(); 
 
      sliceArcSegment.Point = initialPoint; 
 
      sliceArcSegment.SweepDirection = SweepDirection.Clockwise; 
 
      sliceArcSegment.Size = new Size(radius, radius); 
 
      sliceSegmentCollection.Add(sliceArcSegment); 
 

 
      //Representing the second line of the slice from the second point back to the center 
 
      LineSegment secondLineSegment = new LineSegment(); 
 
      secondLineSegment.Point = centerPoint; 
 
      sliceSegmentCollection.Add(secondLineSegment); 
 

 
      pathFigure.Segments = sliceSegmentCollection; 
 
      pathFigureCollection.Add(pathFigure); 
 
      pathGeometry.Figures = pathFigureCollection; 
 
      slicePath.Data = pathGeometry; 
 
      return slicePath; 
 
     }

誰能幫我找出如何繪製arcsegment具有統一半徑一路一輪請。

回答

1

在這些情況下,應設置IsLargeArc屬性以指示圓弧大於180度。看着你的身影,它將180度的上限和頂點的預期中心點移動。