Horizontal Bar Label Clipping Issue

2 posts, 0 answers
  1. Heath Morris
    Heath Morris avatar
    4 posts
    Member since:
    Sep 2009

    Posted 09 May 2013 Link to this post

    I was tasked with removing a clipping issue on the left and right edges of the plot area when using a horizontal bar series. Just wanted to share what i did if it can help anyone else. See the before and after png. I would love some feedback if there is a better way to do this.
    using System.Windows;
    using Telerik.Charting;
    using Telerik.Windows.Controls.ChartView;
     
    namespace MyApplication.labelStrategies
    {
        /// <summary>
        /// This class when applied to a labelDefinition will add padding to the end values
        /// </summary>
        public class HorizontalBarSeriesLabelStrategy : ChartSeriesLabelStrategy
        {
            #region Methods
            public override LabelStrategyOptions Options
            {
                get
                {
                    return LabelStrategyOptions.Arrange;
                }
            }
     
            public override RadRect GetLabelLayoutSlot(DataPoint point, FrameworkElement visual, int labelIndex)
            {
                // get the Plot Area
                RadRect plotArea = ((BarSeries)point.Presenter).Chart.PlotAreaClip;
                 
                // Where is the middle?
                double xPadding = ((point.LayoutSlot.Width - visual.ActualWidth) / 2);
                 
                // if we are clipping on the left side of the plot area
                if (xPadding < 0 && point.LayoutSlot.Location.X <= plotArea.X)
                {
                    // add 5 pixels of padding
                    xPadding = 5;
                }
                // are we clipping on the right edge of plot area?
                else if (point.LayoutSlot.Location.X >= (plotArea.Width + plotArea.X - visual.ActualWidth))
                {
                    // show the label with 5 pixels of padding on the right side.
                    xPadding = (-1 * (visual.ActualWidth + 5)) + (plotArea.Width + plotArea.X - point.LayoutSlot.Location.X);
                }
                     
                // Add the padding
                double x = point.LayoutSlot.X + xPadding;
     
                // set y in the middle
                double y = point.LayoutSlot.Y + ((point.LayoutSlot.Height - visual.ActualHeight) / 2);
     
                // return the corrected RadRect
                return new RadRect(x, y, visual.ActualWidth, visual.ActualHeight);
            }
            #endregion Methods
        }
    }
  2. Evgenia
    Admin
    Evgenia avatar
    1406 posts

    Posted 14 May 2013 Link to this post

    Hello Heath,

     This is indeed the best way to avoid clipping the bar series labels. You may find here some alternatives as well.

    Kind regards,
    Evgenia
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  3. DevCraft banner
Back to Top