Building on the code here, I wanted to put together a demo widget for the Chumby to display mint statistics from my site.

The widget hits a PHP script that returns a JSON object which Flash Lite uses to output the hourly metrics for the past 24 hours.

Code:

var reply_lv = new LoadVars();
var send_lv = new LoadVars();
var stats;
var hour;
var max_hits = 0;
var curr_col;
var scale;

var COLUMN_WIDTH = 12;
function loadMintData() {
    send_lv.sendAndLoad(
        "http://jamesv.org/code/chumby_mint/stats.php", 
        reply_lv, 
        "GET"
    );
    reply_lv.onLoad = loadedMintData; 
}

function loadedMintData(success){
    if(success) {
        // operation was a success

        //Parse stats from JSON
        stats= JSON.parse(reply_lv.site_stats);

        //Find the max hits in the period
        for(hour = 0; hour < stats.length; hour++ ) {
            if(stats[hour][2] > max_hits) {
                max_hits = stats[hour][2];
            }
        }

        hits_per_step = Math.ceil(max_hits / 5);
        scale = 172 / (hits_per_step * 5);

        for(label_id= 0; label_id <= 5; label_id++ ) {
            data_display.gridlines["label_" + label_id].text = 
                hits_per_step * label_id;
        }

        //Draw in the data columns
        for(hour = 0; hour < stats.length; hour++ ) {
            drawDataColumn(hour);
        }

    } else {
        // operation failed
        trace("Sorry Charlie");
    }
}

function drawDataColumn(hour) {
    //trace(stats[hour]);
    data_display.data_columns.attachMovie(
        "data_column", 
        "col_" + hour,
        10 + hour
    );

    curr_col = data_display.data_columns["col_" + hour];
    curr_col._x = hour * COLUMN_WIDTH;

    //Set column heights
    curr_col.total._y = 
        curr_col.masker._height - (scale * stats[hour][2]);
    curr_col.unique._y = 
        curr_col.masker._height - (scale * stats[hour][1]);

    //Set the overlay on or off
    curr_col.overlay._visible = 
        (stats[hour][0] == 0 || stats[hour][0] == 12) 
        ? true : false;

    switch(stats[hour][0]){
        case 0:
        curr_col.title.text = "12a";
        break;

        case 12:
        curr_col.title.text = "12p";
        break;

        case 3:
        case 15:
        curr_col.title.text = "3";
        break;

        case 6:
        case 18:
        curr_col.title.text = "6";
        break;

        case 9:
        case 21:
        curr_col.title.text = "9";
        break;

        default:
        curr_col.title._visible = false;
        curr_col["title-backer"]._visible = false;
        break;
    }

}

function clearTimer(a) {
    this.onEnterFrame = undefined;
}

function setTimer(f,t) {
    this._callbackFunction = f;
    this._duration = t;
    this._timeOut = new Date().getTime()+this._duration;
    this.onEnterFrame = function() {
        var d = new Date().getTime();
        if (d>this._timeOut) {
            this._timeout = d+this._duration;
            this._callbackFunction();
        }
    }
    return 1;
}


clearTimer(skipper);
loadMintData();
skipper = setTimer(loadMintData,36000);

stop();
  • Digg
  • del.icio.us
  • feedmelinks
  • Reddit
  • NewsVine
  • StumbleUpon
  • Technorati

One Trackback

  1. By Mints Stats für den Chumby on May 18, 2007 at 2:21 pm

    […] Wochen handeln. Und für alle Mint-Nutzer gibt es gute Neuigkeiten. jamev hat ein spezielles Mint-Widget entwickelt und in der Version 0.1 zum Download bereit […]

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*