JSXGraph logo
JSXGraph
JSXGraph share

Share

Simple function plotter
QR code
<iframe 
    src="https://www.jsxgraph.uni-bayreuth.de/share/iframe/simple-function-plotter" 
    style="border: 1px solid black; overflow: hidden; width: 550px; aspect-ratio: 55 / 65;" 
    name="JSXGraph example: Simple function plotter" 
    allowfullscreen
></iframe>
This code has to
<form><textarea id="input" rows=7 cols=35 wrap="off">
f = (x) => Math.cos(x);
c = plot(f);

// Derivative:
g = JXG.Math.Numerics.D(f);
plot(g,{strokecolor:'black', dash:1});
</textarea>
    <button onClick="doIt();">Plot function</button>
    <button onClick="clearAll(board);">Clear all</button>
</form>

<div id="board-0-wrapper" class="jxgbox-wrapper " style="width: 100%; ">
   <div id="board-0" class="jxgbox" style="aspect-ratio: 1 / 1; width: 100%;" data-ar="1 / 1"></div>
</div>

<script type = "text/javascript"> 
    /*
    This example is licensed under a 
    Creative Commons Attribution ShareAlike 4.0 International License.
    https://creativecommons.org/licenses/by-sa/4.0/
    
    Please note you have to mention 
    The Center of Mobile Learning with Digital Technology
    in the credits.
    */
    
    const BOARDID = 'board-0';

    var board = JXG.JSXGraph.initBoard(BOARDID, { boundingbox: [-5, 8, 8, -5], axis: true, keepaspectratio: true });
    
    // Macro function plotter
    function addCurve(board, func, atts) {
        var f = board.create('functiongraph', [func], atts);
        return f;
    }
    
    // Simplified plotting of function
    function plot(func, atts) {
        if (atts == null) {
            return addCurve(board, func, { strokewidth: 2 });
        } else {
            return addCurve(board, func, atts);
        }
    }
    
    // Usage of the macro
    function doIt() {
        eval(document.getElementById('input').value);
    }
    
    function clearAll(board) {
        JXG.JSXGraph.freeBoard(board);
        board = JXG.JSXGraph.initBoard('box', { boundingbox: [-5, 8, 8, -5], axis: true });
        return board;
    }
 </script> 
/*
This example is licensed under a 
Creative Commons Attribution ShareAlike 4.0 International License.
https://creativecommons.org/licenses/by-sa/4.0/

Please note you have to mention 
The Center of Mobile Learning with Digital Technology
in the credits.
*/

const BOARDID = 'your_div_id'; // Insert your id here!

var board = JXG.JSXGraph.initBoard(BOARDID, { boundingbox: [-5, 8, 8, -5], axis: true, keepaspectratio: true });

// Macro function plotter
function addCurve(board, func, atts) {
    var f = board.create('functiongraph', [func], atts);
    return f;
}

// Simplified plotting of function
function plot(func, atts) {
    if (atts == null) {
        return addCurve(board, func, { strokewidth: 2 });
    } else {
        return addCurve(board, func, atts);
    }
}

// Usage of the macro
function doIt() {
    eval(document.getElementById('input').value);
}

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

Simple function plotter

Plots a function together with its derivative. First, change the function term in the text box. Second, click on "plot". This is just a simple example to show the concept of interaction. It is not recommended, because the user - has to supply JavaScript code (`(x) => Math.cos(x)` instead of math syntax (`cos(x)` - is allowed to input arbitrary JavaScript code which may pose a security risk. See "self-contained function plotting" as an alternative.
<form><textarea id="input" rows=7 cols=35 wrap="off">
f = (x) => Math.cos(x);
c = plot(f);

// Derivative:
g = JXG.Math.Numerics.D(f);
plot(g,{strokecolor:'black', dash:1});
</textarea>
    <button onClick="doIt();">Plot function</button>
    <button onClick="clearAll(board);">Clear all</button>
</form>
// Define the id of your board in BOARDID

var board = JXG.JSXGraph.initBoard(BOARDID, { boundingbox: [-5, 8, 8, -5], axis: true, keepaspectratio: true });

// Macro function plotter
function addCurve(board, func, atts) {
    var f = board.create('functiongraph', [func], atts);
    return f;
}

// Simplified plotting of function
function plot(func, atts) {
    if (atts == null) {
        return addCurve(board, func, { strokewidth: 2 });
    } else {
        return addCurve(board, func, atts);
    }
}

// Usage of the macro
function doIt() {
    eval(document.getElementById('input').value);
}

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

license

This example is licensed under a Creative Commons Attribution ShareAlike 4.0 International License.
Please note you have to mention The Center of Mobile Learning with Digital Technology in the credits.