Beispiel #1
0
        public override void Plot(bool animate = true)
        {
            var pChart = Chart as PieChart;
            if (pChart == null) return;
            if (pChart.PieTotalSum <= 0) return;
            var rotated = 90d;

            Chart.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));

            var minDimension = Chart.PlotArea.Width < Chart.PlotArea.Height
                ? Chart.PlotArea.Width
                : Chart.PlotArea.Height;
            minDimension -= pChart.DrawPadding;
            minDimension = minDimension < pChart.DrawPadding ? pChart.DrawPadding : minDimension;

            var sliceId = 0;
            var isFist = true;
            foreach (var point in Values.Points)
            {
                var participation = point.Y/pChart.PieTotalSum;
                if (isFist)
                {
                    rotated = participation*-.5;
                    isFist = false;
                }

                var slice = new PieSlice
                {
                    CentreX = 0,
                    CentreY = 0,
                    RotationAngle = 360*rotated,
                    WedgeAngle = 360*participation,
                    Radius = minDimension/2,
                    InnerRadius = pChart.InnerRadius,
                    Fill = Brushes != null && Brushes.Length > sliceId
                        ? Brushes[sliceId]
                        : new SolidColorBrush(GetColorByIndex(sliceId)),
                    Stroke = Chart.Background,
                    StrokeThickness = pChart.SlicePadding
                };
                var wa = new DoubleAnimation
                {
                    From = 0,
                    To = slice.WedgeAngle,
                    Duration = TimeSpan.FromMilliseconds(500)
                };
                var ra = new DoubleAnimation
                {
                    From = 0,
                    To = slice.RotationAngle,
                    Duration = TimeSpan.FromMilliseconds(500)
                };

                Canvas.SetTop(slice, Chart.PlotArea.Height/2);
                Canvas.SetLeft(slice, Chart.PlotArea.Width/2);

                Chart.Canvas.Children.Add(slice);
                Shapes.Add(slice);

                var valueBlock = new TextBlock
                {
                    Text = Chart.AxisY.LabelFormatter == null
                        ? point.Y.ToString(CultureInfo.InvariantCulture)
                        : Chart.AxisY.LabelFormatter(point.Y),
                    FontFamily = Chart.AxisY.FontFamily,
                    FontSize = Chart.AxisY.FontSize,
                    FontStretch = Chart.AxisY.FontStretch,
                    FontStyle = Chart.AxisY.FontStyle,
                    FontWeight = Chart.AxisY.FontWeight,
                    Foreground = System.Windows.Media.Brushes.White
                };

                var hypo = ((minDimension/2) + (pChart.InnerRadius > 10 ? pChart.InnerRadius : 10))/2;
                var gamma = participation*360/2 + rotated*360;
                var cp = new Point(hypo*Math.Sin(gamma*(Math.PI/180)), hypo*Math.Cos(gamma*(Math.PI/180)));

                valueBlock.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));

                Canvas.SetTop(valueBlock, Chart.PlotArea.Height/2 - cp.Y - valueBlock.DesiredSize.Height*.5);
                Canvas.SetLeft(valueBlock, cp.X + Chart.PlotArea.Width/2 - valueBlock.DesiredSize.Width*.5);
                Panel.SetZIndex(valueBlock, int.MaxValue - 1);
                //because math is kind of complex to detetrmine if label fits inside the slide, by now we 
                //will just add it if participation > 5% ToDo: the math!
                if (participation > .05 && Chart.AxisY.ShowLabels)
                {
                    Chart.Canvas.Children.Add(valueBlock);
                    Chart.Shapes.Add(valueBlock);
                }

                if (!Chart.DisableAnimation)
                {
                    if (animate)
                    {
                        slice.BeginAnimation(PieSlice.WedgeAngleProperty, wa);
                        slice.BeginAnimation(PieSlice.RotationAngleProperty, ra);
                    }
                }

                slice.MouseDown += Chart.DataMouseDown;

                slice.MouseEnter += Chart.DataMouseEnter;
                slice.MouseLeave += Chart.DataMouseLeave;

                Chart.HoverableShapes.Add(new HoverableShape
                {
                    Series = this,
                    Shape = slice,
                    Target = slice,
                    Value = point,
                    Label = Labels != null && Labels.Count > point.X ? Labels[(int) point.X] : ""
                });


                sliceId++;
                rotated += participation;
            }
        }
