How to include text in a chart/drawing?
Hi guys
I'm sure you gurus can help me out here:cool:
I'm pretty new to AS and developing a chart. My problem now is: how can I add simple text to the drawing :shifty:?
As the coding for the chart is already rather complex, I've made a sample script to play around with.
My empty MyChartTest.fla calls MyChartTest.as (properties):
Code:
package {
import flash.display.Sprite;
public class MyChartTest extends Sprite {
public function MyChartTest() {
var myChart:MyChart = new MyChart();
addChild(myChart);
myChart.x = stage.stageWidth / 2;
myChart.y = stage.stageHeight / 2;
}
}
}
The class MyChart draws the circle:
Code:
package {
import flash.display.Shape;
public class MyChart extends Shape {
private var _r:uint = 120;
private var _okColor:uint = 0x33CC00;
public function MyChart() {
with (graphics) {
lineStyle(2);
beginFill(_okColor, 0.9);
drawCircle(0, 0, _r);
endFill();
}
}
}
}
I have now a nice green circle :) Let's say, I want to add my text centered in the lower part of the circle, how would I do that?
OK, this would be a class for drawing a sample test:
Code:
package {
import flash.display.Sprite;
import flash.display.LoaderInfo;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
public class ChartTextTest extends Sprite {
private var _MemText:TextField;
public function ChartTextTest() {
configureMemText();
_MemText.appendText("blabla");
}
private function configureMemText():void {
_MemText = new TextField ;
_MemText.autoSize = TextFieldAutoSize.LEFT;
_MemText.background = true;
_MemText.border = false;
var format:TextFormat = new TextFormat ;
format.font = "Verdana";
format.color = 0xFF0000;
format.size = 12;
format.underline = false;
format.bold = true;
_MemText.defaultTextFormat = format;
addChild(_MemText);
}
}
}
if I extend MyChartTest.as to also draw my text....
Code:
package {
import flash.display.Sprite;
public class MyChartTest extends Sprite {
public function MyChartTest() {
var myChart:MyChart = new MyChart();
addChild(myChart);
myChart.x = stage.stageWidth / 2;
myChart.y = stage.stageHeight / 2;
var myText:ChartTextTest = new ChartTextTest(); // added
addChild(myText); // added
}
}
}
I get my text in the upper left corner of the stage... I just have the feeling that this is not the right way anyway... It would be ok for placing a title or so, but not text belonging to the chart. The drawing of the text should somehow be part of my charting class - not an 'add on'...
when I add the lines
Code:
var myText:ChartTextTest = new ChartTextTest();
addChild(myText);
to the MyChart class, I get the error "1180: Call to a possibly undefined method addChild." :sick:
Code:
package {
import flash.display.Shape;
import flash.display.Sprite; // added
public class MyChart extends Shape {
private var _r:uint = 120;
private var _okColor:uint = 0x33CC00;
public function MyChart() {
with (graphics) {
lineStyle(2);
beginFill(_okColor, 0.9);
drawCircle(0, 0, _r);
endFill();
}
var myText:ChartTextTest = new ChartTextTest(); // added
addChild(myText); // added
}
}
}
Can I have a class extend Shape AND Sprite?
How would you guys do that? What would be best practice?
Thanks a lot
gsub