Multiple pie chart labels

4 posts, 0 answers
  1. Christopher
    Christopher avatar
    5 posts
    Member since:
    Jul 2012

    Posted 05 Jul 2012 Link to this post

    Hi,

    I'm trying to display multiple labels simultaneously next to each other on a pie chart. I found an old thread that made reference to this workaround. However, I'm having trouble implementing this strategy, because it seems the DataPoint is always {0, 0, 0, 0}. How can this be done? Thanks!
  2. Victor
    Admin
    Victor avatar
    1351 posts

    Posted 09 Jul 2012 Link to this post

    Hi Chris,

    Thanks for writing.
    Please explain in more detail about your scenario. By "the DataPoint is always {0,0,0,0}" do you mean that the point's LayoutSlot is {0,0,0,0}? The thread your linked to seems to contain a working example. Can you please send a sample application that demonstrates you scenario. This way I can debug/tweak it and help you further.
    I am looking forward to your reply.

    Greetings,
    Victor
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  3. DevCraft banner
  4. Christopher
    Christopher avatar
    5 posts
    Member since:
    Jul 2012

    Posted 09 Jul 2012 Link to this post

    Hi Victor,

    This appears to be a bug in RadPieChart, as I've used this same approach with success in a RadCartesianChart. The RadPieChart I'm using is defined as follows:

    <telerik:RadPieChart>
        <telerikChart:PieSeries ValueBinding="Value">
            <telerikChart:PieSeries.LabelDefinitions>
                <telerikChart:ChartSeriesLabelDefinition>
                    <telerikChart:ChartSeriesLabelDefinition.Strategy>
                        <core:PieChartLabelStrategy />
                    </telerikChart:ChartSeriesLabelDefinition.Strategy>
                </telerikChart:ChartSeriesLabelDefinition>
            </telerikChart:PieSeries.LabelDefinitions>
        </telerikChart:PieSeries>
    </telerikChart:RadPieChart>

    And PieChartLabelStrategy can be anything as simple as:

    public class PieChartLabelStrategy : ChartSeriesLabelStrategy
    {
        public override LabelStrategyOptions Options
        {
            get
            {
                return LabelStrategyOptions.Arrange;
            }
        }
      
        public override RadRect GetLabelLayoutSlot(DataPoint point, FrameworkElement visual, int labelIndex)
        {
            return base.GetLabelLayoutSlot(point, visual, labelIndex);
        }
    }

    Putting a breakpoint on the call to the base method shows that point.LayoutSlot is always {0,0,0,0}.

    Any help you can provide is appreciated! Thanks!
  5. Victor
    Admin
    Victor avatar
    1351 posts

    Posted 12 Jul 2012 Link to this post

    Hi Christopher,

    Thank you for writing again.
    Please have a look at this implementation of the layout strategy. You can use it as a basis for further tweaking.

    public partial class MainPage : PhoneApplicationPage
        {
            public MainPage()
            {
                InitializeComponent();
                this.chart.Series[0].ItemsSource = new int[] { 1,1,1,1 };
     
                PieChartLabelStrategy strategy = new PieChartLabelStrategy(this.chart);
                ChartSeriesLabelDefinition definition = new ChartSeriesLabelDefinition();
                definition.Strategy = strategy;
     
                this.chart.Series[0].LabelDefinitions.Add(definition);
            }
        }
     
        public class PieChartLabelStrategy : ChartSeriesLabelStrategy
        {
            RadPieChart chart;
            public PieChartLabelStrategy(RadPieChart chart)
            {
                this.chart = chart;
            }
     
            public override LabelStrategyOptions Options
            {
                get
                {
                    return LabelStrategyOptions.Arrange;
                }
            }
     
            public override RadRect GetLabelLayoutSlot(DataPoint point, FrameworkElement visual, int labelIndex)
            {
                PieDataPoint piePoint = (PieDataPoint)point;
     
                IChartView view = this.chart as IChartView;
                Point center = new Point(view.ViewportWidth / 2, view.ViewportHeight / 2);
                double radius = (Math.Min(view.ViewportWidth , view.ViewportHeight) / 2) * 0.5;
                visual.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
                Size pointSize = new Size(visual.ActualWidth, visual.ActualHeight);
     
                RadRect result = RadRect.Empty;
                double angle = Math.Round(piePoint.StartAngle + piePoint.SweepAngle / 2) * 0.0174532925;
                result.X = (center.X + radius * Math.Cos(angle)) - pointSize.Width / 2;
                result.Y = (center.Y + radius * Math.Sin(angle)) - pointSize.Height / 2;
                result.Width = pointSize.Width;
                result.Height = pointSize.Height;
     
                return result;
            }
        }

    <telerikChart:RadPieChart x:Name="chart"
                              Palette="Warm">
        <telerikChart:PieSeries ValueBinding="Value"/>
    </telerikChart:RadPieChart>

    Please write again if you need further assistance.

    Regards,
    Victor
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Back to Top