Difference between revisions of "Even simpler function plotter"

From JSXGraph Wiki
Jump to navigationJump to search
Line 2: Line 2:
 
<input type="text" id="input" value="sin(x)*x">
 
<input type="text" id="input" value="sin(x)*x">
 
<input type="button" value="plot" onClick="plotter()">
 
<input type="button" value="plot" onClick="plotter()">
 +
<input type="button" value="add tangent" onClick="addTangent()">
 
<input type="button" value="clear all" onClick="clearAll()">  
 
<input type="button" value="clear all" onClick="clearAll()">  
 
</form>
 
</form>
Line 7: Line 8:
 
<jsxgraph width="600" height="400">
 
<jsxgraph width="600" height="400">
 
var board = JXG.JSXGraph.initBoard('jxgbox', {boundingbox:[-5,8,8,-5], axis:true});
 
var board = JXG.JSXGraph.initBoard('jxgbox', {boundingbox:[-5,8,8,-5], axis:true});
 +
var f, curve; // global objects
  
 
function plotter() {
 
function plotter() {
   var t = JXG.GeonextParser.geonext2JS(document.getElementById('input').value);
+
   var txt = JXG.GeonextParser.geonext2JS(document.getElementById('input').value);
   var f = new Function('x','var y = '+t+'; return y;');
+
   f = new Function('x','var y = '+txt+'; return y;');
   board.create('functiongraph',[f,
+
   curve = board.create('functiongraph',[f,
 
                 function(){  
 
                 function(){  
 
                   var c = new JXG.Coords(JXG.COORDS_BY_SCREEN,[0,0],board);
 
                   var c = new JXG.Coords(JXG.COORDS_BY_SCREEN,[0,0],board);
Line 22: Line 24:
 
               ]);
 
               ]);
 
}
 
}
 +
 
function clearAll() {
 
function clearAll() {
 
     JXG.JSXGraph.freeBoard(board);
 
     JXG.JSXGraph.freeBoard(board);
 
     board = JXG.JSXGraph.initBoard('jxgbox', {boundingbox:[-5,8,8,-5], axis:true});
 
     board = JXG.JSXGraph.initBoard('jxgbox', {boundingbox:[-5,8,8,-5], axis:true});
 
}
 
}
 +
 +
function addTangent() {
 +
    board.suspendUpdate();
 +
    var p = board.create('glider',[0,0,curve], {name:'drag me'});
 +
    board.create('tangent',[p], {name:'drag me'});
 +
    board.unsuspendUpdate();
 +
}
 +
 
</jsxgraph>
 
</jsxgraph>
 
===The underlying JavaScript code===
 
===The underlying JavaScript code===

Revision as of 16:05, 15 July 2010

The underlying JavaScript code

<input type="text" id="input" value="sin(x)*x">
<input type="button" value="plot" onClick="plotter()">
<input type="button" value="clear all" onClick="clearAll()">
var board = JXG.JSXGraph.initBoard('jxgbox', {boundingbox:[-5,8,8,-5], axis:true});

function plotter() {
  var t = JXG.GeonextParser.geonext2JS(document.getElementById('input').value);
  var f = new Function('x','var y = '+t+'; return y;');
  board.create('functiongraph',[f,
                function(){ 
                  var c = new JXG.Coords(JXG.COORDS_BY_SCREEN,[0,0],board);
                  return c.usrCoords[1];
                },
                function(){ 
                  var c = new JXG.Coords(JXG.COORDS_BY_SCREEN,[board.canvasWidth,0],board);
                  return c.usrCoords[1];
                }
              ]);
}
function clearAll() {
    JXG.JSXGraph.freeBoard(board);
    board = JXG.JSXGraph.initBoard('jxgbox', {boundingbox:[-5,8,8,-5], axis:true});
}