1 /** 2 * Copyright (c) 2012 The Chromium Authors. All rights reserved. 3 * Use of this source code is governed by a BSD-style license that can be 4 * found in the LICENSE file. 5 */ 6 'use strict'; 7 8 /** 9 * Main entry point for the rendering of the Viewer component. 10 * @param {Event} evt File select event. 11 */ 12 function handleFileSelectEvent(evt) { 13 var file = evt.target.files[0]; 14 var fileReader = new FileReader(); 15 16 fileReader.onload = (function(theFile) { 17 return function(e) { 18 var dataSet = createViewerDataset(e.target.result); 19 var viewer = new Viewer({ownerDocument:window.document}); 20 viewer.id = 'csvContentsDataTable'; 21 viewer.dataSet = dataSet; 22 viewer.barChartElementId = 'barChart'; 23 viewer.render(); 24 }; 25 })(file); 26 fileReader.readAsText(file); 27 } 28 29 /** 30 * Splits the csv file contents, and creates a square array of strings for 31 * each token in the line. 32 * @param fileContents 33 * @return {String[][]} The data set with the contents of the csv file. 34 */ 35 function createViewerDataset(fileContents) { 36 var rows = fileContents.split('\n'); 37 var dataset = []; 38 rows.forEach(function (row, index, array) { 39 if (row.trim().length == 0) { 40 //Ignore empty lines 41 return; 42 } 43 var newRow = []; 44 var columns = row.split(','); 45 columns.forEach(function (element, index, array) { 46 newRow.push(element); 47 }); 48 dataset.push(newRow); 49 }); 50 return dataset; 51 }