Difference between revisions of "Even simpler function plotter"

From JSXGraph Wiki
Jump to navigationJump to search
Line 28: Line 28:
 
     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});
 +
    f = null;
 +
    curve = null;
 
}
 
}
  
 
function addTangent() {
 
function addTangent() {
     board.suspendUpdate();
+
     if (JXG.isFunction(f)) {
    var p = board.create('glider',[0,0,curve], {name:'drag me'});
+
        board.suspendUpdate();
    board.create('tangent',[p], {name:'drag me'});
+
        var p = board.create('glider',[0,0,curve], {name:'drag me'});
    board.unsuspendUpdate();
+
        board.create('tangent',[p], {name:'drag me'});
 +
        board.unsuspendUpdate();
 +
    }
 
}
 
}
  

Revision as of 16:06, 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});
}