📄 cartesianchart.as
字号:
var seriesLength:int = currentSeries.length; for(var j:int = 0; j < seriesLength; j++) { var item:Object = currentSeries.dataProvider[j]; if(item is Number || !isNaN(Number(item))) { //if we only have a number, then it is safe to convert //to a default type for a category chart. //if it's not a number, then the user is expected to update //the x and y fields so that the plot area knows how to handle it. var point:Object = {}; point[numericField] = item; //we assume it's a category axis if(this._explicitCategoryNames && this._explicitCategoryNames.length > 0) { point[otherField] = this.categoryNames[j]; } else point[otherField] = j; currentSeries.dataProvider[j] = point; } } } } /** * @private * Creates the default axes. Without user intervention, the x-axis is a category * axis and the y-axis is a numeric axis. */ override protected function configUI():void { super.configUI(); //by default, the x axis is for categories. other types of charts will need //to override this if they need a numeric or other type of axis if(!this.horizontalAxis) { var categoryAxis:CategoryAxis = new CategoryAxis(); this.horizontalAxis = categoryAxis; } if(!this.horizontalAxisRenderer) { var RendererClass:Class = this.getStyleValue("horizontalAxisRenderer") as Class; this.horizontalAxisRenderer = new RendererClass(AxisOrientation.HORIZONTAL); this.addChild(DisplayObject(this.horizontalAxisRenderer)); this.horizontalAxis.renderer = this.horizontalAxisRenderer; } if(!this.verticalAxis) { var numericAxis:NumericAxis = new NumericAxis(); numericAxis.stackingEnabled = true; this.verticalAxis = numericAxis; } if(!this.verticalAxisRenderer) { RendererClass = this.getStyleValue("verticalAxisRenderer") as Class; this.verticalAxisRenderer = new RendererClass(AxisOrientation.VERTICAL); this.addChild(DisplayObject(this.verticalAxisRenderer)); this.verticalAxis.renderer = this.verticalAxisRenderer; } } /** * @private * Determines the text that will appear on the data tip. */ override protected function defaultDataTipFunction(item:Object, index:int, series:ISeries):String { var text:String = super.defaultDataTipFunction(item, index, series); if(text.length > 0) { text += "\n"; } var categoryAxis:CategoryAxis = this.verticalAxis as CategoryAxis; var otherAxis:IAxis = this.horizontalAxis; if(!categoryAxis) { categoryAxis = this.horizontalAxis as CategoryAxis; otherAxis = this.verticalAxis; } //if we have a category axis, the category is always displayed first if(categoryAxis) { var categoryValue:Object = this.itemToAxisValue(series, index, categoryAxis, false); text += categoryAxis.valueToLabel(categoryValue) + "\n"; var otherValue:Object = this.itemToAxisValue(series, index, otherAxis, false); text += otherAxis.valueToLabel(otherValue) + "\n"; } //otherwise, display the horizontal axis value first else { var horizontalValue:Object = this.itemToAxisValue(series, index, this.horizontalAxis, false); text += horizontalAxis.valueToLabel(horizontalValue) + "\n"; var verticalValue:Object = this.itemToAxisValue(series, index, this.verticalAxis, false); text += verticalAxis.valueToLabel(verticalValue) + "\n"; } return text; } /** * @private * Positions and updates the series objects. */ protected function drawSeries():void { var contentPadding:Number = this.getStyleValue("contentPadding") as Number; var seriesWidth:Number = this._contentBounds.width; var seriesHeight:Number = this._contentBounds.height; var contentScrollRect:Rectangle = new Rectangle(0, 0, seriesWidth, seriesHeight); this.content.x = contentPadding + this._contentBounds.x; this.content.y = contentPadding + this._contentBounds.y; this.content.scrollRect = contentScrollRect; var seriesCount:int = this.series.length; for(var i:int = 0; i < seriesCount; i++) { var series:UIComponent = this.series[i] as UIComponent; series.width = seriesWidth; series.height = seriesHeight; series.drawNow(); } } /** * @private * Removes the old axis renderers and create new instances. */ protected function updateRenderers():void { //create axis renderers if(this.horizontalAxisRenderer) { this.removeChild(DisplayObject(this.horizontalAxisRenderer)); this.horizontalAxisRenderer = null; } var RendererClass:Class = this.getStyleValue("horizontalAxisRenderer") as Class; this.horizontalAxisRenderer = new RendererClass(AxisOrientation.HORIZONTAL); this.addChild(DisplayObject(this.horizontalAxisRenderer)); this.copyStylesToChild(UIComponent(this.horizontalAxisRenderer), CartesianChart.HORIZONTAL_AXIS_STYLES); this.copyStyleObjectToChild(UIComponent(this.horizontalAxisRenderer), this.getStyleValue("horizontalAxisStyles")); var horizontalAxisTextFormat:TextFormat = this.getStyleValue("horizontalAxisTextFormat") as TextFormat; if(horizontalAxisTextFormat) { UIComponent(this.horizontalAxisRenderer).setStyle("textFormat", horizontalAxisTextFormat); } if(this.verticalAxisRenderer) { this.removeChild(DisplayObject(this.verticalAxisRenderer)); this.verticalAxisRenderer = null; } RendererClass = this.getStyleValue("verticalAxisRenderer") as Class; this.verticalAxisRenderer = new RendererClass(AxisOrientation.VERTICAL); this.addChild(DisplayObject(this.verticalAxisRenderer)); this.copyStylesToChild(UIComponent(verticalAxisRenderer), CartesianChart.VERTICAL_AXIS_STYLES); this.copyStyleObjectToChild(UIComponent(this.verticalAxisRenderer), this.getStyleValue("verticalAxisStyles")); var verticalAxisTextFormat:TextFormat = this.getStyleValue("verticalAxisTextFormat") as TextFormat; if(verticalAxisTextFormat) { UIComponent(this.verticalAxisRenderer).setStyle("textFormat", verticalAxisTextFormat); } //create grid lines renderers if(this.horizontalGridLines) { this.removeChild(DisplayObject(this.horizontalGridLines)); } RendererClass = this.getStyleValue("horizontalAxisGridLinesRenderer") as Class; this.horizontalGridLines = new RendererClass(); this.horizontalGridLines.axisRenderer = this.horizontalAxisRenderer; this.addChild(DisplayObject(this.horizontalGridLines)); this.copyStylesToChild(UIComponent(this.horizontalGridLines), CartesianChart.HORIZONTAL_GRID_LINES_STYLES); this.copyStyleObjectToChild(UIComponent(this.horizontalGridLines), this.getStyleValue("horizontalAxisGridLinesStyles")); if(this.verticalGridLines) { this.removeChild(DisplayObject(this.verticalGridLines)); } RendererClass = this.getStyleValue("verticalAxisGridLinesRenderer") as Class; this.verticalGridLines = new RendererClass(); this.verticalGridLines.axisRenderer = this.verticalAxisRenderer; this.addChild(DisplayObject(this.verticalGridLines)); this.copyStylesToChild(UIComponent(this.verticalGridLines), CartesianChart.VERTICAL_GRID_LINES_STYLES); this.copyStyleObjectToChild(UIComponent(this.verticalGridLines), this.getStyleValue("verticalAxisGridLinesStyles")); } /** * @private * Positions and sizes the axes based on their edge metrics. */ protected function drawAxes():void { var contentPadding:Number = this.getStyleValue("contentPadding") as Number; var axisWidth:Number = this.width - (2 * contentPadding); var axisHeight:Number = this.height - (2 * contentPadding); this.horizontalAxis.renderer = this.horizontalAxisRenderer; if(horizontalAxis is CategoryAxis && this._explicitCategoryNames && this._explicitCategoryNames.length > 0) { CategoryAxis(this.horizontalAxis).categoryNames = this._explicitCategoryNames; } var horizontalAxisRenderer:UIComponent = UIComponent(this.horizontalAxisRenderer); horizontalAxisRenderer.move(contentPadding, contentPadding); horizontalAxisRenderer.setSize(axisWidth, axisHeight); this.setChildIndex(horizontalAxisRenderer, this.numChildren - 1); this.verticalAxis.renderer = this.verticalAxisRenderer; if(verticalAxis is CategoryAxis && this._explicitCategoryNames && this._explicitCategoryNames.length > 0) { CategoryAxis(this.verticalAxis).categoryNames = this._explicitCategoryNames; } var verticalAxisRenderer:UIComponent = UIComponent(this.verticalAxisRenderer); verticalAxisRenderer.move(contentPadding, contentPadding); verticalAxisRenderer.setSize(axisWidth, axisHeight); this.setChildIndex(verticalAxisRenderer, this.numChildren - 1); //TODO: This should probably be done differently... this.horizontalAxisRenderer.title = this.horizontalAxis.title; this.verticalAxisRenderer.title = this.verticalAxis.title; this.updateAxisScalesAndBounds(); horizontalAxisRenderer.drawNow(); verticalAxisRenderer.drawNow(); this.drawGridLines(); } /** * @private * Determines the axis scales, and positions the axes based on their * <code>contentBounds</code> properties. */ protected function updateAxisScalesAndBounds():void { //reset the ticks and minor ticks (start with a clean axis) this.horizontalAxisRenderer.ticks = []; this.horizontalAxisRenderer.minorTicks = []; this.verticalAxisRenderer.ticks = []; this.verticalAxisRenderer.minorTicks = []; /* we need to run this a few times because the axis positions and sizes are slowly corrected until they properly align themselves. NOTE: If this seems to be failing, increase the loop count. more iterations == more accuracy worst case scenario: use a while loop and check to see if calculateContentBounds() has made changes to the sizes or positions of the renderers. if not, then break. if there have been corrections, keep going. I suggest that you stop the loop at 10 iterations because that's most likely an infinite loop. you're probably only running into rounding errors at that point, so there's little reason to continue anyway. */ this.calculateContentBounds(); var count:int = 0; do { var hOldContentBounds:Rectangle = this.horizontalAxisRenderer.contentBounds.clone(); var vOldContentBounds:Rectangle = this.verticalAxisRenderer.contentBounds.clone(); this._horizontalAxis.updateScale(this.series); this._verticalAxis.updateScale(this.series); this.calculateContentBounds(); count++; } //if count == 10, we're close enough while(count < 10 && (!hOldContentBounds.equals(this.horizontalAxisRenderer.contentBounds) || !vOldContentBounds.equals(this.verticalAxisRenderer.contentBounds))) } /** * @private * Combine the content bounds to determine the series positioning. */ protected function calculateContentBounds():void { this.horizontalAxisRenderer.updateBounds(); this.verticalAxisRenderer.updateBounds(); var contentPadding:Number = this.getStyleValue("contentPadding") as Number; var axisWidth:Number = this.width - (2 * contentPadding); var axisHeight:Number = this.height - (2 * contentPadding); var horizontalAxisRenderer:UIComponent = this.horizontalAxisRenderer as UIComponent; var verticalAxisRenderer:UIComponent = this.verticalAxisRenderer as UIComponent; var horizontalBounds:Rectangle = this.horizontalAxisRenderer.contentBounds; var verticalBounds:Rectangle = this.verticalAxisRenderer.contentBounds; this._contentBounds = new Rectangle(); this._contentBounds.x = Math.max(horizontalBounds.x, verticalBounds.x); this._contentBounds.y = Math.max(horizontalBounds.y, verticalBounds.y); this._contentBounds.width = Math.min(horizontalBounds.width, verticalBounds.width); this._contentBounds.height = Math.min(horizontalBounds.height, verticalBounds.height); var hRight:Number = horizontalAxisRenderer.width - horizontalBounds.width - horizontalBounds.x; var hBottom:Number = horizontalAxisRenderer.height - horizontalBounds.height - horizontalBounds.y; var vRight:Number = verticalAxisRenderer.width - verticalBounds.width - verticalBounds.x; var vBottom:Number = verticalAxisRenderer.height - verticalBounds.height - verticalBounds.y; var contentBottom:Number = Math.max(hBottom, vBottom); var contentRight:Number = Math.max(hRight, vRight); horizontalAxisRenderer.x = contentPadding + this._contentBounds.x - horizontalBounds.x; horizontalAxisRenderer.y = contentPadding + this._contentBounds.y - horizontalBounds.y; horizontalAxisRenderer.width = axisWidth - (contentRight - hRight) - (this._contentBounds.x - horizontalBounds.x); horizontalAxisRenderer.height = axisHeight - (contentBottom - hBottom) - (this._contentBounds.y - horizontalBounds.y); verticalAxisRenderer.x = contentPadding + this._contentBounds.x - verticalBounds.x; verticalAxisRenderer.y = contentPadding + this._contentBounds.y - verticalBounds.y; verticalAxisRenderer.width = axisWidth - (contentRight - vRight) - (this._contentBounds.x - verticalBounds.x); verticalAxisRenderer.height = axisHeight - (contentBottom - vBottom) - (this._contentBounds.y - verticalBounds.y); } /** * @private * Draws the axis grid lines, if they exist. */ protected function drawGridLines():void { var contentPadding:Number = this.getStyleValue("contentPadding") as Number; var horizontalAxisRenderer:UIComponent = this.horizontalAxisRenderer as UIComponent; var verticalAxisRenderer:UIComponent = this.verticalAxisRenderer as UIComponent; var index:int = 0; if(this.background) { index++; } if(this.horizontalGridLines) { var horizontalGridLines:UIComponent = this.horizontalGridLines as UIComponent; this.setChildIndex(horizontalGridLines, index++); horizontalGridLines.x = contentPadding + this.contentBounds.x; horizontalGridLines.y = contentPadding + this.contentBounds.y; horizontalGridLines.drawNow(); } if(this.verticalGridLines) { var verticalGridLines:UIComponent = this.verticalGridLines as UIComponent; this.setChildIndex(verticalGridLines, index++); verticalGridLines.x = contentPadding + this.contentBounds.x; verticalGridLines.y = contentPadding + this.contentBounds.y; verticalGridLines.drawNow(); } } /** * @private */ protected function setComplexStyle(complexName:String, subStyleName:String, subStyleValue:Object):void { var container:Object = this.getStyleValue(complexName); var copy:Object = {}; for(var prop:String in container) { copy[prop] = container[prop]; } copy[subStyleName] = subStyleValue; this.setStyle(complexName, copy); } /** * @private */ protected function copyStyleObjectToChild(child:UIComponent, styles:Object):void { if(!child) { return; } for(var prop:String in styles) { child.setStyle(prop, styles[prop]); } } /** * @private */ protected function axisAndSeriesToField(axis:IAxis, series:ISeries):String { var cartesianSeries:CartesianSeries = series as CartesianSeries; var field:String = this.axisToField(axis); var renderer:ICartesianAxisRenderer = this.axisToAxisRenderer(axis); if(renderer.orientation == AxisOrientation.VERTICAL && cartesianSeries.verticalField) { field = cartesianSeries.verticalField; } else if(renderer.orientation == AxisOrientation.HORIZONTAL && cartesianSeries.horizontalField) { field = cartesianSeries.horizontalField; } return field; } /** * @private */ protected function axisToField(axis:IAxis):String { if(axis == this.horizontalAxis) { return this.horizontalField; } else if(axis == this.verticalAxis) { return this.verticalField; } return null; } /** * @private */ protected function fieldToAxis(field:String):IAxis { if(field == this.horizontalField) { return this.horizontalAxis; } else if(field == this.verticalField) { return this.verticalAxis; } return null; } /** * @private * Finds the renderer for the specified axis. */ protected function axisToAxisRenderer(axis:IAxis):ICartesianAxisRenderer { if(axis == this.horizontalAxis) { return this.horizontalAxisRenderer; } else if(axis == this.verticalAxis) { return this.verticalAxisRenderer; } return null; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -