Example #1
0
        /// <summary>
        /// Called when the value of the RatioStringFormatProperty property changes.
        /// </summary>
        /// <param name="d">PieDataPoint that changed its RatioStringFormat.</param>
        /// <param name="e">Event arguments.</param>
        private static void OnRatioStringFormatPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            PieDataPoint source   = d as PieDataPoint;
            string       newValue = e.NewValue as string;

            source.OnRatioStringFormatPropertyChanged(newValue);
        }
Example #2
0
        /// <summary>
        /// Called when the value of the RatioProperty property changes.
        /// </summary>
        /// <param name="d">PieDataPoint that changed its Ratio.</param>
        /// <param name="e">Event arguments.</param>
        private static void OnRatioPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            PieDataPoint source   = (PieDataPoint)d;
            double       oldValue = (double)e.OldValue;
            double       newValue = (double)e.NewValue;

            source.OnRatioPropertyChanged(oldValue, newValue);
        }
Example #3
0
        /// <summary>
        /// Updates a data point.
        /// </summary>
        /// <param name="dataPoint">The data point to update.</param>
        protected override void UpdateDataPoint(DataPoint dataPoint)
        {
            PieDataPoint pieDataPoint = (PieDataPoint)dataPoint;

            pieDataPoint.Width  = ActualWidth;
            pieDataPoint.Height = ActualHeight;
            UpdatePieDataPointGeometry(pieDataPoint, ActualWidth, ActualHeight);
            Canvas.SetLeft(pieDataPoint, 0);
            Canvas.SetTop(pieDataPoint, 0);
        }
