gpt4 book ai didi

javascript - Google 图表未在浏览器/JSON 中显示

转载 作者:行者123 更新时间:2023-11-28 07:30:52 24 4
gpt4 key购买 nike

我正在尝试使用 Google 图表绘制时间序列图,如果有任何帮助,我们将不胜感激!

我正在尝试显示折线图。

我有一个 html 文件,该文件已正确下载到客户端浏览器,但没有显示任何内容。该文件名为gasdata.html

有一个对 php 文件的调用,该文件输出(通过 echo)一些 json 文件以供上述 .html 文件使用。

  1. 我的 json 是否有缺陷(它确实验证正确)?

  2. 我是否在 html 文件中错误地实现了 Google Charts html?

这里没有太多事件部件,但我不知所措。非常感谢任何帮助。

gasdata.html:

<html>
<head>
<!--Load the AJAX API-->
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">

// Load the Visualization API and the piechart package.
google.load('visualization', '1', {'packages':['corechart']});

// Set a callback to run when the Google Visualization API is loaded.
google.setOnLoadCallback(drawChart);

function drawChart() {
var jsonData = $.ajax({
url: "getData.php",
dataType:"json",
async: false
}).responseText;

// Create our data table out of JSON data loaded from server.
var data = new google.visualization.DataTable(jsonData);

// Instantiate and draw our chart, passing in some options.
var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
chart.draw(data, {width: 400, height: 240});
}

</script>
</head>

<body>
<!--Div that will hold the pie chart-->
<div id="chart_div"></div>
</body>
</html>

输出的json:

{
cols: [{label: 'Date', type: 'date'},
{label: 'Production', type: 'number'}
],
rows: [{c:[{v: new Date(2015, 3, 18)},
{v: 76}
]},
{c:[{v: new Date(2015, 3, 17)},
{v: 75}
]},
{c:[{v: new Date(2015, 3, 16)},
{v: 74}
]},
{c:[{v: new Date(2015, 3, 15)},
{v: 73}
]},
{c:[{v: new Date(2015, 3, 14)},
{v: 72}
]}
]
}

谢谢

最佳答案

您的 JSON 已损坏。 JSON 不允许有诸如 Date 之类的对象,并且对象键必须用引号括起来(指定为字符串)。

参见http://json.org/了解有关 JSON 中允许的内容的更多信息。

关于javascript - Google 图表未在浏览器/JSON 中显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29133267/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com