Customizing radial strokes in a Flex PieChart control

by Peter deHaan on November 10, 2007

The following example shows how you can customize the radial stroke in a Flex PieChart control by using the PieSeries class’s radialStroke style and the mx.graphics.Stroke class, as seen in the following snippet:

<mx:PieChart dataProvider="{dp.product}" height="250" width="100%">
    <mx:series>
        <mx:PieSeries id="pieSeries" field="@data">
            <mx:radialStroke>
                <mx:Stroke color="black" weight="2" />
            </mx:radialStroke>
            <mx:filters>
                <mx:Array />
            </mx:filters>
        </mx:PieSeries>
    </mx:series>
</mx:PieChart>

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2007/11/10/customizing-radial-strokes-in-a-flex-piechart-control/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <mx:Script>
        <![CDATA[
            import mx.graphics.Stroke;

            private function setStroke(evt:Event):void {
                var stroke:Stroke = new Stroke();
                stroke.color = colorPicker.selectedColor;
                stroke.weight = slider.value;
                stroke.caps = comboBox.selectedItem.label;
                pieSeries.setStyle("radialStroke", stroke);
            }
        ]]>
    </mx:Script>

    <mx:XML id="dp">
        <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>
    </mx:XML>

    <mx:ApplicationControlBar dock="true">
        <mx:Form styleName="plain">
            <mx:FormItem label="color:">
                <mx:ColorPicker id="colorPicker"
                        change="setStroke(event);" />
            </mx:FormItem>
            <mx:FormItem label="weight:">
                <mx:HSlider id="slider"
                        minimum="0"
                        maximum="10"
                        value="0"
                        liveDragging="true"
                        snapInterval="1"
                        tickInterval="1"
                        change="setStroke(event);" />
            </mx:FormItem>
            <mx:FormItem label="caps:">
                <mx:ComboBox id="comboBox"
                        change="setStroke(event);">
                    <mx:dataProvider>
                        <mx:Array>
                            <mx:Object label="none" />
                            <mx:Object label="round" />
                            <mx:Object label="square" />
                        </mx:Array>
                    </mx:dataProvider>
                </mx:ComboBox>
            </mx:FormItem>
        </mx:Form>
    </mx:ApplicationControlBar>

    <mx:PieChart id="pieChart"
            dataProvider="{dp.product}"
            height="100%"
            width="100%">
        <mx:series>
            <mx:PieSeries id="pieSeries"
                    field="@data"
                    labelPosition="callout">
                <mx:radialStroke>
                    <mx:Stroke color="black"
                            weight="0" caps="none" />
                </mx:radialStroke>
                <mx:filters>
                    <mx:Array />
                </mx:filters>
                <mx:perWedgeExplodeRadius>
                    <mx:Array>
                        <mx:Number>0.2</mx:Number>
                    </mx:Array>
                </mx:perWedgeExplodeRadius>
            </mx:PieSeries>
        </mx:series>
    </mx:PieChart>

</mx:Application>

View source is enabled in the following example.

{ 2 comments… read them below or add one }

RBKB April 24, 2008 at 3:26 am

Thank you for your nice tips on Pie chart. Really very usefull. I am showing a pie chart based on the dates. If there is no data for those dates, pie chart does not appear. But when I use <mx:radialStroke>, a line still appears in place of a pie chart. Any ideas on how to get rid of this line?

Thanks in advance

Reply

Timo August 10, 2009 at 7:12 am

Nice post, but is there a way to define multiple radialStrokes?
Like for example: The orange field gets an orange stroke, the green field gets a green one and so on…

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; .

Anti-Spam Protection by WP-SpamFree

Previous post:

Next post: