The following example shows how you can alternate background row colors in a LineSeries chart in Flex by setting the horizontalAlternateFill style in the GridLines object, as seen in the following snippet:
<mx:LineChart showDataTips="true" dataProvider="{dp}">
<mx:backgroundElements>
<mx:GridLines>
<mx:horizontalAlternateFill>
<mx:SolidColor color="haloSilver" alpha="0.25" />
</mx:horizontalAlternateFill>
</mx:GridLines>
</mx:backgroundElements>
</mx:LineChart>
Full code after the jump.
<?xml version="1.0"?>
<!-- http://blog.flexexamples.com/2007/11/15/alternating-background-row-colors-in-a-flex-lineseries-chart/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="vertical"
verticalAlign="middle"
backgroundColor="white">
<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:LineChart id="lineChart"
showDataTips="true"
dataProvider="{dp}"
width="100%"
height="100%">
<mx:backgroundElements>
<mx:GridLines>
<mx:horizontalAlternateFill>
<mx:SolidColor color="haloSilver" alpha="0.25" />
</mx:horizontalAlternateFill>
</mx:GridLines>
</mx:backgroundElements>
<!-- 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="@open" form="curve" displayName="Open" />
</mx:series>
</mx:LineChart>
</mx:Application>
View source is enabled in the following example.
You can also change the horizontalAlternateFill style using CSS, as seen in the following snippet.
<mx:Style>
GridLines {
horizontalAlternateFill: haloSilver;
}
</mx:Style>
Note that when using CSS, you set the horizontalAlternateFill style to a color name, not a SolidColor object.
If you want to change the horizontalAlternateFill style using ActionScript, you can use one of the following snippets:
myGridLines.setStyle("horizontalAlternateFill", "haloSilver");
import mx.graphics.SolidColor;
...
var solidColor:SolidColor = new SolidColor(0xFF0000, 0.2);
myGridLines.setStyle("horizontalAlternateFill", solidColor);
Note that you must pass a numeric value to the SolidColor constructor (or the SolidColor instance’s color property). If you want to use a named color (such as “red”, or “haloSilver”), you can use the static StyleManager.getColorName() method, as seen in the following snippet:
import mx.graphics.SolidColor;
import mx.styles.StyleManager;
...
var colorAsNumber:uint = StyleManager.getColorName("haloBlue");
var solidColor:SolidColor = new SolidColor(colorAsNumber, 0.1);
myGridLines.setStyle("horizontalAlternateFill", solidColor);
You can also alternate row colors using the horizontalFill style, or use both the horizontalFill and horizontalAlternateFill styles to set the fill colors for both odd and even rows, as seen in the following snippet:
<mx:backgroundElements>
<mx:GridLines direction="both">
<mx:horizontalFill>
<mx:SolidColor color="haloBlue" />
</mx:horizontalFill>
<mx:horizontalAlternateFill>
<mx:SolidColor color="haloSilver" />
</mx:horizontalAlternateFill>
</mx:GridLines>
</mx:backgroundElements>
The following example shows how you can alternate the row colors for even and odd numbered rows by setting both the horizontalFill and horizontalAlternateFill styles using MXML:
<?xml version="1.0"?>
<!-- http://blog.flexexamples.com/2007/11/15/alternating-background-row-colors-in-a-flex-lineseries-chart/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="vertical"
verticalAlign="middle"
backgroundColor="white">
<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:LineChart id="lineChart"
showDataTips="true"
dataProvider="{dp}"
width="100%"
height="100%">
<mx:backgroundElements>
<mx:GridLines direction="both">
<mx:horizontalFill>
<mx:SolidColor color="haloBlue" alpha="0.6" />
</mx:horizontalFill>
<mx:horizontalAlternateFill>
<mx:SolidColor color="haloSilver" alpha="0.6" />
</mx:horizontalAlternateFill>
</mx:GridLines>
</mx:backgroundElements>
<!-- 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="@open" form="curve" displayName="Open" />
</mx:series>
</mx:LineChart>
</mx:Application>
View source is enabled in the following example.




0 Responses to “Alternating background row colors in a Flex LineSeries chart”
Leave a Reply