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 No3 データによってY軸を自動設定

No3
* グラフ要素によって、自動的にy軸が変化するように
Get Adobe Flash player
by NA8 13 Mar 2012
/**
 * Copyright NA8 ( http://wonderfl.net/user/NA8 )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/sksV
 */

/* No3
* グラフ要素によって、自動的にy軸が変化するように
*/
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,5,6"
            data += ",(円),1つ目,2つ目,3つ目,4つ目,5つ目,6つ目";
            data += ",平均,1,1,1,1,1,1";
            data += ",項目A,1,093,83,123,110,114";
            data += ",項目B,10,100,120,110,100,017";
            data += ",項目C,1101,081,123,122,120,109";
            data += ",項目D,1080,1191,132,110,3,10";
            //data += ",項目E,110,080,-122,110,121,-91";
            //            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軸設定
            
            var pit:Array=new Array(1,2,5,10);
            var pitmin:Number=Math.abs((dmax-dmin)/yjmax);

            if(pitmin!=0){
                var kari:Number =0;
                var n:Number=Math.floor(Math.LOG10E*Math.log(Math.abs((dmax-dmin)/yjmax)));
        
                if(n<0){
                    for(var q1:int=0;q1<pit.length;q1++){
                        kari=Math.round(pit[q1]*Math.pow(10,n)*100)/100;
                        if(pitmin<kari){
                            break;
                        }
                    }
                }else{
        
                    for(var q2:int=0;q2<pit.length;q2++){
                        kari=pit[q2]*Math.pow(10,n);
                        if(pitmin<kari){
                            //DrawText(1,1,""+pitmin,1);
                            break;
                        }
                    }
                }
                ypit=Number(kari.toFixed(3));
                ymin=Math.floor(dmin/ypit)*ypit;
                ymin=Number(ymin.toFixed(3));
            }else{
                ypit=Math.pow(10,Math.floor(Math.LOG10E*Math.log(Math.abs(dmin))+0.999));
                ymin=Math.floor((dmin/ypit-yjmax/2)*ypit);
                ymin=Number(ymin.toFixed(3));
            }
            
            ymax=ymin+ypit*yjmax;
            
            //y軸補助線とy軸目盛の出力

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

            for(i=0;i<=yjmax;i++){
                
                var bufnum:Number=(ymax-ypit*i);
                DrawText(yj_x-memori,yj_y,String(Number(bufnum.toFixed(3))),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 s:Sprite=new Sprite;
            s.graphics.lineStyle(1, clr);
            s.graphics.beginFill(clr,0.5);
            s.graphics.drawCircle(x,y ,5);
            s.graphics.endFill();
            
            var t:TextField=DrawText(x+5,y-5,z,7);
            t.visible =false;
            t.background=true;
            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;
        }
        
    }
}