2

I'm trying to plot some JSON Data using the jqPlot library within an HTML 5 app powered by jqMobile. I'm placing the following code within the 'body' of the html page. Is there something I'm missing here?

<script>    
$(document).ready(function() {
    // get the JSON data from server
    $.getJSON("myspecialurl", function(data) {
        success: function(data) {
            plotData(data);
        }
    });
    // plot the data
    function plotData(data) {
        ds = [];
        $(data).find('latitude').each(function() {
            ds.push([$(this).attr('answer'), parseInt($(this).attr('count'))]);
        });
        $.jqplot('chart1', [ds], {
            seriesDefaults: {
                renderer: $.jqplot.DonutRenderer
            },
            legend: {
                show: true
            }
        });
    }
}    
</script>

Edit: New Plot Method

function plotData( data ) {
 // ds = [];
 // $(data).find('latitude').each( function() {
 //   ds.push( [ $(this).attr('answer'), parseInt( $(this).attr('count') ) ] );
 // } );
var array = data.contacts;


$.jqplot('chart1', array[0].latitude, {
seriesDefaults:{
   renderer:$.jqplot.DonutRenderer
},
legend: {show:true}
 });
}
7
  • At the fist glance it appears to be OK. How does the JSON looks like before your operations. Do you enter the plotData()? Commented Jun 21, 2012 at 15:27
  • the console is throwing this error Uncaught SyntaxError: Unexpected token ( Commented Jun 21, 2012 at 15:37
  • I also get this error regarding data Exception: ReferenceError: data is not defined] Commented Jun 21, 2012 at 15:39
  • OK but at which point/step in your code? Plus show the JSON. Commented Jun 21, 2012 at 15:39
  • api.flickr.com/services/feeds/…? is json url Commented Jun 21, 2012 at 15:41

1 Answer 1

1

Of coarse there is a problem and the computer is again right. This is how your code should look like. You were defining success as if you were using ajax method, with getJSON success is passed as the 2nd parameter.

$.getJSON("myspecialurl", function(data) {
    plotData(data);
});

EDIT I spot also that you are not closing the ready function appropriately. It should be }); rather than just }.

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

6 Comments

ahhh, ok yup that cleared up that error, however the console is now throwing this error Uncaught No plot target specified but yet I am specifying a plot...
At which line is the reference exception thrown?
at line 1881 in jquery.jqplot.js file which makes me think that this code is fine, except that I can't access my data. when I try to access a certain piece of data it is held within an index, like 0,1,2,3. When I try to do data.contacts.0.name it does not like the number 0, even though I need that to access the JSON data bit. Is there a way around this?
Please see the EDIT. I hope it wasn't me who removed the ready method's closing.
ok so I have edited my question so that it shows my lastest plot method, which is still showing "Uncaught No Plot target specified" error in console, however I now know that my data is there (I've been able to log in to the console). Am I not understanding something about jqPlot?
|

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.