The following example shows how you can change the direction of a Flex chart’s legend by setting the direction property, as seen in the following snippet:

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

Full code after the jump.

View MXML

<?xml version="1.0"?>
<!-- http://blog.flexexamples.com/2007/11/17/changing-a-chart-legends-direction/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <mx:Script>
        <![CDATA[
            import mx.events.ItemClickEvent;

            private function toggleButtonBar_itemClick(evt:ItemClickEvent):void {
                var idx:uint = ToggleButtonBar(evt.currentTarget).selectedIndex;
                legend.direction = arr[idx];
            }
        ]]>
    </mx:Script>

    <mx:XMLListCollection id="dp">
        <mx:source>
            <mx:XMLList>
                <quote date="8/27/2007" open="40.38" close="40.81" />
                <quote date="8/24/2007" open="40.5" close="40.41" />
                <quote date="8/23/2007" open="40.82" close="40.6" />
                <quote date="8/22/2007" open="40.4" close="40.77" />
                <quote date="8/21/2007" open="40.41" close="40.13" />
                <quote date="8/20/2007" open="40.55" close="40.74" />
                <quote date="8/17/2007" open="40.18" close="40.32" />
                <quote date="8/16/2007" open="39.83" close="39.96" />
                <quote date="8/15/2007" open="40.22" close="40.18" />
                <quote date="8/14/2007" open="41.01" close="40.41" />
                <quote date="8/13/2007" open="41" close="40.83" />
                <quote date="8/10/2007" open="41.3" close="41.06" />
                <quote date="8/9/2007" open="39.9" close="40.75" />
                <quote date="8/8/2007" open="39.61" close="40.23" />
                <quote date="8/7/2007" open="39.08" close="39.42" />
                <quote date="8/6/2007" open="38.71" close="39.38" />
                <quote date="8/3/2007" open="39.47" close="38.75" />
                <quote date="8/2/2007" open="39.4" close="39.52" />
                <quote date="8/1/2007" open="40.29" close="39.58" />
            </mx:XMLList>
        </mx:source>
    </mx:XMLListCollection>

    <mx:Array id="arr">
        <mx:String>horizontal</mx:String>
        <mx:String>vertical</mx:String>
    </mx:Array>

    <mx:ApplicationControlBar dock="true">
        <mx:ToggleButtonBar id="toggleButtonBar"
                dataProvider="{arr}"
                selectedIndex="0"
                itemClick="toggleButtonBar_itemClick(event);" />
    </mx:ApplicationControlBar>

    <mx:LineChart id="lineChart"
            showDataTips="true"
            dataProvider="{dp}"
            width="100%"
            height="100%">

        <!-- series filters -->
        <mx:seriesFilters>
            <mx:Array />
        </mx:seriesFilters>

        <!-- vertical axis -->
        <mx:verticalAxis>
            <mx:LinearAxis baseAtZero="false"
                    title="Price" />
        </mx:verticalAxis>

        <!-- horizontal axis -->
        <mx:horizontalAxis>
            <mx:CategoryAxis id="ca"
                    categoryField="@date"
                    title="Date" />
        </mx:horizontalAxis>

        <!-- horizontal axis renderer -->
        <mx:horizontalAxisRenderers>
            <mx:AxisRenderer axis="{ca}"
                    canDropLabels="true" />
        </mx:horizontalAxisRenderers>

        <!-- series -->
        <mx:series>
            <mx:LineSeries yField="@close"
                    form="curve"
                    displayName="Close" />
            <mx:LineSeries yField="@open"
                    form="curve"
                    displayName="Open" />
        </mx:series>
    </mx:LineChart>

    <mx:ControlBar>
        <mx:Legend id="legend"
                dataProvider="{lineChart}"
                direction="horizontal" />
    </mx:ControlBar>

</mx:Application>

View source is enabled in the following example.

 
Tagged with:
 
About The Author

Peter deHaan

Peter deHaan currently works for Adobe on the Flex SDK QA team. While not working on Flex, Flash, and ColdFusion applications, Peter enjoys making up bios and writing in 3rd person. Peter's rarely updated blog can be found at blogs.adobe.com/pdehaan/, actionscriptexamples.com, airexamples.com, and coldfusionexamples.com.

0 Responses to Changing a chart legend’s direction

  1. Bill Jones says:

    For the love of god is there an easy way to change the font weight in the legend?

  2. peterd says:

    Bill Jones,

    It appears you can use an <mx:Style /> block and set the fontWeight style on the LegendItem selector:

    <mx:Style>
        LegendItem {
            fontWeight: normal;
        }
    </mx:Style>
    

    I’ll file a Flex bug for this as it seems you *should* be able able to set that style on the <mx:Legend /> tag or on the Legend CSS selector.

    Peter

    Bug filed: http://bugs.adobe.com/jira/browse/FLEXDMV-1902

    New entry posted: “Setting the font weight on a charting Legend control in Flex”

  3. Nitin Joshi says:

    Hi,

    I am using AxisRenderer’s canDropLabels property to drop overlapping labels,
    is there a way that I can specify to drop every third label ?

    please help,

    thanks,

    Nitin.

  4. Seb says:

    Hi,
    I used the direction tag to display my legend vertically but because it contains lots of items it is rendered on 2 columns.
    Do you know how to force the legend to be rendered on one column (the legend is in a Canvas and I would like the Canvas to show a vertical scrollbar)?

    thanks

    Seb

  5. Peter deHaan says:

    Seb,

    Try setting an explicit width/height on the parent Canvas.

    Peter

Leave a Reply

Your email address will not be published.

You may 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