📄 defaultaxisrenderer.as
字号:
} } } /** * @private * Draws the axis origin line. */ protected function drawAxis():void { var showAxis:Boolean = this.getStyleValue("showAxis") as Boolean; if(!showAxis) { return; } var axisWeight:int = this.getStyleValue("axisWeight") as int; var axisColor:uint = this.getStyleValue("axisColor") as uint; this.graphics.lineStyle(axisWeight, axisColor); if(this.orientation == AxisOrientation.VERTICAL) { //we round these values because that's what the Flash CS3 components do //with positions var verticalX:Number = this.contentBounds.x; var verticalStart:Number = this.contentBounds.y; var verticalEnd:Number = this.contentBounds.y + this.contentBounds.height; this.graphics.moveTo(verticalX, verticalStart); this.graphics.lineTo(verticalX, verticalEnd); } else //horizontal { var horizontalY:Number = this.contentBounds.y + this.contentBounds.height; var horizontalStart:Number = this.contentBounds.x; var horizontalEnd:Number = this.contentBounds.x + this.contentBounds.width; this.graphics.moveTo(horizontalStart, horizontalY); this.graphics.lineTo(horizontalEnd, horizontalY); } } /** * @private * Draws a set of ticks on the axis. */ protected function drawTicks(data:Array, showTicks:Boolean, tickPosition:String, tickLength:Number, tickWeight:Number, tickColor:uint):void { if(!showTicks) { return; } this.graphics.lineStyle(tickWeight, tickColor); var dataCount:int = data.length; for(var i:int = 0; i < dataCount; i++) { var axisData:AxisData = AxisData(data[i]); if(isNaN(axisData.position)) { //skip bad positions continue; } var position:Number = axisData.position; if(this.orientation == AxisOrientation.VERTICAL) { position += this.contentBounds.y; } else { position += this.contentBounds.x; } position = position; switch(tickPosition) { case TickPosition.OUTSIDE: { if(this.orientation == AxisOrientation.VERTICAL) { this.graphics.moveTo(this.contentBounds.x - tickLength, position); this.graphics.lineTo(this.contentBounds.x, position); } else { this.graphics.moveTo(position, this.contentBounds.y + this.contentBounds.height); this.graphics.lineTo(position, this.contentBounds.y + this.contentBounds.height + tickLength); } break; } case TickPosition.INSIDE: { if(this.orientation == AxisOrientation.VERTICAL) { this.graphics.moveTo(this.contentBounds.x, position); this.graphics.lineTo(this.contentBounds.x + tickLength, position); } else { this.graphics.moveTo(position, this.contentBounds.y + this.contentBounds.height - tickLength); this.graphics.lineTo(position, this.contentBounds.y + this.contentBounds.height); } break; } default: //CROSS { if(this.orientation == AxisOrientation.VERTICAL) { this.graphics.moveTo(this.contentBounds.x - tickLength / 2, position); this.graphics.lineTo(this.contentBounds.x + tickLength / 2, position); } else { this.graphics.moveTo(position, this.contentBounds.y + this.contentBounds.height - tickLength / 2); this.graphics.lineTo(position, this.contentBounds.y + this.contentBounds.height + tickLength / 2); } break; } } } } /** * @private * Saves the label TextFields so that they may be reused. */ protected function createCache():void { this._labelCache = this.labelTextFields.concat(); this.labelTextFields = []; } /** * @private * Removes unused label TextFields. */ protected function clearCache():void { var cacheLength:int = this._labelCache.length; for(var i:int = 0; i < cacheLength; i++) { var label:TextField = TextField(this._labelCache.shift()); this.removeChild(label); } } /** * @private * If labels overlap, some may need to be hidden. */ protected function handleOverlappingLabels():void { var showLabels:Boolean = this.getStyleValue("showLabels"); var hideOverlappingLabels:Boolean = this.getStyleValue("hideOverlappingLabels"); if(!showLabels || !hideOverlappingLabels) { return; } //sort the labels array so that they're in visual order //it doesn't matter if we change the indexes in this Array /*if(this.orientation == AxisOrientation.VERTICAL) { //be sure to reverse if we have a vertical orientation //because the axis starts from the bottom this.labelTextFields = this.labelTextFields.sortOn("y", Array.NUMERIC).reverse(); } else { this.labelTextFields = this.labelTextFields.sortOn("x", Array.NUMERIC); }*/ var labelRotation:Number = this.getStyleValue("labelRotation") as Number; var lastVisibleLabel:TextField; var labelCount:int = this.labelTextFields.length; for(var i:int = 0; i < labelCount; i++) { var index:int = labelRotation >= 0 ? i : (labelCount - i - 1); var label:TextField = TextField(this.labelTextFields[index]); label.visible = true; if(lastVisibleLabel) { if(this.orientation == AxisOrientation.HORIZONTAL) { if(labelRotation >= 0) { var xDifference:Number = label.x - lastVisibleLabel.x; } else { xDifference = (lastVisibleLabel.x + lastVisibleLabel.textWidth) - (label.x + label.textWidth); } if(lastVisibleLabel.textWidth > xDifference) { var offset:Point = Point.polar(xDifference, GeomUtil.degreesToRadians(labelRotation)); if(Math.abs(offset.y) <= label.textHeight) { label.visible = false; } } } else //vertical { if(labelRotation >= 0) { var yDifference:Number = lastVisibleLabel.y - label.y; } else { yDifference = (lastVisibleLabel.y + lastVisibleLabel.textHeight) - (label.y + label.textHeight); } if(lastVisibleLabel.textHeight > yDifference) { offset = Point.polar(yDifference, GeomUtil.degreesToRadians(labelRotation)); if(offset.x <= label.textWidth) { //label.visible = false; } } } } if(label.visible) { lastVisibleLabel = label; } } } /** * @private * Creates the labels, sets their text and styles them. Positions the labels too. */ protected function updateLabels(data:Array, showLabels:Boolean, textFormat:TextFormat, labelDistance:Number, labelRotation:Number, embedFonts:Boolean):void { if(!showLabels) { return; } var dataCount:int = data.length; for(var i:int = 0; i < dataCount; i++) { var axisData:AxisData = AxisData(data[i]); var position:Number = axisData.position; if(isNaN(position)) { //skip bad positions continue; } var label:TextField = this.getLabel(); label.defaultTextFormat = textFormat; label.embedFonts = embedFonts; label.rotation = 0; label.text = axisData.label; this.labelTextFields.push(label); } this.positionLabels(data, showLabels, labelDistance, labelRotation, embedFonts); } /** * @private * Positions a set of labels on the axis. */ protected function positionLabels(labels:Array, showLabels:Boolean, labelDistance:Number, labelRotation:Number, embedFonts:Boolean):void { var labelCount:int = this.labelTextFields.length; for(var i:int = 0; i < labelCount; i++) { var label:TextField = TextField(this.labelTextFields[i]); label.rotation = 0; var axisData:AxisData = AxisData(this.ticks[i]); var position:Number = axisData.position; if(this.orientation == AxisOrientation.VERTICAL) { position += this.contentBounds.y; if(showLabels) { label.x = this.contentBounds.x - label.width - labelDistance; label.y = position - label.height / 2; } if(!embedFonts || labelRotation == 0) { //do nothing. already ideally positioned } else if(labelRotation < 90 && labelRotation > 0) { label.x -= (label.height * labelRotation / 180); DynamicRegistration.rotate(label, new Point(label.width, label.height / 2), labelRotation); } else if(labelRotation > -90 && labelRotation < 0) { label.x -= (label.height * Math.abs(labelRotation) / 180); DynamicRegistration.rotate(label, new Point(label.width, label.height / 2), labelRotation); } else if(labelRotation == -90) { label.y -= label.width / 2; label.x -= (label.height * Math.abs(labelRotation) / 180); DynamicRegistration.rotate(label, new Point(label.width, label.height / 2), labelRotation); } else //90 { label.y += label.width / 2; label.x -= (label.height * Math.abs(labelRotation) / 180); DynamicRegistration.rotate(label, new Point(label.width, label.height / 2), labelRotation); } } else //horizontal { position += this.contentBounds.x; if(showLabels) { label.y = this.contentBounds.height + labelDistance; } if(embedFonts && labelRotation > 0) { label.x = position; label.y -= (label.height * labelRotation / 180); DynamicRegistration.rotate(label, new Point(0, label.height / 2), labelRotation); } else if(embedFonts && labelRotation < 0) { label.x = position - label.width; label.y -= (label.height * Math.abs(labelRotation) / 180); DynamicRegistration.rotate(label, new Point(label.width, label.height / 2), labelRotation); } else //labelRotation == 0 { label.x = position - label.width / 2; } } } this.handleOverlappingLabels(); } /** * @private * Either creates a new label TextField or retrieves one from the cache. */ protected function getLabel():TextField { if(this._labelCache.length > 0) { return TextField(this._labelCache.shift()); } var label:TextField = new TextField(); label.selectable = false; label.autoSize = TextFieldAutoSize.LEFT; this.addChild(label); return label; } /** * @private * Determines the rectangular bounds where data may be drawn within this axis. */ protected function calculateContentBounds():void { var oldContentBounds:Rectangle = this.contentBounds; this._contentBounds = new Rectangle(0, 0, this.width, this.height); var overflowEnabled:Boolean = this.getStyleValue("overflowEnabled"); if(overflowEnabled) { return; } var left:Number = 0; var top:Number = 0; var labelCount:int = this.labelTextFields.length; for(var i:int = 0; i < labelCount; i++) { var label:TextField = TextField(this.labelTextFields[i]); if(!label.visible) { continue; } var labelBounds:Rectangle = label.getBounds(this); var xLabelOffset:Number = oldContentBounds.x - labelBounds.x; if(xLabelOffset > 0) { left = Math.max(left, xLabelOffset); } var yLabelOffset:Number = oldContentBounds.y - labelBounds.y; if(yLabelOffset > 0) { top = Math.max(top, yLabelOffset); } } left = Math.min(left, this._contentBounds.width); top = Math.min(top, this._contentBounds.height); this._contentBounds.x += left; this._contentBounds.y += top; this._contentBounds.width -= left; this._contentBounds.height -= top; var right:Number = 0; var bottom:Number = 0; for(i = 0; i < labelCount; i++) { label = TextField(this.labelTextFields[i]); if(!label.visible) { continue; } labelBounds = label.getBounds(this); var xLabelOverflow:Number = (labelBounds.x + labelBounds.width) - (oldContentBounds.x + oldContentBounds.width); if(xLabelOverflow > 0) { right = Math.max(right, xLabelOverflow); } var yLabelOverflow:Number = (labelBounds.y + labelBounds.height) - (oldContentBounds.y + oldContentBounds.height); if(yLabelOverflow > 0) { bottom = Math.max(bottom, yLabelOverflow); } } //don't let the axis calculate a negative size. //this should let it fail gracefully right = Math.min(right, this._contentBounds.width); bottom = Math.min(bottom, this._contentBounds.height); this._contentBounds.width -= right; this._contentBounds.height -= bottom; var showTicks:Boolean = this.getStyleValue("showTicks") as Boolean; var showMinorTicks:Boolean = this.getStyleValue("showMinorTicks") as Boolean; var tickLength:Number = this.getStyleValue("tickLength") as Number; var minorTickLength:Number = this.getStyleValue("minorTickLength") as Number; var tickPosition:String = this.getStyleValue("tickPosition") as String; var minorTickPosition:String = this.getStyleValue("minorTickPosition") as String; if(this.orientation == AxisOrientation.VERTICAL) { var tickContentBoundsX:Number = 0; if(showTicks) { switch(tickPosition) { case TickPosition.OUTSIDE: case TickPosition.CROSS: tickContentBoundsX = tickLength; break; } } if(showMinorTicks) { switch(minorTickPosition) { case TickPosition.OUTSIDE: case TickPosition.CROSS: tickContentBoundsX = Math.max(tickContentBoundsX, minorTickLength); } } tickContentBoundsX = Math.min(tickContentBoundsX, this.contentBounds.width); this._contentBounds.x += tickContentBoundsX; this._contentBounds.width -= tickContentBoundsX; } else { var tickHeight:Number = 0; if(showTicks) { switch(tickPosition) { case TickPosition.OUTSIDE: case TickPosition.CROSS: tickHeight = tickLength; break; } } if(showMinorTicks) { switch(minorTickPosition) { case TickPosition.OUTSIDE: case TickPosition.CROSS: tickHeight = Math.max(tickHeight, minorTickLength); break; } } tickHeight = Math.min(tickHeight, this.contentBounds.height); this._contentBounds.height -= tickHeight; } //account for the axis title var showTitle:Boolean = this.getStyleValue("showTitle") as Boolean; if(showTitle && this.title) { if(this.orientation == AxisOrientation.VERTICAL) { var titleContentBoundsX:Number = Math.min(this.titleTextField.width, this.contentBounds.width); this._contentBounds.x += titleContentBoundsX; this._contentBounds.width -= titleContentBoundsX; } else { var titleHeight:Number = Math.min(this.titleTextField.height, this.contentBounds.height); this._contentBounds.height -= titleHeight; } } //correct for CS3 pixel rounding var oldX:Number = this._contentBounds.x; this._contentBounds.x = Math.round(this._contentBounds.x); if(oldX > this._contentBounds.x) { this._contentBounds.width = Math.floor(this._contentBounds.width); } else { this._contentBounds.width = Math.ceil(this._contentBounds.width); } var oldY:Number = this._contentBounds.y; this._contentBounds.y = Math.round(this._contentBounds.y); if(oldY > this._contentBounds.y) { this._contentBounds.height = Math.floor(this._contentBounds.height); } else { this._contentBounds.height = Math.ceil(this._contentBounds.height); } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -