In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

Practice No2

Get Adobe Flash player
by NA8 21 May 2010
/**
 * Copyright NA8 ( http://wonderfl.net/user/NA8 )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/dNfJ
 */

package{
	import flash.display.*;
	import flash.text.*;
	import flash.filters.DropShadowFilter;
    import flash.events.MouseEvent;
    import flash.events.Event;
	import flash.net.*;

	[SWF(backgroundColor=0xffffff, width=470, height=470, frameRate=15)]

	public class DrawGraph extends Sprite{
	
		public function DrawGraph(){
			
			var i:uint=0;
			var j:uint=0;
			
			const HLINECOLOR:uint=0xAAAAAA;
						
			//本当はデータによって変動
			var memori:Number=5;
			
			var ymin:Number=0;
			var ymax:Number=500;
			var ypit:Number=100;
			var yjmax:uint=5;
			
			var gt:Number=40;
			var gl:Number=40;
			var gw:Number=420;
			var gh:Number=240;

			var lwidth:Number;
			var drawcolor:Array=new Array(0xFF0000
										,0x0099FF
										,0x0000FF
										,0x00FF00
										,0x990099
										,0xFF9900
										,0x669933
										,0xFF0033
										);

/*

			var data:String;
			var loader:URLLoader = new URLLoader(new URLRequest("graphdata.csv"))
			loader.addEventListener(Event.COMPLETE, function(event:Event):void {
				data = loader.data;
			});
*/

			var data:String="6,6,6"
			data += ",(円),1つ目,2つ目,3つ目,4つ目,5つ目,6つ目";
			data += ",平均,150,200,160,180,130,140";
			data += ",項目A,50,53,53,53,40,140";
			data += ",項目B,100,100,120,110,100,140";
			data += ",項目C,200,210,230,220,200,140";
			data += ",項目D,300,310,320,150,300,140";
			data += ",項目E,170,80,220,50,130,140";
//			data += ",項目F,450,50,460,500,400,140";

			var items:Array=data.split(",");
			
			var gcnt:uint=items[i++];		//グラフx軸のMax
			var hcnt:uint=items[i++];		//凡例数(項目数)
			var dcnt:uint=items[i++];		//x軸データ数

			var ylabel:String;
			var yl_x:Number=gl-(gw/gcnt)/2;
			var yl_y:Number=gh+gt+memori;

			//単位
			DrawText(gl,gt-10,items[i++],9);

			//x軸補助線と項目の出力
			
			for(j=0;j<dcnt;j++){

				yl_x+=(gw/gcnt);

				var xl:Sprite=new Sprite();
				xl.graphics.lineStyle(1,HLINECOLOR);
				xl.graphics.moveTo(yl_x,gt);
				xl.graphics.lineTo(yl_x,yl_y);
				addChild(xl);

				DrawText(yl_x,yl_y+memori,items[i++],2);
			}
			
			//データ部分取込

			var jmax:int=items.length-i;

			var d:Array=new Array();

			var cnt:int=-1;
			var dmin:Number=99999;
			var dmax:Number=-99999;

			//凡例位置
			var lx:Number=gl+50;
			var ly:Number=gh+gt+35;

			DrawText(gl,ly,"凡例",6);

			for(j=0;j<jmax;j++){
				if(j%(dcnt+1)==0){

					//凡例出力
					var ht:TextField=DrawText(lx,ly
									,": "+items[i++],4);
				
					var hl:Sprite=new Sprite();
					if(j==0){
						lwidth=4;
					}else{
						lwidth=2;
					}
					hl.graphics.lineStyle(lwidth,uint(drawcolor[int(j/(dcnt+1))]));
					hl.graphics.moveTo(lx-35,ly);
					hl.graphics.lineTo(lx-5,ly);
					addChild(hl);
					ly+=ht.textHeight+2;

				}else{
					
					//データ取込
					var dd:Number=items[i++];
					d.push(dd);
					if(dmin>dd) { dmin=dd; }
					if(dmax<dd) { dmax=dd; }
				}
			}

			//y軸補助線とy軸目盛の出力

			var yj_x:Number=gl-memori;
			var yj_y:Number=gt

			for(i=0;i<=yjmax;i++){

				DrawText(yj_x-memori,yj_y,String(ymax-ypit*i),6);

				var yl:Sprite = new Sprite();
				yl.graphics.lineStyle(1, HLINECOLOR);
				yl.graphics.moveTo(yj_x, yj_y);
				yl.graphics.lineTo(gl+gw, yj_y);
				addChild(yl);
				
				yj_y+=(gh/yjmax);
			}

			//プロット枠の出力
			var s:Sprite = new Sprite();
			s.graphics.lineStyle(2, 0);
			s.graphics.drawRect(gl, gt, gw, gh);
			addChild(s);

			//データポイント出力

			var pt:Array=new Array();
			var pl:Sprite;
			var px:Number;
			var py:Number;

			i=0;
			for(j=0;j<d.length;j++){

				var k:uint=j%dcnt;
				px=gl+(gw/gcnt)*(k+0.5);
				py=gt+gh/(ymax-ymin)*(ymax-d[j]);

				//ポイント作成				
				pt.push(DrawPoint(px,py
						,String(d[j])
						,drawcolor[i]));

				//ポリライン作成

				if(k==0){
					pl=new Sprite();
					if(j==0){
						lwidth=4;
					}else{
						lwidth=2;
					}
					pl.graphics.lineStyle(lwidth, drawcolor[i]);
					pl.graphics.moveTo(px, py);
				}else{
					pl.graphics.lineTo(px, py);
				}

				if(k==dcnt-1){					
					addChild(pl);
					for(var m:uint=0;m<dcnt;m++){
						addChild(pt[0]);
						pt.shift();
					}
					
					i++;
				}
			}
		}
		
		private function DrawPoint(x:Number
								,y:Number
								,z:String
								,clr:uint):Sprite{
			
			var t:TextField=new TextField;
			var s:Sprite=new Sprite;
			s.graphics.lineStyle(1, clr);
			s.graphics.beginFill(clr,0.5);
			s.graphics.drawCircle(x,y ,5);
			s.graphics.endFill();
			
			t.text =z;
			t.autoSize ="left";
			t.visible =false;
			t.background=true;

			t.x=x+5;
			t.y=y-t.textHeight;
			s.addChild(t);

			s.addEventListener(MouseEvent.MOUSE_OVER
			,function(event:MouseEvent):void {
				t.visible=true;
			});

			s.addEventListener(MouseEvent.MOUSE_OUT
			,function(event:MouseEvent):void {
				t.visible=false;
			});
/*
			s.addEventListener(MouseEvent.CLICK
			,function(event:MouseEvent):void {
				if(t.visible){
					t.visible=false;
				}else{
					t.visible=true;
				}
			});
*/			
			return s;
		}
		
		private function DrawText(x:Number
								,y:Number
								,z:String
								,position:uint):TextField{

			var tx:TextField = new TextField();
			tx.text=z;
			switch(position%3){
				case 1:
					tx.autoSize="left";
					tx.x=x;
					break;
				case 2:
					tx.autoSize="center";
					tx.x=x-tx.textWidth/2;
					break;
				case 0:
					tx.autoSize="right";
					tx.x=x-tx.textWidth;
					break;
				default:
					break;
			}
			switch(int((position-1)/3)){
				case 0:
					tx.y=y;
					break;
				case 1:
					tx.y=y-tx.textHeight/2;
					break;
				case 2:
					tx.y=y-tx.textHeight;
					break;
				default:
					break;
			}
			addChild(tx);

			return tx;
		}
		
	}
}