Main.mxml


<?xml version=”1.0″ encoding=”utf-8″?>

<mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml” >

<mx:Script>

import mx.collections.ArrayCollection;


[Bindable]

public var dataCollection:ArrayCollection = new ArrayCollection([

{Month: "Jan", planned: 2000, actual:1856, average:1790 },

{Month: "Feb", planned: 1590, actual:1624, average:1790},

{Month: "Mar", planned: 1820, actual:1832,average:1790},

{Month: "Apr", planned: 1450, actual:1524, average:1790},

{Month: "May", planned: 1775, actual:1695, average:1790}]);

</mx:Script>


<mx:Panel>

<mx:ColumnChart id=”myChart” dataProvider=”{dataCollection}”

showDataTips=”true”>

<mx:horizontalAxis>

<mx:CategoryAxis dataProvider=”{dataCollection}”

categoryField=”Month”/>

</mx:horizontalAxis>

<mx:series>

<mx:ColumnSeries xField=”Month” yField=”planned” displayName=”Planned”/>

<mx:ColumnSeries xField=”Month” yField=”actual” displayName=”Actual”/>

<mx:ColumnSeries xField=”Month” yField=”average” displayName=”Average”

itemRenderer=”AverageLineRenderer”/>

</mx:series>

</mx:ColumnChart>

</mx:Panel>

</mx:Application>


AverageLineRenderer.as


package

{

import flash.display.Graphics;

import mx.charts.ChartItem;

import mx.charts.series.items.ColumnSeriesItem;

import mx.charts.series.items.LineSeriesItem;

import mx.controls.Label;

import mx.core.IDataRenderer;

import mx.core.UIComponent;


public class AverageLineRenderer extends UIComponent implements IDataRenderer

{

private var _chartItem:ChartItem;

private var _label:Label; // A label to place on the top of the column.


public function AverageLineRenderer():void

{

super();


// Add the label

_label = new Label();

addChild(_label);

_label.setStyle(“color”,0×000000);

}


public function get data():Object

{

return _chartItem;

}


public function set data(value:Object):void

{

if(value is ColumnSeriesItem)

{

_chartItem = value as ColumnSeriesItem;


if(_chartItem != null)

{

// Assigns the yValue to the label

if (ColumnSeriesItem(_chartItem).yValue!=null)

_label.text = “Average: ” + ColumnSeriesItem(_chartItem).yValue.toString();

else

_label.text=”";

}

}

}


override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void

{


if ( _chartItem.index == 0) // Do it only for the first chartItem

{

super.updateDisplayList(unscaledWidth, unscaledHeight);


var g:Graphics = graphics;

g.clear();

g.beginFill(0xFF0000); // Red line, you can select your own color

// Draw the line across the chart. This is actually a rectangle with height 1.

g.drawRect(-20,0,this.parent.width+20, 1);

g.endFill();

//Place the label

_label.setActualSize(_label.getExplicitOrMeasuredWidth(),_label.getExplicitOrMeasuredHeight());

_label.move(unscaledWidth ,-15);

}


}

}

}

Posted by 미랭군