Creating a linear gradient fill on a ColumnChart control’s column series in Flex
The following example shows how you can create a linear gradient fill for a ColumnSeries in Flex.
Full code after the jump.
<?xml version="1.0"?>
<!-- http://blog.flexexamples.com/2007/11/20/creating-a-linear-gradient-fill-on-a-columnchart-controls-column-series-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 currencyFormatter.format(item);
}
private function categoryAxis_labelFunc(item:Object, prevValue:Object, axis:CategoryAxis, categoryItem:Object):String {
var datNum:Number = Date.parse(item);
var tempDate:Date = new Date(datNum);
return dateFormatter.format(tempDate).toUpperCase();
}
]]>
</mx:Script>
<mx:DateFormatter id="dateFormatter" formatString="DD" />
<mx:CurrencyFormatter id="currencyFormatter" precision="2" />
<mx:XMLListCollection id="dp">
<mx:source>
<mx:XMLList>
<quote date="8/1/2007" open="40.29" close="39.58" />
<quote date="8/2/2007" open="39.4" close="39.52" />
<quote date="8/3/2007" open="39.47" close="38.75" />
<quote date="8/6/2007" open="38.71" close="39.38" />
<quote date="8/7/2007" open="39.08" close="39.42" />
<quote date="8/8/2007" open="39.61" close="40.23" />
<quote date="8/9/2007" open="39.9" close="40.75" />
<quote date="8/10/2007" open="41.3" close="41.06" />
<quote date="8/13/2007" open="41" close="40.83" />
<quote date="8/14/2007" open="41.01" close="40.41" />
<quote date="8/15/2007" open="40.22" close="40.18" />
<quote date="8/16/2007" open="39.83" close="39.96" />
<quote date="8/17/2007" open="40.18" close="40.32" />
<quote date="8/20/2007" open="40.55" close="40.74" />
<quote date="8/21/2007" open="40.41" close="40.13" />
<quote date="8/22/2007" open="40.4" close="40.77" />
<quote date="8/23/2007" open="40.82" close="40.6" />
<quote date="8/24/2007" open="40.5" close="40.41" />
<quote date="8/27/2007" open="40.38" close="40.81" />
</mx:XMLList>
</mx:source>
</mx:XMLListCollection>
<mx:ColumnChart id="columnChart"
showDataTips="true"
dataProvider="{dp}"
width="100%"
height="100%">
<!-- vertical axis -->
<mx:verticalAxis>
<mx:LinearAxis baseAtZero="false"
labelFunction="linearAxis_labelFunc" />
</mx:verticalAxis>
<!-- horizontal axis -->
<mx:horizontalAxis>
<mx:CategoryAxis id="ca"
categoryField="@date"
title="August 2007"
labelFunction="categoryAxis_labelFunc" />
</mx:horizontalAxis>
<!-- horizontal axis renderer -->
<mx:horizontalAxisRenderers>
<mx:AxisRenderer axis="{ca}"
canDropLabels="true" />
</mx:horizontalAxisRenderers>
<!-- series -->
<mx:series>
<mx:ColumnSeries displayName="Open"
xField="@date"
yField="@open">
<mx:fill>
<mx:LinearGradient id="linearGradient">
<mx:entries>
<mx:Array>
<mx:GradientEntry color="red"
ratio="0.0"
alpha="1.0" />
<mx:GradientEntry color="black"
ratio="1.0"
alpha="1.0" />
</mx:Array>
</mx:entries>
</mx:LinearGradient>
</mx:fill>
</mx:ColumnSeries>
</mx:series>
<!-- series filters -->
<mx:seriesFilters>
<mx:Array />
</mx:seriesFilters>
</mx:ColumnChart>
</mx:Application>
View source is enabled in the following example.
By default, the gradient goes left-to-right, if you want to create a vertical gradient or 45° gradient (or any arbitrary number), you can set the angle property in the LinearGradient tag, as seen in the following snippet:
<mx:LinearGradient id="linearGradient" angle="90">
<mx:entries>
<mx:Array>
<mx:GradientEntry color="red"
ratio="0.0"
alpha="1.0" />
<mx:GradientEntry color="black"
ratio="1.0"
alpha="1.0" />
</mx:Array>
</mx:entries>
</mx:LinearGradient>
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.
-
Add Widgets (Content Sidebar)
This is your Content Sidebar. Edit this content that appears here in the widgets panel by adding or removing widgets in the Content Sidebar area.
7 Responses to Creating a linear gradient fill on a ColumnChart control’s column series in Flex
Leave a Reply Cancel reply
-
Categories
- Accordion
- AccordionHeader
- ActionScript
- AddChild
- AdvancedDataGrid
- Alert
- alpha
- Animate
- AnimateProperties
- Application
- Application (Spark)
- ArrayCollection
- BarChart
- baseColor
- beta
- beta1
- beta2
- Bitmap
- Bitmap/BitmapData
- BitmapData
- BitmapFill
- BitmapFill (Spark)
- BitmapGraphic
- BitmapImage
- BitmapImage (Spark)
- BitmapImageResizeMode
- Border (Spark)
- BorderContainer (Spark)
- Box
- BuildInfo
- Button
- Button (Spark)
- ButtonBar
- ButtonBar (Spark)
- ByteArray
- Camera
- Charting
- CheckBox
- CheckBox (Spark)
- ClassFactory
- CollectionEvent
- Color
- ColorPicker
- ColorUtil
- ComboBox
- ComboBoxArrowSkin
- Compiler
- Component
- Component (Spark)
- Configuration
- Container
- ContextMenu
- ContextMenuEvent
- ContextMenuItem
- CSSCondition
- CSSSelector
- CSSStyleDeclaration
- CurrencyFormatter
- CursorManager
- Data Binding
- DataGrid
- DataGrid (Spark)
- DataGridColumn
- Date
- DateBase
- DateChooser
- DateField
- DateFormatter
- Debugging
- DefaultComplexItemRenderer
- DefaultTileListEffect
- DropDownList
- DropDownList (Spark)
- DropDownListButtonSkin
- DropDownListSkin
- DropShadowFilter
- E4X
- Effects
- Ellipse
- EmailValidator
- Embed
- Event
- Fade
- FileFilter
- FileReference
- fill
- Filters
- Flash
- Flash Integration
- FlashVars
- Flex 3 SDK
- Flex Builder
- Flex Builder 3
- Flex SDK
- Flex4
- FLVPlayback
- FocusManager
- FontLookup
- Fonts
- Form
- Form (Spark)
- FormHeading (Spark)
- FormItem
- FormItem (Spark)
- Forms
- FTETextField (Spark)
- FullScreen
- FullScreenEvent
- FxAnimateColor
- FxButtonBar
- FxCheckBox
- FXG
- FxHScrollBar
- FxHSlider
- FxList
- FxNumericStepper
- FxRadioButton
- FxRotate3D
- FxScroller
- FxTextArea
- FxTextInput
- FxToggleButton
- FxVScrollBar
- FxVSlider
- getStyleDeclaration()
- GradientEntry
- Graphic (Spark)
- HBox
- HDividedBox
- HGroup (Spark)
- HorizontalLayout
- HorizontalList
- HSBColor (Spark)
- HScrollBar (Spark)
- HSlider
- HSlider (Spark)
- HTML template
- ID3Info
- Image
- Image (Spark)
- ImageSnapshot
- itemRenderer
- JointStyle
- Label
- Label (Spark)
- Legend
- LegendItem
- LigatureLevel
- Line
- LinearGradientStroke
- LineScaleMode
- LinkBar
- LinkButton
- List
- List (Spark)
- Menu
- MenuBar
- Metadata
- MetadataEvent
- Model
- Mouse
- MouseCursor
- MouseEvent
- Move
- Namespace
- NavigatorContent (Spark)
- needsSWF
- NetConnection
- NetStream
- Nightly Builds
- NumberBaseRoundType
- NumberFormatter
- NumberValidator
- NumericCompare
- NumericStepper
- NumericStepper (Spark)
- ObjectProxy
- ObjectUtil
- paddingLeft
- paddingRight
- Panel
- Panel (Spark)
- Parallel
- Path
- PieChart
- PieSeries
- PieSeriesItem
- PopUpAnchor (Spark)
- PopUpButton
- PopUpManager
- ProgrammaticSkin
- ProgressBar
- PropertyChangeEvent
- QName
- RadialGradient
- RadioButton
- RadioButton (Spark)
- RadioButtonGroup
- RadioButtonGroup (Spark)
- Rect
- RegExp
- Regular Expressions
- Repeater
- RichEditableText
- RichText
- RichText (Spark)
- RichTextEditor
- Rotate
- Rotate3D (Spark)
- Scroller (Spark)
- Sequence
- setStyle()
- SimpleText
- SimpleText (Spark)
- skinClass
- Slider
- SliderEvent
- SolidColor
- SolidColorStroke
- Sort
- SortField
- Sound
- SoundEffect
- Spinner (Spark)
- SpriteVisualElement (Spark)
- StageDisplayState
- States
- StringUtil
- StringValidator
- StyleManager
- Styles
- SWFLoader
- SWFObject
- System
- SystemManager
- TabBar
- TabBar (Spark)
- TabNavigator
- TabStopFormat
- Text
- Text Layout Framework (TLF)
- TextArea
- TextArea (Spark)
- TextBox
- TextConverter
- TextEvent
- TextFlow
- TextFlowUtil
- TextFormat
- TextGraphic
- TextInput
- TextInput (Spark)
- TextLayoutFormat
- TextView
- Themes
- TileLayout
- TileList
- TileOrientation
- Timer
- TitleWindow
- TitleWindow (Spark)
- TLF
- ToggleButton (Spark)
- ToggleButtonBar
- ToolTip
- Transition
- Tree
- TruncationOptions
- UIComponent
- UIFTETextField
- Updater
- URLLoader
- URLRequest
- URLUtil
- URLVariables
- ValidationResultEvent
- Validator
- Validators
- VBox
- VDividedBox
- Vector
- VerticalLayout
- VerticalLayout (Spark)
- VGroup (Spark)
- Video
- VideoDisplay
- VideoElement
- VideoElement (Spark)
- VideoEvent
- VideoPlayer (Spark)
- VideoPlayerScrubBar
- ViewStack
- VScrollBar (Spark)
- VSlider
- VSlider (Spark)
- XML
- XMLList
- XMLListCollection
- ZipCodeValidator
- ZipCodeValidatorDomainType
- Zoom
-
Articles
- December 2010
- November 2010
- October 2010
- September 2010
- August 2010
- July 2010
- June 2010
- May 2010
- April 2010
- March 2010
- February 2010
- January 2010
- December 2009
- November 2009
- October 2009
- September 2009
- August 2009
- July 2009
- June 2009
- May 2009
- April 2009
- March 2009
- February 2009
- January 2009
- December 2008
- November 2008
- October 2008
- September 2008
- August 2008
- July 2008
- June 2008
- May 2008
- April 2008
- March 2008
- February 2008
- January 2008
- December 2007
- November 2007
- October 2007
- September 2007
- August 2007
- July 2007
-
Meta


