Created
November 20, 2013 04:21
-
-
Save marram/7557667 to your computer and use it in GitHub Desktop.
Rate charts for Keen.IO
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Divide two Keen series. | |
* by @marram for TriggerMail | |
*/ | |
Keen.RateChart = function(numerator, denominator, chartOptions, element){ | |
this.numerator = numerator; | |
this.denominator = denominator; | |
this.options = chartOptions; | |
this.__numdata = {}; | |
this.__dendata = {}; | |
this.__d = []; | |
this.divided = {result: []}; | |
this.element = element; | |
this.attributes = numerator.attributes; | |
// Multiline? | |
this.multiline = !_.isUndefined(this.numerator.attributes.groupBy); | |
var getNumResponse = _.bind(function(response){ | |
this.__d.push(response); | |
this.__numdata = response; | |
if (this.__d.length==2){ | |
return this._build(); | |
} | |
}, this); | |
var getDenResponse = _.bind(function(response){ | |
this.__d.push(response); | |
this.__dendata = response; | |
if (this.__d.length==2){ | |
return this._build(); | |
} | |
}, this); | |
this.numerator.getResponse(getNumResponse); | |
this.denominator.getResponse(getDenResponse); | |
}; | |
Keen.RateChart.prototype._build = function(){ | |
if (this.multiline){ | |
return this._buildMultiLine(); | |
} | |
return this._buildLine(); | |
} | |
Keen.RateChart.prototype._buildLine = function(){ | |
var num = this.__numdata.result; | |
var den = this.__dendata.result; | |
console.debug(this.__d); | |
for (var i=0; i<num.length; i++){ | |
this.divided.result[i] = {}; | |
this.divided.result[i]["timeframe"] = num[i]["timeframe"]; | |
this.divided.result[i]["value"] = 100*(num[i]["value"]/den[i]["value"]); | |
} | |
this.draw(); | |
} | |
Keen.RateChart.prototype._buildMultiLine = function(){ | |
var num = this.__numdata.result; | |
var den = this.__dendata.result; | |
console.debug(this.__d); | |
for (var i=0; i<num.length; i++){ | |
this.divided.result[i] = {}; | |
this.divided.result[i]["timeframe"] = num[i]["timeframe"]; | |
this.divided.result[i]["value"] = []; | |
for (var j=0; j<num[i].value.length; j++){ | |
v = jQuery.extend({}, num[i].value[j]); | |
v.result = 100*(num[i].value[j].result / den[i].value[j].result); | |
this.divided.result[i]["value"][j] = v; | |
} | |
} | |
console.debug(this.divided); | |
this.draw(); | |
} | |
Keen.RateChart.prototype.draw = function(){ | |
var options = this.options; | |
var element = this.element; | |
if(_.isUndefined(this.numerator.attributes.groupBy)){ | |
var lineChart = new Keen.LineChart(this, options); | |
lineChart.draw(element, this.divided); | |
} | |
else{ | |
var multiLineChart = new Keen.MultiLineChart(this, options); | |
multiLineChart.draw(element, this.divided); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment