UNPKG

lumenize

Version:

Illuminating the forest AND the trees in your data.

224 lines (206 loc) 7.54 kB
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>The source code</title> <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" /> <script type="text/javascript" src="../resources/prettify/prettify.js"></script> <style type="text/css"> .highlight { display: block; background-color: #ddd; } </style> <script type="text/javascript"> function highlight() { document.getElementById(location.hash.replace(/#/, "")).className = "highlight"; } </script> </head> <body onload="prettyPrint(); highlight();"> <pre class="prettyprint lang-js">/* &lt;CoffeeScript&gt; {utils, Time} = require(&#39;tztime&#39;) csvStyleArray_To_ArrayOfMaps = (csvStyleArray, rowKeys) -&gt; &lt;/CoffeeScript&gt; */ <span id='Lumenize-method-csvStyleArray_To_ArrayOfMaps'> /** </span> * @method csvStyleArray_To_ArrayOfMaps * @member Lumenize * @param {Array[]} csvStyleArray The first row is usually the list of column headers but if not, you can * provide your own such list in the second parameter * @param {String[]} [rowKeys] specify the column headers like `[&#39;column1&#39;, &#39;column2&#39;]`. If not provided, it will use * the first row of the csvStyleArray * @return {Object[]} * * `csvStyleArry_To_ArryOfMaps` is a convenience function that will convert a csvStyleArray like: * * {csvStyleArray_To_ArrayOfMaps} = require(&#39;../&#39;) * * csvStyleArray = [ * [&#39;column1&#39;, &#39;column2&#39;], * [1 , 2 ], * [3 , 4 ], * [5 , 6 ] * ] * * to an Array of Maps like this: * * console.log(csvStyleArray_To_ArrayOfMaps(csvStyleArray)) * * # [ { column1: 1, column2: 2 }, * # { column1: 3, column2: 4 }, * # { column1: 5, column2: 6 } ] */ /* &lt;CoffeeScript&gt; arrayOfMaps = [] if rowKeys? i = 0 else rowKeys = csvStyleArray[0] i = 1 tableLength = csvStyleArray.length while i &lt; tableLength inputRow = csvStyleArray[i] outputRow = {} for key, index in rowKeys outputRow[key] = inputRow[index] arrayOfMaps.push(outputRow) i++ return arrayOfMaps arrayOfMaps_To_CSVStyleArray = (arrayOfMaps, fields) -&gt; &lt;/CoffeeScript&gt; */ <span id='Lumenize-method-arrayOfMaps_To_CSVStyleArray'> /** </span> * @method arrayOfMaps_To_CSVStyleArray * @member Lumenize * @param {Object[]} arrayOfMaps * @param {String[]} [fields] If not provided, it will use the first row and get all fields * @return {Array[]} The first row will be the column headers * * `arrayOfMaps_To_CSVStyleArray` is a convenience function that will convert an array of maps like: * * {arrayOfMaps_To_CSVStyleArray} = require(&#39;../&#39;) * * arrayOfMaps = [ * {column1: 10000, column2: 20000}, * {column1: 30000, column2: 40000}, * {column1: 50000, column2: 60000} * ] * * to a CSV-style array like this: * * console.log(arrayOfMaps_To_CSVStyleArray(arrayOfMaps)) * * # [ [ &#39;column1&#39;, &#39;column2&#39; ], * # [ 10000, 20000 ], * # [ 30000, 40000 ], * # [ 50000, 60000 ] ] */ /* &lt;CoffeeScript&gt; if arrayOfMaps.length == 0 return [] csvStyleArray = [] outRow = [] unless fields? fields = [] for key, value of arrayOfMaps[0] fields.push(key) csvStyleArray.push(fields) for inRow in arrayOfMaps outRow = [] for key in fields outRow.push(inRow[key]) csvStyleArray.push(outRow) return csvStyleArray arrayOfMaps_To_HighChartsSeries = (arrayOfMaps, config) -&gt; &lt;/CoffeeScript&gt; */ <span id='Lumenize-method-arrayOfMaps_To_HighChartsSeries'> /** </span> * @method arrayOfMaps_To_HighChartsSeries * @member Lumenize * @param {Array[]} arrayOfMaps * @param {Object} config You can use the same config you used to call TimeSeriesCalculator including your yAxis specifications * @return {Object[]} in HighCharts form * * Takes an array of arrays that came from a call to TimeSeriesCalculator and looks like this: * * {arrayOfMaps_To_HighChartsSeries} = require(&#39;../&#39;) * * arrayOfMaps = [ * {&quot;Series 1&quot;: 8, &quot;Series 2&quot;: 5, &quot;Series3&quot;: 10}, * {&quot;Series 1&quot;: 2, &quot;Series 2&quot;: 3}, * {&quot;Series 1&quot;: 1, &quot;Series 2&quot;: 2, &quot;Series3&quot;: 40}, * ] * * and a list of series configurations * * config = [ * {name: &quot;Series 1&quot;, yAxis: 1}, * {name: &quot;Series 2&quot;}, * {name: &quot;Series3&quot;} * ] * * and extracts the data into seperate series * * console.log(arrayOfMaps_To_HighChartsSeries(arrayOfMaps, config)) * # [ { name: &#39;Series 1&#39;, data: [ 8, 2, 1 ], yAxis: 1 }, * # { name: &#39;Series 2&#39;, data: [ 5, 3, 2 ] }, * # { name: &#39;Series3&#39;, data: [ 10, null, 40 ] } ] * * Notice how the extra fields from the series array are included in the output. Also, notice how the missing second * value for Series3 was replaced with a null. HighCharts will skip right over this for category charts as you would * expect. */ /* &lt;CoffeeScript&gt; preOutput = {} seriesNames = [] for a in config seriesNames.push(a.name) for s in seriesNames preOutput[s] = [] for aggregationRow in arrayOfMaps value = aggregationRow[s] unless value? value = null preOutput[s].push(value) # Squash the nameField into each sub row output = [] for s, idx in seriesNames outputRow = {name: s, data: preOutput[s]} seriesRow = config[idx] for key, value of seriesRow unless key in [&#39;name&#39;, &#39;data&#39;] outputRow[key] = value output.push(outputRow) return output csvString_To_CSVStyleArray = (s, asterixForUndefined = true) -&gt; # This is not robust yet. Adding a comma inside a string will break it. It ignores stuff that fails JSON.parse. Etc. rows = s.split(&#39;\n&#39;) headerLength = rows[0].split(&#39;,&#39;).length out = [] for row, index in rows newRow = [] rawRowArray = row.split(&#39;,&#39;) if rawRowArray.length is headerLength for c in rawRowArray if asterixForUndefined and c is &#39;*&#39; cValue = undefined else try cValue = JSON.parse(c) catch error # Not sure what to do if this fails newRow.push(cValue) out.push(newRow) else # throw new Error(&#39;Row length does not match header length.&#39;) console.log(&quot;Warning: Skipping row because length does not match header length in row #{index}: #{row}&quot;) return out csvStyleArray_To_CSVString = (csvStyleArray) -&gt; s = &#39;&#39; for row in csvStyleArray for value in row s += JSON.stringify(value) + &#39;, &#39; s += &quot;\n&quot; return s exports.arrayOfMaps_To_CSVStyleArray = arrayOfMaps_To_CSVStyleArray exports.csvStyleArray_To_ArrayOfMaps = csvStyleArray_To_ArrayOfMaps exports.arrayOfMaps_To_HighChartsSeries = arrayOfMaps_To_HighChartsSeries exports.csvString_To_CSVStyleArray = csvString_To_CSVStyleArray exports.csvStyleArray_To_CSVString = csvStyleArray_To_CSVString &lt;/CoffeeScript&gt; */</pre> </body> </html>