15
Oct
07

Rotating labels in a Flex chart axis using the labelRotation style

The following example shows how you can rotate the vertical and horizontal axis renderer’s label by setting the labelRotation style.

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2007/10/15/rotating-labels-in-a-flex-chart-axis-using-the-labelrotation-style/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <mx:Style>
        @font-face {
            src: local("Arial");
            font-family: EmbeddedArial;
        }

        BarChart {
            font-family: EmbeddedArial;
        }
    </mx:Style>

    <mx:ArrayCollection id="arrColl">
        <mx:source>
            <mx:Array>
                <mx:Object name="R Winn" obp=".353" slg=".445" avg=".300" />
                <mx:Object name="P Feliz" obp=".290" slg=".418" avg=".253" />
                <mx:Object name="O Vizquel" obp=".305" slg=".316" avg=".246" />
                <mx:Object name="B Molina" obp=".298" slg=".433" avg=".276" />
                <mx:Object name="R Durham" obp=".295" slg=".343" avg=".218" />
            </mx:Array>
        </mx:source>
    </mx:ArrayCollection>

    <mx:ApplicationControlBar dock="true">
        <mx:Form>
            <mx:FormHeading label="labelRotation" />
            <mx:FormItem label="horizontalAxisRenderer:">
                <mx:HSlider id="horizontalLabelRotation"
                        minimum="-90"
                        maximum="90"
                        value="0"
                        liveDragging="true"
                        snapInterval="10"
                        tickInterval="10" />
            </mx:FormItem>
            <mx:FormItem label="verticalAxisRenderer:">
                <mx:HSlider id="verticalLabelRotation"
                        minimum="-90"
                        maximum="90"
                        value="0"
                        liveDragging="true"
                        snapInterval="10"
                        tickInterval="10" />
            </mx:FormItem>
        </mx:Form>

        <mx:Spacer width="100%" />

        <mx:Legend dataProvider="{barChart}"/>
    </mx:ApplicationControlBar>

    <mx:BarChart id="barChart"
             showDataTips="true"
             dataProvider="{arrColl}"
             width="100%"
             height="100%">

        <!-- vertical axis -->
        <mx:verticalAxis>
            <mx:CategoryAxis categoryField="name" title="Player"/>
        </mx:verticalAxis>

        <!-- vertical axis renderer -->
        <mx:verticalAxisRenderer>
            <mx:AxisRenderer labelRotation="{verticalLabelRotation.value}" />
        </mx:verticalAxisRenderer>

        <!-- horizontal axis renderer -->
        <mx:horizontalAxisRenderer>
            <mx:AxisRenderer labelRotation="{horizontalLabelRotation.value}" />
        </mx:horizontalAxisRenderer>

        <!-- series -->
        <mx:series>
            <mx:BarSeries id="obpSeries"
                    yField="name"
                    xField="obp"
                    displayName="obp" />
            <mx:BarSeries id="slgSeries"
                    yField="name"
                    xField="slg"
                    displayName="slg" />
            <mx:BarSeries id="avgSeries"
                    yField="name"
                    xField="avg"
                    displayName="avg" />
        </mx:series>
    </mx:BarChart>

</mx:Application>

View source is enabled in the following example.


12 Responses to “Rotating labels in a Flex chart axis using the labelRotation style”


  1. 1 Mike Rankin Oct 16th, 2007 at 9:57 am

    What we need is a way to rotate the label for the vertical axis 180 degrees. It’s upside down.

  2. 2 Tianzhen Lin Oct 16th, 2007 at 3:01 pm

    Nice to know that. I notice that font has to be embedded in order to have font rotation done correctly.

  3. 3 peterd Oct 16th, 2007 at 6:16 pm

    Tianzhen Lin,

    Yeah, the general rule of thumb in Flash/Flex is that if a font is going to be rotated or faded, it needs to be embedded.

    Peter

  4. 4 peterd Oct 16th, 2007 at 6:18 pm

    Mike,

    Can you please file a bug in the public Flex bug base, http://bugs.adobe.com/flex/

    Thanks,

    Peter

  5. 5 John C. Bland II Oct 16th, 2007 at 7:36 pm

    Tianzhen, you can add a filter to a textfield and rotate without embedding.

  6. 6 Ginger Booth Feb 22nd, 2008 at 1:17 pm

    Mike,

    Ooh, I found it! (Wasn’t easy…) They’ve already fixed the flipped-180 vertical axis title thing, released already in my version (Flex 3.0 beta). You have to add an axis renderer though:

    Why that isn’t the default, I do not know.

    Cheers,
    Ginger

  7. 7 Ginger Booth Feb 22nd, 2008 at 1:20 pm

    Hmph. Sorry about that last, posting a comment killed my inline source code.

    Basically:

    the vertical LinearAxis or whatever needs id and title fields set
    under the mx:verticalAxis block, you need an mx:verticalAxisRenderers block, with an AxisRenderer inside it
    That AxisRenderer sets axis=the linear Axis id, and verticalAxisTitleAlignment=”vertical”

  8. 8 Wang Baoyuan Jul 23rd, 2008 at 6:35 am

    LabelRotation works in in application directly, however it does not work through module. Is that a bug of flex ?

  9. 9 peterd Jul 23rd, 2008 at 8:03 am

    Wang Baoyuan,

    Can you please file a bug at http://bugs.adobe.com/flex/ and include a simple test case (if possible) and somebody can take a look.

    Also, if you can post the bug number here, a few of us can subscribe/vote on the bug.

    Thanks,
    Peter

  10. 10 Wang Baoyuan Jul 24th, 2008 at 3:04 am
  11. 11 carlos Aug 18th, 2008 at 3:16 pm

    Hi, good job btw…

    i need to display the DataTips in to the column, if some one haves the knowledge plz let me know..

  12. 12 carlos Aug 18th, 2008 at 3:20 pm

Leave a Reply

This blog is terrible at eating HTML tags. If you plan on posting code/XML, please escape your "<" characters as "&lt;" and your ">" characters as "&gt;".