0

highstock グラフを取得するために csv をインポートする際に問題が発生しています。私は ohlc の例 (ローカルで正常に動作します) と同じコードを使用していますが、私のローカルホストで php によって作成された別の CSV を使用しています。

CSVを取得するPHP

<?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]); //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) {

      // 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>

結局、空白のページが表示されるだけです...これはlocalhostを使用していることが原因ですか? 配列の順序 (昇順ではなく降順)?

ヘルプ?

更新: json_encode が追加されましたが、まだ機能しません。

4

1 に答える 1

0

さらに編集 ajax の問題がある可能性があるようです。分離テストを試してみてください

SimpleTest.php(コードについてはOLDセクションを参照)を使用して、現在の と同じサーバーでホストし、teste03.phpそこからチャートにアクセスします。

 $.getJSON('http://localhost/SimpleTest.php', function(data) {
 ...
 }

また

<script type="text/javascript">
$(function() {
// $.getJSON('http://localhost/teste03.php', function(data) {
   var data= [[1000000,1,2,3,4],[2000000,3,2,3,4],[1000000,1,2,3,4]];
  // create the chart
  chart = new Highcharts.StockChart({
   ...

上記のアプローチのいずれかが機能する場合は、ハイチャートの問題ではなく、ajax の問題があることを意味します。

EDITED(返されたjsonに関するコメントに基づく)

データのタイムスタンプ値は昇順である必要があります。次のjsonから

 [[1344290400,622.77,625,618.04,618.26,10373100],[1344204000,617.29,624.87,615.26‌​,619.89,10789400]

1344290400>1344204000 したがって、機能しません。

OLDは、json の形成にjson_encode
メソッドを 使用します。 それに渡す必要があるのは、外側の配列が CSV の行数と同じサイズである配列の配列であり、この外側の配列の各要素は 5 つの要素を持つ別の配列です。タイムスタンプ、始値、高値、安値、終値。

SimpleTest.php:

<?php
  // JSON header
  header('Content-type: application/json');

  // Do not cache the response
  header('Cache-Control: no-cache, must-revalidate');
  header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');  

  // Parse CSV, and populate the 5 arrays viz. $timeStamp, $open, $high, $low, $close
  $count=3;
  $timeStamp=array(1000000,2000000,3000000); // In ascending order of time
  $open=array(5,10,15);
  $high=array(10,15,20);
  $low=array(0,5,10);
  $close=array(8,12,18);

  $dataArray=array();   // Outer array of array

  for( $i=0; $i<$count; $i++ ){
     // push an array into $dataArray for each data group
     $dataArray[] = array($timeStamp[$i], $open[$i], $high[$i], $low[$i],$close[$i]);
  }

  echo json_encode($dataArray); // Encode php array of array into json and echo/print it to output
?>

あなたのコードを見ると、 $arrCSV を微調整して、必要な配列に変換できると思います。

于 2012-08-13T19:23:44.000 に答える