This is a migrated thread and some comments may be shown as answers.

Horizontal Bar Label Clipping Issue

1 Answer 34 Views
ChartView
This is a migrated thread and some comments may be shown as answers.
Heath Morris
Top achievements
Rank 2
Heath Morris asked on 09 May 2013, 06:51 PM
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
    }
}

1 Answer, 1 is accepted

Sort by
0
Evgenia
Telerik team
answered on 14 May 2013, 01:25 PM
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.

Tags
ChartView
Asked by
Heath Morris
Top achievements
Rank 2
Answers by
Evgenia
Telerik team
Share this question
or