fractal5 fractal5 - 10 months ago 92
Javascript Question

Highstock chart - JSON input not working

I am trying to create a chart with Highstock from Highcharts, but can't figure out how to supply the correct JSON data from the PHP file.

This is my HTML file. The original URL used for fetching the data in

is' + name.toLowerCase() + '-c.json&callback=?

Three different calls are being made. For example, one is:


<script src=""></script>
<script src=""></script>
<script src=""></script>


<div id="container" style="height: 400px; min-width: 310px"></div>

$(function () {
var seriesOptions = [],
seriesCounter = 0,
names = ['MSFT', 'AAPL', 'GOOG'];

* Create the chart when all data is loaded
* @returns {undefined}
function createChart() {

Highcharts.stockChart('container', {

rangeSelector: {
selected: 4

yAxis: {
labels: {
formatter: function () {
return (this.value > 0 ? ' + ' : '') + this.value + '%';
plotLines: [{
value: 0,
width: 2,
color: 'silver'

plotOptions: {
series: {
compare: 'percent',
showInNavigator: true

tooltip: {
pointFormat: '<span style="color:{series.color}">{}</span>: <b>{point.y}</b> ({point.change}%)<br/>',
valueDecimals: 2,
split: true

series: seriesOptions

$.each(names, function (i, name) {

console.log('name: '+name);

$.getJSON('http://localhost/projects/AGF/testobject.php', function (data) {


seriesOptions[i] = {
name: name,
data: data

// As we're loading the data asynchronously, we don't know what order it will arrive. So
// we keep a counter and create the chart when all the data is loaded.
seriesCounter += 1;

if (seriesCounter === names.length) {


I simply copied the content from that the PHP file is returning and echoed it from my own PHP file


echo "?([

I shortened the JSON to 2 objects and removed the comments. The first
is required by the Highstock, which is added as a callback parameter in the original URL. The first number in each object is the integer value of the date.

Eventually I will be the querying the data from a database and outputting it in this format.

My question is why isn't this working, if the responses are essentially the same?


Answer Source

That question mark is not required by highstock neither are the () so your JSON is invalid. That markup is for JSONP because the highchart is requesting data from a different domain.