Difference between revisions of "Even simpler function plotter"

From JSXGraph Wiki
Jump to navigationJump to search
Line 12: Line 12:
  
 
function plotter() {
 
function plotter() {
   var txt = JXG.GeonextParser.geonext2JS(document.getElementById('input').value);
+
   var txtraw = document.getElementById('input').value;
 +
  var txt = JXG.GeonextParser.geonext2JS(txtraw);
 
   f = new Function('x','var y = '+txt+'; return y;');
 
   f = new Function('x','var y = '+txt+'; return y;');
 
   curve = board.create('functiongraph',[f,
 
   curve = board.create('functiongraph',[f,
Line 23: Line 24:
 
                   return c.usrCoords[1];
 
                   return c.usrCoords[1];
 
                 }
 
                 }
               ],{name:txt, withLabel:true});
+
               ],{name:txtraw, withLabel:true});
 
}
 
}
  
Line 55: Line 56:
 
     }
 
     }
 
}
 
}
 
 
 
</jsxgraph>
 
</jsxgraph>
 
===The underlying JavaScript code===
 
===The underlying JavaScript code===
Line 71: Line 70:
  
 
function plotter() {
 
function plotter() {
   var txt = JXG.GeonextParser.geonext2JS(document.getElementById('input').value);
+
   var txtraw = document.getElementById('input').value;
 +
  var txt = JXG.GeonextParser.geonext2JS(txtraw);
 
   f = new Function('x','var y = '+txt+'; return y;');
 
   f = new Function('x','var y = '+txt+'; return y;');
 
   curve = board.create('functiongraph',[f,
 
   curve = board.create('functiongraph',[f,
Line 81: Line 81:
 
                   var c = new JXG.Coords(JXG.COORDS_BY_SCREEN,[board.canvasWidth,0],board);
 
                   var c = new JXG.Coords(JXG.COORDS_BY_SCREEN,[board.canvasWidth,0],board);
 
                   return c.usrCoords[1];
 
                   return c.usrCoords[1];
                 }]);
+
                 }
 +
              ],{name:txtraw, withLabel:true});
 
}
 
}
  

Revision as of 16:17, 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()"> 
<input type="button" value="add tangent" onClick="addTangent()">
<input type="button" value="add Derivative" onClick="addDerivative()">
var board = JXG.JSXGraph.initBoard('jxgbox', {boundingbox:[-5,8,8,-5], axis:true});
var f, curve; // global objects

function plotter() {
  var txtraw = document.getElementById('input').value;
  var txt = JXG.GeonextParser.geonext2JS(txtraw);
  f = new Function('x','var y = '+txt+'; return y;');
  curve = 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];
                }
              ],{name:txtraw, withLabel:true});
}

function clearAll() {
    JXG.JSXGraph.freeBoard(board);
    board = JXG.JSXGraph.initBoard('jxgbox', {boundingbox:[-5,8,8,-5], axis:true});
    f = null;
    curve = null;
}

function addTangent() {
    if (JXG.isFunction(f)) {
        board.suspendUpdate();
        var p = board.create('glider',[1,0,curve], {name:'drag me'});
        board.create('tangent',[p], {name:'drag me'});
        board.unsuspendUpdate();
    }
}

function addDerivative() {
    if (JXG.isFunction(f)) {
        board.create('functiongraph',[JXG.Math.Numerics.D(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];
                }], {dash:2});
    }
}