3

I have created a php to return scores from the db through json encode. Flot just doesnt seem to want them. Theres no errors being returned in firebug. So i am stuck. All i get is a blank chart-

enter image description here

The code..

    <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>

The php that returns the data is

    $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));

And the return Json is

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

1 Answer 1

4

This is the offending line of code:

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

You JSON is already in the dataformat flot wants.

Fiddle here.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.