You are reading a single comment by @MrTimcakes and its replies. Click here to read the full conversation.
  • And here is Graph.js

    /* Copyright (c) 2017 MrTimcakes. See the file LICENSE for copying permission. */
    /*
    Live graphs that update from right to left
    
    var Graph = require("graph");
    var myGraph = new Graph(90, 48);
    myGraph.initData([0,1,2,3,4,5,10,15,20,2­5,30]);
    
    function draw(){
      g.clear();
      myGraph.drawGraph(g, 5, 5);
      g.flip();
    }
    */
    
    /* Create new graph, datapoints to keep (width), height, range of data from 0 */
    function Graph(historyLength, height = 64, range = 100) {
      this.history = new Int8Array(historyLength);
      this.height = height;
      this.range = range;
    }
    
    Graph.prototype.setHeight = function(height) {
      this.height = height;
    };
    
    Graph.prototype.setRange = function(range) {
      this.range = range;
    };
    
    /* Cycle all history values, add new ones to the end*/
    Graph.prototype.update = function(data) {
      for (var i=1;i<this.history.length;i++){
        this.history[i-1] = this.history[i];
      }
      this.history[this.history.length-1] = data;
    };
    
    /* Replace end of history with the array given */
    Graph.prototype.initData = function(data) {
      if(Array.isArray(data)){
        for (var i=0;i<data.length;i++){
          this.history[this.history.length-i] = data[data.length-i];
        }
      }
    };
    
    /* Draw axis, the convert each value to a y and plot */
    Graph.prototype.drawGraph = function(g, x, y) {
      g.drawLine(x, y, x, y+this.height);
      g.drawLine(x, y+this.height, x+this.history.length, y+this.height);
    
      g.moveTo(x, (y - (this.height/this.range)*this.history[0]­) + this.height);
      for (i=1;i<this.history.length;i++){
        g.lineTo(x+i, (y - (this.height/this.range)*this.history[i]­) + this.height);
      }
    };
    
    exports = Graph;
    
    
About

Avatar for MrTimcakes @MrTimcakes started