2012-08-14 54 views
0

我在导入csv时遇到了一些问题,以获取highstock图。 我使用与ohlc示例相同的代码(本地工作正常),但使用php在本地主机上创建的另一个CSV。无法让getjson在highstock上工作

PHP获得CSV

<?PHP 

// Declare the new variable as an array 
$arrCSV = array(); 

// Open the CSV file 
if (($handle = fopen("http://ichart.finance.yahoo.com/table.csv?s=AAPL&d=7&e=7&f=2012&g=d&a=8&b=7&c=1984&ignore=.csv", "r")) !==FALSE) 
{ 

// Set the parent array key to 0 
$key = 0; 
// While there is data available loop through unlimited times (0) using separator (,) 
while (($data = fgetcsv($handle, 0, ",")) !==FALSE) { 

    // Count the total keys in each row 
    $c = count($data); 
    //print $c . "<BR>"; // <------ 7 o numero de colunas 

    //Populate the array 
    If ($key != 0) { 
     $arrCSV[$key-1][0] = strtotime($data[0])*1000; //Time 
     $arrCSV[$key-1][1] = $data[1];   //Open 
     $arrCSV[$key-1][2] = $data[2];   //High 
     $arrCSV[$key-1][3] = $data[3];   //Low 
     $arrCSV[$key-1][4] = $data[6];   //Adj Close 
     $arrCSV[$key-1][5] = $data[5];   //Volume 
    } 

    $key++; 
} // end while 

$keymax = $key; 

// Close the CSV file 
fclose($handle); 
} // end if 

print "?(/* AAPL historical OHLC data from the Google Finance API */<BR>"; 
echo json_encode($arrCSV,JSON_NUMERIC_CHECK); 
print ");"; 

?> 

代码导入和创建图形:

<!DOCTYPE HTML> 
<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
     <title>Highstock Example</title> 

      <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> 
     <script type="text/javascript"> 
$(function() { 
    $.getJSON('http://localhost/teste03.php', function(data) { 
     console.log("1"); 
     // create the chart 
     chart = new Highcharts.StockChart({ 
     chart : { 
      renderTo : 'container' 
     }, 

     rangeSelector : { 
      selected : 2 
     }, 

     title : { 
      text : 'AAPL Stock Price' 
     }, 

     series : [{ 
      type : 'ohlc', 
      name : 'AAPL Stock Price', 
      data : data, 
      dataGrouping : { 
       units : [[ 
        'week', // unit name 
        [1] // allowed multiples 
       ], [ 
        'month', 
        [1, 2, 3, 4, 6] 
       ]] 
      } 
     }] 
     }); 
    }); 
}); 

     </script> 
    </head> 
    <body> 
<script src="js/highstock.js"></script> 
<script src="js/modules/exporting.js"></script> 

<div id="container" style="height: 500px; min-width: 500px"></div> 
    </body> 
</html> 

最终它只是让我一个空白页... 在Chrome控制台我'无法获取getjson中的控制台日志,并且它报告好像一切正​​常:

[23:39:29.980] GET http://localhost/TraderMananger/Highstock/ohlc4.htm [HTTP/1.1 304 Not Modified 1ms] 
[23:39:30.036] GET http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js [HTTP/1.1 304 Not Modified 87ms] 
[23:39:30.055] GET http://localhost/TraderMananger/Highstock/js/highstock.js [HTTP/1.1 304 Not Modified 1ms] 
[23:39:30.073] GET http://localhost/TraderMananger/Highstock/js/modules/exporting.js [HTTP/1.1 304 Not Modified 1ms] 
[23:39:30.219] GET http://localhost/TraderMananger/Highstock/teste04.php [HTTP/1.1 200 OK 2056ms] 

帮助?

感谢

回答

0

现在解决了,问题是在CHOS前后json_encode后添加,没有必要将它们添加

print "?(/* AAPL historical OHLC data from the Google Finance API */<BR>"; 
echo json_encode($arrCSV,JSON_NUMERIC_CHECK); 
print ");"; 

所以,删除它们就解决了这个问题。