Arjunsos / simile-widgets

Automatically exported from code.google.com/p/simile-widgets
0 stars 0 forks source link

TIMEPLOT: Added JSON data as an option, in addition to text and xml... #71

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
I wanted to retrieve data dynamically using JSON, so I extendedTimeplot as
follows:

/*
 * Mimic the timeplot.loadText function
 * - Only needed to change eventSource.loadText to eventSource.loadJSON
 */
Timeplot._Impl.prototype.loadJSON=function(url,separator,eventSource,filter){
    if(this._active){
        var tp=this;

var fError=function(statusText,status,xmlhttp){
            tp.hideLoadingMessage();
            $('#my-timeplot').empty().html("<span class='error'>Failed to
load JSON data from "+url+". Error: "+statusText+"</span");
        };

var fDone=function(xmlhttp){
            try{
                if(xmlhttp.responseText.replace(/(\[|\])/g,'').length>0){
// added to check for empty result set

eventSource.loadJSON(xmlhttp.responseText,separator,url,filter);
                }
                else {
                    $('#my-timeplot').empty().html("<span class='error'>No
data found</span>");
                }
            }catch(e){
                SimileAjax.Debug.exception(e);
            }finally{
                tp.hideLoadingMessage();
            }
        };

this.showLoadingMessage();

window.setTimeout(function(){SimileAjax.XmlHttp.get(url,fError,fDone);},0);
    }
}

/*
 * Mimic the eventSource.loadText function
 * - Do not parse all data, only the JSON value when creating an evt
 * - Parse the JSON into an Object
 */
Timeplot.DefaultEventSource.prototype.loadJSON=function(jsonText,separator,url,f
ilter){
    if(jsonText==null){
        return;
    }

var data = jsonText.parseJSON(); // parse JSON into an Object

    this._events.maxValues=new Array();
    var base=this._getBaseURL(url);

var dateTimeFormat='iso8601';
    var parseDateTimeFunction=this._events.getUnit().getParser(dateTimeFormat);

var added=false;

if(filter){
        data=filter(data);
    }

if(data){
        for(var i=0;i<data.length;i++){
            var row=data[i];
            if(row.date){
                var evt=new Timeplot.DefaultEventSource.NumericEvent(
                    parseDateTimeFunction(row.date),
                    this._parseJSONValue(row.value,separator)
                );
                this._events.add(evt);
                added=true;
            }
        }
    }

if(added){
        this._fire('onAddMany',[]);
    }
}

/*
 * Turn the JSON value into an array so that it can be correctly processed
 * by Timeplot
 */
Timeplot.DefaultEventSource.prototype._parseJSONValue=function(value,separator){
    value=value.replace(/\r\n?/g,'\n');
    var pos=0;
    var len=value.length;
    var line=[];
    while(pos<len){
        var nextseparator=value.indexOf(separator,pos);
        var nextnline=value.indexOf('\n',pos);
        if(nextnline<0)nextnline=len;
        if(nextseparator>-1&&nextseparator<nextnline){
            line[line.length]=value.substr(pos,nextseparator-pos);
            pos=nextseparator+1;
        }else{
            line[line.length]=value.substr(pos,nextnline-pos);
            pos=nextnline+1;
            break;
        }
    }
    if(line.length<0)return;
    return line;
}

I utilized some of the jQuery function/features and added some error
messages. The goal was to get data returned in the following format:
[data: value, data: value, etc...]
Value would allow for delimeted data, which would use the separator value
to parse, as in the loadText and loadXML functions. (I haven't tested this yet)

* One note (possible bug), it appears that the parseJSON function that is
included with Timeplot (json.js) is not working correctly. I had to include
my own version of it...but I could have just been doing something wrong.

If you feel it would be useful in Timeplot, feel free to add/modify as needed.

Thanks,
Eric

[Submitted by Eric Martin on simile.mit.edu]

Original issue reported on code.google.com by stefano.mazzocchi@gmail.com on 25 Mar 2009 at 5:06

GoogleCodeExporter commented 9 years ago

Original comment by stefano.mazzocchi@gmail.com on 25 Mar 2009 at 6:42

GoogleCodeExporter commented 9 years ago
Hello, When can we expect to this to be added to the baseline code.? Thanks

Original comment by rowan.sh...@wanadoo.fr on 30 Apr 2009 at 2:48

GoogleCodeExporter commented 9 years ago
Looking through the mail lists it appears that the timeline project has similar 
code. 
http://groups.google.com/group/simile-widgets/browse_thread/thread/5a9214d83b895
e70/171f6e9846e4e081?lnk=gst&q=timeplot#171f6e9846e4e081

Maybe you can make it common to both projects? It would be nice to have a 
callback
function for the timeplot.loadJSON like the one in timeline as you often want 
to do
other things with the json data besides plot.

Original comment by rowan.sh...@wanadoo.fr on 30 Apr 2009 at 4:56