Example #4
0
        /// <summary>
        /// Detaches event handlers from a data point.
        /// </summary>
        /// <param name="dataPoint">The data point.</param>
        protected override void DetachEventHandlersFromDataPoint(DataPoint dataPoint)
        {
            PieDataPoint pieDataPoint = dataPoint as PieDataPoint;

            pieDataPoint.ActualRatioChanged       -= OnPieDataPointActualRatioChanged;
            pieDataPoint.ActualOffsetRatioChanged -= OnPieDataPointActualOffsetRatioChanged;
            pieDataPoint.RatioChanged             -= OnPieDataPointRatioChanged;
            pieDataPoint.OffsetRatioChanged       -= OnPieDataPointOffsetRatioChanged;

            base.DetachEventHandlersFromDataPoint(dataPoint);
        }
        /// <summary>
        /// Creates a legend item for each data point.
        /// </summary>
        /// <param name="dataPoint">The data point added.</param>
        protected override void AddDataPoint(DataPoint dataPoint)
        {
            base.AddDataPoint(dataPoint);
            PieDataPoint pieDataPoint = (PieDataPoint)dataPoint;

            int        index      = ActiveDataPoints.IndexOf(dataPoint) + 1;
            LegendItem legendItem = CreatePieLegendItem(dataPoint, index);

            // Grab a style enumerator if we don't have one already.
            if (_resourceDictionaryEnumerator == null)
            {
                _resourceDictionaryEnumerator = GetResourceDictionaryWithTargetType(this, typeof(PieDataPoint), true);
            }

            if (_resourceDictionaryEnumerator.MoveNext())
            {
                ResourceDictionary paletteResources =
#if SILVERLIGHT
                    _resourceDictionaryEnumerator.Current.ShallowCopy();
#else
                    _resourceDictionaryEnumerator.Current;
#endif
                pieDataPoint.PaletteResources = paletteResources;
                pieDataPoint.Resources.MergedDictionaries.Add(paletteResources);
            }
            else
            {
                pieDataPoint.PaletteResources = null;
            }
            pieDataPoint.ActualDataPointStyle = DataPointStyle ?? pieDataPoint.Resources[DataPointStyleName] as Style;
            pieDataPoint.SetBinding(PieDataPoint.StyleProperty, new Binding(PieDataPoint.ActualDataPointStyleName)
            {
                Source = pieDataPoint
            });
            pieDataPoint.ActualLegendItemStyle = LegendItemStyle ?? (pieDataPoint.Resources[LegendItemStyleName] as Style);
            legendItem.SetBinding(LegendItem.StyleProperty, new Binding(ActualLegendItemStyleName)
            {
                Source = pieDataPoint
            });

            _dataPointLegendItems[dataPoint] = legendItem;
            LegendItems.Add(legendItem);
            UpdateLegendItemIndexes();
        }
        /// <summary>
        /// Adds the labels to each of its PieDataPoints.
        /// </summary>
        protected override void OnAfterUpdateDataPoints()
        {
            var chart = this.SeriesHost as Chart;

            if (chart != null)
            {
                Canvas labelArea = chart.Template.FindName("LabelArea_PART", chart) as Canvas;
                if (labelArea != null && this.ItemsSource != null)
                {
                    foreach (object dataItem in this.ItemsSource)
                    {
                        PieDataPoint pieDataPoint = this.GetDataPoint(dataItem) as PieDataPoint;
                        if (pieDataPoint != null)
                        {
                            this.AddLabelPieDataPoint(pieDataPoint, labelArea);
                        }
                    }
                }
            }
        }
        /// <summary>
        /// Creates a new PieChartLabel control and adds it to the "LabelArea_PART" canvas.
        /// </summary>
        /// <param name="pieDataPoint">PieDataPoint that corresponds to PieChartLabel.</param>
        /// <param name="labelStyle">The style to be applied to the PieChartLabel.</param>
        /// <param name="labelArea">The canvas where PieChartLabel will be added.</param>
        private void AddLabelPieDataPoint(PieDataPoint pieDataPoint, Canvas labelArea)
        {
            PieChartLabel label = new PieChartLabel();
            label.Style = this.PieChartLabelStyle;
            label.ContentTemplate = this.PieChartLabelItemTemplate;

            Binding contentBinding = new Binding("DataContext") { Source = pieDataPoint };
            label.SetBinding(ContentControl.ContentProperty, contentBinding);

            Binding dataContextBinding = new Binding("DataContext") { Source = pieDataPoint };
            label.SetBinding(ContentControl.DataContextProperty, contentBinding);

            Binding formattedRatioBinding = new Binding("FormattedRatio") { Source = pieDataPoint };
            label.SetBinding(PieChartLabel.FormattedRatioProperty, formattedRatioBinding);

            Binding visibilityBinding = new Binding("Ratio") { Source = pieDataPoint, Converter = new DoubleToVisibilityConverter() };
            label.SetBinding(PieChartLabel.VisibilityProperty, visibilityBinding);

            Binding geometryBinding = new Binding("Geometry") { Source = pieDataPoint };
            label.SetBinding(PieChartLabel.GeometryProperty, geometryBinding);

            Binding displayModeBinding = new Binding("LabelDisplayMode") { Source = this };
            label.SetBinding(PieChartLabel.DisplayModeProperty, displayModeBinding);

            labelArea.Children.Add(label);
            pieDataPoint.Unloaded += delegate
            {
                labelArea.Children.Remove(label);
            };

            pieDataPoint.Loaded += delegate
            {
                if (label.Parent == null)
                {
                    labelArea.Children.Add(label);
                }
            };
        }
        /// <summary>
        /// Creates a new PieChartLabel control and adds it to the "LabelArea_PART" canvas.
        /// </summary>
        /// <param name="pieDataPoint">PieDataPoint that corresponds to PieChartLabel.</param>
        /// <param name="labelStyle">The style to be applied to the PieChartLabel.</param>
        /// <param name="labelArea">The canvas where PieChartLabel will be added.</param>
        private void AddLabelPieDataPoint(PieDataPoint pieDataPoint, Canvas labelArea)
        {
            PieChartLabel label = new PieChartLabel();

            label.Style           = this.PieChartLabelStyle;
            label.ContentTemplate = this.PieChartLabelItemTemplate;

            Binding contentBinding = new Binding("DataContext")
            {
                Source = pieDataPoint
            };

            label.SetBinding(ContentControl.ContentProperty, contentBinding);

            Binding dataContextBinding = new Binding("DataContext")
            {
                Source = pieDataPoint
            };

            label.SetBinding(ContentControl.DataContextProperty, contentBinding);

            Binding formattedRatioBinding = new Binding("FormattedRatio")
            {
                Source = pieDataPoint
            };

            label.SetBinding(PieChartLabel.FormattedRatioProperty, formattedRatioBinding);

            Binding visibilityBinding = new Binding("Ratio")
            {
                Source = pieDataPoint, Converter = new DoubleToVisibilityConverter()
            };

            label.SetBinding(PieChartLabel.VisibilityProperty, visibilityBinding);

            Binding geometryBinding = new Binding("Geometry")
            {
                Source = pieDataPoint
            };

            label.SetBinding(PieChartLabel.GeometryProperty, geometryBinding);

            Binding displayModeBinding = new Binding("LabelDisplayMode")
            {
                Source = this
            };

            label.SetBinding(PieChartLabel.DisplayModeProperty, displayModeBinding);

            labelArea.Children.Add(label);
            pieDataPoint.Unloaded += delegate
            {
                labelArea.Children.Remove(label);
            };

            pieDataPoint.Loaded += delegate
            {
                if (label.Parent == null)
                {
                    labelArea.Children.Add(label);
                }
            };
        }
        /// <summary>
        /// Updates the PieDataPoint's Geometry property.
        /// </summary>
        /// <param name="pieDataPoint">PieDataPoint instance.</param>
        /// <param name="plotAreaWidth">PlotArea width.</param>
        /// <param name="plotAreaHeight">PlotArea height.</param>
        internal static void UpdatePieDataPointGeometry(PieDataPoint pieDataPoint, double plotAreaWidth, double plotAreaHeight)
        {
            double diameter = (plotAreaWidth < plotAreaHeight) ? plotAreaWidth : plotAreaHeight;
            diameter *= 0.95;
            double plotAreaRadius = diameter / 2;
            double maxDistanceFromCenter = 0.0;
            double sliceRadius = plotAreaRadius - maxDistanceFromCenter;

            Point translatePoint = new Point(plotAreaWidth / 2, plotAreaHeight / 2);

            if (pieDataPoint.ActualRatio == 1)
            {
                foreach (DependencyProperty dependencyProperty in new DependencyProperty[] { PieDataPoint.GeometryProperty, PieDataPoint.GeometrySelectionProperty, PieDataPoint.GeometryHighlightProperty })
                {
                    Geometry geometry =
                        new EllipseGeometry
                        {
                            Center = translatePoint,
                            RadiusX = sliceRadius,
                            RadiusY = sliceRadius
                        };
                    pieDataPoint.SetValue(dependencyProperty, geometry);
                }
            }
            else
            {
                if (pieDataPoint.ActualRatio == 0.0)
                {
                    pieDataPoint.Geometry = null;
                    pieDataPoint.GeometryHighlight = null;
                    pieDataPoint.GeometrySelection = null;
                }
                else
                {
                    double ratio = pieDataPoint.ActualRatio;
                    double offsetRatio = pieDataPoint.ActualOffsetRatio;
                    double currentRatio = offsetRatio + ratio;

                    Point offsetRatioPoint = ConvertRatioOfRotationToPoint(offsetRatio, sliceRadius, sliceRadius);

                    Point adjustedOffsetRatioPoint = offsetRatioPoint.Translate(translatePoint);

                    // Calculate the last clockwise point in the pie slice
                    Point currentRatioPoint =
                        ConvertRatioOfRotationToPoint(currentRatio, sliceRadius, sliceRadius);

                    // Adjust point using center of plot area as origin
                    // instead of 0,0
                    Point adjustedCurrentRatioPoint =
                        currentRatioPoint.Translate(translatePoint);

                    foreach (DependencyProperty dependencyProperty in new DependencyProperty[] { PieDataPoint.GeometryProperty, PieDataPoint.GeometrySelectionProperty, PieDataPoint.GeometryHighlightProperty })
                    {
                        // Creating the pie slice geometry object
                        PathFigure pathFigure = new PathFigure { IsClosed = true };
                        pathFigure.StartPoint = translatePoint;
                        pathFigure.Segments.Add(new LineSegment { Point = adjustedOffsetRatioPoint });
                        bool isLargeArc = (currentRatio - offsetRatio) > 0.5;
                        pathFigure.Segments.Add(
                            new ArcSegment
                            {
                                Point = adjustedCurrentRatioPoint,
                                IsLargeArc = isLargeArc,
                                Size = new Size(sliceRadius, sliceRadius),
                                SweepDirection = SweepDirection.Clockwise
                            });

                        PathGeometry pathGeometry = new PathGeometry();
                        pathGeometry.Figures.Add(pathFigure);
                        pieDataPoint.SetValue(dependencyProperty, pathGeometry);
                    }
                }
            }
        }
