The following example shows how you can customize the column width in a Flex ColumnChart control by setting the columnWidthRatio style, as seen in the following snippet:

<mx:ColumnChart id="columnChart" columnWidthRatio="0.85">

    // ...

</mx:ColumnChart>

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2007/11/24/changing-the-default-column-width-ratio-of-a-columnchart-control-in-flex/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <mx:Script>
        <![CDATA[
            import mx.charts.chartClasses.IAxis;

            private function linearAxis_labelFunc(item:Object, prevValue:Object, axis:IAxis):String {
                return numberFormatter.format(item);
            }
        ]]>
    </mx:Script>

    <mx:NumberFormatter id="numberFormatter" precision="3" />

    <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:Label text="columnWidthRatio:" />
        <mx:HSlider id="slider"
                minimum="0.1"
                maximum="0.9"
                value="0.1"
                liveDragging="true"
                snapInterval="0.1"
                tickInterval="0.1" />
    </mx:ApplicationControlBar>

    <mx:ColumnChart id="columnChart"
            showDataTips="true"
            dataProvider="{arrColl}"
            columnWidthRatio="{slider.value}"
            width="100%"
            height="100%">

        <mx:horizontalAxis>
            <mx:CategoryAxis id="ca"
                    categoryField="name" />
        </mx:horizontalAxis>

        <mx:verticalAxis>
            <mx:LinearAxis baseAtZero="false"
                    minimum="0.200"
                    maximum="0.310"
                    labelFunction="linearAxis_labelFunc" />
        </mx:verticalAxis>

        <mx:horizontalAxisRenderers>
            <mx:AxisRenderer axis="{ca}"
                    canDropLabels="false" />
        </mx:horizontalAxisRenderers>

        <mx:series>
            <mx:ColumnSeries id="columnSeries"
                    xField="name"
                    yField="avg"
                    displayName="avg" />
        </mx:series>

        <mx:seriesFilters>
            <mx:Array />
        </mx:seriesFilters>

    </mx:ColumnChart>

</mx:Application>

View source is enabled in the following example.

You can also set the columnWidthRatio style using CSS, as shown in the following snippet:

<mx:Style>
    ColumnChart {
        columnWidthRatio: 0.85;
    }
</mx:Style>

Or, you can set the columnWidthRatio style using ActionScript, as seen in the following snippet:

columnChart.setStyle("columnWidthRatio", 0.85);
 
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.

4 Responses to Changing the default column width ratio of a ColumnChart control in Flex

  1. mozz says:

    OK, but how would you create another serie in the chart with a different width?

  2. David says:

    Is it possible to have columns of varying widths within the same Flex app.
    I.e., let’s say I have a columnChart with two columns, A and B.
    Can I have A be twice as wide as B? If so, how? If not, why?

  3. Peter deHaan says:

    David,

    I’m sure it’s possible, but you may have to extend a class and add some code. Unfortunately I am not intimately familiar with the Charting components. You may be better off asking on somewhere like the FlexCoders mailing list.

    Regards,
    Peter

  4. Dwayne Henderson says:

    If one sets columnWidthRatio=”1″, the space between the columns (and the X axis) will disappear. How does one remedy this?

    –Dwayne

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