Is there a way to change the style of Flex Chart according to the values. For example, in column chart, set green for positive value and red for negative value?
1 Answers
1
votes
To need to give your ColumnSeries a fillFunction property that is used to calulate the IFill based on the value for the given column. Something like:
<?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>