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

Pie Chart Databinding

9 Answers 422 Views
Chart (Obsolete)
This is a migrated thread and some comments may be shown as answers.
rwozniak
Top achievements
Rank 1
rwozniak asked on 24 Sep 2008, 07:08 PM
I'm binding my Pie chart to a list of objects that have two properties, Name and RowCount. The pie image renders correctly in that the slices are divided up based on the value of the RowCount property. However, the legend labels say Item 1, Item 2, etc.

How do I get it to use the value of the Name property for the legend label text?

Thanks,
Ross

9 Answers, 1 is accepted

Sort by
0
Ves
Telerik team
answered on 26 Sep 2008, 12:40 PM
Hi Ross,

You can wire ItemDataBound event and populate each item's Name property. The value set to this property will appear in the legend. Here is an example:

protected void RadChart1_ItemDataBound(object sender, ChartItemDataBoundEventArgs e) 
    e.SeriesItem.Name = ((MyObject)e.DataItem).Name; 
 

Hope this helps.

All the best,
Ves
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
rwozniak
Top achievements
Rank 1
answered on 26 Sep 2008, 04:41 PM
Thanks Ves.

I was able to get it to work, but unfortunately not in quite the same manner as your example. When binding to a list of objects, e.DataItem is a DataRowView, so I can't cast it to my object type. Not sure why (since I'm not binding to a DataTable), but the code you posted threw an exception for that reason.

I was able to get it to work via the following, but this is kinda messy.

protected void RadChart1_ItemDataBound(object sender, ChartItemDataBoundEventArgs e)
{
        DataRowView drv = (DataRowView)e.DataItem;
        e.SeriesItem.Name = drv.Row["Name"].ToString();
}

What I'd really like to do is achieve the same effect by settting one property on the chart prior to calling DataBind(). Roughly, something like this:

protected override void PrepareChart()
{
        RadChart1.DataSource = MyApiCall();
        RadChart1.LegendLabelField = "Name";
        RadChart1.DataBind();
}

Ross
0
Ves
Telerik team
answered on 26 Sep 2008, 04:45 PM
Hi Ross,

Thanks for the suggestion. Unfortunately, this is not possible at the moment. I will forward your request to our developers, so they will consider it for implementation in the future. Your Telerik points have been updated.

All the best,
Ves
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
MikeK
Top achievements
Rank 1
answered on 02 Oct 2008, 01:33 PM
I have to thank both Ross and Telerik for this posting. I just ran across the same issue and was able to do it in seconds with Ross' followup.

For Telerik - I'd second the request for the ability to set properties as suggested.

I would also like to add a request or more information on how I could append or swap the text from the series.item.name with the DefaultLabelValue?

What I would like to do is to not display the legend, but rather have the name of the series item display by the pie chart in addition to the percentage. Example:

Right now it's showing 53% and 47%, but the legend shows Not Seen 53% and Seen 47%.  How do I go about having this combined and shown on the chart rather than in the legend?  If I change defaultlabelvalue to #ITEM it shows Item 1 and Item 2, not very useful to the user.
0
Ves
Telerik team
answered on 03 Oct 2008, 06:39 AM
Hi Michael,

You can wire ItemDataBound event and set the item's Name. Then, the "#NAME" wildcard in the DefaultLabelValue will be replaced by the actual item name. Please, check this blog post for additional details.

Regards,
Ves
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
MikeK
Top achievements
Rank 1
answered on 03 Oct 2008, 02:56 PM
    Protected Sub RadChart1_ItemDataBound(ByVal sender As Object, ByVal e As Telerik.Charting.ChartItemDataBoundEventArgs) Handles RadChart1.ItemDataBound
        Dim item As Telerik.Charting.ChartSeriesItem = e.SeriesItem
        Dim dataItem As Data.DataRowView = CType(e.DataItem, Data.DataRowView)
        item.Label.TextBlock.Text = (dataItem("ClientInfo").ToString + (" " + dataItem("TotalCount").ToString))
    End Sub

That worked great, included the item name with the value. But it dropped my % calculation. How do I make this give me the values as percent?
0
Ves
Telerik team
answered on 03 Oct 2008, 03:38 PM
Hi Michael,

You can just keep the "#%" wildcard in the label, it will be replaced later with the actual value:

item.Label.TextBlock.Text = (dataItem("ClientInfo").ToString + (" " + dataItem("TotalCount").ToString)) + "(#%)"

Sincerely,
Ves
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Tony Ser
Top achievements
Rank 1
answered on 27 Aug 2010, 07:43 PM
[edit: I figured out the solution, see my post below this one]

I am also facing this issue, and it seems silly to me that so much work has to go into getting the Labels to be displayed instead of "Item 0", "item 1"... perhaps I'm missing something.  The pie chart legend obviously knows which colors map to with item, so why wouldn't it also know the label for that item.

I have tried many combinations of setting LegendDisplayMode, SmartLabelsEnabled and UseAutoGeneratedItems.  I really don't want to have to write a separate event handler for the ItemDataBound event, or setup custom label items since then I also have to deal with setting up correct Style palettes so that the items have the correct colors.

This is how I'm setting up my Pie Series:

// Create my Pie series definition
SeriesMapping sMap = new SeriesMapping();
sMap.ChartAreaName = "_ChartArea"// Custom layout chart area
sMap.CollectionIndex = SeriesIndex;
sMap.LegendLabel = sInfo.Title;
 
PieSeriesDefinition PieDefintion = new PieSeriesDefinition();
PieDefintion.LegendDisplayMode = LegendDisplayMode.DataPointLabel;
sMap.SeriesDefinition = PieDefintion;
 
// I tried mapping my "Label" item variable to both the DataPointMemeber Label and XCategory, in hopes that it would be picked up by the above LegendDisplayMode.
sMap.ItemMappings.Add(new ItemMapping("Label", DataPointMember.Label));
sMap.ItemMappings.Add(new ItemMapping("Label", DataPointMember.XCategory));
sMap.ItemMappings.Add(new ItemMapping("Value", DataPointMember.YValue));
 
// Add mapping
_RadChart.SeriesMappings.Add(sMap);
 
// My data is Updated at a later time, this code is just the series setup.

But I am still faced with "Item 0", "Item 1" etc... in my legend.  There must be an elegant way to handle this that I am missing.

Thanks!
0
Tony Ser
Top achievements
Rank 1
answered on 27 Aug 2010, 07:59 PM
It always seems as soon as I give up and post, I find the answer ;)

If I change
  sMap.ItemMappings.Add(new ItemMapping("Label", DataPointMember.Label));
to this
  sMap.ItemMappings.Add(new ItemMapping("Label", DataPointMember.LegendLabel));

Then it all works!
Tags
Chart (Obsolete)
Asked by
rwozniak
Top achievements
Rank 1
Answers by
Ves
Telerik team
rwozniak
Top achievements
Rank 1
MikeK
Top achievements
Rank 1
Tony Ser
Top achievements
Rank 1
Share this question
or