Smart label strategy

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

    Posted 28 Oct 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)
    02.{
    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;
    15.}

    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; }
    04. 
    05.        public static readonly DependencyProperty VerticalMinValueProperty = DependencyProperty.RegisterAttached("VerticalMinValue", typeof(double), typeof(ChartSmartLabelsStrategyBase), new PropertyMetadata(default(double), OnVerticalMinValueChanged));
    06. 
    07.        private static void OnVerticalMinValueChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
    08.        {
    09.            var c = obj as TwoLineSeriesLabelsStrategy;
    10.            c.VerticalMinValue = (double)args.NewValue;
    11.        }
    12. 
    13.        public static void SetVerticalMinValue(DependencyObject element, double value)
    14.        {
    15.            element.SetValue(VerticalMinValueProperty, value);
    16.        }
    17. 
    18.        public static double GetVerticalMinValue(DependencyObject element)
    19.        {
    20.            return (double)element.GetValue(VerticalMinValueProperty);
    21.        }
    22.....
    23.....
    24.....
    25. 
    26.}

     

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

     

    Thanks,

    Vlad

     

     

     

     

     

  2. Martin
    Admin
    Martin avatar
    1101 posts

    Posted 02 Nov 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.

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