From bbc4f5613277801b0d7a778fb87a979574d3e4ba Mon Sep 17 00:00:00 2001 From: Takayuki MATSUOKA Date: Sat, 4 Apr 2015 06:47:29 +0900 Subject: [PATCH] Add type estimation to rendercharts.js to improve CSV portability --- ...k-silesia-1000mbps-offline-compression.csv | 18 +++--- csv/benchmark-silesia-1000mbps.csv | 18 +++--- js/rendercharts.js | 61 ++++++++++++++++++- 3 files changed, 76 insertions(+), 21 deletions(-) diff --git a/csv/benchmark-silesia-1000mbps-offline-compression.csv b/csv/benchmark-silesia-1000mbps-offline-compression.csv index 0f4cc0e..e6a8513 100644 --- a/csv/benchmark-silesia-1000mbps-offline-compression.csv +++ b/csv/benchmark-silesia-1000mbps-offline-compression.csv @@ -1,9 +1,9 @@ -"Compressor:string" ,"Transfer:number" ,"Decompression:number" ,"Compression:number" -LZ4 (r101) -1 , 1.0170 , 0.1165 , 0.0000 -LZO-2.06 , 1.0064 , 0.3532 , 0.0000 -QuickLZ 1.5.1b6 , 0.94743 , 0.5046 , 0.0000 -Snappy 1.1.0 , 1.0136 , 0.1981 , 0.0000 -LZF , 1.0204 , 0.3718 , 0.0000 -zlib 1.2.8 -1 , 0.77633 , 0.7569 , 0.0000 -LZ4 HC (r101) -9 , 0.77919 , 0.1019 , 0.0000 -zlib 1.2.8 -6 , 0.68389 , 0.7065 , 0.0000 +Compressor ,Transfer ,Decompression ,Compression +LZ4 (r101) -1 ,1.0170 ,0.1165 ,0.0000 +LZO-2.06 ,1.0064 ,0.3532 ,0.0000 +QuickLZ 1.5.1b6 ,0.94743 ,0.5046 ,0.0000 +Snappy 1.1.0 ,1.0136 ,0.1981 ,0.0000 +LZF ,1.0204 ,0.3718 ,0.0000 +zlib 1.2.8 -1 ,0.77633 ,0.7569 ,0.0000 +LZ4 HC (r101) -9 ,0.77919 ,0.1019 ,0.0000 +zlib 1.2.8 -6 ,0.68389 ,0.7065 ,0.0000 diff --git a/csv/benchmark-silesia-1000mbps.csv b/csv/benchmark-silesia-1000mbps.csv index 4db9a12..169573f 100644 --- a/csv/benchmark-silesia-1000mbps.csv +++ b/csv/benchmark-silesia-1000mbps.csv @@ -1,9 +1,9 @@ -"Compressor:string" ,"Transfer:number" ,"Decompression:number" ,"Compression:number" -LZ4 (r101) -1 , 1.0170 , 0.1165 , 0.5022 -LZO-2.06 , 1.0064 , 0.3532 , 0.5119 -QuickLZ 1.5.1b6 , 0.94743 , 0.5046 , 0.5682 -Snappy 1.1.0 , 1.0136 , 0.1981 , 0.6562 -LZF , 1.0204 , 0.3718 , 0.7850 -zlib 1.2.8 -1 , 0.77633 , 0.7569 , 3.2606 -LZ4 HC (r101) -9 , 0.77919 , 0.1019 , 8.4776 -zlib 1.2.8 -6 , 0.68389 , 0.7065 , 10.092 +Compressor ,Transfer ,Decompression ,Compression +LZ4 (r101) -1 ,1.0170 ,0.1165 ,0.5022 +LZO-2.06 ,1.0064 ,0.3532 ,0.5119 +QuickLZ 1.5.1b6 ,0.94743 ,0.5046 ,0.5682 +Snappy 1.1.0 ,1.0136 ,0.1981 ,0.6562 +LZF ,1.0204 ,0.3718 ,0.7850 +zlib 1.2.8 -1 ,0.77633 ,0.7569 ,3.2606 +LZ4 HC (r101) -9 ,0.77919 ,0.1019 ,8.4776 +zlib 1.2.8 -6 ,0.68389 ,0.7065 ,10.092 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);