2016-12-29 74 views
0

我想通過一個文件,以獲得CSV數據到下面熱平衡圖:谷歌熱平衡圖與CSV數據

我使用https://developers.google.com/chart/interactive/docs/gallery/sankey

<html> 
<body> 
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script> 

<div id="sankey_multiple" style="width: 900px; height: 300px;"></div> 

<script type="text/javascript"> 
    google.charts.load("current", {packages:["sankey"]}); 
    google.charts.setOnLoadCallback(drawChart); 
    function drawChart() { 
    var data = new google.visualization.DataTable(); 
    data.addColumn('string', 'From'); 
    data.addColumn('string', 'To'); 
    data.addColumn('number', 'Weight'); 
    data.addRows([ 
     [ 'Brazil', 'Portugal', 5 ], 
     [ 'Brazil', 'France', 1 ], 
     [ 'Brazil', 'Spain', 1 ], 
     [ 'Brazil', 'England', 1 ], 
     [ 'Canada', 'Portugal', 1 ], 
     [ 'Canada', 'France', 5 ], 
     [ 'Canada', 'England', 1 ], 
     [ 'Mexico', 'Portugal', 1 ], 
     [ 'Mexico', 'France', 1 ], 
     [ 'Mexico', 'Spain', 5 ], 
     [ 'Mexico', 'England', 1 ], 
     [ 'USA', 'Portugal', 1 ], 
     [ 'USA', 'France', 1 ], 
     [ 'USA', 'Spain', 1 ], 
     [ 'USA', 'England', 5 ], 
     [ 'Portugal', 'Angola', 2 ], 
     [ 'Portugal', 'Senegal', 1 ], 
     [ 'Portugal', 'Morocco', 1 ], 
     [ 'Portugal', 'South Africa', 3 ], 
     [ 'France', 'Angola', 1 ], 
     [ 'France', 'Senegal', 3 ], 
     [ 'France', 'Mali', 3 ], 
     [ 'France', 'Morocco', 3 ], 
     [ 'France', 'South Africa', 1 ], 
     [ 'Spain', 'Senegal', 1 ], 
     [ 'Spain', 'Morocco', 3 ], 
     [ 'Spain', 'South Africa', 1 ], 
     [ 'England', 'Angola', 1 ], 
     [ 'England', 'Senegal', 1 ], 
     [ 'England', 'Morocco', 2 ], 
     [ 'England', 'South Africa', 7 ], 
     [ 'South Africa', 'China', 5 ], 
     [ 'South Africa', 'India', 1 ], 
     [ 'South Africa', 'Japan', 3 ], 
     [ 'Angola', 'China', 5 ], 
     [ 'Angola', 'India', 1 ], 
     [ 'Angola', 'Japan', 3 ], 
     [ 'Senegal', 'China', 5 ], 
     [ 'Senegal', 'India', 1 ], 
     [ 'Senegal', 'Japan', 3 ], 
     [ 'Mali', 'China', 5 ], 
     [ 'Mali', 'India', 1 ], 
     [ 'Mali', 'Japan', 3 ], 
     [ 'Morocco', 'China', 5 ], 
     [ 'Morocco', 'India', 1 ], 
     [ 'Morocco', 'Japan', 3 ] 
    ]); 

    // Set chart options 
    var options = { 
     width: 600, 
    }; 

    // Instantiate and draw our chart, passing in some options. 
    var chart = new google.visualization.Sankey(document.getElementById('sankey_multiple')); 
    chart.draw(data, options); 
    } 
</script> 
</body> 
</html> 

這段代碼是什麼我想要做的就是導入CSV文件將3個變量放入此Sankey圖表中,而不是像這裏所做的那樣對其進行硬編碼。 CSV文件的

實施例可以是:

從,到,計數

google.com,amazon.com,5

google.com,ford.com,10

表的形式,如果它是減少混亂:

From    To  count 
google.com amazon.com  5 
google.com ford.com  10 
+0

您可以添加csv或給它的結構的一個小樣本? –

+0

已上傳結構示例 –

+0

請查看 - https://developers.google.com/chart/interactive/docs/dev/dsl_javadocs/com/google/visualization/datasource/util/CsvDataSourceHelper – Amit

回答

1

谷歌圖表不支持加載CSV˚F ILE出的即裝即用,但你可以使用CSV插件,將其轉換爲一個數組。我會建議使用這一個jquery-csv。使用它像這樣

<body> 
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script> 
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-csv/0.71/jquery.csv-0.71.min.js"></script> 
    <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script> 

<div id="sankey_multiple" style="width: 900px; height: 300px;"></div> 

<script type="text/javascript"> 
    google.charts.load("current", {packages:["sankey"]}); 
    google.charts.setOnLoadCallback(drawChart); 
    function drawChart() { 
    $.get("data.csv", function(csvString) { 
     // fetch CSV file using ajax 
     var arrayData = $.csv.toArrays(csvString, {onParseValue: $.csv.hooks.castToScalar}); 
     // after parsing , arrayData is 2-D array holding all the data contained in CSV file 

     var data = new google.visualization.DataTable(); 
     data.addColumn('string', 'From'); 
     data.addColumn('string', 'To'); 
     data.addColumn('number', 'Weight'); 
     data.addRows(arrayData); 

     // Set chart options 
     var options = { 
     width: 600, 
     }; 

     // Instantiate and draw our chart, passing in some options. 
     var chart = new google.visualization.Sankey(document.getElementById('sankey_multiple')); 
     chart.draw(data, options); 

    }); 
    } 

</script> 
</body> 

你data.csv文件可能是這樣的,不需要標頭

google.com,amazon.com,5 
google.com,ford.com,10 
google.com,stackoverflow.com,50 
google.com,ford.com,10