[英]Highstock - Creating an OHLC graph by csv created using php
我在導入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]); //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>
最后,它只是給我一個空白頁...這是由於使用本地主機引起的嗎? 陣列的順序(子代代替方興未艾)?
救命?
更新:已添加json_encode,但仍然無法正常工作。
進一步編輯似乎您可能遇到了ajax問題,請嘗試通過以下方式進行隔離測試:
使用SimpleTest.php
(有關代碼,請參見OLD部分)並將其托管在當前teste03.php
所在的服務器上,並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問題而不是highcharts問題。
編輯 (根據您對返回的json的評論)
數據中的時間戳值需要按升序排列。 從您以下的json
[[1344290400,622.77,625,618.04,618.26,10373100],[1344204000,617.29,624.87,615.26,619.89,10789400]
1344290400> 1344204000因此無法正常工作。
舊
使用json_encode方法進行json形成。
您需要傳遞給它的是一個數組數組,其中外部數組的大小與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轉換為所需的數組。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.