【发布时间】:2016-03-18 15:54:11
【问题描述】:
我计划使用 googlecharts 绘制存储在本地数据库中的数据图表 - googlecharts 代码也将存储在本地服务器上。我使用 XAMPP 创建了一个本地目录('http://http://127.0.0.1/)。在进入项目之前,我正在测试一个示例脚本(我从 google 搜索中找到),它从 json 文件(从 php 脚本调用)获取数据,然后将其提供给 googlecharts 绘图函数。按照建议,这 3 个文件都存储在文件夹“C:\xampp\htdocs”中,但是当我在浏览器 (http://127.0.0.1/test_php_long.html) 中加载页面时,页面显示为空白(在 Chrome、IE 和 Firefox 上)。当我运行将数据硬编码到 HTML 中的 googlecharts 脚本时,它可以正常工作。我已经在网上搜索了几个小时,但还没有找到可行的解决方案。
这是主要的 html 文件('test_php_long.html'):
<html>
<head>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript" src="https://www.maxcdn.com/one//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">
google.load('visualization', '1', {'packages':['corechart']});
google.setOnLoadCallback(drawChart);
function drawChart() {
var jsonData = $.ajax({
//fetchjson.php is a php script that will fetch the JSON data e generated using above php code
url: "fetchjson.php",
dataType:"json",
async: false
}).responseText;
var data = new google.visualization.DataTable(jsonData);
var chart = new google.visualization.LineChart(document.getElementById('chart_flow'));
chart.draw(data, {width: 1200, height: 300, lineWidth: 1, pointSize: 1.3, pointWidth: 3});
}
</script>
</head>
<body>
<div id="chart_flow"></div>
</body>
</html>
这是 php 文件('fetchjson.php')
<html>
<head>
<title></title>
</head>
<body>
<?php
//Fill the ‘string’ variable with JSON data from file we used as json container populated by php //code: ‘flow.json’
$string = file_get_contents("flow.json");
//push JSON data out
echo $string;
?>
</body>
</html>
这是 json 文件('flow.json'):
{"cols":[{"id":"","label":"Time","pattern":"","type":"string"},{"id":""," label":"Cache Hits","pattern":"","type":"number"},{"id":"","label":"Non-Cache Hits","pattern":"", "type":"number"}],"rows":[{"c":[{"v":"2014-01-21 00:00:00"},{"v":2},{" v":23}]},{"c":[{"v":"2014-01-21 01:00:00"},{"v":2},{"v":52}]} , ... ,{"c":[{"v":"2014-01-23 01:00:00"},{"v":0},{"v":34}]},]}
谢谢!
【问题讨论】:
标签: php json google-visualization