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

Trouble Binding Pie/Doughnut Chart with Custom Legend

1 Answer 124 Views
Chart
This is a migrated thread and some comments may be shown as answers.
Seth
Top achievements
Rank 1
Seth asked on 25 Sep 2009, 12:18 AM
I have a simple object that contains a list of status names and their respective counts.  I need to display a pie or doughnut chart with the legend showing each status.  However, I'm having trouble doing this through the backend code.  My logic as follows:

 

SeriesMapping seriesMapping = new SeriesMapping();

 seriesMapping.SeriesDefinition =

new DoughnutSeriesDefinition();

 

 

 

ItemMapping itemMapping = new ItemMapping();

 

 

 itemMapping.DataPointMember =

DataPointMember.LegendLabel;

 

 

 itemMapping.FieldName =

"Status";

 

 

 seriesMapping.ItemMappings.Add(itemMapping);

 

this.charWorkOrderTypes.SeriesMappings.Add(seriesMapping);

 

 

 

this.charWorkOrderTypes.ItemsSource = myData;

 

 

 

this.charWorkOrderTypes.DefaultView.ChartArea.Extensions.Add(new CameraExtension());

I'd really appreciate some assistance with this code.  Also, ideally, I'd prefer to bind everything through the XAML.  If you could provide an example of that using my test scenario I'd greatly appreciate it.

Regards,
Seth

 

 

 

 

 

1 Answer, 1 is accepted

Sort by
0
Velin
Telerik team
answered on 29 Sep 2009, 01:08 PM
Hi Seth,

Please, find attached a small example demonstrating this behavior.
 
To achieve this behavior you need to set up two ItemMapping instances to map the YValue and LegendLabel data point members. Here is the code behind:
using System; 
using System.Collections.Generic; 
using System.Windows.Controls; 
using Telerik.Windows.Controls.Charting; 
 
namespace SilverlightApplication1 
    public partial class MainPage : UserControl 
    { 
        public MainPage() 
        { 
            InitializeComponent(); 
 
            SeriesMapping seriesMapping = new SeriesMapping(); 
 
            seriesMapping.SeriesDefinition = new DoughnutSeriesDefinition(); 
             
            ItemMapping itemMapping = new ItemMapping(); 
            itemMapping.DataPointMember = DataPointMember.LegendLabel; 
            itemMapping.FieldName = "S"
            seriesMapping.ItemMappings.Add(itemMapping); 
 
            ItemMapping itemMapping2 = new ItemMapping(); 
            itemMapping2.DataPointMember = DataPointMember.YValue; 
            itemMapping2.FieldName = "D"
            seriesMapping.ItemMappings.Add(itemMapping2); 
 
            RadChart1.SeriesMappings.Add(seriesMapping); 
            RadChart1.ItemsSource = A.Fill(5); 
        } 
    } 
 
    public class A 
    { 
        public double D { get; set; } 
        public DateTime DT { get; set; } 
        public string S { get; set; } 
 
        public static IEnumerable<A> Fill(int n) 
        { 
            DateTime now = DateTime.Now; 
            for (int i = 1; i <= n; i++) 
            { 
                A a = new A() { D = iDT = now.AddDays(i) }; 
                aa.S = a.DT.ToShortDateString(); 
                yield return a; 
            } 
        } 
    } 
 


Best wishes,
Velin
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Tags
Chart
Asked by
Seth
Top achievements
Rank 1
Answers by
Velin
Telerik team
Share this question
or