Smart label strategy

2 posts, 0 answers
  1. Vladimir Skvortsov
    Vladimir Skvortsov avatar
    6 posts
    Member since:
    Oct 2009

    Posted 28 Oct 2016 Link to this post

    Hello Telerik!

    I create my custom label strategy. All labels draw at the top of the chart. I use 30% of  plotAreaClip.Bottom to get max bottom pixel.

    01.private IEnumerable<RadRect> GetSlots(RadRect pointMark, RadRect label, RadRect plotAreaClip, int labelIndex)
    03.    var bottomBorder = plotAreaClip.Bottom*0.3;
    04.    var leftShift = 25;
    05.    for (var i = 0;; i++)
    06.    {
    07.        var top = label.Height * i + labelIndex;
    08.        if (top > bottomBorder || top < plotAreaClip.Y)
    09.        {
    10.            break;
    11.        }
    12.        yield return new RadRect(pointMark.Center.X + leftShift, top, label.Width, label.Height);
    13.    }
    14.    yield return label;

    I would like to draw all my labels above 0 depth value (look at the attachment). The area above 0 depth area has dynamic height depending on data. 

    My idea is to pass Min and Max depth values from View Model into the strategy and based on the values dynamically compute needed pixel. 

    I wanted to create Dependency Property but ChartSmartLabelsStrategyBase is not inherited from DependencyObject

    01.public class TwoLineSeriesLabelsStrategy : ChartSmartLabelsStrategyBase
    02.    {
    03.        public double VerticalMinValue { get; set; }
    05.        public static readonly DependencyProperty VerticalMinValueProperty = DependencyProperty.RegisterAttached("VerticalMinValue", typeof(double), typeof(ChartSmartLabelsStrategyBase), new PropertyMetadata(default(double), OnVerticalMinValueChanged));
    07.        private static void OnVerticalMinValueChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
    08.        {
    09.            var c = obj as TwoLineSeriesLabelsStrategy;
    10.            c.VerticalMinValue = (double)args.NewValue;
    11.        }
    13.        public static void SetVerticalMinValue(DependencyObject element, double value)
    14.        {
    15.            element.SetValue(VerticalMinValueProperty, value);
    16.        }
    18.        public static double GetVerticalMinValue(DependencyObject element)
    19.        {
    20.            return (double)element.GetValue(VerticalMinValueProperty);
    21.        }


    Are there any ways to pass parameters from my ViewModel directly to the label strategy?  Or any workarounds to draw labels above 0 value?









  2. Martin Ivanov
    Martin Ivanov avatar
    2391 posts

    Posted 02 Nov 2016 Link to this post

    Hello Vlad,

    I guess you want to use the minimum and maximum of the chart's axes in the GetSlots() method which is called in the override of CalculateLabelsPositions(). If so, instead of getting the view model's minimum and maximum properties (which will couple the UI logic with the models), you can use the ChartSeriesLabelPositionInfo object to get the chart and use its axis' Minimum and Maximum properties. The label position info object contains the corresponding DataPoint object which knows about its Presenter (the series). The presenter on its side knows about the chart.

    Here is an example:
    public class TwoLineSeriesLabelsStrategy : ChartSmartLabelsStrategyBase
        protected override void CalculateLabelsPositions(RadRect plotAreaClip, System.Collections.ObjectModel.ReadOnlyCollection<ChartSeriesLabelPositionInfo> labelPositionInfos)
            CartesianSeries series = (CartesianSeries)labelPositionInfos[0].DataPoint.Presenter;
            RadCartesianChart chart = (RadCartesianChart)series.Chart;
            LinearAxis numericAxis = (LinearAxis)chart.VerticalAxis;
            double min = numericAxis.ActualRange.Minimum;
            double max = numericAxis.ActualRange.Maximum;

    I hope this helps.

    Telerik by Progress
    Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
Back to Top