private void Update() { if (!IsChartInitialized) { return; } 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.UpdateLayout(); RightLabel.UpdateLayout(); LeftLabel.Visibility = LabelsVisibility; RightLabel.Visibility = LabelsVisibility; t = LeftLabel.ActualHeight; } else { LeftLabel.Visibility = Visibility.Hidden; RightLabel.Visibility = Visibility.Hidden; } 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.Radius = r; PieBack.InnerRadius = InnerRadius ?? r * .6; PieBack.RotationAngle = 270; PieBack.WedgeAngle = angle; Pie.Radius = PieBack.Radius; Pie.InnerRadius = PieBack.InnerRadius; Pie.RotationAngle = PieBack.RotationAngle; Canvas.SetLeft(PieBack, ActualWidth / 2); Canvas.SetTop(PieBack, top); Canvas.SetLeft(Pie, ActualWidth / 2); Canvas.SetTop(Pie, top); Canvas.SetTop(LeftLabel, top); Canvas.SetTop(RightLabel, top); Canvas.SetRight(LeftLabel, ActualWidth / 2 + (r + PieBack.InnerRadius) / 2 - LeftLabel.ActualWidth / 2); Canvas.SetRight(RightLabel, ActualWidth / 2 - (r + PieBack.InnerRadius) / 2 - RightLabel.ActualWidth / 2); MeasureTextBlock.FontSize = HighFontSize ?? Pie.InnerRadius * .4; MeasureTextBlock.Text = (LabelFormatter ?? defFormatter)(Value); MeasureTextBlock.UpdateLayout(); Canvas.SetTop(MeasureTextBlock, top - MeasureTextBlock.ActualHeight * (Uses360Mode ? .5 : 1)); Canvas.SetLeft(MeasureTextBlock, ActualWidth / 2 - MeasureTextBlock.ActualWidth / 2); PercentTextBlock.FontSize = MeasureTextBlock.FontSize * .4; PercentTextBlock.Text = "%"; PercentTextBlock.UpdateLayout(); Canvas.SetTop(PercentTextBlock, top - PercentTextBlock.ActualHeight * (Uses360Mode ? .5 : 1)); Canvas.SetLeft(PercentTextBlock, r * 2 - PercentTextBlock.ActualWidth * 2); TitleTextBlock.FontSize = 14; TitleTextBlock.Text = Title; TitleTextBlock.UpdateLayout(); Canvas.SetTop(TitleTextBlock, r * 2 + TitleTextBlock.ActualHeight * (Uses360Mode ? .5 : 1)); Canvas.SetLeft(TitleTextBlock, 0); 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; } if (DisableAnimations) { Pie.WedgeAngle = completed * angle; } else { Pie.BeginAnimation(PieSlice.WedgeAngleProperty, new DoubleAnimation(completed * angle, AnimationsSpeed)); } if (GaugeActiveFill == null) { ((SolidColorBrush)Pie.Fill).BeginAnimation(SolidColorBrush.ColorProperty, new ColorAnimation(interpolatedColor, AnimationsSpeed)); } else { Pie.Fill = GaugeActiveFill; } IsNew = false; }
private void Update() { if (!IsChartInitialized) { return; } Func <double, string> defFormatter = x => x.ToString(CultureInfo.InvariantCulture); var completed = Value / (To - From); var t = 0d; completed = completed > 1 ? 1 : (completed < 0 ? 0 : completed); var angle = Uses360Mode ? 360 : 180; if (!string.IsNullOrWhiteSpace(Title)) { TitleTextBlock.UpdateLayout(); t = TitleTextBlock.ActualHeight; Canvas.SetLeft(TitleTextBlock, ActualWidth / 2 - TitleTextBlock.ActualWidth / 2); } if (!Uses360Mode) { LeftLabel.Text = (LabelFormatter ?? defFormatter)(From); RightLabel.Text = (LabelFormatter ?? defFormatter)(To); LeftLabel.UpdateLayout(); RightLabel.UpdateLayout(); LeftLabel.Visibility = Visibility.Visible; RightLabel.Visibility = Visibility.Visible; t = LeftLabel.ActualHeight; } else { LeftLabel.Visibility = Visibility.Hidden; RightLabel.Visibility = Visibility.Hidden; } 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; } r = r / 2 - 2 * t; top = ActualHeight / 2 + r / 2; } PieBack.Radius = r; PieBack.InnerRadius = InnerRadius ?? r * .6; PieBack.RotationAngle = 270; PieBack.WedgeAngle = angle; Pie.Radius = PieBack.Radius; Pie.InnerRadius = PieBack.InnerRadius; Pie.RotationAngle = PieBack.RotationAngle; Canvas.SetLeft(PieBack, ActualWidth / 2); Canvas.SetTop(PieBack, top); Canvas.SetLeft(Pie, ActualWidth / 2); Canvas.SetTop(Pie, top); Canvas.SetTop(LeftLabel, top); Canvas.SetTop(RightLabel, top); Canvas.SetRight(LeftLabel, ActualWidth / 2 + (r + PieBack.InnerRadius) / 2 - LeftLabel.ActualWidth / 2); Canvas.SetRight(RightLabel, ActualWidth / 2 - (r + PieBack.InnerRadius) / 2 - RightLabel.ActualWidth / 2); MeasureTextBlock.FontSize = HighFontSize ?? Pie.InnerRadius * .4; MeasureTextBlock.Text = (LabelFormatter ?? defFormatter)(Value); MeasureTextBlock.UpdateLayout(); Canvas.SetTop(MeasureTextBlock, top - MeasureTextBlock.ActualHeight * (Uses360Mode ? .5 : 1)); Canvas.SetLeft(MeasureTextBlock, ActualWidth / 2 - MeasureTextBlock.ActualWidth / 2); var interpolatedColor = new Color { R = LinearInterpolation(ToColor.R, FromColor.R), G = LinearInterpolation(ToColor.G, FromColor.G), B = LinearInterpolation(ToColor.B, FromColor.B), A = 255 }; if (IsNew) { Pie.Fill = new SolidColorBrush(ToColor); Pie.WedgeAngle = 0; } Pie.BeginAnimation(PieSlice.WedgeAngleProperty, new DoubleAnimation(completed * angle, AnimationsSpeed)); ((SolidColorBrush)Pie.Fill).BeginAnimation(SolidColorBrush.ColorProperty, new ColorAnimation(interpolatedColor, AnimationsSpeed)); IsNew = false; }