Beispiel #2
0
        private VisualHelper GetVisual(ChartPoint point)
        {
            var map = _isPrimitive
                ? Chart.ShapesMapper.FirstOrDefault(x => x.Series.Equals(this) &&
                                                         x.ChartPoint.Key == point.Key)
                : Chart.ShapesMapper.FirstOrDefault(x => x.Series.Equals(this) &&
                                                         x.ChartPoint.Instance == point.Instance);

            var pChart = Chart as PieChart;
            if (pChart == null)
                throw new InvalidCastException("Unexpected error converting chart to pie chart.");

            if (map == null)
            {
                var newSlice = new PieSlice
                {
                    Fill = Brushes != null && Brushes.Length > point.X
                        ? Brushes[(int) point.X]
                        : new SolidColorBrush(GetColorByIndex((int) point.X)),
                    Stroke = Stroke,
                    StrokeThickness = pChart.SlicePadding,
                    CentreX = 0,
                    CentreY = 0,
                    InnerRadius = pChart.InnerRadius
                };
                return new VisualHelper
                {
                    HoverShape = newSlice,
                    PointShape = newSlice,
                    IsNew = true
                };
            }

            return new VisualHelper
            {
                PointShape = (PieSlice) map.Shape,
                HoverShape = (PieSlice) map.HoverShape,
                IsNew = false
            };
        }
        public override void Plot(bool animate = true)
        {
            var pChart = Chart as PieChart;
            if (pChart == null) return;
            if (pChart.PieTotalSum <= 0) return;
            var rotated = 0d;

            Chart.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));

            var minDimension = Chart.DesiredSize.Width < Chart.DesiredSize.Height
                ? Chart.DesiredSize.Width : Chart.DesiredSize.Height;
            minDimension -= pChart.DrawPadding;
            minDimension = minDimension < pChart.DrawPadding ? pChart.DrawPadding : minDimension;

            var sliceId = 0;
            for (var index = 0; index < PrimaryValues.Count; index++)
            {
                var value = PrimaryValues[index];
                var participation = value/pChart.PieTotalSum;
                if (index == 0) rotated = participation *-.5;

                var slice = new PieSlice
                {
                    CentreX = 0,
                    CentreY = 0,
                    RotationAngle = 360 * rotated,
                    WedgeAngle = 360 * participation,
                    Radius = minDimension / 2,
                    InnerRadius = pChart.InnerRadius,
                    Fill = new SolidColorBrush {Color = GetColorByIndex(sliceId), Opacity = .8},
                    Stroke = Chart.Background,
                    StrokeThickness = pChart.SlicePadding
                };

                var wa = new DoubleAnimation
                {
                    From = 0,
                    To = slice.WedgeAngle,
                    Duration = TimeSpan.FromMilliseconds(300)
                };
                var ra = new DoubleAnimation
                {
                    From = 0,
                    To = slice.RotationAngle,
                    Duration = TimeSpan.FromMilliseconds(300)
                };

                Canvas.SetTop(slice, Chart.ActualHeight/2);
                Canvas.SetLeft(slice, Chart.ActualWidth/2);

                Chart.Canvas.Children.Add(slice);
                Shapes.Add(slice);

                var valueBlock = new TextBlock
                {
                    Text = Chart.PrimaryAxis.LabelFormatter == null
                        ? value.ToString(CultureInfo.InvariantCulture)
                        : Chart.PrimaryAxis.LabelFormatter(value),
                    FontFamily = Chart.PrimaryAxis.FontFamily,
                    FontSize = Chart.PrimaryAxis.FontSize,
                    FontStretch = Chart.PrimaryAxis.FontStretch,
                    FontStyle = Chart.PrimaryAxis.FontStyle,
                    FontWeight = Chart.PrimaryAxis.FontWeight,
                    Foreground = Brushes.White
                };

                var hypo = ((minDimension/2) + (pChart.InnerRadius > 10 ? pChart.InnerRadius : 10))/2;
                var gamma = participation*360/2 + rotated*360;
                var cp = new Point(hypo*Math.Sin(gamma*(Math.PI/180)), hypo*Math.Cos(gamma*(Math.PI/180)));

                valueBlock.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));

                Canvas.SetTop(valueBlock, Chart.ActualHeight/2 -cp.Y - valueBlock.DesiredSize.Height*.5);
                Canvas.SetLeft(valueBlock, cp.X + Chart.ActualWidth/2 - valueBlock.DesiredSize.Width*.5);
                Panel.SetZIndex(valueBlock, int.MaxValue);
                //because math is kind of complex to detetrmine if label fits inside the slide, by now we
                //will just add it if participation > 5% ToDo: the math!
                if (participation > .05 && Chart.PrimaryAxis.PrintLabels)
                {
                    Chart.Canvas.Children.Add(valueBlock);
                    Chart.AxisLabels.Add(valueBlock);
                }

                if (!Chart.DisableAnimation)
                {
                    if (animate)
                    {
                        slice.BeginAnimation(PieSlice.WedgeAngleProperty, wa);
                        slice.BeginAnimation(PieSlice.RotationAngleProperty, ra);
                    }
                }

                if (Chart.Hoverable)
                {
                    slice.MouseEnter += Chart.OnDataMouseEnter;
                    slice.MouseLeave += Chart.OnDataMouseLeave;
                    Chart.HoverableShapes.Add(new HoverableShape
                    {
                        Serie = this,
                        Shape = slice,
                        Target = slice,
                        Value = new Point(0, value),
                        Label = Labels.Length > index ? Labels[index] : ""
                    });
                }

                sliceId++;
                rotated += participation;
            }
        }
