2015-12-02 32 views
0

我有一个csv文件,我读入一个JavaScript变量。我现在尝试为此创建一个可视化。解析json数据列

<html> 
    <head> 
    <script type="text/javascript" src="https://www.google.com/jsapi"></script> 

    <script src="http://code.jquery.com/jquery-1.8.2.js"></script> 
    <link href="http://code.jquery.com/ui/1.9.0/themes/cupertino/jquery-ui.css" rel="stylesheet" /> 
    <script src="http://code.jquery.com/ui/1.9.0/jquery-ui.js"></script> 
    <script></script> 
    <script src="http://jquery-csv.googlecode.com/git/src/jquery.csv.js"></script> 


    <script type="text/javascript"> 
     google.load("visualization", "1", {packages:["motionchart"]}); 
     google.setOnLoadCallback(drawChart); 
     function drawChart() { 

     $.get("stockdata.csv", function(csvString) { 
var arrayData = $.csv.toArrays(csvString, {onParseValue: $.csv.hooks.castToScalar}); 
    // this new DataTable object holds all the data 
    var data = new google.visualization.arrayToDataTable(arrayData); 

但是,数据变量的第二列必须被类型转换为日期。该字符串已经是yyyy-mm-dd格式,但这不被可视化接受。我如何才能将第二列转换为日期类型。

回答

0

只需在可视化之前修改您的数据即可。 map函数将收集对象(或数组的一行)的每个元素并将其作为新数组返回。您将在返回之前修改每一行。

arrayData = arrayData.map(function(row) { 
    row[1] = new Date(row[1]); 
    return row; 
}