2

如何在 jqplot 图表中使用 php 值(由 ajax 发送)?

下面粘贴的代码不会创建图表。当我手动键入一组值时,图表将绘制图表。我已将图表发送到 ID 为“chart1”的 div。

--- 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' }
     }
  );
 });

如果我提醒(图表)我得到这个:

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

所以我认为我的格式很好。

哇,对不起,如果那很长。(抱歉格式不好)

4

1 回答 1

0

试试看

$(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' }
    });
});
于 2012-10-14T02:52:30.113 回答