Beispiel #4
0
        public override void Plot(bool animate = true)
        {
            if (Visibility != Visibility.Visible) return;
            var pChart = Chart as PieChart;
            if (pChart == null) return;
            if (pChart.PieTotalSum <= 0) return;
            var rotated = 0d;

            Chart.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));

            var minDimension = Chart.PlotArea.Width < Chart.PlotArea.Height
                ? Chart.PlotArea.Width
                : Chart.PlotArea.Height;
            minDimension -= pChart.DrawPadding;
            minDimension = minDimension < pChart.DrawPadding ? pChart.DrawPadding : minDimension;

            var sliceId = 0;
            var isFist = true;

            var f = Chart.GetFormatter(Chart.AxisY);
            var pie = (PieChart) Chart;

            foreach (var point in Values.Points)
            {
                var participation = point.Y/pChart.PieTotalSum;
                if (isFist)
                {
                    rotated = participation*-.5  + (pie.PieRotation/360);
                    isFist = false;
                }

                var slice = new PieSlice
                {
                    CentreX = 0,
                    CentreY = 0,
                    RotationAngle = 360*rotated,
                    WedgeAngle = 360*participation,
                    Radius = minDimension/2,
                    InnerRadius = pChart.InnerRadius,
                    Fill = Brushes != null && Brushes.Length > sliceId
                        ? Brushes[sliceId]
                        : new SolidColorBrush(GetColorByIndex(sliceId)),
                    Stroke = Stroke,
                    StrokeThickness = pChart.SlicePadding
                };
                var wa = new DoubleAnimation
                {
                    From = 0,
                    To = slice.WedgeAngle,
                    Duration = TimeSpan.FromMilliseconds(500)
                };
                var ra = new DoubleAnimation
                {
                    From = 0,
                    To = slice.RotationAngle,
                    Duration = TimeSpan.FromMilliseconds(500)
                };

                Canvas.SetTop(slice, Chart.PlotArea.Height/2);
                Canvas.SetLeft(slice, Chart.PlotArea.Width/2);

                Chart.Canvas.Children.Add(slice);
                Shapes.Add(slice);

                if (DataLabels)
                {
                    var valueBlock = BuildATextBlock(0);
                    valueBlock.Text = f(point.Y);

                    var hypo = ((minDimension / 2) + (pChart.InnerRadius > 10 ? pChart.InnerRadius : 10)) / 2;
                    var gamma = participation * 360 / 2 + rotated * 360;
                    var cp = new Point(hypo * Math.Sin(gamma * (Math.PI / 180)), hypo * Math.Cos(gamma * (Math.PI / 180)));

                    valueBlock.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));

                    Canvas.SetTop(valueBlock, Chart.PlotArea.Height / 2 - cp.Y - valueBlock.DesiredSize.Height * .5);
                    Canvas.SetLeft(valueBlock, cp.X + Chart.PlotArea.Width / 2 - valueBlock.DesiredSize.Width * .5);
                    Panel.SetZIndex(valueBlock, int.MaxValue - 1);
                    //because math is kind of complex to detetrmine if label fits inside the slide, by now we 
                    //will just add it if participation > 5% ToDo: the math!
                    if (participation > .05 && Chart.AxisY.ShowLabels)
                    {
                        Chart.Canvas.Children.Add(valueBlock);
                        Chart.Shapes.Add(valueBlock);
                    }
                }

                if (!Chart.DisableAnimation)
                {
                    if (animate)
                    {
                        slice.BeginAnimation(PieSlice.WedgeAngleProperty, wa);
                        slice.BeginAnimation(PieSlice.RotationAngleProperty, ra);
                    }
                }

                slice.MouseDown += Chart.DataMouseDown;

                slice.MouseEnter += Chart.DataMouseEnter;
                slice.MouseLeave += Chart.DataMouseLeave;

                Chart.ShapesMapper.Add(new ShapeMap
                {
                    Series = this,
                    HoverShape = slice,
                    Shape = slice,
                    ChartPoint = point
                });


                sliceId++;
                rotated += participation;
            }
        }
