0

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>

2 Answers 2

1

The contents of your pastebin show that the JSON string you're outputting is invalid JSON.

var data = [{ label: "Random Values",data: dataset1}];

will validate if it's changed to:

var data = [{"label": "Random Values","data": "dataset1"}]

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.

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

2 Comments

I got the chart working with a set js array (using my original code) then tried to fill the array from the database then it broke, so i know the chart drawing is right. The only place I can think the problem is is in the conversion but it looks ok to me
Why don't you output your json string from PHP along with the code to build the string (append to your answer) and we can de-bug it from there.
0

Try delaying creating the graph until the DOM is loaded:

jQuery(document).ready(function ($){
    var plotarea = $("#placeholder");
    $.plot( plotarea , data);
});

2 Comments

Can you post a sample of it to jsfiddle? (Just copy the output of your php, including the serialized mysql data). I'll have a look
I was taking data from a local db so instead of doing that i ran it locally and here is the output pastebin.com/HeeuTi4Z does this help? If not I can try figuring out jsfiddle

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.