AS3: Setting a dynamic position for a text field relative to another dynamic text field
I'm creating an XML-driven pie chart in AS3 with 2 text boxes in each pie slice. Both text fields are dynamic in the sense that they are populated by the XML doc and then told where to place themselves in the AS3. I've got them both using the same x and y position to place themselves at the moment (which of course puts them right one top of each other), but I'd like to make one of the fields (which acts like a label or a title to the larger number and % text field) place itself in a particular spot around the other text field. The result I'm looking for is to have the smaller "title" text field appear approximately 5 pixels above and left-justified to the larger "percentage" field. Does that make sense? I am awful at this type of code, so I'm looking to you guys for help. And thanks in advance for that.
Anyway, here are two sections of code that I've currently got. The first chunk, for the Tags portion, sets the position for the "percentage" text field using a good old x and y method. The second chunk, for Titles, is setting the position for the smaller "title" text.
//evaluate tags
private function evaluateTags():void{
for (s=0; s
//setting the position
arTags[s].x= -objPie.radius 开发者_如何学JAVA*
objTag.distance*Math.cos(objData.midAngles[s]+Math.PI/2) - arTags[s].width/2;
arTags[s].y= -objPie.radius *objTag.distance*Math.sin(objData.midAngles[s]+Math.PI/2) - arTags[s].height/2;
//calculatind corner angles
var angO1:Number = (360+90+Math.atan2(arTags[s].y,arTags[s].x)*180/Math.PI)%360
var angO2:Number = (360+90+Math.atan2(arTags[s].y+arTags[s].height,arTags[s].x)*180/Math.PI)%360
var angO3:Number = (360+90+Math.atan2(arTags[s].y,arTags[s].x+arTags[s].width)*180/Math.PI)%360
var angO4:Number = (360+90+Math.atan2(arTags[s].y+arTags[s].height,arTags[s].x+arTags[s].width)*180/Math.PI)%360
//min angle
var minO:Number = Math.min(angO1,angO2);
minO = Math.min(minO,angO3);
minO = Math.min(minO,angO4);
minO = minO * Math.PI / 180;
//max angle
var maxO:Number = Math.max(angO1,angO2);
maxO = Math.max(maxO,angO3);
maxO = Math.max(maxO,angO4);
maxO = maxO * Math.PI / 180;
//evaluating if the tag must be showed or not
if(minO<objData.iniAngles[s] || maxO>objData.endAngles[s]){
arTags[s].textShowed = false;
}else{
arTags[s].textShowed = true;
}
}
}
//evaluate titles
private function evaluateTitles():void{
for (s=0; s<nbS; s++) {
//setting the position
arTitles[s].x= -objPie.radius * objTitles.distance*Math.cos(objData.midAngles[s]+Math.PI/3) - arTitles[s].width/2;
arTitles[s].y= -objPie.radius * objTitles.distance*Math.sin(objData.midAngles[s]+Math.PI/3) - arTitles[s].height/2;
//calculatind corner angles
var angO1:Number = (360+90+Math.atan2(arTitles[s].y,arTitles[s].x)*180/Math.PI)%360
var angO2:Number = (360+90+Math.atan2(arTitles[s].y+arTitles[s].height,arTitles[s].x)*180/Math.PI)%360
var angO3:Number = (360+90+Math.atan2(arTitles[s].y,arTitles[s].x+arTitles[s].width)*180/Math.PI)%360
var angO4:Number = (360+90+Math.atan2(arTitles[s].y+arTitles[s].height,arTitles[s].x+arTitles[s].width)*180/Math.PI)%360
//min angle
var minO:Number = Math.min(angO1,angO2);
minO = Math.min(minO,angO3);
minO = Math.min(minO,angO4);
minO = minO * Math.PI / 180;
//max angle
var maxO:Number = Math.max(angO1,angO2);
maxO = Math.max(maxO,angO3);
maxO = Math.max(maxO,angO4);
maxO = maxO * Math.PI / 180;
//evaluating if the titles must be shown or not
if(minO<objData.iniAngles[s] || maxO>objData.endAngles[s]){
arTitles[s].textShowed = false;
}else{
arTitles[s].textShowed = true;
}
}
}
All good, I figured it out. The solution is:
//evaluate titles
private function evaluateTitles():void{
for (s=0; s<nbS; s++) {
//setting the position
arTitles[s].x= -objPie.radius * objTag.distance*Math.cos(objData.midAngles[s]+Math.PI/2) - arTags[s].width/2;
arTitles[s].y= -objPie.radius * objTag.distance*Math.sin(objData.midAngles[s]+Math.PI/2) - arTags[s].height/2 - arTitles[s].height + 4;
//calculatind corner angles
精彩评论