using System; using System.Collections.Generic; using System.Linq; using System.Text; using Abt.Controls.SciChart.Visuals.Axes; namespace Abt.Controls.SciChart { public class NumericAxisEx:NumericAxis { public static readonly DependencyProperty IsLeftMarginalLabelProperty = DependencyProperty.RegisterAttached("IsLeftMarginalLabel", typeof(bool), typeof(NumericAxisEx), new PropertyMetadata(false, (d, e) => OnIsMarginalLabelChanged(d, e, HorizontalAnchorPoint.Left))); public static bool GetIsLeftMarginalLabel(DependencyObject obj) { return (bool)obj.GetValue(IsLeftMarginalLabelProperty); } public static void SetIsLeftMarginalLabel(DependencyObject obj, bool value) { obj.SetValue(IsLeftMarginalLabelProperty, value); } public static readonly DependencyProperty IsRightMarginalLabelProperty = DependencyProperty.RegisterAttached("IsRightMarginalLabel", typeof(bool), typeof(NumericAxisEx), new PropertyMetadata(false, (d, e) => OnIsMarginalLabelChanged(d, e, HorizontalAnchorPoint.Right))); public static bool GetIsRightMarginalLabel(DependencyObject obj) { return (bool)obj.GetValue(IsRightMarginalLabelProperty); } public static void SetIsRightMarginalLabel(DependencyObject obj, bool value) { obj.SetValue(IsRightMarginalLabelProperty, value); } protected override void DrawTickLabels(AxisCanvas canvas, TickCoordinates tickCoords, float offset) { base.DrawTickLabels(canvas, tickCoords, offset); foreach (UIElement child in canvas.Children) { SetIsLeftMarginalLabel(child, false); SetIsRightMarginalLabel(child, false); } if (canvas.Children.Count >= 2) { var leftLabel = canvas.Children[0] as DefaultTickLabel; var rightLabel = canvas.Children[canvas.Children.Count - 1] as DefaultTickLabel; leftLabel.MeasureArrange(); rightLabel.MeasureArrange(); var isInBounds = leftLabel.Position.X - leftLabel.ActualWidth/2 >= 0; if (!isInBounds) { leftLabel.Position = new Point(0, leftLabel.Position.Y); SetIsLeftMarginalLabel(leftLabel, true); } isInBounds = rightLabel.Position.X + rightLabel.ActualWidth / 2 <= this.ActualWidth; if (!isInBounds) { rightLabel.Position = new Point(0, rightLabel.Position.Y); SetIsRightMarginalLabel(rightLabel, true); } } } private static void OnIsMarginalLabelChanged(DependencyObject d, DependencyPropertyChangedEventArgs e, HorizontalAnchorPoint anchorPoint) { var label = d as DefaultTickLabel; if (label != null) { label.HorizontalAnchorPoint = (bool)e.NewValue ? anchorPoint : HorizontalAnchorPoint.Center; } } } }