Created
September 26, 2014 09:42
-
-
Save vnys/9a78c7f0082544e14fa5 to your computer and use it in GitHub Desktop.
C3 graphs from Google Spreadsheets with tabletop.js
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
The [Google Spreadheetfile](https://docs.google.com/spreadsheets/d/1d03vSAqzfAXTH7IRRGwUt-ynK9IOlf4jmWwI0MMW4JA/edit#gid=0) must have two sheets, one called data and one called meta |
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
var graphContainer = document.querySelector('.chart'); | |
Tabletop.init({ | |
key: '1d03vSAqzfAXTH7IRRGwUt-ynK9IOlf4jmWwI0MMW4JA', | |
wanted: ['data', 'meta'], | |
callback: function(data, tabletop) { | |
var meta = {}; | |
tabletop.sheets().meta.elements.forEach(function(element) { | |
meta[element.key] = element.value; | |
}); | |
var columns = tabletop.sheets().data.toArray().map(function(arr) { | |
return arr.slice(0, 2); | |
}); | |
removeSummaryRow(columns); | |
console.table(columns); | |
console.table(meta); | |
//makeChart(columns, meta); | |
//makeBarChart(columns); | |
}, | |
simpleSheet: false | |
}); | |
function removeSummaryRow(columns) { | |
var summaryKeys = ['total', 'totalt', 'tilsammen', 'sum', 'summary']; | |
var lastRowKey = columns[columns.length - 1][0].toLowerCase(); | |
var lastRowIsSummary = summaryKeys.filter(function(element) { | |
return element === lastRowKey; | |
}).length > 0; | |
if (lastRowIsSummary) { | |
columns.pop(); | |
}; | |
}; | |
function makeChart(columns, meta) { | |
var legend = {}; | |
var type = meta.type; | |
if (meta.legend) { | |
if (meta.legend === 'bottom' || meta.legend === 'right') { | |
legend.position = meta.legend; | |
} else if (meta.legend === 'hide') { | |
legend.show = false; | |
} | |
}; | |
c3.generate({ | |
bindto: graphContainer, | |
data: { | |
columns: columns, | |
type: type | |
}, | |
legend: legend | |
}); | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment