如何从上传的csv文件绘制Google散点图?
显示空白页。
我尝试了以下代码:
正在加载脚本:
<script src="https://www.google.com/jsapi"></script>
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-csv/0.71/jquery.csv-0.71.min.js"></script>
然后我从一个示例中得到了这一部分:
<script> // wait till the DOM is loaded
$(function() {
// grab the CSV
$.get("https://www.batemo.de/wp-content/uploads/general/data_chart_gravimetric.csv", function(csvString) {
// display the contents of the CSV
$("#chart").html(csvString); }); });
// load the visualization library from Google and set a listener
google.load("visualization", "1", {packages:["corechart"]});
google.setOnLoadCallback(drawChart);
function drawChart() {
// grab the CSV
$.get("https://www.batemo.de/wp-content/uploads/general/data_chart_gravimetric.csv", function(csvString) {
// transform the CSV string into a 2-dimensional array
var arrayData = $.csv.toArrays(csvString, {onParseValue: $.csv.hooks.castToScalar});
// this new DataTable object holds all the data
var data = new google.visualization.arrayToDataTable(arrayData);
// this view can select a subset of the data at a time
var view = new google.visualization.DataView(data); view.setColumns([0,1]);
var options = {};
var chart = new google.visualization.ScatterChart(document.getElementById('chart'));
chart.draw(view, options);
}); }
</script>
该示例有一个错误。
以下行末尾的括号...
$("#chart").html(csvString); }); }); <-- these two sets of brackets
需要向下移动到其余代码的下方,这是完整的,经过更正的代码段...
$(function() {
// grab the CSV
$.get("https://www.batemo.de/wp-content/uploads/general/data_chart_gravimetric.csv", function(csvString) {
// display the contents of the CSV
$("#chart").html(csvString);
// load the visualization library from Google and set a listener
google.load("visualization", "1", {packages:["corechart"]});
google.setOnLoadCallback(drawChart);
function drawChart() {
// grab the CSV
$.get("https://www.batemo.de/wp-content/uploads/general/data_chart_gravimetric.csv", function(csvString) {
// transform the CSV string into a 2-dimensional array
var arrayData = $.csv.toArrays(csvString, {onParseValue: $.csv.hooks.castToScalar});
// this new DataTable object holds all the data
var data = new google.visualization.arrayToDataTable(arrayData);
// this view can select a subset of the data at a time
var view = new google.visualization.DataView(data); view.setColumns([0,1]);
var options = {};
var chart = new google.visualization.ScatterChart(document.getElementById('chart'));
chart.draw(view, options);
}); }
}); });