Main Code for drawing a bar chart on a map using Sprite in flex

Source: Internet
Author: User
Tags cos

1. Main Code:

VaR sprite: SPRITE = new sprite ();
VaR columnsys: columnsymbol = new columnsymbol ();
VaR fieldarr: array = new array ("Green total", "Yellow total ");
VaR colorarr: array = new array ("0x00ff00", "0xffff00 ");
Columnsys. Fields = fieldarr;
Columnsys. colors = colorarr;
Columnsys. maxvalue = Number (hftjnum );
VaR gra: graphic = new graphic (mppoint, columnsys, OBJ );

 

2. The referenced class columnsymbol (mainly contains two-dimensional cylindrical, square column, and three-dimensional cylindrical and square column ):

Public class columnsymbol extends symbol
{
Private VaR _ fields: array;
Private VaR _ colors: array;
Private VaR _ color: uint = 0x333333;
Private VaR _ maxvalue: number;
Private VaR _ columnwidth: Number = 20;
Private VaR _ maxheight: Number = 10;

Public Function columnsymbol ()
{
Super ();
}

Public Function get fields (): Array
{
Return this. _ fields;
}

Public Function set fields (value: array): void
{
This. _ fields = value;
}

Public Function get colors (): Array
{
Return this. _ colors;
}

Public Function set colors (value: array): void
{
This. _ colors = value;
}

Public Function get color (): uint
{
Return this. _ color;
}

Public Function set color (value: uint): void
{
This. _ color = value;
}

Public Function get maxvalue (): Number
{
Return this. _ maxvalue;
}

Public Function set maxvalue (value: Number): void
{
This. _ maxvalue = value;
}

Override public function draw (sprite: Sprite, geometry: geometry, attributes: object, MAP: Map): void
{
If (! Geometry) return;
If (! (Geometry is mappoint) return;
VaR point: mappoint = (geometry as mappoint );
Sprite. Graphics. Clear ();
Removeallchildren (sprite );
This. drawcolumn (sprite, attributes, MAP, point );
}

Private function drawcolumn (sprite: Sprite, attributes: object, MAP: map, point: mappoint): void
{
VaR H: Number = This. _ maxheight;
VaR W: Number = This. _ fields. length * _ columnwidth;
Sprite. x = math. Ceil (toscreenx (MAP, point. X)-W/2 ));
Sprite. Y = math. Ceil (toscreeny (MAP, point. Y)-W/2 ));
Sprite. width = W;
Sprite. Height = H + 30;
VaR columnheight: Number = 0;
VaR columnx: Number = 30;
VaR columny: Number = 0;
For (var j: uint = 0; j <_ fields. length; j ++)
{
Var val: Number = attributes [_ fields [J]. tostring ()];

Columnheight = Val * _ maxheight/_ maxvalue;
Columny = _ maxheight-columnheight + 5;
VaR color: uint = This. _ colors [J];
Sprite. Graphics. linestyle (null, 0 xffffff );

// Draw a plane column
Sprite. Graphics. beginfill (color );
Sprite. Graphics. drawrect (columnx, columny, _ columnwidth, columnheight );
Sprite. Graphics. endfill ();

// Increase the stereoscopic effect
Drawcolumn3d (sprite, columnx, columny, _ columnwidth, columnheight, color );
Columnx + = _ columnwidth;

VaR dy2: Number = _ columnwidth * Math. Cos (math. PI/6 );
VaR _ textfield: textfield = new textfield ();
_ Textfield. Text = Val. tostring ();
_ Textfield. x = columnx-10;
_ Textfield. Y = columny-dy2;
_ Textfield. width = 15;
_ Textfield. Height = 15;
_ Textfield. Visible = true;
VaR _ textformat: textformat = new textformat ();
_ Textformat. size = 10;
_ Textfield. settextformat (_ textformat );
Sprite. addchild (_ textfield );
}
}

