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
}
}