Beispiel #5
0
        private VisualHelper GetVisual(ChartPoint point)
        {
            var map = _isPrimitive
                ? Chart.ShapesMapper.FirstOrDefault(x => x.Series.Equals(this) &&
                                                         x.ChartPoint.Key == point.Key)
                : Chart.ShapesMapper.FirstOrDefault(x => x.Series.Equals(this) &&
                                                         x.ChartPoint.Instance == point.Instance);

            var pChart = Chart as PieChart;
            if (pChart == null)
                throw new InvalidCastException("Unexpected error converting chart to pie chart.");

            if (map == null)
            {
                var newSlice = new PieSlice
                {
                    CentreX = 0,
                    CentreY = 0
                };

                BindingOperations.SetBinding(newSlice, Shape.StrokeProperty,
                    new Binding { Path = new PropertyPath("Stroke"), Source = this });
                BindingOperations.SetBinding(newSlice, Shape.FillProperty,
                    new Binding { Path = new PropertyPath("Fill"), Source = this });
                BindingOperations.SetBinding(newSlice, Shape.StrokeThicknessProperty,
                    new Binding { Path = new PropertyPath("StrokeThickness"), Source = this });
                BindingOperations.SetBinding(newSlice, VisibilityProperty,
                    new Binding { Path = new PropertyPath("Visibility"), Source = this });

                return new VisualHelper
                {
                    HoverShape = newSlice,
                    PointShape = newSlice,
                    IsNew = true
                };
            }

            map.ChartPoint.X = point.X;
            map.ChartPoint.Y = point.Y;

            return new VisualHelper
            {
                PointShape = (PieSlice) map.Shape,
                HoverShape = (PieSlice) map.HoverShape,
                IsNew = false
            };
        }