You are reading a single comment by @HughB and its replies. Click here to read the full conversation.
  • // test data 
    //var data = new Uint16Array([1023, 1020, 5300, 3178, 1500, 10600, 5700, 1008, 5090, 1010, 6100, 712]);
    
    // OR pull data from the health app
    var data = new Uint16Array(31);
    require("health").readDailySummaries(new­ Date(), h=>data[h.day]+=h.steps);
    
    const w = g.getWidth();
    const h = g.getHeight();
    
    // find the max value in the array, using a loop due to array size 
    function max(arr) {
      var m = -Infinity;
    
      for(var i=0; i< arr.length; i++)
        if(arr[i] > m) m = arr[i];
      return m;
    }
    
    // find the end of the data, the array might be for 31 days but only have 2 days in it
    function get_data_length(arr) {
      var nlen = arr.length;
      
      for(var i = arr.length - 1; i > 0 && arr[i] == 0;  i--)
        nlen--;
      
      return nlen;
    }
    
    // choose initial index that puts the last day on the end
    const len = get_data_length(data);
    var index = Math.max(len - 5, -5);
    const max_datum = max(data);  // find highest bar, for scaling
    
    function draw() {
      const bar_bot = 140;
      const bar_width = (w - 2) / 9;  // we want 9 bars, bar 5 in the centre
      var bar_top;
      var bar;
      
      g.setColor(g.theme.bg);
      g.fillRect(0,24,w,h);
      
      for (bar = 1; bar < 10; bar++) {
    
        if (bar == 5) {
          g.setFont('6x8', 2);
          g.setFontAlign(0,-1)
          g.setColor(g.theme.fg);
          g.drawString("DAY " + (index + bar) + "   " + data[index + bar - 1], g.getWidth()/2, 150);
          g.setColor("#00f");
        } else {
          g.setColor("#0ff");
        }
    
        // draw a fake 0 height bar if index is outside the bounds of the array
        if ((index + bar - 1) >= 0 && (index + bar - 1) < len) 
          bar_top = bar_bot - 100 * (data[index + bar - 1]) / max_datum;
        else
          bar_top = bar_bot;
    
        g.fillRect( 1 + (bar - 1)* bar_width, bar_bot, 1 + bar*bar_width, bar_top);
        g.setColor(g.theme.fg);
        g.drawRect( 1 + (bar - 1)* bar_width, bar_bot, 1 + bar*bar_width, bar_top);
      }
    }
    
    function next() {
      index = Math.min(len - 5, index + 1);
      //console.log("next="+index);
    }
    
    function prev() {
      index = Math.max(-4, index - 1);
      //console.log("prev="+index);
    }
    
    Bangle.on('swipe', dir => {
      if (dir == 1) prev(); else next();
      draw();
    });
    
    // handle switch display on by pressing BTN1
    Bangle.on('lcdPower', function(on) {
      if (on) draw();
    });
    
    draw();
    Bangle.loadWidgets();
    Bangle.drawWidgets();
    Bangle.setUI("clock");  // returns to launcher for now
    
    
About

Avatar for HughB @HughB started