2016-08-12 27 views
0

我想使用我的數據在JavaScript中製作圖表,但我不知道如何。 這裏是圖表,我想用:https://www.amcharts.com/demos/professional-candlesticks/圖表數據javascript

和內部代碼我發現這樣的事情:

{"dataProvider": [ { 
"date": "2011-08-01", 
"open": "136.65", 
"high": "136.96", 
"low": "134.15", 
"close": "136.49" 

}

等 予加載數據到PHP數組:

<?php $csv = array_map('str_getcsv', file('../projekt/lataset_2009.csv'));?> 

CSV值:

DATE,開盤價,最高價,最低價,收盤價

然後我使用JavaScript代碼:var jArray= <?php echo json_encode($csv); ?>; 到rewrit PHP數組的JavaScript陣列 這裏是問題: 如何改變

日期 「:」 2011-08- 01" ,

「開放」: 「136.65」,

「高」: 「136.96」,

「低」: 「134.15」,

「close」:「136.49」

與陣列數據。 請幫助

回答

0

我已經重寫在多維數組的解決方案中的數據這將工作:

//get the csv data 
$records = array_map('str_getcsv', file('../projekt/lataset_2009.csv')); 

//create empty array 
$data = array(); 

//loop through the csv data 
foreach($records as $row){ 

    //ignore the first array line so only the lines containing the data is used 
    if($row[0] != 'date'){ 

     //create an array into the parent $data array 
     $data[] = array(
      'date' => $row[0], 
      'open' => $row[1], 
      'high' => $row[2], 
      'low' => $row[3], 
      'close' => $row[4], 
     ); 
    } 
} 

//turn the array to json 
$chartData = json_encode($data); 

//alter the date so it's in a valid date format 
$chartData = str_replace('\/', '/', $chartData); 

//output the data 
echo $chartData; 
+0

您好,感謝的答覆。我把你的代碼,仍然有問題。加載文件顯示「試圖獲取非對象的屬性在.....行12 – Ruben

+0

把你的代碼,並仍然有問題。加載文件顯示」試圖獲得非對象的屬性在.....行12 我的php代碼現在是 '<?php $ records = array_map('str_getcsv',file(''../ projekt/lataset_2009.csv'')); $ chartdata = null; foreach($ records as $ row){ $ chartdata。=「{date:\」$ row-> date \「,open:\」$ row-> open \「,high:\」$ row- >高\「,低:\」$ row-> low \「,關閉:\」$ row-> close \「},」;我的JS代碼是: ''dataProvider「:[<?= $ chartdata;?>]' 我仍然在尋找解決方案,因爲這是行不通的。 – Ruben

+0

我已經更改了答案,因此它基於您的CSV而非以前的數據庫日期。 – Dave