I'm using this Google Charts tutorial on how to convert an array into a graph.
The data in the tutorial is already in the function. Now I want to insert my own data into the function from a PHP array. I managed to convert my PHP array into a JavaScript array. Below is my code:
$jsArray = array();
foreach($movingAverages as $movingAverage) {
$jsArray[] = array((int) $movingAverage['unix'], (int) $movingAverage['closing-prices']);
}
Below is my sample code:
Array
(
[0] => Array
(
[0] => 1505040240
[1] => 3452
)
[1] => Array
(
[0] => 1505040300
[1] => 3451
)
[2] => Array
(
[0] => 1505040360
[1] => 3446
)
[3] => Array
(
[0] => 1505040420
[1] => 3449
)
This is my current Javascript Code:
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript">
var js_array = <?php echo json_encode($jsArray);?>;
alert(js_array);
google.charts.load('current', {'packages':['corechart']});
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable([
['Time', 'Closing Prices'],
[js_array]
]);
var options = {
title: 'BTC-EUR',
curveType: 'function',
legend: { position: 'top' }
};
var chart = new google.visualization.LineChart(document.getElementById('curve_chart'));
chart.draw(data, options);
}
</script>
</head>
<body>
<div id="curve_chart" style="width: 100%; height: 500px"></div>
</body>
</html>
When I call alert(js_array); The array is called.
I don't know why the chart is not being displayed. I'm not sure how to insert the js_array into my function drawChart().
This is the first time I'm using JavaScript. So thank you for your help.
console.log(js_array)?