Hey Peter,
How do I go about using gradient fills when setting the fill property via CSS? I’m trying…
var linearGradient:LinearGradient = new LinearGradient();
linearGradient.entries.push(new GradientEntry(0xF68620, 0, 1));
linearGradient.entries.push(new GradientEntry(0xF68620, 0.5, 0.25));
colSeries.setStyle(‘fill’, linearGradient);
but it doesn’t apply the gradient. Is there a different CSS property to set when using gradients?
Thanks,
Greg.
Try it,
var linearGradient:LinearGradient = new LinearGradient(); var arr:Array = []; arr.push(new GradientEntry(0xF68620, 0, 1)); arr.push(new GradientEntry(0xF68620, 0.5, 0.25)); linearGradient.angle = 90; linearGradient.entries = arr; serie.setStyle("areaFill", linearGradient);Hey Peter,
when I use
var linearGradient:LinearGradient = new LinearGradient(); var arr:Array = []; arr.push(new GradientEntry(0xF68620, 0, 1)); arr.push(new GradientEntry(0xF68620, 0.5, 0.25)); linearGradient.angle = 90; linearGradient.entries = arr; serie.setStyle("areaFill", linearGradient);,but it doesn’t work!
Any chance that we can set a different fill (non-gradient) for the legend?
Replaced ColumnChart to AreaChart and ColumnSeries to AreaSeries, and the gradient doesn’t work.
Is it possible to make area chart with gradient? If yes how?
thanx
Use:
instead of:
Thank you for the information on this post. I found it very helpful!