2012-08-08 71 views
0

我使用jqplot和已經構建用下面的PHP代碼jqplot不評估串

if($fiveRes){ 
    $lastNum = mysql_num_rows($fiveRes); 

    $testarray = array(); 

    while($row = mysql_fetch_array($fiveRes, MYSQL_ASSOC)) { 
     $testarray[] = array($row['GameDate'], floatval($row['WKP'])); 
    } 

    echo json_encode($testarray); 

} 

該代碼輸出正確的代碼,我需要插入jqplot函數數組。下面是由上述代碼生成的數組:

[["2011-12-24",0],["2011-12-19",14],["2011-12-08",22],["2011-12-04",14],["2011-11-27",12]] 

所以我目前打印該陣列到屏幕上,然後使用jQuery的.text()來捕獲字符串,並將其放置在變量中。我可以回顯出分配給數組字符串的變量,並且它可以正常工作,但是當我將它傳遞給jqplot函數時,它什麼也不做。

var p1array = $('.col.first .parray').text(); 

alert(p1array); //Alerts the correct array formatted like above. 

    var plot1 = $.jqplot('jqplot0', [p1array], { 
      title:'Last 5 Fantasy Points', 
      axes:{ 
      xaxis:{ 
       renderer:$.jqplot.DateAxisRenderer, 
       tickOptions:{ 
       formatString:'%b %#d' 
       } 
      }, 
      yaxis:{ 
       tickOptions:{ 
       formatString:'' 
       } 
      } 
      }, 
      highlighter: { 
      show: true, 
      sizeAdjust: 7.5 
      }, 
      cursor: { 
      show: false 
      } 
     }); 

爲了使它更加複雜,如果我複製PHP生成的字符串並將其硬編碼到JS中的變量中,它就起作用。任何想法爲什麼jqplot插件不會評估使用$(this).text();的字符串。

這裏是jQplot例子中,我以後造型:使用此方法http://www.jqplot.com/tests/cursor-highlighter.php

+0

我不知道爲什麼文本()不工作,但你試過/或是否有任何問題使用p1array = <?php echo $ php_string_you_created;?>; – TigerTiger 2012-08-08 16:55:53

+0

如果你使用沒有括號的p1array,會發生什麼情況?即var plot1 = $ .jqplot('jqplot0',p1array,{... – Jim 2012-08-08 16:56:51

+0

試過沒有括號,我得到了相同的結果 – jsheffers 2012-08-08 16:59:10

回答

0

使用[p1array]直接與出括號,並嘗試...它應該工作就像p1array

+0

jQplot使用該語法,沒關係,如果方括號在它附近我試過了 – jsheffers 2012-08-09 17:05:21

+0

can y ou try var p1array = eval('{'+ $('。col.first .parray')。text()+'}');這可能會幫助你......希望如此 – 2012-08-10 05:27:22