Private function drawcolumn3d (sprite: Sprite, X: Number, Y: Number, W: Number, H: Number, color: uint): void
{
VaR DW: Number = W * Math. Sin (math. PI/6 );
VaR DX: Number = W * Math. Sin (math. PI/6 );
VaR DY: Number = W * Math. Cos (math. PI/6 );
Sprite. Graphics. beginfill (color );
Sprite. Graphics. moveTo (x, y );
Sprite. Graphics. lineto (x + dx, Y-dy );
Sprite. Graphics. lineto (x + dx + W, Y-dy );
Sprite. Graphics. lineto (x + W, y );
Sprite. Graphics. lineto (x, y );
Sprite. Graphics. endfill ();
Sprite. Graphics. beginfill (color );
Sprite. Graphics. moveTo (x + W, y );
Sprite. Graphics. lineto (x + dx + W, Y-dy );
Sprite. Graphics. lineto (x + dx + W, Y + H-dy );
Sprite. Graphics. lineto (x + W, Y + H );
Sprite. Graphics. lineto (x + W, y );
Sprite. Graphics. endfill ();
}

Public Function drawcylinde (sprite: Sprite, attributes: object, MAP: map, point: mappoint): void
{
VaR gap: Number = 0;
VaR H: Number = This. _ maxheight;
VaR W: Number = This. _ fields. length * (_ columnwidth + gap );
Sprite. x = math. Ceil (toscreenx (MAP, point. X)-W/2 ));
Sprite. Y = math. Ceil (toscreeny (MAP, point. Y)-W/2 ));
Sprite. width = W;
Sprite. Height = h;
VaR columnheight: Number = 0;
VaR columnx: Number = gap/2;
VaR columny: Number = 0;
For (var j: uint = 0; j <_ fields. length; j ++)
{
Var val: Number = attributes [_ fields [J]. tostring ()];
Columnheight = Val * _ maxheight/_ maxvalue;
Columny = _ maxheight-columnheight;
VaR color: uint = This. _ colors [J];
Sprite. Graphics. linestyle (null, 0 xffffff, 0 );
Sprite. Graphics. beginfill (color );
Sprite. Graphics. drawrect (columnx, columny, _ columnwidth, columnheight );
Sprite. Graphics. endfill ();
Drawcylindeu (sprite, columnx, columny, _ columnwidth, columnheight, color );
Columnx + = _ columnwidth + gap;
}
}

Private function drawcylindeup (sprite: Sprite, X: Number, Y: Number, W: Number, H: Number, color: uint): void
{
VaR DW: Number = W * Math. Sin (math. PI/6 );
VaR DX: Number = W * Math. Sin (math. PI/6 );
VaR DY: Number = W * Math. Cos (math. PI/6 );
Sprite. Graphics. beginfill (color );
Sprite. Graphics. moveTo (x, y );
Sprite. Graphics. lineto (x + dx, Y-dy );
Sprite. Graphics. lineto (x + dx + W, Y-dy );
Sprite. Graphics. lineto (x + W, y );
Sprite. Graphics. lineto (x, y );
Sprite. Graphics. endfill ();
Sprite. Graphics. beginfill (color );
Sprite. Graphics. moveTo (x + W, y );
Sprite. Graphics. lineto (x + dx + W, Y-dy );
Sprite. Graphics. lineto (x + dx + W, Y + H-dy );
Sprite. Graphics. lineto (x + W, Y + H );
Sprite. Graphics. lineto (x + W, y );
Sprite. Graphics. endfill ();
}

Private function drawcylindeu (sprite: Sprite, X: Number, Y: Number, W: Number, H: Number, color: uint): void
{
VaR DY: Number = W * Math. Cos (math. PI/6 );
Sprite. Graphics. linestyle (0.5 xffffff );
Sprite. Graphics. beginfill (color );
Sprite. Graphics. drawellipse (X, Y-(dy/2), W, Dy );
Sprite. Graphics. drawellipse (X, Y + H-(dy/2), W, Dy );
Sprite. Graphics. endfill ();
}

Private function drawcylinded (sprite: Sprite, X: Number, Y: Number, W: Number, H: Number, color: uint): void
{
VaR DY: Number = W * Math. Cos (math. PI/6 );
Y = Y + h;
Sprite. Graphics. linestyle (0.5 xffffff );
Sprite. Graphics. beginfill (color );
Sprite. Graphics. drawellipse (X, Y-(dy/2), W, Dy );
Sprite. Graphics. endfill ();
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.