PDA

View Full Version : How to show Legend Label for Pie chart


salim_designer
11-19-2009, 07:39 AM
I am working with a pie chart and I want to show Legend also. I can see the legends in the bottom of the chart but there is no label for legends. I can see only three different color boxes (without label on it). How can I show the label.

Here is the code
<?xml version="1.0"?>
<!-- Simple example to demonstrate the PieChart control. -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">

<mx:Script>
<![CDATA[

import mx.collections.ArrayCollection;

[Bindable]
private var medalsAC:ArrayCollection = new ArrayCollection( [
{ Country: "USA", Gold: 35, Silver:39, Bronze: 29 },
{ Country: "China", Gold: 32, Silver:17, Bronze: 14 },
{ Country: "Russia", Gold: 27, Silver:27, Bronze: 38 } ]);

private function displayGold(data:Object, field:String, index:Number, percentValue:Number):String {
var temp:String= (" " + percentValue).substr(0,6);
return data.Country + ": " + '\n' + "Total Gold: " + data.Gold + '\n' + temp + "%";
}
]]>
</mx:Script>

<mx:Panel title="Olympics 2004 Medals Tally Panel" height="340" width="340">

<mx:PieChart id="chart2" height="100%" width="100%"
paddingRight="5" paddingLeft="5"
showDataTips="true" dataProvider="{medalsAC}">

<mx:series>
<mx:PieSeries labelPosition="callout" field="Gold" labelFunction="displayGold">
<mx:calloutStroke>
<mx:Stroke weight="0" color="0x888888" alpha="1.0"/>
</mx:calloutStroke>
<mx:radialStroke>
<mx:Stroke weight="0" color="#FFFFFF" alpha="0.20"/>
</mx:radialStroke>
<mx:stroke>
<mx:Stroke color="0" alpha="0.20" weight="2"/>
</mx:stroke>
</mx:PieSeries>
</mx:series>
</mx:PieChart>
<mx:Legend dataProvider="{chart2}" autoLayout="true" width="100%"
height="100%" labelPlacement="right" markerWidth="10"/>

</mx:Panel>
</mx:Application>

ronniecr7
11-19-2009, 09:53 AM
Hi Saleem...
U have to specify the "dataprovider" for the legend so that the labels are seen....

salim_designer
11-19-2009, 10:27 AM
Please see the code that I have attached. "dataprovider" is already there

ronniecr7
11-19-2009, 11:33 AM
My bad salim :)
Check dis link... It shud help you....
http://northam.blogspot.com/2009/05/flex-showing-data-in-legend-of-pie.html

ronniecr7
11-19-2009, 11:54 AM
Hi salim,
Try using the "Direction parameter as well in the "Legend" tag.......
That shud make it work.

salim_designer
11-19-2009, 01:00 PM
Thank you so much! This link helped me a lot.