The following example shows how you can set a custom label function on a Flex PieChart control by setting the labelFunction property on the PieSeries object.
Full code after the jump.
<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/09/26/setting-a-custom-label-function-on-a-piechart-in-flex/ -->
<mx:Application name="PieSeries_labelFunction_test"
xmlns:mx="http://www.adobe.com/2006/mxml"
layout="vertical"
verticalAlign="middle"
backgroundColor="white">
<mx:Script>
<![CDATA[
import mx.utils.StringUtil;
private function pieSeries_labelFunc(item:Object, field:String, index:Number, percentValue:Number):String {
return StringUtil.substitute("{0} ({1}%)",
item.@label,
percentValue.toFixed(1));
}
]]>
</mx:Script>
<mx:XMLListCollection id="dp">
<mx:source>
<mx:XMLList>
<product label="Product 1" data="3" />
<product label="Product 2" data="1" />
<product label="Product 3" data="4" />
<product label="Product 4" data="1" />
<product label="Product 5" data="5" />
<product label="Product 6" data="9" />
</mx:XMLList>
</mx:source>
</mx:XMLListCollection>
<mx:Panel styleName="opaquePanel"
width="100%"
height="100%">
<mx:PieChart id="pieChart"
dataProvider="{dp}"
showDataTips="false"
height="100%"
width="100%">
<mx:series>
<mx:PieSeries id="pieSeries"
field="@data"
nameField="@label"
labelPosition="callout"
labelFunction="pieSeries_labelFunc" />
</mx:series>
</mx:PieChart>
<mx:ControlBar width="100%">
<mx:Legend dataProvider="{pieChart}"
direction="horizontal"
horizontalGap="100"
width="100%" />
</mx:ControlBar>
</mx:Panel>
</mx:Application>
View source is enabled in the following example.
Due to popular demand, here is the “same” example in a more ActionScript friendly format:
<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/09/26/setting-a-custom-label-function-on-a-piechart-in-flex/ -->
<mx:Application name="PieSeries_labelFunction_test"
xmlns:mx="http://www.adobe.com/2006/mxml"
layout="vertical"
verticalAlign="middle"
backgroundColor="white"
initialize="init();">
<mx:Script>
<![CDATA[
import mx.charts.Legend;
import mx.charts.PieChart;
import mx.charts.chartClasses.Series;
import mx.charts.series.PieSeries;
import mx.collections.XMLListCollection;
import mx.containers.ControlBar;
import mx.containers.Panel;
import mx.containers.TileDirection;
import mx.utils.StringUtil;
private var dp:XMLListCollection;
private var legend:Legend;
private var panel:Panel;
private var pieChart:PieChart;
private var pieSeries:PieSeries;
private function init():void {
var productsXML:XML = <products>
<product label="Product 1" data="3" />
<product label="Product 2" data="1" />
<product label="Product 3" data="4" />
<product label="Product 4" data="1" />
<product label="Product 5" data="5" />
<product label="Product 6" data="9" />
</products>;
dp = new XMLListCollection(productsXML.product);
pieSeries = new PieSeries();
pieSeries.field = "@data";
pieSeries.nameField = "@label";
pieSeries.labelFunction = pieSeries_labelFunc;
pieSeries.setStyle("labelPosition", "callout");
pieChart = new PieChart();
pieChart.dataProvider = dp;
pieChart.showDataTips = false;
pieChart.percentWidth = 100;
pieChart.percentHeight = 100;
pieChart.series = [pieSeries];
legend = new Legend();
legend.dataProvider = pieChart;
legend.direction = TileDirection.HORIZONTAL;
legend.setStyle("horizontalGap", 100);
legend.percentWidth = 100;
var controlBar:ControlBar = new ControlBar();
controlBar.percentWidth = 100;
controlBar.addChild(legend);
panel = new Panel();
panel.styleName = "opaquePanel";
panel.percentWidth = 100;
panel.percentHeight = 100;
panel.addChild(pieChart);
panel.addChild(controlBar);
addChild(panel);
}
private function pieSeries_labelFunc(item:Object, field:String, index:Number, percentValue:Number):String {
return StringUtil.substitute("{0} ({1}%)",
item.@label,
percentValue.toFixed(1));
}
]]>
</mx:Script>
</mx:Application>




0 Responses to “Setting a custom label function on a PieChart in Flex”
Leave a Reply