2012-09-21 65 views
0

有一點有一些代碼,我已經寫了一個問題。基本上,它所做的是取3個不斷變化的值,並以累積折線圖的形式隨時間變化。它幾乎可以工作,除非我在整個舞臺上繪製這條奇怪的線,而且我無法弄清楚問題所在。完整的代碼如下,你可以將它粘貼到Flash中運行。AS3:折線圖出問題

import flash.display.Shape; 
import flash.display.MovieClip; 
import flash.display.Sprite; 
import flash.utils.Timer; 

var y1:Array = new Array(); 
var y2:Array = new Array(); 
var y3:Array = new Array(); 
var avg:Array = new Array(); 

var y1Shape:Shape = new Shape(); 
var y2Shape:Shape = new Shape(); 
var y3Shape:Shape = new Shape(); 
var avgShape:Shape = new Shape(); 

var container:Sprite = new Sprite(); 

var scale:uint = 1; 

var redrawGraph:int = setInterval(reDraw,500); 

var y1Int:int = 0; 
var y2Int:int = 0; 
var y3Int:int = 0; 

container.addChild(y1Shape); 
container.addChild(y2Shape); 
container.addChild(y3Shape); 
container.addChild(avgShape); 
this.addChild(container); 

function reDraw():void 
{ 
    y1Shape.graphics.clear(); 
    y2Shape.graphics.clear(); 
    y3Shape.graphics.clear(); 
    avgShape.graphics.clear(); 

    y1Shape.graphics.lineStyle(1, 0x0066FF, 1); 
    y1Shape.graphics.beginFill(0x0066FF, 0.5); 
    y2Shape.graphics.lineStyle(1, 0x009900, 1); 
    y2Shape.graphics.beginFill(0x009900, 0.5); 
    y3Shape.graphics.lineStyle(1, 0x990000, 1); 
    y3Shape.graphics.beginFill(0x990000, 0.5); 
    avgShape.graphics.lineStyle(1, 0x000000, 1); 

    y1Int = rand(); 
    y2Int = rand(); 
    y3Int = rand(); 

    trace(y1Int, y2Int, y3Int); 

    y1.unshift(y1Int); 
    y2.unshift(y2Int); 
    y3.unshift(y3Int); 
    popOut(y1); 
    popOut(y2); 
    popOut(y3); 

    var i:uint,sum:uint,aLength:uint,len:uint = y1.length,max:int = 0,height_:int = 400; 
    scale = 10; 
    for (i=0; i<len; i++) 
    { 
     max = Math.max(y1[i] + y2[i] + y3[i],max); 
    } 

    for (i=0; i<len; i++) 
    { 
     sum += y1[i] + y2[i] + y3[i]; 
    } 

    avg.unshift(Math.round(sum/len)); 

    /*--------------------------------MATCHED GRAPH------------------------------------------*/ 
    var y1_commands:Vector.<int> = new Vector.<int>(); 
    var y1_coord:Vector.<Number>= new Vector.<Number>(); 
    var y1_coord_rev:Vector.<Number> = new Vector.<Number>(); 
    y1_commands.push(1); 
    y1_coord.push(400,height_); 

    for (i=0; i<len; i++) 
    { 
     y1_commands.push(2); 
     y1_coord.push((400-i*scale),height_-(Math.round((y1[i]/max)*height_))); 
     y1_coord_rev.unshift((400-i*scale),height_-(Math.round((y1[i]/max)*height_))); 
    } 
    for (i=len; i>0; i--) 
    { 
     y1_commands.push(2); 
     y1_coord.push(400 - i*scale,height_); 
    } 
    y1_commands.push(2); 
    y1_coord.push(400,height_); 

    /*--------------------------------MATCHED GRAPH------------------------------------------*/ 

    /*----------------------------------BUSY GRAPH-------------------------------------------*/ 
    var y2_commands:Vector.<int> = new Vector.<int>(); 
    var y2_coord:Vector.<Number>= new Vector.<Number>(); 
    var y2_coord_rev:Vector.<Number> = new Vector.<Number>(); 
    y2_commands.push(1); 
    y2_coord.push(400,height_-(Math.round((y1[i]/max)*height_))); 

    for (i=0; i<len; i++) 
    { 
     y2_commands.push(2); 
     y2_coord.push((400-i*scale),height_-(Math.round(((y1[i]+y2[i])/max)*height_))); 
     y2_coord_rev.unshift((400-i*scale),height_-(Math.round(((y1[i]+y2[i])/max)*height_))); 
    } 
    for (i=len; i>0; i--) 
    { 
     y2_commands.push(2); 
     y2_coord.push(400 - i*scale, height_-(Math.round((y1[i]/max)*height_))); 
    } 
    y2_commands.push(2); 
    y2_coord.push(400,height_-(Math.round((y1[i]/max)*height_))); 

    /*----------------------------------BUSY GRAPH-------------------------------------------*/ 

    /*----------------------------------VAC GRAPH-------------------------------------------*/ 
    var y3_commands:Vector.<int> = new Vector.<int>(); 
    var y3_coord:Vector.<Number>= new Vector.<Number>(); 
    var y3_coord_rev:Vector.<Number> = new Vector.<Number>(); 
    y3_commands.push(1); 
    y3_coord.push(400,height_-(Math.round(((y1[i]+y2[i])/max)*height_))); 

    for (i=0; i<len; i++) 
    { 
     y3_commands.push(2); 
     y3_coord.push((400-i*scale),height_-(Math.round(((y1[i]+y2[i]+y3[i])/max)*height_))); 
     y3_coord_rev.unshift((400-i*scale),height_-(Math.round(((y1[i]+y2[i]+y3[i])/max)*height_))); 
    } 
    for (i=len; i>0; i--) 
    { 
     y3_commands.push(2); 
     y3_coord.push(400 - i*scale, height_-(Math.round(((y1[i]+y2[i])/max)*height_))); 
    } 
    y2_commands.push(2); 
    y2_coord.push(400,height_-(Math.round(((y1[i]+y2[i])/max)*height_))); 

    /*----------------------------------BUSY GRAPH-------------------------------------------*/ 
    //y3Shape.graphics.drawPath(y3_commands, y3_coord); 
    y2Shape.graphics.drawPath(y2_commands, y2_coord); 
    y1Shape.graphics.drawPath(y1_commands, y1_coord); 

} 

