jquery - 使用 jQuery 和 Flot 插件绘制图表

标签 jquery mysql css graph flot

 <?php

        while($row = mysql_fetch_assoc($sql))
        {
            $dataset1[] = array($row['dateReport'],$row['totalUniqueView']);
        }

        echo json_encode($dataset1);
    ?>

$(document).ready(function () {
    //Graph scripts here.

        var graphData = <?php echo json_encode($dataset1); ?>;

大家好,我需要帮助。我设法按照这里的教程进行操作:http://designmodo.com/create-interactive-graph-css3-jquery/

而且它运行良好。我试图修改它,以便它可以从我的数据库中读取数据。从上面的代码中可以看出,我更改并添加了一些 php 行并修改了脚本。但是,它不起作用,它向我的图表返回了奇怪的数据。请帮忙。

最佳答案

好的,您的数据存在一些问题。

首先,日期/时间必须以 JavaScript 时间戳的形式提供;摘自the docs :

The time series support in Flot is based on Javascript timestamps, i.e. everywhere a time value is expected or handed over, a Javascript timestamp number is used. This is a number, not a Date object. A Javascript timestamp is the number of milliseconds since January 1, 1970 00:00:00 UTC. This is almost the same as Unix timestamps, except it's in milliseconds, so remember to multiply by 1000!

其次,您的数据用引号括起来,这意味着它是作为字符串而不是数字传递的 - 这将阻止 Flot 能够正确绘制数据。来自 the FAQ :

Flot isn't working when I'm using JSON data as source!

Actually, Flot loves JSON data, you just got the format wrong. Double check that you're not inputting strings instead of numbers, like [["0", "-2.13"], ["5", "4.3"]]. This is most common mistake, and the error might not show up immediately because Javascript can do some conversion automatically.

您应该能够通过生成如下所示的数据来解决这两个问题:

    while($row = mysql_fetch_assoc($sql))
    {
        $dataset1[] = array(strtotime($row['dateReport']) * 1000, (int)$row['totalUniqueView']);
    }

关于jquery - 使用 jQuery 和 Flot 插件绘制图表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20261116/

相关文章:

javascript - Jquery/JS - 箭头和返回键选项卡索引

javascript - 将 HighMaps JQuery 的数据更改为本地文件

javascript - 返回对象的 ID 为零(在数据库中创建记录)

html - 如何使用适当的标记和 CSS 完成此布局?

css - 在 Eclipse 4 中使用 CSS 设置背景颜色和背景图像

javascript - 模态窗口未关闭问题

jquery - 同位素动态添加 getSortData

javascript - 当我到达某个 div 时,scrollTop 不起作用

python - 在 Heroku 上使用 Python/Django 访问 ClearDB 数据库被拒绝

mysql - 关系代数银行数据库