summaryrefslogtreecommitdiffstats
path: root/js
diff options
context:
space:
mode:
authorTakayuki MATSUOKA <takayuki.matsuoka@gmail.com>2015-04-03 21:47:29 (GMT)
committerTakayuki MATSUOKA <takayuki.matsuoka@gmail.com>2015-04-03 21:47:29 (GMT)
commitbbc4f5613277801b0d7a778fb87a979574d3e4ba (patch)
tree755f1c18f2c7b54efd1c8f104ecd3ae8b7781356 /js
parentd895ea390248770294c9afa9d789aeec1870c767 (diff)
downloadlz4-bbc4f5613277801b0d7a778fb87a979574d3e4ba.zip
lz4-bbc4f5613277801b0d7a778fb87a979574d3e4ba.tar.gz
lz4-bbc4f5613277801b0d7a778fb87a979574d3e4ba.tar.bz2
Add type estimation to rendercharts.js to improve CSV portability
Diffstat (limited to 'js')
-rw-r--r--js/rendercharts.js61
1 files changed, 58 insertions, 3 deletions
diff --git a/js/rendercharts.js b/js/rendercharts.js
index 24bce90..aaa3ffb 100644
--- a/js/rendercharts.js
+++ b/js/rendercharts.js
@@ -1,13 +1,21 @@
(function(window, document) {
// http://stackoverflow.com/a/1414175/2132223
- var stringToBoolean = function (str) {
+ var stringToBoolean1 = function (str) {
switch(str.toLowerCase()) {
case "true": case "yes": case "1": return true;
case "false": case "no": case "0": case null: return false;
- default: return Boolean(str);
+ default: return null;
}
};
+ var stringToBoolean = function (str) {
+ var b = stringToBoolean1(str);
+ if(b === null) {
+ b = Boolean(str);
+ }
+ return b;
+ };
+
// http://stackoverflow.com/a/4981700/2132223
var getNestedObjectByName = function (strObjName) {
var parts = strObjName.split(".");
@@ -109,6 +117,40 @@
};
// end of http://code.google.com/p/csv-to-array/
+ var estimateColumnType = function (csvAsArray, columnIndex) {
+ var isColumnBoolean = function (csvAsArray, columnIndex) {
+ for(var row = 0, nRow = csvAsArray.length; row < nRow; ++row) {
+ if(stringToBoolean1(csvAsArray[row][columnIndex]) === null) {
+ return false;
+ }
+ }
+ return true;
+ };
+
+ var isColumnNumber = function (csvAsArray, columnIndex) {
+ // http://stackoverflow.com/a/1830844/2132223
+ var isNumber = function(n) {
+ return !isNaN(parseFloat(n)) && isFinite(n);
+ }
+ for(var row = 0, nRow = csvAsArray.length; row < nRow; ++row) {
+ if(! isNumber(csvAsArray[row][columnIndex])) {
+ return false;
+ }
+ }
+ return true;
+ };
+
+ if(isColumnBoolean(csvAsArray, columnIndex)) {
+ return "boolean";
+ }
+
+ if(isColumnNumber(csvAsArray, columnIndex)) {
+ return "number";
+ }
+
+ return "string";
+ };
+
var drawTable = function (gvCtor, gvOptions, csvUrl, csvEl) {
var r = new XMLHttpRequest();
r.open("GET", csvUrl, true);
@@ -119,6 +161,7 @@
var gvdt = new google.visualization.DataTable()
var csvHeaderLine = csvAsArray.shift();
for(var i = 0; i < csvHeaderLine.length; i++) {
+/*
var csvHeaderElement = csvHeaderLine[i].split(":");
var csvHeaderName = "?";
var csvHeaderType = "string";
@@ -137,7 +180,19 @@
case "boolean": csvAsArray[j][i] = stringToBoolean(csvAsArray[j][i]); break;
}
}
- }
+*/
+ var csvHeaderType = estimateColumnType(csvAsArray, i);
+// alert("column" + i + " = " + csvHeaderType);
+ gvdt.addColumn(csvHeaderType, csvHeaderLine[i]);
+ for(var j = 0; j < csvAsArray.length; j++) {
+ switch(csvHeaderType) {
+ default:
+ case "string": csvAsArray[j][i] = String(csvAsArray[j][i]); break;
+ case "number": csvAsArray[j][i] = Number(csvAsArray[j][i]); break;
+ case "boolean": csvAsArray[j][i] = stringToBoolean(csvAsArray[j][i]); break;
+ }
+ }
+ }
gvdt.addRows(csvAsArray);
var ctor = getNestedObjectByName(gvCtor);
var table = new ctor (csvEl);