Pie Chart Series Naming

4 posts, 0 answers
  1. Developer
    Developer avatar
    1 posts
    Member since:
    Apr 2008

    Posted 05 Dec 2008 Link to this post

    Is there currently a way to label the series on a pie chart from a data field ?
  2. Giuseppe
    Admin
    Giuseppe avatar
    2363 posts

    Posted 08 Dec 2008 Link to this post

    Hi Boyd,

    You can bind the DataPoint.Label to a data field and then instruct the control to display these labels in the legend via setting the SeriesDefinition.LegendDisplayMode property to the "DataPointLabel" enumeration value like this:

    public partial class Window1 : Window 
        private static readonly Random rand = new Random(DateTime.Now.Millisecond); 
     
        protected override void OnInitialized(EventArgs e) 
        { 
            base.OnInitialized(e); 
     
            PieSeriesDefinition definition = new PieSeriesDefinition(); 
            definition.LegendDisplayMode = LegendDisplayMode.DataPointLabel; 
            this.RadChart1.DefaultSeriesDefinition = definition
     
            this.SetDataMappings("DataTable"); 
            this.RadChart1.ItemsSource = GetDataTable(); 
        } 
     
        private void SetDataMappings(string seriesLabel) 
        { 
            RadChart1.DefaultView.ChartLegend.Header = seriesLabel
     
            SeriesMapping seriesMapping = new SeriesMapping(); 
     
            ItemMapping itemMapping = new ItemMapping(); 
            itemMapping.DataPointMember = DataPointMember.YValue; 
            itemMapping.FieldName = "DoubleData"
            seriesMapping.ItemMappings.Add(itemMapping); 
     
            ItemMapping itemMapping2 = new ItemMapping(); 
            itemMapping2.DataPointMember = DataPointMember.Label; 
            itemMapping2.FieldName = "StringData"
            seriesMapping.ItemMappings.Add(itemMapping2); 
     
            this.RadChart1.SeriesMappings.Add(seriesMapping); 
        } 
     
        public static DataTable GetDataTable() 
        { 
            DataTable dataTable = new DataTable(); 
            dataTable.Columns.Add("DoubleData", typeof(double)); 
            dataTable.Columns.Add("StringData", typeof(string)); 
     
            for (int index = 0; index < rand.Next(5, 10); index++) 
            { 
                DataRow dataRow = dataTable.NewRow(); 
                dataRow["DoubleData"] = rand.Next(0, 100); 
                dataRow["StringData"] = string.Format("CustomItem {0}", index); 
                dataTable.Rows.Add(dataRow); 
            } 
     
            return dataTable; 
        } 


    Hope this helps.


    Kind regards,
    Manuel
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  3. Jon Masters
    Jon Masters avatar
    4 posts
    Member since:
    Sep 2008

    Posted 12 Mar 2009 Link to this post

    Has anyone gotten this code to work?

    I've tried and i'm still getting the default labels instead of the labels defined in the DataTable.

  4. Giuseppe
    Admin
    Giuseppe avatar
    2363 posts

    Posted 16 Mar 2009 Link to this post

    Hello Jon Masters,

    You just need to replace the DataPointMember.Label mapping to point to DataPointMember.LegendLabel in order to achieve the desired functionality with the latest version of the control (there is now separation between the series item labels and the legend item labels):

    ItemMapping itemMapping2 = new ItemMapping(); 
    itemMapping2.DataPointMember = DataPointMember.LegendLabel; 
    itemMapping2.FieldName = "StringData"
    seriesMapping.ItemMappings.Add(itemMapping2); 


    Hope this helps.


    Greetings,
    Manuel
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
Back to Top