| [833] | 1 | package com.jeroenwijering.utils { |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | import com.jeroenwijering.events.*; |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | import flash.display.*; |
|---|
| 8 | import flash.events.*; |
|---|
| 9 | import flash.geom.Rectangle; |
|---|
| 10 | import flash.text.*; |
|---|
| 11 | import flash.utils.*; |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | /** |
|---|
| 15 | * Class to display constantly updating sparklines. |
|---|
| 16 | **/ |
|---|
| 17 | public class Sparkline extends Sprite { |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | /** Reference to the background clip. **/ |
|---|
| 21 | private var line:Sprite; |
|---|
| 22 | /** Range of the sparkline. **/ |
|---|
| 23 | private var range:Array; |
|---|
| 24 | /** Bounding box of the chart. **/ |
|---|
| 25 | private var area:Sprite; |
|---|
| 26 | /** The number of sparks in the line. **/ |
|---|
| 27 | private var sparks:Number = 0; |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | /** Constructor. Setup the sparkline. **/ |
|---|
| 31 | public function Sparkline(wid:Number=100,hei:Number=20,min:Number=0,max:Number=1):void { |
|---|
| 32 | range = new Array(min,max); |
|---|
| 33 | line = new Sprite(); |
|---|
| 34 | line.graphics.lineStyle(2,0xCCCCCC); |
|---|
| 35 | area = new Sprite(); |
|---|
| 36 | area.graphics.beginFill(0x000000); |
|---|
| 37 | area.graphics.drawRect(0,0,wid,hei); |
|---|
| 38 | line.mask = area; |
|---|
| 39 | addChild(line); |
|---|
| 40 | addChild(area); |
|---|
| 41 | }; |
|---|
| 42 | |
|---|
| 43 | |
|---|
| 44 | /** Change the width of the sparkline. **/ |
|---|
| 45 | public function resize(wid:Number):void { |
|---|
| 46 | var hei:Number = area.height; |
|---|
| 47 | area.graphics.clear(); |
|---|
| 48 | area.graphics.beginFill(0x000000); |
|---|
| 49 | area.graphics.drawRect(0,0,wid,hei); |
|---|
| 50 | }; |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 53 | /** Add a datapoint. **/ |
|---|
| 54 | public function spark(val:Number):void { |
|---|
| 55 | var xps:Number = sparks+1; |
|---|
| 56 | var yps:Number = (range[1]-val) * area.height / (range[1]-range[0]); |
|---|
| 57 | if(yps < 1) { |
|---|
| 58 | yps = 1; |
|---|
| 59 | } else if (yps > area.height - 1) { |
|---|
| 60 | yps = area.height - 1; |
|---|
| 61 | } |
|---|
| 62 | if(sparks) { |
|---|
| 63 | line.graphics.lineTo(xps,yps); |
|---|
| 64 | } else { |
|---|
| 65 | line.graphics.moveTo(0,yps); |
|---|
| 66 | } |
|---|
| 67 | if(line.width > area.width) { |
|---|
| 68 | line.x = area.width - line.width; |
|---|
| 69 | } else { |
|---|
| 70 | line.x = 0; |
|---|
| 71 | } |
|---|
| 72 | sparks++; |
|---|
| 73 | }; |
|---|
| 74 | |
|---|
| 75 | |
|---|
| 76 | |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | |
|---|
| 80 | } |
|---|