I'm new to javascript so please forgive me. I am trying to make a line chart (with 4 lines) which get updated every hour. The information will be found in a JSON file however I am unsure how to write it. Previously I have tried to use data.addColumn() and data.addRows() but I couldn't find a way to connec that with a JSON file. Say if you were to make an example, please show the number of sales of four different items
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
google.charts.load('current', {
packages: ['corechart']
}).then(function () {
var options = {
hAxis: {
title: 'Time'
},
vAxis: {
title: 'Sales'
},
title: 'Today',
height: 600
};
var chart = new google.visualization.LineChart(document.getElementById('dagens'));
var jsonData = $.ajax({
url: 'getData.php',
dataType: 'json',
})
drawChart(jsonData);
}).done(drawChart);
function drawChart(jsonData) {
var data = new google.visualization.DataTable(jsonData);
chart.draw(data, options);
}
</script>
In the getData.php file this is written:
$string = file_get_contents("sampleData.json");
echo $string;
I wish to use google's chart system if possible. Many thanks in advance.
Edit: JSON file:
{
cols: [{label: 'Time', type: 'number'},
{label: 'Sales 1', type: 'number'},
{label: 'Sales 2', type: 'number'},
{label: 'Sales 3', type: 'number'},
{label: 'Sales 4', type: 'number'}
],
rows: [
{c:[{v: 0}, {v: 1000}, {v: 1000}, {v: 1000}, {v: 1000}]},
{c:[{v: 1}, {v: 1000}, {v: 1000}, {v: 1000}, {v: 1000}]},
{c:[{v: 2}, {v: 1000}, {v: 1000}, {v: 1000}, {v: 1000}]},
{c:[{v: 3}, {v: 1000}, {v: 1000}, {v: 1000}, {v: 1000}]},
{c:[{v: 4}, {v: 1000}, {v: 1000}, {v: 1000}, {v: 1000}]},
{c:[{v: 5}, {v: 1000}, {v: 1000}, {v: 1000}, {v: 1000}]},
{c:[{v: 6}, {v: 1000}, {v: 1000}, {v: 1000}, {v: 1000}]},
{c:[{v: 7}, {v: 1000}, {v: 1000}, {v: 1000}, {v: 1000}]},
{c:[{v: 8}, {v: 1000}, {v: 1000}, {v: 1000}, {v: 1000}]},
{c:[{v: 9}, {v: 1000}, {v: 1000}, {v: 1000}, {v: 1000}]},
{c:[{v: 10}, {v: 1000}, {v: 1000}, {v: 1000}, {v: 1000}]},
{c:[{v: 11}, {v: 1000}, {v: 1000}, {v: 1000}, {v: 1000}]},
{c:[{v: 12}, {v: 1000}, {v: 1000}, {v: 1000}, {v: 1000}]},
{c:[{v: 13}, {v: 1000}, {v: 1000}, {v: 1000}, {v: 1000}]},
{c:[{v: 14}, {v: 1000}, {v: 1000}, {v: 1000}, {v: 1000}]},
{c:[{v: 15}, {v: 1000}, {v: 1000}, {v: 1000}, {v: 1000}]},
{c:[{v: 16}, {v: 1000}, {v: 1000}, {v: 1000}, {v: 1000}]},
{c:[{v: 17}, {v: 1000}, {v: 1000}, {v: 1000}, {v: 1000}]},
{c:[{v: 18}, {v: 1000}, {v: 1000}, {v: 1000}, {v: 1000}]},
{c:[{v: 19}, {v: 1000}, {v: 1000}, {v: 1000}, {v: 1000}]},
{c:[{v: 20}, {v: 1000}, {v: 1000}, {v: 1000}, {v: 1000}]},
{c:[{v: 21}, {v: 1000}, {v: 1000}, {v: 1000}, {v: 1000}]},
{c:[{v: 22}, {v: 1000}, {v: 1000}, {v: 1000}, {v: 1000}]},
{c:[{v: 23}, {v: 1000}, {v: 1000}, {v: 1000}, {v: 1000}]}
]
}