According to highcharts documentation.
It is not possible to use the jQuery.getJSON()
function on JSON files outside of your own domain. It is, however, possible to use JSONP.
Cross-domain data. This is their example:
Source:
https://www.highcharts.com/docs/working-with-data/getting-data-across-domains-jsonp
The serverside php file:
<?php
header("content-type: application/json");
$array = array(7,4,2,8,4,1,9,3,2,16,7,12);
echo $_GET['callback']. '('. json_encode($array) . ')';
?>
The JavaScript calling the callback function using jQuery.
$(document).ready(function() {
var options = {
chart: {
renderTo: 'container',
type: 'spline'
},
series: [{}]
};
var url = "http://url-to-your-remote-server/jsonp.php?callback=?";
$.getJSON(url, function(data) {
options.series[0].data = data;
var chart = new Highcharts.Chart(options);
});
});
How would I do the same with a live data JSON API call, with a setTimeout request on for instance 30 seconds?
JSON LIVE DATA URL: https://canvasjs.com/services/data/datapoints.php?xstart=1&ystart=10&length=10&type=json
I have searched this answer for 2 days, I can only find this example, it seems to be a different approach using jQuery ajax()
function creating random data, not from an external URL:
https://www.highcharts.com/docs/working-with-data/live-data
Modified code with own api: http://marialaustsen.com/apii.json
<html>
<head>
<title>Team memberttttts received and sent eCards</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://code.highcharts.com/highcharts.js"></script>
<script>
function visitorData (data) {
$('#container').highcharts({
chart: {
type: 'column'
},
title: {
text: 'Average Visitors'
},
xAxis: {
type: 'datetime',
labels: {
formatter: function () {
return Highcharts.dateFormat('%b %e. %Y', this.value, 1);
}
}
},
yAxis: {
title: {
text: 'Number of visitors'
}
},
series: data,
});
}
$(document).ready(function() {
$.ajax({
url: 'http://marialaustsen.com/apii.json?callback=?',
type: 'GET',
async: true,
dataType: "json",
success: function (data) {
visitorData(data);
}
});
});
</script>
</head>
<body>
<div id="container" style="width: 75%; height: 500px; margin: 0 auto"></div>
</body>
</html>
I removed the header in the api file, and replaced curly brackets with square brackets:
[
["Timestamp":"1262304000","Sent ecards":"843","Sent recognition":"736","Received ecards":"21","Received recognition":"4311"]
]
I get this error in the console: marialaustsen.com/apii.json?callback=jQuery321017746123134921787_1514030331673&_=1514030331674:2 Uncaught SyntaxError: Unexpected token :
As I have to be able to automatically update my api request, I need to find a way to fetch or output my json api correctly for highcharts. I read somewhere highcharts only reads timestamp in milliseconds, but it seems like the syntax is incorrect and not the data format.