function popOut(a:Array):void 
{ 
    if (a.length >=Math.ceil(400/scale)) 
    { 
     a.pop(); 
    } 
} 

function rand():int 
{ 
    return Math.floor(Math.random() * (1 + 5 - 0) + 0); 
} 

y3Shape被註釋了直到與y2Shape問題是固定的(具有兩個引出只是使問題更難以找出)。

任何想法可能是什麼呢?

感謝

回答

1

如果將跟蹤近.drawPath您的載體,你會看到類似的東西:

trace(y2_commands); // 1,2,2,2,2 
trace(y2_coord); // 400,171,400,57,390,NaN,400,171,400,57 

所以,NaN(非數字)意味着,你必須在座標誤差計算。

ps。 y1[i]在第一次計算BUSY GRAPH時未定義

+0

謝謝,這導致我的問題,返回環路(從右到左,因爲它最初從左到右繪製)從陣列外的一個點開始,現在它的工作非常棒!還要感謝您提出'y1 [i]'問題。 –

+0

不客氣! –

0

您似乎在使用beginFill(),當您繪製一個未循環的路徑時,Flash需要您的路徑才能循環以填充某些東西。因此,它通過在起始點添加一行並填充它來隱式循環。爲了接收非重疊的路徑,補充兩點到您的路徑,這將是正下方(X軸)0點,一個合適的下你的圖中的最後一點,右下圖上的第一點之一。

其實,你已經安裝它們,但由於某些原因,你將不是2分,但一大堆的EM,以及該行的高度位置顯然是錯誤的。您的代碼狀態:

for (i=len; i>0; i--) 
{ 
    y2_commands.push(2); 
    y2_coord.push(400 - i*scale, height_-(Math.round((y1[i]/max)*height_))); 
} 

你應該有:

for (i=len; i>0; i--) 
{ 
    y2_commands.push(2); 
    y2_coord.push(400 - i*scale, height_); 
} 

甚至更​​好:

y2_commands.push(2); 
y2_commands.push(2); 
y2_coord.push(400 - len*scale, height_); 
y2_coord.push(400, height_); 
+0

其原因我的代碼包括' - (Math.round((Y1 [I] /最大)*高度_))'是這樣的第二個「水平」犯規重疊第一底部,它們有效地在彼此疊加。 –

+0

您可以通過更改圖表的AddChild()命令,一旦你釘的問題做到這一點。另外,你已經有了一個y1_coord_rev座標的數組,從那裏添加它們! – Vesper