ajax要求json不填充flot chart

时间:2013-11-13 13:04:00

标签: jquery json flot

我创建了一个php来从db通过json编码返回分数。 Flot似乎并不想要它们。在萤火虫中没有错误返回。所以我被卡住了。我得到的只是一张空白图表 -

enter image description here

代码..

    <script type="text/javascript">

    $(function() {

        var options = {
             bars: {
                show: true
            }
        }

        $.plot("#placeholder", data, options);

        var iteration = 0;

        function fetchData() {

            ++iteration;

            function onDataReceived(series) {

                data = [ series ];

                $.plot("#placeholder", data, options);
            }

            $.ajax({
                url: "http://localhost/charts/data",
                type: "GET",
                dataType: "json",
                success: onDataReceived
            });

            if (iteration < 5) {
                setTimeout(fetchData, 10000);
            } else {
                data = [];
                alreadyFetched = {};
            }
        }
        fetchData();
        setTimeout(fetchData, 10000);



    });

    </script>
</head>
<body>

    <div id="header">
        <h2>AJAX</h2>
    </div>

    <div id="content">

        <div class="demo-container">
            <div id="placeholder" class="demo-placeholder"></div>
        </div>

    </div>


</body>
</html>

返回数据的php是

    $data = array(array('1999',3.0),array('2000',3.9),array('2001',2.0),array('2002',1.2)); 
    $array = array('label' => 'Scores','data'=>$data);
    echo json_encode(array($array));

返回Json是

[{"label":"Scores","data":[["1999",3],["2000",3.9],["2001",2],["2002",1.2]]}]

1 个答案:

答案 0 :(得分:4)

这是令人讨厌的代码行:

data = [ series ]; // do not wrap in another array

你JSON已经在dataformat flot中了。

小提琴here