弹性图表:更改正值和负值的样式

时间:2009-07-06 07:55:11

标签: flex coding-style charts

有没有办法根据值更改Flex Chart的样式。 例如,在柱形图中,将绿色设置为正值,将红色设置为负值?

1 个答案:

答案 0 :(得分:1)

需要为ColumnSeries提供fillFunction属性,该属性用于根据给定列的值来计算IFill。类似的东西:

<?xml version="1.0"?>
<!-- Simple example to demonstrate the ColumnChart and BarChart controls. -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
    <mx:Script>
        <![CDATA[
            import mx.charts.ChartItem;
            import mx.graphics.IFill;          
            import mx.collections.ArrayCollection;

            private function fillFunction (item:ChartItem, index:Number):IFill
            {  
                if(item.item.Gold > 0)
                {
                    return new SolidColor(0x00FF00);
                } else {
                    return new SolidColor(0xFF0000);
                }
            }


            [Bindable]
            private var medalsAC:ArrayCollection = new ArrayCollection( [
                { Country: "USA", Gold: 35},
                { Country: "China", Gold: -5},
                { Country: "Russia", Gold: 27} ]);
        ]]>
    </mx:Script>

    <!-- Define custom colors for use as fills. -->
    <mx:SolidColor id="sc1" color="yellow" alpha=".8"/>

    <!-- Define custom Strokes for the columns. -->
    <mx:Stroke id="s1" color="yellow" weight="2"/>

    <mx:Panel title="ColumnChart and BarChart Controls Example" 
        height="100%" width="100%" layout="horizontal">
        <mx:ColumnChart id="column" 
            height="100%" 
            width="100%" 
            paddingLeft="5" 
            paddingRight="5" 
            showDataTips="true" 
            dataProvider="{medalsAC}"
        >                
            <mx:horizontalAxis>
                <mx:CategoryAxis categoryField="Country"/>
            </mx:horizontalAxis>

            <mx:series>
                <mx:ColumnSeries 
                    xField="Country" 
                    yField="Gold" 
                    displayName="Gold"
                    fill="{sc1}"
                    stroke="{s1}"
                    fillFunction="{fillFunction}"
                />
            </mx:series>
        </mx:ColumnChart>

        <mx:Legend dataProvider="{column}"/>

    </mx:Panel>
</mx:Application>