Skip to content

Instantly share code, notes, and snippets.

@bycoffe
Created September 9, 2011 16:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bycoffe/1206659 to your computer and use it in GitHub Desktop.
Save bycoffe/1206659 to your computer and use it in GitHub Desktop.
Basic Google Charts API wrapper in CoffeeScript
class window.GoogleChart
constructor: ->
@width = 0
@height = 0
@encoded = []
@data = []
simpleEncoding: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
extendedMap: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-.',
types: {'line': 'lc', }
chart_type: -> @types['line']
base_url: 'http://chart.apis.google.com/chart?'
add_data: (values) ->
@data.push [values]
@encoded.push @encode values
url: ->
pieces = {
'cht': @chart_type(),
'chs': @width + 'x' + @height,
'chd': @encoded.join(','),
}
@base_url + ("#{key}=#{value}" for key, value of pieces).join('&')
max: (values) ->
Math.max.apply(Math, values)
encode: (values) ->
if @height > 100
@extendedEncode(values)
else
@simpleEncode(values)
simpleEncode: (values) ->
chartData = ['s:']
maxValue = @max(values)
for currentValue in values
if not isNaN currentValue and currentValue >= 0
val = Math.round ((@simpleEncoding.length - 1) * (currentValue / maxValue))
chartData.push @simpleEncoding.charAt val
else
chartData.push '_'
chartData.join ''
extendedEncode: (values) ->
chartData = 'e:'
mapLength = @extendedMap.length
maxValue = @max(values)
for currentValue in values
numericalVal = new Number currentValue
scaledVal = Math.floor mapLength * mapLength * numericalVal / maxValue
if scaledVal > (mapLength * mapLength) - 1
chartData += '..'
else if scaledVal < 0
chartData += '__'
else
quotient = Math.floor scaledVal / mapLength
remainder = scaledVal - mapLength * quotient
chartData += @extendedMap.charAt(quotient) + @extendedMap.charAt(remainder)
chartData
// Above compiles to
(function() {
window.GoogleChart = (function() {
function GoogleChart() {
this.width = 0;
this.height = 0;
this.encoded = [];
this.data = [];
}
GoogleChart.prototype.simpleEncoding = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
GoogleChart.prototype.extendedMap = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-.';
GoogleChart.prototype.types = {
'line': 'lc'
};
GoogleChart.prototype.chart_type = function() {
return this.types['line'];
};
GoogleChart.prototype.base_url = 'http://chart.apis.google.com/chart?';
GoogleChart.prototype.add_data = function(values) {
this.data.push([values]);
return this.encoded.push(this.encode(values));
};
GoogleChart.prototype.url = function() {
var key, pieces, value;
pieces = {
'cht': this.chart_type(),
'chs': this.width + 'x' + this.height,
'chd': this.encoded.join(',')
};
return this.base_url + ((function() {
var _results;
_results = [];
for (key in pieces) {
value = pieces[key];
_results.push("" + key + "=" + value);
}
return _results;
})()).join('&');
};
GoogleChart.prototype.max = function(values) {
return Math.max.apply(Math, values);
};
GoogleChart.prototype.encode = function(values) {
if (this.height > 100) {
return this.extendedEncode(values);
} else {
return this.simpleEncode(values);
}
};
GoogleChart.prototype.simpleEncode = function(values) {
var chartData, currentValue, maxValue, val, _i, _len;
chartData = ['s:'];
maxValue = this.max(values);
for (_i = 0, _len = values.length; _i < _len; _i++) {
currentValue = values[_i];
if (!isNaN(currentValue && currentValue >= 0)) {
val = Math.round((this.simpleEncoding.length - 1) * (currentValue / maxValue));
chartData.push(this.simpleEncoding.charAt(val));
} else {
chartData.push('_');
}
}
return chartData.join('');
};
GoogleChart.prototype.extendedEncode = function(values) {
var chartData, currentValue, mapLength, maxValue, numericalVal, quotient, remainder, scaledVal, _i, _len;
chartData = 'e:';
mapLength = this.extendedMap.length;
maxValue = this.max(values);
for (_i = 0, _len = values.length; _i < _len; _i++) {
currentValue = values[_i];
numericalVal = new Number(currentValue);
scaledVal = Math.floor(mapLength * mapLength * numericalVal / maxValue);
if (scaledVal > (mapLength * mapLength) - 1) {
chartData += '..';
} else if (scaledVal < 0) {
chartData += '__';
} else {
quotient = Math.floor(scaledVal / mapLength);
remainder = scaledVal - mapLength * quotient;
chartData += this.extendedMap.charAt(quotient) + this.extendedMap.charAt(remainder);
}
}
return chartData;
};
return GoogleChart;
})();
}).call(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment