To set a title on the x-axis or y-axis use setXtitle ( ) or setYtitle ( ):
var wGraph = new cGraph(400,200); wGraph.setXrange(-3,3); wGraph.drawFunction("sin(Math.PI*x)"); wGraph.setXtitle("x / pi"); wGraph.setYtitle("sin ( x )"); wGraph.printInWindow("","Axis Titles");
It's often useful to label the drawings, especially when there are more than drawing on the graph. To write a label on the last drawing drawn on the graph, use addLabel ( ):
var wGraph = new cGraph(400,200); wGraph.setXrange(-3,3); wGraph.setXtitle("x / pi"); wGraph.drawFunction("sin(Math.PI*x)"); wGraph.addLabel("sin(x)",0.9,0.8); wGraph.drawFunction("cos(Math.PI*x)"); wGraph.addLabel("cos(x)",-0.9,0.9); wGraph.printInWindow("","Labeling the Drawings");
The cGraph method addLabel ( ) adds to the last cDrawing object added to the graph. You can add a label to any drawing any place before print(), using the cDrawing addLabel ( ) method, but this means that you need to identify the drawings (by naming them). The above graph could have been written:
var wGraph = new cGraph(400,200); wGraph.setXrange(-3,3); wGraph.setXtitle("x / pi"); var wFunc1 = wGraph.drawFunction("sin(Math.PI*x)"); var wFunc2 = wGraph.drawFunction("cos(Math.PI*x)"); wFunc1.addLabel("sin(x)",0.9,0.8); wFunc2.addLabel("cos(x)",-0.9,0.9); wGraph.printInWindow("","Labeling the Drawings");
Note that there can only be one label per drawing, and that the label is drawn in the same colour as the drawing itself.
You can only add one label per drawing, but you can write as much free text as you like, using the drawText ( ) method. These texts are not linked to any particular drawing, and they are always coloured black:
var wGraph = new cGraph(200,200); wGraph.setXgrid(0); wGraph.setYstretch(1); wGraph.drawCurve("cos(t)","sin(t)",Math.PI/2,5*Math.PI/2,5); wGraph.drawText("Pentagon",-0.35,0); wGraph.printInWindow("","Text on a Diagram");