【问题标题】:simple php data to jquery (json)简单的 php 数据到 jquery (json)
【发布时间】:2011-03-11 16:59:47
【问题描述】:

我想使用来自 PHP 和 jqplot 的数据绘制一个简单的图表。

在 PHP 中我只是这样做了:

$test = array('1' => '2', '3' => '4'); 
echo json_encode($test); 

我在js端得到的结果是:{"1":"2","3":"4"},我检查了这个。

所以,我尝试绘制这个简单的数组,但没有成功:

$.ajax({  
      type: "POST",  
      url: "ajax.php",  
      data: "fname="+ fname +"& lname="+ lname,
      dataType:"json", 
      success: function(data)
        {  
        $('#dissapear').hide(function(){$('#chartdiv').css("height:400px;width:300px;");$('#chartdiv').fadeIn();});
        $.jqplot
         ('chartdiv', [data], 
           {
           title:'Jitter',
           axes:{yaxis:{min:0,max:240},xaxis:{renderer:$.jqplot.DateAxisRenderer}},
           series:[{lineWidth:4, markerOptions:{style:'square'}},{ show: true } ]
           }
         );

我错过了什么?请帮帮我,我已经尝试了至少 20 种代码组合,但没有运气。是的,我确实查看了 jqplot 中的 dataRenderer 示例,但它对我没有帮助。

【问题讨论】:

    标签: php arrays json jqplot


    【解决方案1】:

    我不熟悉 jqplot,但在我看来:

    $test = array('1' => '2', '3' => '4'); 
    

    应该是

    $test = array(1 => 2, 3 => 4);
    

    也就是说,你不需要数组值是字符串。

    【讨论】:

    • 是的,我同意,我错过了那个。但即使改变了这一点,也没有。问题是我不明白 jqplot 期望什么样的格式。
    • 你看到这个文档了吗? jqplot.com/docs/files/usage-txt.html 。看起来您想要生成如下所示的 json: [[[1, 2],[3,5.12],[5,13.1],[7,33.6],[9,85.9],[11,219.9]]]
    【解决方案2】:

    我相信当您返回 json 时会出现问题...您的 javascript 不明白它是一个数组。没有“as array”类型转换,所以我在这个问题上摸不着头脑。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多