艺虎动画 > flash设计制作中:使用Flex Chart画移动平均线

flash设计制作中:使用Flex Chart画移动平均线

翼虎动漫   2010-8-2

 

 

 

移动平均线通常是指算术移动平均线,在计算移动平均值时,通常采用最近n天的收市价格。我们把新的收市价格逐日地加入数组,而往前倒数的第n+1个收市价则被剔去。然后再把新的总和除以n,就得到了新的一天的平均值(n天平均值)。

那如何在Flex Chart中画移动平均线呢?请看下面的方法:

private function getMACollection(collection:ArrayCollection, offset:int):ArrayCollection
{
 var newCollection:ArrayCollection = new ArrayCollection();
 var len:int = collection.length;
 for (var i:int = 0; i < len - offset + 1; i++) 
 {
  var sumClose:Number = 0;
  for (var j:int = i ; j < offset + i; j++) 
  {
   var o:Object = collection.getItemAt(j);
   sumClose +=  o.stockA;
  } 
  var item:Object = new Object();
  item.date = collection.getItemAt(i  + offset - 1).date; 
  item.stockA = sumClose/offset;
  newCollection.addItem(item);
 } 
 return newCollection;
}

查看该实例最终效果:http://www.riafan.com/flex/movingaverage/
下载该实例的源码: http://www.riafan.com/flex/movingaverage.zip