private void OnSliceAdded(IPieSlice slice) { var labelItem = new PieChartLabelItem { Content = slice.Label, ContentTemplate = LabelTemplate }; SetZIndex(labelItem, LabelZindex); _titleItems.Add(slice, labelItem); InternalChildren.Add(labelItem); var valueItem = new PieChartValueItem { Content = slice.DisplayedValue, ContentTemplate = ValueTemplate }; SetZIndex(valueItem, ValueZIndex); _valueItems.Add(slice, valueItem); InternalChildren.Add(valueItem); var sliceItem = new PieChartSliceItem { Slice = slice }; SetZIndex(sliceItem, SliceZIndex); _sliceItems.Add(slice, sliceItem); InternalChildren.Add(sliceItem); }
/// <summary> /// Arranges the content of a System.Windows.Controls.Canvas element. /// </summary> /// <param name="arrangeSize"></param> /// <returns></returns> protected override System.Windows.Size ArrangeOverride(System.Windows.Size arrangeSize) { var offset = new Vector(arrangeSize.Width / 2, arrangeSize.Height / 2); if (_slices != null) { double radius = Math.Min(arrangeSize.Width / 2, arrangeSize.Height / 2); double startAngle = 0; var fullCircle = new Circle { Radius = radius }; double minimumArcAngle = MinimumArcLength / fullCircle.Circumference * 2 * Math.PI; foreach (IPieSlice slice in _slices) { PieChartSliceItem item = _sliceItems[slice]; double relativeValue = item.Slice.Value / SumOfSlices; double openAngle = Math.Max(relativeValue * 2 * Math.PI, minimumArcAngle); item.Center = (Point)offset; item.StartAngle = startAngle; item.OpenAngle = openAngle; item.Radius = radius; item.Arrange(new Rect(0, 0, arrangeSize.Width, arrangeSize.Height)); startAngle += openAngle; } } foreach (var pair in _valueItems) { IPieSlice slice = pair.Key; PieChartValueItem item = pair.Value; PieChartSliceItem sliceItem = _sliceItems[slice]; System.Windows.Size desiredSize = item.DesiredSize; Rect rect = FindValuePosition(sliceItem.Shape, desiredSize); if (sliceItem.Shape.Contains(rect)) { item.Visibility = Visibility.Visible; item.Arrange(rect); } else { item.Visibility = Visibility.Collapsed; } } foreach (var pair in _titleItems) { IPieSlice slice = pair.Key; PieChartLabelItem item = pair.Value; PieChartSliceItem sliceItem = _sliceItems[slice]; System.Windows.Size desiredSize = item.DesiredSize; Vector specificOffset; switch (sliceItem.Direction) { case SliceDirection.TopRight: specificOffset = new Vector(0, -desiredSize.Height); break; case SliceDirection.BottomRight: specificOffset = new Vector(0, 0); break; case SliceDirection.BottomLeft: specificOffset = new Vector(-desiredSize.Width, 0); break; default: specificOffset = new Vector(-desiredSize.Width, -desiredSize.Height); break; } Circle circle = sliceItem.Shape.Circle; circle.Radius += LabelMargin; Point position = circle.GetPoint(sliceItem.Angle) + specificOffset; var rect = new Rect(position, desiredSize); item.Arrange(rect); } return(arrangeSize); }