/// <summary> /// Get the Rectangle definition of the slice at the current frame of /// animation, if there is a slice key defined for the frame /// </summary> /// <param name="sliceName">The name of the slice</param> /// <returns> /// A Rectangle definition of the frame slice, at the xy-coordinate of /// this sprite. If no slice key exists for the current frame, /// null is returned. /// </returns> /// <exception cref="ArgumentException"> /// Thrown if the slice name provided does not exist in the animation definitions slice dictionary /// </exception> public bool TryGetCurrentFrameSlice(string sliceName, out SliceKey sliceKey) { // Ensure that we have a slice defined with the given name if (Slices.TryGetValue(sliceName, out Slice slice)) { // Ensure we have a slice key at the current animation frame index if (slice.Keys.TryGetValue(CurrentFrameIndex, out sliceKey)) { // Update the xy-coordinate of the slicekey bounds to match the positiona // data of this animated sprite. sliceKey.Bounds.X += (int)Position.X; sliceKey.Bounds.Y += (int)Position.Y; return(true); } else { // There is no slicekey for the current frame index, so we return false. return(false); } } else { // No slice exists with the given name, return false sliceKey = new SliceKey(); return(false); } }
/// <summary> /// Gets the defined color of the slice with the given name /// </summary> /// <param name="sliceName"> /// The name of the slice /// </param> /// <returns> /// The color of the slice /// </returns> /// <exception cref="ArgumentException"> /// Thrown if no <see cref="Slice"/> instance exists with the name given /// in <paramref name="sliceName"/> /// </exception> public Color GetSliceColor(string sliceName) { // Ensure we have a slice defined with the given name if (Slices.TryGetValue(sliceName, out Slice slice)) { // Return the color of the slice return(slice.Color); } else { // No slice exists with the given name, throw error throw new ArgumentOutOfRangeException($"No slice exists with the given name {sliceName}"); } }
internal void Draw() { if (!IsControlLaoded) { return; } //No cache for you gauge :( kill and redraw please foreach (var child in Canvas.Children .Where(x => !Equals(x, Stick) && !(x is AngularSection) && !(x is PieSlice)).ToArray()) { Canvas.Children.Remove(child); } Wedge = Wedge > 360 ? 360 : (Wedge < 0 ? 0 : Wedge); var fromAlpha = (360 - Wedge) * .5; var toAlpha = 360 - fromAlpha; var d = ActualWidth < ActualHeight ? ActualWidth : ActualHeight; Stick.Height = d * .5 * .8; Stick.Width = Stick.Height * .2; Canvas.SetLeft(Stick, ActualWidth * .5 - Stick.Width * .5); Canvas.SetTop(Stick, ActualHeight * .5 - Stick.Height * .9); var ticksHi = d * .5; var ticksHj = d * .47; var labelsHj = d * .44; foreach (var section in Sections) { PieSlice slice; section.Owner = this; if (!Slices.TryGetValue(section, out slice)) { slice = new PieSlice(); Slices[section] = slice; } var p = (Canvas)section.Parent; p?.Children.Remove(section); Canvas.Children.Add(section); var ps = (Canvas)slice.Parent; ps?.Children.Remove(slice); Canvas.Children.Add(slice); } UpdateSections(); for (var i = FromValue; i <= ToValue; i += TicksStep) { var alpha = LinearInterpolation(fromAlpha, toAlpha, FromValue, ToValue, i) + 90; var tick = new Line { X1 = ActualWidth * .5 + ticksHi * Math.Cos(alpha * Math.PI / 180), X2 = ActualWidth * .5 + ticksHj * Math.Cos(alpha * Math.PI / 180), Y1 = ActualHeight * .5 + ticksHi * Math.Sin(alpha * Math.PI / 180), Y2 = ActualHeight * .5 + ticksHj * Math.Sin(alpha * Math.PI / 180) }; Canvas.Children.Add(tick); tick.SetBinding(Shape.StrokeProperty, new Binding { Path = new PropertyPath("TicksForeground"), Source = this }); tick.SetBinding(Shape.StrokeThicknessProperty, new Binding { Path = new PropertyPath("TicksStrokeThickness"), Source = this }); } for (var i = FromValue; i <= ToValue; i += LabelsStep) { var alpha = LinearInterpolation(fromAlpha, toAlpha, FromValue, ToValue, i) + 90; var tick = new Line { X1 = ActualWidth * .5 + ticksHi * Math.Cos(alpha * Math.PI / 180), X2 = ActualWidth * .5 + labelsHj * Math.Cos(alpha * Math.PI / 180), Y1 = ActualHeight * .5 + ticksHi * Math.Sin(alpha * Math.PI / 180), Y2 = ActualHeight * .5 + labelsHj * Math.Sin(alpha * Math.PI / 180) }; Canvas.Children.Add(tick); var label = new TextBlock { Text = LabelFormatter(i) }; //label.SetBinding(EffectProperty, //new Binding {Path = new PropertyPath("LabelsEffect"), Source = this}); Canvas.Children.Add(label); label.UpdateLayout(); Canvas.SetLeft(label, alpha < 270 ? tick.X2 : (Math.Abs(alpha - 270) < 4 ? tick.X2 - label.ActualWidth * .5 : tick.X2 - label.ActualWidth)); Canvas.SetTop(label, tick.Y2); tick.SetBinding(Shape.StrokeProperty, new Binding { Path = new PropertyPath("TicksForeground"), Source = this }); tick.SetBinding(Shape.StrokeThicknessProperty, new Binding { Path = new PropertyPath("TicksStrokeThickness"), Source = this }); } MoveStick(); }
internal void Draw() { if (!IsControlLaoded) return; //No cache for you gauge :( kill and redraw please foreach (var child in Canvas.Children.Cast<UIElement>() .Where(x => !Equals(x, Stick) && !(x is AngularSection) && !(x is PieSlice)).ToArray()) Canvas.Children.Remove(child); Wedge = Wedge > 360 ? 360 : (Wedge < 0 ? 0 : Wedge); var fromAlpha = (360-Wedge)*.5; var toAlpha = 360 - fromAlpha; var d = ActualWidth < ActualHeight ? ActualWidth : ActualHeight; Stick.Height = d*.5*.8; Stick.Width = Stick.Height*.2; Canvas.SetLeft(Stick, ActualWidth*.5 - Stick.Width*.5); Canvas.SetTop(Stick, ActualHeight*.5 - Stick.Height*.9); var ticksHi = d*.5; var ticksHj = d*.47; var labelsHj = d*.44; foreach (var section in Sections) { PieSlice slice; section.Owner = this; if (!Slices.TryGetValue(section, out slice)) { slice = new PieSlice(); Slices[section] = slice; } var p = (Canvas)section.Parent; if (p != null) p.Children.Remove(section); Canvas.Children.Add(section); var ps = (Canvas)slice.Parent; if (ps != null) ps.Children.Remove(slice); Canvas.Children.Add(slice); } UpdateSections(); var ts = double.IsNaN(TicksStep) ? DecideInterval((ToValue - FromValue)/5) : TicksStep; if (ts / (FromValue - ToValue) > 300) throw new LiveChartsException("TicksStep property is too small compared with the range in " + "the gauge, to avoid performance issues, please increase it."); for (var i = FromValue; i <= ToValue; i += ts) { var alpha = LinearInterpolation(fromAlpha, toAlpha, FromValue, ToValue, i) + 90; var tick = new Line { X1 = ActualWidth*.5 + ticksHi*Math.Cos(alpha*Math.PI/180), X2 = ActualWidth*.5 + ticksHj*Math.Cos(alpha*Math.PI/180), Y1 = ActualHeight*.5 + ticksHi*Math.Sin(alpha*Math.PI/180), Y2 = ActualHeight*.5 + ticksHj*Math.Sin(alpha*Math.PI/180) }; Canvas.Children.Add(tick); tick.SetBinding(Shape.StrokeProperty, new Binding {Path = new PropertyPath(TicksForegroundProperty), Source = this}); tick.SetBinding(Shape.StrokeThicknessProperty, new Binding { Path = new PropertyPath(TicksStrokeThicknessProperty), Source = this }); } var ls = double.IsNaN(LabelsStep) ? DecideInterval((ToValue - FromValue) / 5) : LabelsStep; if (ls / (FromValue - ToValue) > 300) throw new LiveChartsException("LabelsStep property is too small compared with the range in " + "the gauge, to avoid performance issues, please increase it."); for (var i = FromValue; i <= ToValue; i += ls) { var alpha = LinearInterpolation(fromAlpha, toAlpha, FromValue, ToValue, i) + 90; var tick = new Line { X1 = ActualWidth*.5 + ticksHi*Math.Cos(alpha*Math.PI/180), X2 = ActualWidth*.5 + labelsHj*Math.Cos(alpha*Math.PI/180), Y1 = ActualHeight*.5 + ticksHi*Math.Sin(alpha*Math.PI/180), Y2 = ActualHeight*.5 + labelsHj*Math.Sin(alpha*Math.PI/180) }; Canvas.Children.Add(tick); var label = new TextBlock { Text = LabelFormatter(i) }; label.SetBinding(EffectProperty, new Binding {Path = new PropertyPath(LabelsEffectProperty), Source = this}); Canvas.Children.Add(label); label.UpdateLayout(); Canvas.SetLeft(label, alpha < 270 ? tick.X2 : (Math.Abs(alpha - 270) < 4 ? tick.X2 - label.ActualWidth*.5 : tick.X2 - label.ActualWidth)); Canvas.SetTop(label, tick.Y2); tick.SetBinding(Shape.StrokeProperty, new Binding { Path = new PropertyPath(TicksForegroundProperty), Source = this }); tick.SetBinding(Shape.StrokeThicknessProperty, new Binding { Path = new PropertyPath(TicksStrokeThicknessProperty), Source = this }); } MoveStick(); }
internal override void Draw() { if (!IsControlLaoded) { return; } foreach (var child in Canvas.Children.Cast <UIElement>() .Where(x => !Equals(x, Stick) && !(x is LinearSection) && !(x is PieSlice)).ToArray()) { Canvas.Children.Remove(child); } foreach (var section in Sections) { Rectangle slice; section.Owner = this; if (!Slices.TryGetValue(section, out slice)) { slice = new Rectangle(); Slices[section] = slice; } var p = (Canvas)section.Parent; if (p != null) { p.Children.Remove(section); } Canvas.Children.Add(section); var ps = (Canvas)slice.Parent; if (ps != null) { ps.Children.Remove(slice); } Canvas.Children.Add(slice); } //画区域 UpdateSections(); var ts = double.IsNaN(TicksStep) ? DecideInterval((ToValue - FromValue) / 5) : TicksStep; var unit = (GetActualHeight() / (ToValue - FromValue)); //画短刻度 for (var i = FromValue; i <= ToValue; i += ts) { var bottom = (i - FromValue) * unit; var tick = new Line { X1 = ActualWidth * .5, X2 = ActualWidth * .5 + 5, Y1 = ActualHeight - bottom, Y2 = ActualHeight - bottom }; Canvas.Children.Add(tick); tick.SetBinding(Shape.StrokeProperty, new Binding { Path = new PropertyPath(TicksForegroundProperty), Source = this }); tick.SetBinding(Shape.StrokeThicknessProperty, new Binding { Path = new PropertyPath(TicksStrokeThicknessProperty), Source = this }); } var ls = double.IsNaN(LabelsStep) ? DecideInterval((ToValue - FromValue) / 5) : LabelsStep; if (ls / (FromValue - ToValue) > 300) { throw new LiveChartsException("LabelsStep property is too small compared with the range in " + "the gauge, to avoid performance issues, please increase it."); } //画长刻度与数字 for (var i = FromValue; i <= ToValue; i += ls) { var bottom = (i - FromValue) * unit; var tick = new Line { X1 = ActualWidth * .5, X2 = ActualWidth * .5 + 10, Y1 = ActualHeight - bottom, Y2 = ActualHeight - bottom, }; Canvas.Children.Add(tick); var label = new TextBlock { Text = LabelFormatter(i) }; label.SetBinding(EffectProperty, new Binding { Path = new PropertyPath(LabelsEffectProperty), Source = this }); Canvas.Children.Add(label); label.UpdateLayout(); Canvas.SetLeft(label, ActualWidth * .5 - label.ActualWidth * .5 - 10); Canvas.SetBottom(label, bottom - (label.ActualHeight / 2)); tick.SetBinding(Shape.StrokeProperty, new Binding { Path = new PropertyPath(TicksForegroundProperty), Source = this }); tick.SetBinding(Shape.StrokeThicknessProperty, new Binding { Path = new PropertyPath(TicksStrokeThicknessProperty), Source = this }); } ValueRec.Width = BackWidth / 4; Canvas.Children.Add(ValueRec); Canvas.SetLeft(ValueRec, ActualWidth * 0.5 + ValueRec.Width * 1.5); Canvas.SetBottom(ValueRec, 0); MoveStick(); }
internal override void Draw() { if (!IsControlLaoded) return; //No cache for you gauge :( kill and redraw please foreach (var child in Canvas.Children.Cast<UIElement>() .Where(x => !Equals(x, Stick) && !(x is CompassSection) && !(x is PieSlice)).ToArray()) Canvas.Children.Remove(child); var fromAlpha = (360 - Wedge) * .5; var toAlpha = 360 - fromAlpha; var d = ActualWidth < ActualHeight ? ActualWidth : ActualHeight; Stick.Height = d * .5 * .8; Stick.Width = Stick.Height * .2; Canvas.SetLeft(Stick, ActualWidth * .5 - Stick.Width * .5); Canvas.SetTop(Stick, ActualHeight * .5 - Stick.Height * .9); var ticksHi = d * .5; var ticksHj = d * .47; var labelsHj = d * .44; foreach (var section in Sections) { PieSlice slice; section.Owner = this; if (!Slices.TryGetValue(section, out slice)) { slice = new PieSlice(); Slices[section] = slice; } var p = (Canvas)section.Parent; if (p != null) p.Children.Remove(section); Canvas.Children.Add(section); var ps = (Canvas)slice.Parent; if (ps != null) ps.Children.Remove(slice); Canvas.Children.Add(slice); } UpdateSections(); var ts = double.IsNaN(TicksStep) ? DecideInterval((ToValue - FromValue) / 5) : TicksStep; if (ts / (FromValue - ToValue) > 300) throw new LiveChartsException("TicksStep property is too small compared with the range in " + "the gauge, to avoid performance issues, please increase it."); for (var i = FromValue; i <= ToValue; i += ts) { var alpha = LinearInterpolation(fromAlpha, toAlpha, FromValue, ToValue, i) - 90; var tick = new Line { X1 = ActualWidth * .5 + ticksHi * Math.Cos(alpha * Math.PI / 180), X2 = ActualWidth * .5 + ticksHj * Math.Cos(alpha * Math.PI / 180), Y1 = ActualHeight * .5 + ticksHi * Math.Sin(alpha * Math.PI / 180), Y2 = ActualHeight * .5 + ticksHj * Math.Sin(alpha * Math.PI / 180) }; Canvas.Children.Add(tick); tick.SetBinding(Shape.StrokeProperty, new Binding { Path = new PropertyPath(TicksForegroundProperty), Source = this }); tick.SetBinding(Shape.StrokeThicknessProperty, new Binding { Path = new PropertyPath(TicksStrokeThicknessProperty), Source = this }); } var ls = double.IsNaN(LabelsStep) ? 45 : LabelsStep; if (ls / (FromValue - ToValue) > 300) throw new LiveChartsException("LabelsStep property is too small compared with the range in " + "the gauge, to avoid performance issues, please increase it."); for (var i = FromValue; i <= ToValue; i += ls) { if (i % 90 == 0 && !IsMil) continue; DrawTick(fromAlpha, toAlpha, ticksHi, labelsHj, i); } if (!IsMil) { //绘制东西南北标签 for (var i = FromValue; i <= ToValue; i += (ToValue + 1) / 4) { DrawTick(fromAlpha, toAlpha, ticksHi, labelsHj, i); } } MoveStick(); }