从 .CSV 文件绘制 Google 可视化图表

问题描述 投票:0回答:1

我不是程序员

我想从 .CSV 文件绘制 Google 可视化图表

我有如下 .csv 文件:

time,temperature
2023-08-21 12:00:00,25
2023-08-21 12:01:00,26
2023-08-21 12:02:00,27

这是我的代码:

<!DOCTYPE html><html><head><title>Temperature vs. Time Chart</title><script src="https://www.google.com/jsapi"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.csv/0.71/jquery.csv.min.js"></script></head><body><div id="chart"></div>
<script>// Load the Google Visualization API and the jQuery-CSV library.google.load("visualization", "1", {packages: ["corechart", "controls"]});$.getScript("https://cdnjs.cloudflare.com/ajax/libs/jquery.csv/0.71/jquery.csv.min.js");
// Function to draw the chart.
function drawChart() {
  // Get the data from the CSV file.
  var csvData = $.csv("temp.csv");

  // Create a new DataTable object from the CSV data.
  var data = new google.visualization.arrayToDataTable(csvData);

  // Create a new chart object.
  var chart = new google.visualization.LineChart(document.getElementById("chart"));

  // Set the chart options.
  chart.options.title = "Temperature vs. Time";
  chart.options.width = 600;
  chart.options.height = 400;

  // Draw the chart.
  chart.draw(data);
}

// Call the drawChart() function when the page loads.
google.setOnLoadCallback(drawChart);
</script></body></html>

不幸的是无法工作,给我一个空白页

csv charts google-visualization read.csv
1个回答
0
投票

首先,需要确保您正在调用google的

load
语句,在提供的代码中没有看到它。

google.charts.load('current', {packages: ['corechart']});

并且您可以使用

load
语句返回的承诺来了解页面何时加载。

接下来,

options
应该是一个单独的变量,并作为第二个参数传递给
draw
方法。

请参阅以下片段...

google.charts.load('current', {
  packages: ['corechart']
}).then(function () {
  // Get the data from the CSV file.
  var csvData = $.csv("temp.csv");

  // Create a new DataTable object from the CSV data.
  var data = new google.visualization.arrayToDataTable(csvData);

  // Create a new chart object.
  var chart = new google.visualization.LineChart(document.getElementById("chart"));

  // Set the chart options.
  var options = {};
  options.title = "Temperature vs. Time";
  options.width = 600;
  options.height = 400;

  // Draw the chart.
  chart.draw(data, options);
});
© www.soinside.com 2019 - 2024. All rights reserved.