Skip to content

Instantly share code, notes, and snippets.

@earth2marsh
Created February 14, 2009 21:09
Show Gist options
  • Save earth2marsh/64474 to your computer and use it in GitHub Desktop.
Save earth2marsh/64474 to your computer and use it in GitHub Desktop.
Ubiquity Charts
var noun_type_chart = {
_name: "Chart Type",
chartTypes: [
"pie",
"pie3d",
"barh"
],
_chart : {
pie: 'p',
pie3d : 'p3',
barh : 'bhs',
},
chartType: function(text){
var thisType = noun_type_chart._chart[text];
if(thisType){
return thisType;
} else {
return noun_type_chart._chart.pie3d;
}
},
suggest: function(text, html ) {
var suggestions = [];
var chartTypes = noun_type_chart.chartTypes;
//TODO: implement a better match algorithm
for ( var n in noun_type_chart.chartTypes ) {
var thisType = noun_type_chart.chartTypes[n];
if (thisType.match(text, "i")){
suggestions.push( CmdUtils.makeSugg(thisType) );
}
}
// Return a list of input objects, limited to at most five:
return suggestions.splice(0, 5);
}
};
CmdUtils.CreateCommand({
name: "chart",
takes: {"type": noun_type_chart},
icon: "chrome://ubiquity/content/icons/calculator.png",
description: "Makes a pie chart out of two columns of data.",
help: "",
homepage: "http://earth2marsh.com/ubiquity/",
author: {name: "Marsh Gardiner", email: "ubiquity@earth2marsh.com"},
license: "MPL",
_cleanData: function( string ) {
string = string.replace(/\r\n/g," ")
var dirtyData = string.split(/\W/);
var labels = "&chl=";
var values = "&chd=t:";
for(var i=0; i<dirtyData.length; i++){
var datum = dirtyData[i];
if( i % 2 == 0 ){
labels += datum;
if (i<dirtyData.length-2) {
labels += "|";
}
}
else {
values += parseFloat( datum );
if (i<dirtyData.length-2) {
values += ",";
}
}
var data = labels + values;
}
return data;
},
_dataToPieChart: function( string, type ) {
var data = this._cleanData( string );
if( data.length < 2 ) return null;
var img = "<img src='http://chart.apis.google.com/chart?cht=%type%&chs=300x150%data%'/>";
img = img.replace(/%type%/, type);
img = img.replace(/%data%/, data);
return img;
},
preview: function(pblock, type) {
var data = CmdUtils.getSelection();
var text = type.text;
Utils.reportInfo('Type: ' + text);
var chart_type = noun_type_chart.chartType(type.text);
Utils.reportInfo('Chart Type: ' + chart_type);
var img = this._dataToPieChart( data , chart_type );
if( !img )
jQuery(pblock).text( "Requires numbers to graph." );
else
jQuery(pblock).empty().append( img ).height( "15px" );
},
execute: function( input, mods ) {
var data = CmdUtils.getSelection();
var chart_type = noun_type_chart.chartType(type.text);
var img = this._dataToPieChart( data , chart_type );
if( img ) CmdUtils.setSelection( img );
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment