在 Flot 图表上显示数据库中的值(使用 mySQL)
我正在尝试使用 php (mySQL) 从数据库读取值,然后将它们显示在 flot 中的图表上。我知道这些值已正确读取,并且没有收到任何错误,但图表不会显示。 帮助不大?
提前致谢。
<?php
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
$graphdata[] = array( (int)$row[0], (int)$row[1] );
}
?>
/////
<div id="placeholder" style="width:600px;height:300px"></div>
<script language="javascript" type="text/javascript">
var dataset1 = <?php echo json_encode($graphdata);?>;
var data = [
{
label: "Random Values",
data: dataset1
}
];
var plotarea = $("#placeholder");
$.plot( plotarea , data);
</script>
I'm trying to read in values from a db using php (mySQL) then have them show on a graph in flot. I know the values are read in correctly and I'm not getting any errors but the graph won't show.
Little help?
Thanks in advance.
<?php
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
$graphdata[] = array( (int)$row[0], (int)$row[1] );
}
?>
/////
<div id="placeholder" style="width:600px;height:300px"></div>
<script language="javascript" type="text/javascript">
var dataset1 = <?php echo json_encode($graphdata);?>;
var data = [
{
label: "Random Values",
data: dataset1
}
];
var plotarea = $("#placeholder");
$.plot( plotarea , data);
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Pastebin 的内容显示您输出的 JSON 字符串是无效的 JSON。
将验证它是否更改为:
这只是一个示例,但我怀疑 Flot 正在寻找稍微不同的格式,因此您必须根据其文档准确验证他们正在寻找的内容。我现在正在使用 FusionCharts 进行同样的练习,所以我能感受到你的痛苦。 jsonlint.com 是你的朋友,输出你的 JSON 并经常验证它。我还建议一开始就让它工作,从一串 JSON(甚至是从他们的示例中复制的字符串)开始,然后将其直接放入代码中。首先让图表工作,然后让 PHP 单独复制示例 JSON 字符串。
The contents of your pastebin show that the JSON string you're outputting is invalid JSON.
will validate if it's changed to:
That's just an example, but I suspect that Flot is looking for a slightly different format, so you'll have to verify exactly what they're looking for against their documentation. I'm going through the same exercise right now with FusionCharts, so I'm feeling your pain. jsonlint.com is your friend on this one, output your JSON and verify it frequently. I'd also recommend that to initially get it working, start with just a string of JSON (even one that you copy from their examples) that you put right in your code. Get the chart working first, then work on getting your PHP to duplicate the example JSON string separately.
尝试延迟创建图表,直到加载 DOM:
Try delaying creating the graph until the DOM is loaded: