0

I would like to make chart in javascript with my data but I don't know how. Here is chart which I would like to use: https://www.amcharts.com/demos/professional-candlesticks/ :

and inside code I found something like this:

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

}

etc. I load data to php array:

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

VALUES in CSV:

DATE,OPEN,HIGH,LOW,CLOSE

then I use javascript code:var jArray= <?php echo json_encode($csv); ?>; to rewrit php array to javascript array AND here is problem: How to change

date": "2011-08-01",

"open": "136.65",

"high": "136.96",

"low": "134.15",

"close": "136.49"

with array data. PLEASE HELP

1 Answer 1

0

I've rewritten the solution as the data in an multidimensional array this will work:

//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; 
Sign up to request clarification or add additional context in comments.

6 Comments

Hi, thanks for reply. I put your code and still have problem. Loaded file show "trying to get property of non-object in..... line 12
put your code and still have problem. Loaded file show "trying to get property of non-object in..... line 12 My php code now is <?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->high\", low: \"$row->low\", close: \"$row->close\"},"; } My JS code is : "dataProvider": [<?=$chartdata;?>] And I still looking solution because is doesn't work.
I've changed the answer so it's based on your CSV and not database date as it was before.
thanks. It's working. Next step is to add chart with lines to candelstick chart. Also with amcharts.
Sorry for delay but I was looing button accept, haha.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.