0

我在导入 csv 以获取 highstock 图表时遇到了一些问题。我使用与 ohlc 示例相同的代码(在本地运行良好),但使用了另一个 CSV,它是由 php 在我的本地主机上创建的。

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]

帮助?

谢谢

4

1 回答 1

0

现在解决了,问题是在json_encode前后添加的chos,不需要添加

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

因此,删除它们就解决了问题。

于 2012-08-16T20:26:53.660 回答