Skip to content

Instantly share code, notes, and snippets.

@wetzler
Last active August 29, 2015 13:56
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wetzler/9127225 to your computer and use it in GitHub Desktop.
Save wetzler/9127225 to your computer and use it in GitHub Desktop.
Divide to series queries (line charts) from Keen IO and chart the resulting line chart.
Keen.onChartsReady(function() {
var totalRevenueSeries = new Keen.Series("transactions", {
analysisType: "sum",
timeframe: "last_30_days",
interval: "daily",
targetProperty: "amount"
});
var totalActiveUsersSeries = new Keen.Series("plays", {
analysisType: "count_unique",
targetProperty: "player_id",
timeframe: "last_30_days",
interval: "daily"
});
divideTwoLineSeriesChart(totalRevenueSeries, totalActiveUsersSeries, interval, "chart1B", "Revenue per User")
}
function divideTwoLineSeriesChart(series1, series2, interval, div, title) {
var solutionArray = [] // variable to store results of the divided queries
var result1 = null // Create variables for holding query results
var result2 = null
series1.getResponse(function(response) { // Get the values for the first query
result1 = response.result
divideResults()
});
series2.getResponse(function(response) { // Get the values for the second Series query
result2 = response.result
divideResults()
});
function divideResults() {
// Results should look like:
// [{
// "value": 266,
// "timeframe": {
// "start": "2014-01-20T08:00:00.000Z",
// "end": "2014-01-21T08:00:00.000Z"
// }
// },
// {
// "value": 838,
// "timeframe": {
// "start": "2014-01-21T08:00:00.000Z",
// "end": "2014-01-22T08:00:00.000Z"
// }
// }]
if ((result1 != null) && (result2 != null)) {
i = 0
while (i < result1.length) {
solutionArray[i] = {
timeframe: result1[i]["timeframe"],
value: result1[i]["value"] / result2[i]["value"]
}
i++;
}
drawMyLineChart(solutionArray, interval, div, title)
}
else {
// do nothing
}
}
}
function drawMyLineChart(data, interval, div, title) {
// The final formatting required so that the result can be processed by the Keen.MultiLineChart draw method.
var formattedResult = {
result: data
}
// Create a Series object so that it can be referenced by the draw method.
// This is kind of a hack since we are passing in our own result object and not really querying the collection specified here.
// The "placeholder" is used instead of a collection name, since this is not used.
var placeholderSeries = new Keen.Series("placeholder", {
// analysisType: "count",
interval: interval
})
var placeholderLineChart = new Keen.LineChart(placeholderSeries, {
height: chartHeight,
width: chartWidth,
lineWidth: lineWidth,
chartAreaWidth: chartAreaWidth,
chartAreaLeft: chartAreaLeft,
title: title,
color: "rgb(75, 107, 148)", // kongregate blue
// color: "#fc9", // kongregate light orange
// color: "#FF9900" // orange peel,
showLegend: false,
xAxisLabelAngle: xAxisLabelAngle
});
placeholderLineChart.draw(document.getElementById(div), formattedResult);
}
@brentchow
Copy link

Line 18 syntax error; should be:

 });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment