Add type estimation to rendercharts.js to improve CSV portability

This commit is contained in:
Takayuki MATSUOKA 2015-04-04 06:47:29 +09:00
parent d895ea3902
commit bbc4f56132
3 changed files with 76 additions and 21 deletions

View File

@ -1,9 +1,9 @@
"Compressor:string" ,"Transfer:number" ,"Decompression:number" ,"Compression:number" Compressor ,Transfer ,Decompression ,Compression
LZ4 (r101) -1 , 1.0170 , 0.1165 , 0.0000 LZ4 (r101) -1 ,1.0170 ,0.1165 ,0.0000
LZO-2.06 , 1.0064 , 0.3532 , 0.0000 LZO-2.06 ,1.0064 ,0.3532 ,0.0000
QuickLZ 1.5.1b6 , 0.94743 , 0.5046 , 0.0000 QuickLZ 1.5.1b6 ,0.94743 ,0.5046 ,0.0000
Snappy 1.1.0 , 1.0136 , 0.1981 , 0.0000 Snappy 1.1.0 ,1.0136 ,0.1981 ,0.0000
LZF , 1.0204 , 0.3718 , 0.0000 LZF ,1.0204 ,0.3718 ,0.0000
zlib 1.2.8 -1 , 0.77633 , 0.7569 , 0.0000 zlib 1.2.8 -1 ,0.77633 ,0.7569 ,0.0000
LZ4 HC (r101) -9 , 0.77919 , 0.1019 , 0.0000 LZ4 HC (r101) -9 ,0.77919 ,0.1019 ,0.0000
zlib 1.2.8 -6 , 0.68389 , 0.7065 , 0.0000 zlib 1.2.8 -6 ,0.68389 ,0.7065 ,0.0000

Can't render this file because it contains an unexpected character in line 1 and column 22.

View File

@ -1,9 +1,9 @@
"Compressor:string" ,"Transfer:number" ,"Decompression:number" ,"Compression:number" Compressor ,Transfer ,Decompression ,Compression
LZ4 (r101) -1 , 1.0170 , 0.1165 , 0.5022 LZ4 (r101) -1 ,1.0170 ,0.1165 ,0.5022
LZO-2.06 , 1.0064 , 0.3532 , 0.5119 LZO-2.06 ,1.0064 ,0.3532 ,0.5119
QuickLZ 1.5.1b6 , 0.94743 , 0.5046 , 0.5682 QuickLZ 1.5.1b6 ,0.94743 ,0.5046 ,0.5682
Snappy 1.1.0 , 1.0136 , 0.1981 , 0.6562 Snappy 1.1.0 ,1.0136 ,0.1981 ,0.6562
LZF , 1.0204 , 0.3718 , 0.7850 LZF ,1.0204 ,0.3718 ,0.7850
zlib 1.2.8 -1 , 0.77633 , 0.7569 , 3.2606 zlib 1.2.8 -1 ,0.77633 ,0.7569 ,3.2606
LZ4 HC (r101) -9 , 0.77919 , 0.1019 , 8.4776 LZ4 HC (r101) -9 ,0.77919 ,0.1019 ,8.4776
zlib 1.2.8 -6 , 0.68389 , 0.7065 , 10.092 zlib 1.2.8 -6 ,0.68389 ,0.7065 ,10.092

Can't render this file because it contains an unexpected character in line 1 and column 22.

View File

@ -1,13 +1,21 @@
(function(window, document) { (function(window, document) {
// http://stackoverflow.com/a/1414175/2132223 // http://stackoverflow.com/a/1414175/2132223
var stringToBoolean = function (str) { var stringToBoolean1 = function (str) {
switch(str.toLowerCase()) { switch(str.toLowerCase()) {
case "true": case "yes": case "1": return true; case "true": case "yes": case "1": return true;
case "false": case "no": case "0": case null: return false; 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 // http://stackoverflow.com/a/4981700/2132223
var getNestedObjectByName = function (strObjName) { var getNestedObjectByName = function (strObjName) {
var parts = strObjName.split("."); var parts = strObjName.split(".");
@ -109,6 +117,40 @@
}; };
// end of http://code.google.com/p/csv-to-array/ // 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 drawTable = function (gvCtor, gvOptions, csvUrl, csvEl) {
var r = new XMLHttpRequest(); var r = new XMLHttpRequest();
r.open("GET", csvUrl, true); r.open("GET", csvUrl, true);
@ -119,6 +161,7 @@
var gvdt = new google.visualization.DataTable() var gvdt = new google.visualization.DataTable()
var csvHeaderLine = csvAsArray.shift(); var csvHeaderLine = csvAsArray.shift();
for(var i = 0; i < csvHeaderLine.length; i++) { for(var i = 0; i < csvHeaderLine.length; i++) {
/*
var csvHeaderElement = csvHeaderLine[i].split(":"); var csvHeaderElement = csvHeaderLine[i].split(":");
var csvHeaderName = "?"; var csvHeaderName = "?";
var csvHeaderType = "string"; var csvHeaderType = "string";
@ -137,7 +180,19 @@
case "boolean": csvAsArray[j][i] = stringToBoolean(csvAsArray[j][i]); break; 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); gvdt.addRows(csvAsArray);
var ctor = getNestedObjectByName(gvCtor); var ctor = getNestedObjectByName(gvCtor);
var table = new ctor (csvEl); var table = new ctor (csvEl);