forked from: Graph
MainStage
/**
* Copyright aobyrne ( http://wonderfl.net/user/aobyrne )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/xzEo
*/
// forked from hashito's Graph
// MainStage
package {
import com.bit101.charts.LineChart;
import com.bit101.components.HUISlider;
import com.bit101.components.PushButton;
import com.bit101.components.VBox;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.geom.Point;
[SWF(frameRate=60,width=465,height=465)]
public class GraphClient extends Sprite
{
private var graphPlotter:GraphPlotter;
private var bitmapData:BitmapData;
private var count:int;
private var lineChartArray:Array;
private var lineChart:LineChart;
private var base:HUISlider;
public function GraphClient() {
stage.scaleMode = StageScaleMode.NO_SCALE;
graphPlotter = new GraphPlotter();
graphPlotter.alpha = 0.62;
addChild(graphPlotter);
bitmapData = new BitmapData(200, 200);
bitmapData.perlinNoise(100, 1, 1, 1, true, true, 1, true);
addChild(new Bitmap(bitmapData));
var vbox:VBox = new VBox(this, 0, 0);
lineChart = new LineChart(vbox, 0, 0, []);
lineChart.alpha = 0.62;
lineChartArray = [];
//new PushButton(vbox, 0, 0, 'step', doLoop);
//new PushButton(vbox, 0, 0, 'step', doLoop);
base = new HUISlider(vbox, 0, 0, 'base');
base.maximum = 200;
base.value = 100;
addEventListener(Event.ENTER_FRAME, loop);
}
private function doLoop(e:Event):void
{
loop(null);
}
private function loop(e:Event):void
{
var array:Array = [];
//var number:int = 100;
//for (var i:int = 0; i < number; i++)
//{
//array[i] = Math.random() * 100;
//}
count++;
array.length = 0;
var b:BitmapData = bitmapData;
bitmapData.perlinNoise(base.value, base.value, 1, 1, true, true, 1, true, [new Point(0, count)]);
var vector:Vector.<uint> = bitmapData.getVector(bitmapData.rect);
var vl:int = vector.length;
for (var j:int = 0; j < bitmapData.width; j++)
{
var pixel32:uint = vector[j];
var pixel:uint = pixel32&0xff;
array[j] = 100*(pixel)/0xff;
}
graphPlotter.array = array;
lineChart.data = array.concat();
}
}
}
import flash.display.*;
import flash.filters.BlurFilter;
import flash.geom.Point;
import flash.geom.ColorTransform;
import flash.events.*;
import flash.text.TextField;
import net.hires.debug.Stats;
class GraphPlotter extends Sprite{
public var H:Number = 465;
public var W:Number = 465;
private var _array:Array;
private var d:Vector.<GraphData> = new Vector.<GraphData>();
private var g:Graph = new Graph(W,H,100,200);
private var l1:GraphLine;
//Constructor
public function GraphPlotter() {addEventListener(Event.ADDED_TO_STAGE, init);}
//init
public function init(e:*):void {
array = [];
l1 = new GraphLine(g, d);
l1.filters = [new BlurFilter(2, 2, 1)];
//reset();
g.addLine(l1);
addChild(g);
//stage.addEventListener(MouseEvent.MOUSE_DOWN,reset);
//reset();
}
public function reset(e:MouseEvent=null):void{
var i:int;
for(i=0;i<200;i++){
d[i]=new GraphData(Math.random()*100,Math.random()*100);
}
d.sort(GraphData.Sortfunc);
g.draw();
}
public function get array():Array
{
return _array;
}
public function set array(value:Array):void
{
_array = value;
for (var i:int = 0; i < _array.length; i++)
{
d[i] = new GraphData(i,_array[i]);
}
//l1.setData(new Vector.<GraphData>(_array));
g.draw();
}
}
//graph
class Graph extends Sprite{
private var row:Number,col:Number,tick:Number,H:Number,W:Number;
private var map:Bitmap=new Bitmap(),mapd:BitmapData,pen:Shape=new Shape(),board:Sprite=new Sprite();
private const SPACE_W:Number=10,SPACE_H:Number=10;
private const TICK_COLOR:uint = 0,BACK_COLOR:uint = 0xffffff ,SOLID_COLOR:uint=0xffffff;
private var lines:Vector.<GraphLine> = new Vector.<GraphLine>(),lineBoard:Sprite=new Sprite;
// constructor
public function Graph(w:Number,h:Number,row:Number=100,col:Number=100,tick:Number=10):void{
this.row=row;this.col=col;this.tick=tick;
board.addChild(pen);
addChild(map);
addChild(lineBoard);
setSize(w, h);
//map.filters=[new BlurFilter(2,2)]
trace( "h : " + h );
trace( "w : " + w );
}
public function get graphW():Number{return W - SPACE_W;}
public function get graphH():Number{return H - SPACE_H;}
public function get graphCol():Number{return col;}
public function get graphRow():Number{return row;}
// set size
public function setSize(w:Number,h:Number):void{
mapd = new BitmapData((W = w),(H = h),false,BACK_COLOR);
map.bitmapData = mapd;
// col row 基準線
pen.graphics.lineStyle(1,SOLID_COLOR);
pen.graphics.moveTo(SPACE_W , 0 );
pen.graphics.lineTo(SPACE_W , graphH );
pen.graphics.lineTo( W , graphH );
// col row 目盛り線
pen.graphics.lineStyle(1,TICK_COLOR);
var t:int/* tick count */,s:Number/* tick size */,i:int;
//縦目盛り
t=Math.floor(row/tick);
s=(W-SPACE_W)/tick;
for(i=1;i<t;i++){
pen.graphics.moveTo(s*i+SPACE_W,0);
pen.graphics.lineTo(s*i+SPACE_W,H-SPACE_H);
}
//横目盛り
t=Math.floor(col/tick);
s=(H-SPACE_H)/tick;
for(i=1;i<t;i++){
pen.graphics.moveTo(SPACE_W,s*i);
pen.graphics.lineTo(W,s*i);
}
mapd.draw(board);
pen.graphics.clear();
draw();
}
// line add
public function addLine(l:GraphLine):int{
lineBoard.addChild(l);
l.x = SPACE_W;
return lines.push(l);
}
// 描画
public function draw():void{
for each(var l:GraphLine in lines){
l.draw();
}
}
// line remove
public function removeLine(l:GraphLine):GraphLine{
lineBoard.removeChild(l);
return lines.splice(lines.indexOf(l),1)[0];
}
}
//graph line
class GraphLine extends Bitmap{
private var d:Vector.<GraphData> = new Vector.<GraphData>();
private var color:uint;
private var bitdata:BitmapData, pen:Shape = new Shape(), board:Sprite = new Sprite;
private var map:Graph;
private const BACK_COLOR:uint=0x000001,TEXT_COLOR:uint=0xffeeee;
public function GraphLine(map:Graph,d:Vector.<GraphData>=null,color:uint=0xff0000){
this.d=d;
this.map=map;
this.color=color;
board.addChild(pen);
}
//描画
public function draw():void{
var w:Number = map.graphW ,h:Number = map.graphH,c:Number = map.graphCol ,r:Number = map.graphRow,i:int;
bitdata=new BitmapData(w,h,true,BACK_COLOR);
// 描画処理
pen.graphics.clear();
pen.graphics.lineStyle(1,color);
pen.graphics.moveTo((d[i].time/c)*w,(d[i].data/r)*h);
var dl:int = d.length;
var xFactor:Number = w / c;
var yFactor:Number = h / r;
for (i = 1; i < dl; i++)
{
var px:Number=d[i].time*xFactor , py:Number=d[i].data*yFactor;
pen.graphics.lineTo(px,py);
bitdata.draw(board);//描画
}
bitmapData=bitdata;
}
public function getData():Vector.<GraphData>{return d;}
public function setData(d:Vector.<GraphData>):void{this.d=d;}
}
//graph data
class GraphData{
public var time:Number,data:Number;
public function GraphData(t:Number,d:Number){time=t;data=d;}
public static function Sortfunc(a:GraphData,b:GraphData):Number{
// sort に渡す関数の戻り値は小数点以下は認識しないため
var t:Number=(a.time-b.time);
if(t<0) return -1;
else if(t>0) return 1;
else return 0;
}
}