Create a PieChart control from the selected columns in the ColumnChart control.
<!--
Code from Flex 4 Documentation "Using Adobe Flex 4".
This user guide is licensed for use under the terms of the Creative Commons Attribution
Non-Commercial 3.0 License.
This License allows users to copy, distribute, and transmit the user guide for noncommercial
purposes only so long as
(1) proper attribution to Adobe is given as the owner of the user guide; and
(2) any reuse or distribution of the user guide contains a notice that use of the user guide is governed by these terms.
The best way to provide notice is to include the following link.
To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/3.0/
-->
<!-- charts/MakeChartFromSelection.mxml -->
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:mx="library://ns.adobe.com/flex/mx" xmlns:s="library://ns.adobe.com/flex/spark"
creationComplete="initApp();" height="600">
<fx:Declarations>
<mx:SeriesInterpolate id="interpol" duration="1000"
elementOffset="0" minimumElementDuration="200" />
</fx:Declarations>
<fx:Script>
import mx.collections.ArrayCollection;
import mx.charts.chartClasses.ChartBase;
import mx.charts.ChartItem;
import mx.charts.series.items.ColumnSeriesItem;
import mx.charts.PieChart;
import mx.charts.series.PieSeries;
import mx.charts.events.ChartItemEvent;
import mx.charts.Legend;
[Bindable]
public var expenses:ArrayCollection = new ArrayCollection([
{Month:"Jan", Profit:20, Expenses:15, Amount:145},
{Month:"Feb", Profit:1, Expenses:2, Amount:60},
{Month:"Mar", Profit:15, Expenses:5, Amount:3}
]);
[Bindable]
public var expensesAC:ArrayCollection;
[Bindable]
public var newDataProviderAC:ArrayCollection;
private function resultHandler():void {
expensesAC = expenses;
}
private function initApp():void {
myColumnChart.addEventListener(ChartItemEvent.CHANGE, createNewChart);
setupPieChart();
}
private function getNewDataProvider():ArrayCollection {
newDataProviderAC = new ArrayCollection();
for (var i:int=0; i<series1.selectedItems.length; i++) {
var o:Object = new Object();
o.Month = expensesAC.getItemAt(series1.selectedIndices[i]).month;
o.Expenses = expensesAC.getItemAt(series1.selectedIndices[i]).expenses;
newDataProviderAC.addItem(o);
}
return newDataProviderAC;
}
private var newChart:PieChart;
private var newSeries:PieSeries;
[Bindable]
private var explodedPiece:Array;
private function explodePiece(e:Event):void {
explodedPiece = new Array();
explodedPiece[newSeries.selectedIndex] = .2;
newSeries.perWedgeExplodeRadius = explodedPiece;
}
private function setupPieChart():void {
newChart = new PieChart();
newChart.showDataTips = true;
newChart.selectionMode = "single";
newSeries = new PieSeries();
newSeries.field = "Expenses";
newSeries.nameField = "Month";
newSeries.setStyle("labelPosition", "callout");
newSeries.setStyle("showDataEffect", "interpol");
var newSeriesArray:Array = new Array();
newSeriesArray.push(newSeries);
newChart.series = newSeriesArray;
newChart.addEventListener(ChartItemEvent.CHANGE, explodePiece);
// Create a legend for the new chart.
var newLegend:Legend = new Legend();
newLegend.dataProvider = newChart;
p1.addElement(newChart);
p1.addElement(newLegend);
}
private function createNewChart(e:Event):void {
newChart.dataProvider = getNewDataProvider();
}
</fx:Script>
<s:layout>
<s:VerticalLayout />
</s:layout>
<s:Panel id="p1" title="Column Chart">
<s:layout>
<s:VerticalLayout />
</s:layout>
<mx:ColumnChart id="myColumnChart" height="207" width="350"
showDataTips="true" dataProvider="{expenses}"
selectionMode="multiple">
<mx:series>
<mx:ColumnSeries id="series1" yField="Expenses"
displayName="Expenses" selectable="true" />
</mx:series>
<mx:horizontalAxis>
<mx:CategoryAxis categoryField="Month" />
</mx:horizontalAxis>
</mx:ColumnChart>
</s:Panel>
</s:Application>
Related examples in the same category