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

Pie Chart Series Naming

3 Answers 151 Views
Chart
This is a migrated thread and some comments may be shown as answers.
Developer
Top achievements
Rank 1
Developer asked on 05 Dec 2008, 04:28 PM
Is there currently a way to label the series on a pie chart from a data field ?

3 Answers, 1 is accepted

Sort by
0
Giuseppe
Telerik team
answered on 08 Dec 2008, 04:54 PM
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.
0
Jon Masters
Top achievements
Rank 1
answered on 12 Mar 2009, 04:45 PM
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.

0
Giuseppe
Telerik team
answered on 16 Mar 2009, 02:36 PM
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.
Tags
Chart
Asked by
Developer
Top achievements
Rank 1
Answers by
Giuseppe
Telerik team
Jon Masters
Top achievements
Rank 1
Share this question
or