Animating a Flex PieChart control’s rotation when a user clicks on an item

by Peter deHaan on November 22, 2007

in Charting, PieChart, SeriesInterpolate

In a previous example, “Rotating a Flex PieChart control when a user clicks on an item”, we looked at changing a PieChart’s rotation when the user clicked on a pie wedge. In the following example, we look at how to add a nice animation effect with some easing to make the effect a bit more smooth.

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2007/11/22/animating-a-flex-piechart-controls-rotation-when-a-user-clicks-on-an-item/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <mx:Script>
        <![CDATA[
            import mx.effects.easing.*;
            import mx.charts.series.items.PieSeriesItem;
            import mx.charts.events.ChartItemEvent;

            private function pieChart_itemClick(evt:ChartItemEvent):void {
                var item:PieSeriesItem = evt.hitData.chartItem as PieSeriesItem;
                var degrees:Number = radiansToDegrees(item.startAngle);
                var arr:Array = [];
                if (checkBox.selected) {
                    arr[item.index] = 0.2;
                }
                pieSeries.perWedgeExplodeRadius = arr;
                pieSeries.startAngle -= degrees;

                dp.refresh();
            }

            private function radiansToDegrees(radians:Number):Number {
                return radians * (180 / Math.PI);
            }
        ]]>
    </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:ApplicationControlBar dock="true">
        <mx:CheckBox id="checkBox"
                label="Use perWedgeExplodeRadius:"
                labelPlacement="left"
                selected="true" />
    </mx:ApplicationControlBar>

    <mx:PieChart id="pieChart"
            dataProvider="{dp}"
            showDataTips="true"
            itemClick="pieChart_itemClick(event);"
            height="100%"
            width="100%">
        <mx:series>
            <mx:PieSeries id="pieSeries"
                    field="@data"
                    nameField="@label">
                <mx:showDataEffect>
                    <mx:SeriesInterpolate duration="1500"
                            easingFunction="{Elastic.easeOut}" />
                </mx:showDataEffect>
                <mx:filters>
                    <mx:DropShadowFilter />
                </mx:filters>
            </mx:PieSeries>
        </mx:series>
    </mx:PieChart>

    <mx:Legend dataProvider="{pieChart}" direction="horizontal" />

</mx:Application>

View source is enabled in the following example.

But remember kids, just because you can add easing, doesn’t mean you always should! ;)

{ 9 comments… read them below or add one }

1 Jonathan November 23, 2007 at 5:40 am

Very Cool! I did notice that depending on where you click and after the animation the datatip is not updated and hovers over the wrong piece of pie.

Neat effect though.

Reply

2 fidi February 20, 2008 at 3:41 am

Amazing, i have a request, beside the source code can you give me the xml files ? i mean the data of the chart.

Reply

3 peterd February 20, 2008 at 8:35 am

fidi,

There aren’t any external XML files. The chart uses an mx:XMLListCollection as a data provider. All the required code should either be posted above, or you can right-click the Flex SWF and select “View Source” from the context menu.

Peter

Reply

4 Raghavendra June 5, 2008 at 2:51 am

Dear peterd,

Its very good example which helped me a lot and I am a very beginner to this and i started doing this flex charts using php mysql and outputting this as xml data but it could not able to do that could u help me out on this….

Reply

5 David Beckwith July 27, 2008 at 4:09 am

Hi,
I love this example. Thanks a lot! However, I have a question. When I changed the data provider to be XML from a server call, dp.refresh() was no longer defined. What’s the equivalent of refresh() for XML? I suspect that some redisplay event is being called. And now that I can’t redisplay my data, the piechart is not spinning anymore.
Thank you,
David :)

Reply

6 Alex December 7, 2008 at 6:34 pm

@ David: Just call the dataprovider in the script.

here an example:

piechart0.dataProvider=e.result;

This line i call after my remotedataobject is loaded.

in remoteobject:
<mx:method name=”getStatistics” result=”getStatisticresults( event );” />

The Function getStatisticresults:

private function getStatisticresults(e:ResultEvent):void {
piechart0.dataProvider=e.result;
}
—–
showDataEffect=”seriesInterpolate” help you to see a nice reload-animation (<mx:PieSeries …)

<mx:SeriesInterpolate id=”seriesInterpolate” duration=”1500″ />

Best Regards from Austria
Alex

Reply

7 anand singh January 1, 2009 at 11:58 pm

thank to you

I have find a solution for pie chart by your help.

Reply

8 Vitoto March 27, 2009 at 8:39 pm

Hi, is posible show simbol % inside the PIE area.

Example 10%, 50%, etc .. ??

Reply

9 Kiran April 17, 2009 at 8:54 am

Hi Peter , Excellent
It saved my time a lot .
But If you have some time , Could you please provide the same impact for the combo box event . I mean to say I have a combobox with Product 1, Product 2 …. . If I select any product , it should get react exactly the same whatever you did.

Thanks again for your help
Regards
Kiran

Reply

Leave a Comment

Sorry, this blog is terrible at eating HTML comments.
If you're pasting any HTML/XML/MXML code, you need to convert your < characters to &lt; and your > characters to &gt; .

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Anti-Spam Protection by WP-SpamFree

Previous post:

Next post: