How can I use php values(sent by ajax) in a jqplot chart?

The code pasted below does not create a graph. When I manually type an array of values the graph will chart. I have the chart sent to a div with the id "chart1".

--- PHP ---

    $array = array(
    array("Books", 12),
    array("Movies", 9)
       );
 $json = json_encode($array);
 var_dump($json); 

---- JAVASCRIPT ----

$(document).ready(function(){
  var charts;
  $.ajax({     
  type: "POST",  
  async: false, 
  dataType: "json", 
  url: "submitform.php",    
  data: 'action=chart',  
  success: function(data){       
  charts = data;
       }    
        });   


   var plot1 = jQuery.jqplot ('chart1', [charts],
     { 
  seriesDefaults: {
      shadow: false,
    renderer: jQuery.jqplot.PieRenderer, 
    rendererOptions: {
      highlightMouseOver: false,
      showDataLabels: true
    }
  }, 
  seriesColors: [ "#CCF", "#FCC", "#6CF", "#6C6", "#FF9"],
      legend: { show:true, location: 'e' }
     }
  );
 });

If I alert(charts) I get this:

[["Books",12],["Movies",9]]

So I think my format is fine.

whew, sorry if that was long. (sorry for the poor formatting)

share|improve this question
feedback

1 Answer

up vote 0 down vote accepted

try it

$(document).ready(function(){
    $(".success").fadeIn("fast");
    var charts = null;
    $.ajax({     
        type: "POST",  
        async: false,  
        url: "submitform.php",    
        data: 'action=chart',  
        dataType: "json", 
        success: function(data){     
            charts = data;
            $(".success").html(charts + "charts "); 
        }    
    });   


    var chartData = [["Books",12], ["Movies",9]];
    alert(charts);
    var plot1 = jQuery.jqplot ('chart1', [charts], { 
        seriesDefaults: {
            shadow: false,
            renderer: jQuery.jqplot.PieRenderer, 
            rendererOptions: {
                highlightMouseOver: false,
                showDataLabels: true
            }
        }, 
        seriesColors: [ "#CCF", "#FCC", "#6CF", "#6C6", "#FF9"],
        legend: { show:true, location: 'e' }
    });
});
share|improve this answer
try new version – Catalin Ene yesterday
same results, I can alert out the results and they are formatted correctly, but the graph doesnt use them. I think it might be something with my javascript, and not my php. – user1744232 yesterday
Can you post a link ? – Catalin Ene yesterday
replace the js code – Catalin Ene yesterday
wow! so what was my error? and thanks so much!! – user1744232 yesterday
show 1 more comment
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.