📄 pieseries.as
字号:
package com.yahoo.astra.fl.charts.series{ import com.yahoo.astra.animation.Animation; import com.yahoo.astra.animation.AnimationEvent; import com.yahoo.astra.fl.charts.PieChart; import com.yahoo.astra.fl.charts.legend.LegendItemData; import com.yahoo.astra.fl.charts.skins.RectangleSkin; import com.yahoo.astra.utils.GeomUtil; import com.yahoo.astra.utils.GraphicsUtil; import com.yahoo.astra.utils.NumberUtil; import fl.core.InvalidationType; import fl.core.UIComponent; import flash.display.Shape; import flash.display.Sprite; import flash.events.Event; import flash.geom.Point; import flash.text.TextField; import flash.text.TextFieldAutoSize; import flash.text.TextFormat; import flash.text.TextFormatAlign; //-------------------------------------- // Styles //-------------------------------------- /** * The colors of the markers in this series. * * @default [0x729fcf, 0xfcaf3e, 0x73d216, 0xfce94f, 0xad7fa8, 0x3465a4] */ [Style(name="fillColors", type="Array")] /** * If true, a label is displayed on each marker. The label text is created * with the labelFunction property of the series. The default label function * sets the label to the percentage value of the item. * * @default false * @see PieSeries#labelFunction */ [Style(name="showLabels", type="Boolean")] /** * If true, marker labels that overlap previously-created labels will be * hidden to improve readability. * * @default true */ [Style(name="hideOverlappingLabels", type="Boolean")] /** * Renders data points as a series of pie-like wedges. * * @author Josh Tynjala */ public class PieSeries extends Series implements ICategorySeries { //-------------------------------------- // Class Variables //-------------------------------------- /** * @private */ private static var defaultStyles:Object = { fillColors: [ 0x00b8bf, 0x8dd5e7, 0xc0fff6, 0xffa928, 0xedff9f, 0xd00050, 0xc6c6c6, 0xc3eafb, 0xfcffad, 0xcfff83, 0x444444, 0x4d95dd, 0xb8ebff, 0x60558f, 0x737d7e, 0xa64d9a, 0x8e9a9b, 0x803e77 ], markerSkins: [RectangleSkin], showLabels: false, hideOverlappingLabels: true //see textFormat default style defined in constructor below //works around stylemanager global style bug! }; /** * @private */ private static const RENDERER_STYLES:Object = { fillColor: "fillColors", skin: "markerSkins" }; //-------------------------------------- // Class Methods //-------------------------------------- /** * @copy fl.core.UIComponent#getStyleDefinition() */ public static function getStyleDefinition():Object { return mergeStyles(defaultStyles, Series.getStyleDefinition()); } //-------------------------------------- // Constructor //-------------------------------------- /** * Constructor. */ public function PieSeries(data:Object = null) { super(data); //we have to set this as an instance style because textFormat is //defined as a global style in StyleManager, and that takes //precedence over shared/class styles this.setStyle("textFormat", new TextFormat("_sans", 11, 0x000000, true, false, false, "", "", TextFormatAlign.LEFT, 0, 0, 0, 0)); } //-------------------------------------- // Properties //-------------------------------------- /** * @private * The text fields used to display labels over each marker. */ protected var labels:Array = []; /** * @private * Holds the labels created by the previous redraw so that they can * be reused. */ protected var labelsCache:Array; /** * @private * Storage for the masks that define the shapes of the markers. */ protected var markerMasks:Array = []; /** * @private * The Animation instance that controls animation in this series. */ private var _animation:Animation; /** * @private */ private var _previousData:Array = []; /** * @private * Storage for the dataField property. */ private var _dataField:String; /** * The field used to access data for this series. */ public function get dataField():String { return this._dataField; } /** * @private */ public function set dataField(value:String):void { if(this._dataField != value) { this._dataField = value; this.dispatchEvent(new Event("dataChange")); this.invalidate(InvalidationType.DATA); } } /** * @private * Storage for the categoryField property. */ private var _categoryField:String; /** * @copy com.yahoo.astra.fl.charts.series.ICategorySeries#categoryField */ public function get categoryField():String { return this._categoryField; } /** * @private */ public function set categoryField(value:String):void { if(this._categoryField != value) { this._categoryField = value; this.dispatchEvent(new Event("dataChange")); this.invalidate(InvalidationType.DATA); } } /** * @private * Storage for the categoryNames property. */ private var _categoryNames:Array; /** * @copy com.yahoo.astra.fl.charts.series.ICategorySeries#categoryNames */ public function get categoryNames():Array { return this._categoryNames; } /** * @private */ public function set categoryNames(value:Array):void { this._categoryNames = value; } /** * @private * Storage for the labelFunction property. */ private var _labelFunction:Function = defaultLabelFunction; /** * A function may be set to determine the text value of the labels. * * <pre>function labelFunction(item:Object):String</pre> */ public function get labelFunction():Function { return this._labelFunction; } /** * @private */ public function set labelFunction(value:Function):void { this._labelFunction = value; this.invalidate(); } //-------------------------------------- // Public Methods //-------------------------------------- /** * @inheritDoc */ override public function clone():ISeries { var series:PieSeries = new PieSeries(); if(this.dataProvider is Array) { //copy the array rather than pass it by reference series.dataProvider = (this.dataProvider as Array).concat(); } else if(this.dataProvider is XMLList) { series.dataProvider = (this.dataProvider as XMLList).copy(); } series.displayName = this.displayName; return series; } /** * Converts an item to it's value. */ public function itemToData(item:Object):Number { var primaryDataField:String = PieChart(this.chart).seriesToDataField(this); if(primaryDataField) { return Number(item[primaryDataField]); } return Number(item); } /** * Converts an item to the category in which it is displayed. */ public function itemToCategory(item:Object, index:int):String { var primaryCategoryField:String = PieChart(this.chart).seriesToCategoryField(this); if(primaryCategoryField) { return item[primaryCategoryField]; } if(this._categoryNames && index >= 0 && index < this._categoryNames.length) { return this._categoryNames[index]; } return index.toString(); } /** * Converts an item's value to its percentage equivilent. */ public function itemToPercentage(item:Object):Number { var totalValue:Number = this.calculateTotalValue(); if(totalValue == 0) { return 0; } return 100 * (this.itemToData(item) / totalValue); } /** * @inheritDoc */ public function createLegendItemData():Array { var items:Array = []; var markerSkins:Array = this.getStyleValue("markerSkins") as Array; var fillColors:Array = this.getStyleValue("fillColors") as Array; var legendItemCount:int = this.length; for(var i:int = 0; i < legendItemCount; i++) { var item:Object = this.dataProvider[i]; var categoryName:String = this.itemToCategory(item, i); var markerSkin:Object = markerSkins[i % markerSkins.length] var fillColor:uint = fillColors[i % fillColors.length]; var data:LegendItemData = new LegendItemData(categoryName, markerSkin, fillColor); items.push(data); } return items; } //-------------------------------------- // Protected Methods //-------------------------------------- /** * @private */ override protected function draw():void { var stylesInvalid:Boolean = this.isInvalid(InvalidationType.STYLES); var sizeInvalid:Boolean = this.isInvalid(InvalidationType.SIZE); var dataInvalid:Boolean = this.isInvalid(InvalidationType.DATA); super.draw(); this.drawMarkers(stylesInvalid, sizeInvalid); var showLabels:Boolean = this.getStyleValue("showLabels") as Boolean; this.createLabelCache(); if(showLabels) { this.drawLabels(); } this.clearLabelCache(); this.beginAnimation(); } /** * @private * All markers are removed from the display list. */ override protected function removeAllMarkers():void {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -