Column Chart with Item Renderer – updated
Okay, it didn’t take me long to get back and do some more fiddling with the Column Chart Item Renderer. I went ahead and changed the static ArrayCollection to a dynamic one, because I was trying to figure out something with Themes and needed a button and the button should do something. The ArrayCollection is fairly simple for this chart, so all I did was create a couple of random values from 0 to 100 and made the “date” value the counter time 10. I set it up so that you can press a button and create new data, which is helpful to see the changes to the columns.
// Create some random data for Yesterday and blank data for today
private function dataCreation():void {
currentArray.removeAll();
for(var h:int = 0; h < 4; h++) {
// Generate Random Data
var randomClose:Number = Math.floor(Math.random() * 100);
var randomOpen:Number = Math.floor(Math.random() * 100);
// create a temporary object and add the three values
var tmpObject:Object = new Object();
tmpObject.date = (h + 1) * 10;
tmpObject.close = randomClose;
tmpObject.open = randomOpen;
// insert new tmpObject
currentArray.addItem(tmpObject);
}
// Refresh the data
currentArray.refresh();
}
After that was fixed, I went ahead to modify the ItemRenderer again, but this time to use a GradientFill and a BitMapFill. I had to reduce what was defined for the rectangle, to just a Graphic item as a holder.
s:Graphic id="graphicHolder" />
And then I declared three different rectangles, one for each type of fill. Again, this was to simplify things and also to make sure I was doing the right thing for each fill. As you can see in the Gradient Fill, I had to set the three GradientEntry items to create the look I need. You could do this all dynamically, but I don’t think most people will try different Gradients for each column. Which is why my Gradient fill only changes the Start, Mid and End color of the Gradient.
<fx:Declarations> <s:Rect id="imageRect"> <s:stroke> <s:SolidColorStroke id="rectImageStroke" /> </s:stroke> <s:fill> <s:BitmapFill id="imageRectFill" fillMode="repeat" scaleX=".70" scaleY=".70" smooth="true" /> </s:fill> </s:Rect> <s:Rect id="gradientRect"> <s:stroke> <s:SolidColorStroke id="rectLinGradStroke" /> </s:stroke> <s:fill> <s:LinearGradient id="rectLinGrad"> <s:entries> <s:GradientEntry id="linGradColorStart" ratio="0" alpha=".5" /> <s:GradientEntry id="linGradColorMid" ratio=".25" alpha=".5" /> <s:GradientEntry id="linGradColorEnd" ratio=".66" alpha=".5" /> </s:entries> </s:LinearGradient> </s:fill> </s:Rect> <s:Rect id="solidRect"> <s:stroke> <s:SolidColorStroke id="rectSolidStroke" /> </s:stroke> <s:fill> <s:SolidColor id="rectSolidColor" /> </s:fill> </s:Rect> </fx:Declarations>
From here, it was just setting the color for the Gradients and the appropriate image for the BitMap Fills. Except that it took me a while to figure out the correct syntax for the Image. I couldn’t declare it. I couldn’t just Embed it as the source. I had to Embed it as a Bindable Class and then set the BitmapAsset to that Class and finally make it the source for the BitMapFill.
// Embed the Red Apple Image [Embed(source="../assets/images/red-apple.png")] [Bindable] public var imgRedApple:Class; // Red Image Fill var imgRedAppleObj:BitmapAsset = new imgRedApple() as BitmapAsset; imageRectFill.source = imgRedAppleObj;
I simplified the code so that different columns do only two fills. I had columns doing all three fills, but decided that was too much code and could get really confusing. With the example below;
- If the 1st column has more than 45, then the Red Apple Image Fill is used, else the Red Gradient Fill
- If the 2nd column has more than 45, then the Blue Gradient Fill is used, else the Blue Solid Fill
- If the 3rd column has more than 45, then the Green Apple Image Fill is used, else the Green Solid Fill
- If the 4th column has more than 45, then the Purple Solid Fill is used, else the Purple Gradient Fill
I created a second file for this example, so that the first example and this example could be viewed separately.
View Source

