Real-time graphing: Difference between revisions

From JSXGraph Wiki
No edit summary
No edit summary
Line 9: Line 9:
<jsxgraph width="700" height="400">
<jsxgraph width="700" height="400">
var brd, g,  
var brd, g,  
     xdat = [], ydat = [],
     xdata = [], ydata = [],
     turt,i;
     turt,i;


Line 27: Line 27:
             if (200 == transport.status) {
             if (200 == transport.status) {
                 t = transport.responseText;
                 t = transport.responseText;
                document.getElementById('debug').innerHTML = t+'_'+xdat.length+'_'+ydat.length;
                 a = parseFloat(t);
                 a = parseFloat(t);
                 if (xdat.length<30) {
                 if (xdata.length<30) {
                     xdat.push(xdat.length);
                     xdata.push(xdata.length);
                 } else {
                 } else {
                     ydat.splice(0,1);
                     ydata.splice(0,1);
                 }
                 }
                 ydat.push(a);
                 ydata.push(a);
                 if (!g) {                  // If the curve does not exist yet, create it.
                 if (!g) {                  // If the curve does not exist yet, create it.
                     g = brd.createElement('curve', [xdat,ydat],{strokeWidth:3, strokeColor:'yellow'});  
                     g = brd.createElement('curve', [xdata,ydata],{strokeWidth:3, strokeColor:'yellow'});  
                 }  
                 }  
                 brd.update();
                 brd.update();
Line 49: Line 48:
Start();
Start();
</jsxgraph>
</jsxgraph>
<html><div id="debug"></div></html>


===The underlying JavaScript code===
===The underlying JavaScript code===

Revision as of 08:41, 18 December 2009

The underlying JavaScript code

  <script type="text/javascript" src="http://jsxgraph.uni-bayreuth.de/distrib/prototype.js"></script>
  <style>
    #jxgbox {
       background-color:black;
    }
  </style>
var brd, g, 
    x = [], y = [];

brd = JXG.JSXGraph.initBoard('jxgbox', {axis:true, boundingbox:[0,20,30,-2]});

fetchData = function() {
    new Ajax.Request('/ajax/fakesensor.php', {
        onComplete: function(transport) {
            var t, a;
            if (200 == transport.status) {
                t = transport.responseText;
                a = parseFloat(t);
                if (x.length<30) {
                    x.push(x.length);
                } else {
                    y.splice(0,1);
                }
                y.push(a);
                if (!g) {                   // If the curve does not exist yet, create it.
                    g = brd.createElement('curve', [x,y],{strokeWidth:3, strokeColor:'green'}); 
                } 
                brd.update();
            };
    }});
};

Start = function() {
    periodical = setInterval(fetchData,1000);  // Start the periodical update
};

Start();

The PHP code of fakesensor.php

<?php
   echo mt_rand(0,20);
?>