Example #10
0
        /// <summary>
        ///     Updates the PieDataPoint's Geometry property.
        /// </summary>
        /// <param name="pieDataPoint">PieDataPoint instance.</param>
        /// <param name="plotAreaWidth">PlotArea width.</param>
        /// <param name="plotAreaHeight">PlotArea height.</param>
        internal static void UpdatePieDataPointGeometry(PieDataPoint pieDataPoint, double plotAreaWidth,
                                                        double plotAreaHeight)
        {
            double diameter = (plotAreaWidth < plotAreaHeight) ? plotAreaWidth : plotAreaHeight;

            diameter *= 0.95;
            double plotAreaRadius        = diameter / 2;
            double maxDistanceFromCenter = 0.0;
            double sliceRadius           = plotAreaRadius - maxDistanceFromCenter;

            var translatePoint = new Point(plotAreaWidth / 2, plotAreaHeight / 2);

            if (pieDataPoint.ActualRatio == 1)
            {
                foreach (
                    DependencyProperty dependencyProperty in
                    new[]
                {
                    PieDataPoint.GeometryProperty, PieDataPoint.GeometrySelectionProperty,
                    PieDataPoint.GeometryHighlightProperty
                })
                {
                    Geometry geometry =
                        new EllipseGeometry
                    {
                        Center  = translatePoint,
                        RadiusX = sliceRadius,
                        RadiusY = sliceRadius
                    };
                    pieDataPoint.SetValue(dependencyProperty, geometry);
                }
            }
            else
            {
                if (pieDataPoint.ActualRatio == 0.0)
                {
                    pieDataPoint.Geometry          = null;
                    pieDataPoint.GeometryHighlight = null;
                    pieDataPoint.GeometrySelection = null;
                }
                else
                {
                    double ratio        = pieDataPoint.ActualRatio;
                    double offsetRatio  = pieDataPoint.ActualOffsetRatio;
                    double currentRatio = offsetRatio + ratio;

                    Point offsetRatioPoint = ConvertRatioOfRotationToPoint(offsetRatio, sliceRadius, sliceRadius);

                    Point adjustedOffsetRatioPoint = offsetRatioPoint.Translate(translatePoint);

                    // Calculate the last clockwise point in the pie slice
                    Point currentRatioPoint =
                        ConvertRatioOfRotationToPoint(currentRatio, sliceRadius, sliceRadius);

                    // Adjust point using center of plot area as origin
                    // instead of 0,0
                    Point adjustedCurrentRatioPoint =
                        currentRatioPoint.Translate(translatePoint);

                    foreach (
                        DependencyProperty dependencyProperty in
                        new[]
                    {
                        PieDataPoint.GeometryProperty, PieDataPoint.GeometrySelectionProperty,
                        PieDataPoint.GeometryHighlightProperty
                    })
                    {
                        // Creating the pie slice geometry object
                        var pathFigure = new PathFigure {
                            IsClosed = true
                        };
                        pathFigure.StartPoint = translatePoint;
                        pathFigure.Segments.Add(new LineSegment {
                            Point = adjustedOffsetRatioPoint
                        });
                        bool isLargeArc = (currentRatio - offsetRatio) > 0.5;
                        pathFigure.Segments.Add(
                            new ArcSegment
                        {
                            Point          = adjustedCurrentRatioPoint,
                            IsLargeArc     = isLargeArc,
                            Size           = new Size(sliceRadius, sliceRadius),
                            SweepDirection = SweepDirection.Clockwise
                        });

                        var pathGeometry = new PathGeometry();
                        pathGeometry.Figures.Add(pathFigure);
                        pieDataPoint.SetValue(dependencyProperty, pathGeometry);
                    }
                }
            }
        }