I have column chart I am customizing, in the following order:
- If the column value is negative, the column color is red. Otherwise it's black
- I then change it so that all the data will appear above the x-axis by changing all negative values to positives
I can get #1 & #2 to display perfectly individually, but when I try to implement both, I get only black columns (ie the code will convert the negative values to positives and THEN will apply the colors, thus all columns are black....even though in my code I have the color applied BEFORE the absolute value part of the code. Any suggestions on how I correct this?
Below is my actionscript code:
package utils
{
import mx.core.IDataRenderer;
import mx.core.UIComponent;
import flash.display.Graphics;
import flash.geom.Rectangle;
import mx.charts.ChartItem;
import mx.charts.ColumnChart;
import mx.charts.chartClasses.LegendData;
public class ColorColumnChartRenderer extends UIComponent implements IDataRenderer
{
public function ColorColumnChartRenderer():void
{
super();
}
private var _chartItem:ChartItem;
public function set data(value:Object):void
{
if (_chartItem == value)
return;
// setData also is executed if there is a Legend Data
//defined for the chart. We validate that only chartItems are
//assigned to the chartItem class.
if (value is LegendData)
return;
_chartItem = ChartItem(value);
}
public function get data():Object
{
return _chartItem;
}
override protected functi开发者_如何学运维on
updateDisplayList(unscaledWidth:Number,unscaledHeight:Number):void
{
super.updateDisplayList(unscaledWidth, unscaledHeight);
var rc:Rectangle = new Rectangle(0, 0, width , height );
var columnColor:uint;
var g:Graphics = graphics;
g.clear();
g.moveTo(rc.left,rc.top);
// Only if the _chartItem has data
if (_chartItem == null)
return;
// Only if the _chartItem has the attributes
if( _chartItem.item.hasOwnProperty("price") )
{
if ( Number(_chartItem.item.price) > 0 ){
// black
g.beginFill( 0x000000 );
}
if ( Number(_chartItem.item.price) < 0 ){
// red
g.beginFill( 0xF04448 );
}
}
// Draw the column
g.lineTo(rc.right,rc.top);
g.lineTo(rc.right,rc.bottom);
g.lineTo(rc.left,rc.bottom);
g.lineTo(rc.left,rc.top);
g.endFill();
_chartItem.item.price= Math.abs( _chartItem.item.price);
}
}
}
_chartItem.item.price= Math.abs( _chartItem.item.price);
You are losing information here. Probably the chart is drawn more than once, and second time all is positive and black. Store original value in other variable so it won't be lost.
精彩评论