Exemplo n.º 1
0
        private void Update()
        {
            if (!IsChartInitialized)
            {
                return;
            }

            var ms = new Size(double.PositiveInfinity, double.PositiveInfinity);

            Measure(ms);
            Canvas.Width  = Width;
            Canvas.Height = Height;

            Func <double, string> defFormatter = x => x.ToString(CultureInfo.InvariantCulture);

            var completed = (Value - From) / (To - From);

            var t = 0d;

            if (double.IsNaN(completed) || double.IsInfinity(completed))
            {
                completed = 0;
            }

            completed = completed > 1 ? 1 : (completed < 0 ? 0 : completed);
            var angle = Uses360Mode ? 360 : 180;

            if (!Uses360Mode)
            {
                LeftLabel.Text  = (LabelFormatter ?? defFormatter)(From);
                RightLabel.Text = (LabelFormatter ?? defFormatter)(To);

                LeftLabel.Visibility  = LabelsVisibility;
                RightLabel.Visibility = LabelsVisibility;

                t = LeftLabel.ActualHeight;
            }
            else
            {
                LeftLabel.Visibility  = Visibility.Collapsed;
                RightLabel.Visibility = Visibility.Collapsed;
            }

            double r, top;

            if (Uses360Mode)
            {
                r   = ActualWidth > ActualHeight ? ActualHeight: ActualWidth;
                r   = r / 2 - 2 * t;
                top = ActualHeight / 2;
            }
            else
            {
                r = ActualWidth;

                if (ActualWidth > ActualHeight * 2)
                {
                    r = ActualHeight * 2;
                }
                else
                {
                    t = 0;
                }

                r = r / 2 - 2 * t;

                top = ActualHeight / 2 + r / 2;
            }

            if (r < 0)
            {
                r = 1;
            }

            PieBack.Height = Canvas.ActualHeight;
            PieBack.Width  = Canvas.ActualWidth;
            Pie.Height     = Canvas.ActualHeight;
            Pie.Width      = Canvas.ActualWidth;

            PieBack.Radius        = r;
            PieBack.InnerRadius   = double.IsNaN(InnerRadius) ? r * .6 : InnerRadius;
            PieBack.RotationAngle = 270;
            PieBack.WedgeAngle    = angle;

            Pie.Radius        = PieBack.Radius;
            Pie.InnerRadius   = PieBack.InnerRadius;
            Pie.RotationAngle = PieBack.RotationAngle;

            Pie.YOffset     = top - ActualHeight / 2;
            PieBack.YOffset = top - ActualHeight / 2;

            Canvas.SetTop(LeftLabel, top);
            Canvas.SetTop(RightLabel, top);
            LeftLabel.Measure(ms);
            RightLabel.Measure(ms);
            Canvas.SetLeft(LeftLabel,
                           Canvas.ActualWidth * .5 -
                           (Pie.InnerRadius + (Pie.Radius - Pie.InnerRadius) * .5 + LeftLabel.DesiredSize.Width * .5));
            Canvas.SetLeft(RightLabel,
                           Canvas.ActualWidth * .5 +
                           (Pie.InnerRadius + (Pie.Radius - Pie.InnerRadius) * .5 - RightLabel.DesiredSize.Width * .5));

            MeasureTextBlock.FontSize = double.IsNaN(HighFontSize) ? Pie.InnerRadius * .4 : HighFontSize;
            MeasureTextBlock.Text     = (LabelFormatter ?? defFormatter)(Value);

            MeasureTextBlock.Measure(ms);
            Canvas.SetTop(MeasureTextBlock, top - MeasureTextBlock.DesiredSize.Height * (Uses360Mode ? .5 : 1));
            Canvas.SetLeft(MeasureTextBlock, ActualWidth / 2 - MeasureTextBlock.DesiredSize.Width / 2);

            var interpolatedColor = new Color
            {
                R = LinearInterpolation(FromColor.R, ToColor.R),
                G = LinearInterpolation(FromColor.G, ToColor.G),
                B = LinearInterpolation(FromColor.B, ToColor.B),
                A = LinearInterpolation(FromColor.A, ToColor.A)
            };

            if (IsNew)
            {
                Pie.Fill       = new SolidColorBrush(FromColor);
                Pie.WedgeAngle = 0;
            }

            Pie.BeginDoubleAnimation(nameof(PieSlice.WedgeAngle), completed * angle, AnimationsSpeed);

            if (GaugeActiveFill == null)
            {
                ((SolidColorBrush)Pie.Fill).BeginColorAnimation(nameof(SolidColorBrush.Color), interpolatedColor, AnimationsSpeed);
            }
            else
            {
                Pie.Fill = GaugeActiveFill;
            }

            IsNew = false;
        }