Highchart不会填充php JSON调用

时间:2013-01-21 16:16:06

标签: php json highcharts

我在使用JSON数据填充我的高图时遇到问题。当我在网页中执行脚本时,数据将填充图表需要的特定区域,但是当我尝试使用highcharts时,没有任何内容出现。

当我摆脱我的php并插入一个静态数字时,图表就会自行创建。

总而言之,当我运行我的php脚本(但正在生成数据)时,Highcharts将无法工作,当我插入静态数字并删除我的php脚本时,图表会运行。

  $response = file_get_contents('https://graph.facebook.com/'.FACEBOOK_PAGE_ID.'/insights  /page_views?access_token='.FACEBOOK_ACCESS_TOKEN);
  $page = json_decode($response);



 if ($page && $page->data) {

$test = $page->data[0]->values[2]->end_time; $newDate = date("d-m-Y", strtotime($test));  
$gender = $page->data[0]->values[2]->value; 

$test2 = $page->data[0]->values[1]->end_time;  $newDate1 = date("d-m-Y", strtotime($test2)); 
$gender1 = $page->data[0]->values[1]->value; 

$test3 = $page->data[0]->values[0]->end_time;  $newDate2 = date("d-m-Y", strtotime($test3)); 
$gender2 = $page->data[0]->values[0]->value; 


}

 header('Content-Type: application/json');
 ?>
 {
 chart: {
  renderTo: 'container',
  height: 220,
  type: 'column'
  },
  title: {
  text: ''
  },
  xAxis: {
  categories: ['apples'],
  lineWidth: 0,
  tickWidth: 0,
   },
   yAxis: {
   min: 0,
   title: {
   text: ''
   },
  stackLabels: {
   enabled: true,
   style: {
   fontWeight: 'bold',

   }
   }
   },
   legend: {
   align: 'left',
   x: 0,
   verticalAlign: 'left',
   y: 15,
   floating: true,
   backgroundColor: 'white',
   borderColor: '#CCC',
   borderWidth: 1,
   shadow: false
    },
   tooltip: {
   formatter: function () {
   return '<b>' + this.x + '</b><br/>' + this.series.name + ': ' + this.y + '<br/>' +
   'Total: ' + this.point.stackTotal;
   }
   },
   plotOptions: {
   column: {
   stacking: 'normal',
    dataLabels: {
   enabled: true,
   color: 'white'
   }
   }
   },
        series: [{
            name: '<?php echo $newDate; ?> ',
            data: [<?php echo $gender; ?>]
        }, {
            name: '<?php echo $newDate1; ?>',
            data: [<?php echo $gender1; ?>]
        }, {
            name: '<?php echo $newDate2;  ?>',
            data: [<?php echo $gender2;  ?>]
        }]

      });
     });

     });

这是我得到的输出。

{
chart: {
renderTo: 'container',
height: 220,
type: 'column'
},
 title: {
 text: ''
 },
 xAxis: {
 categories: ['apples'],
 lineWidth: 0,
 tickWidth: 0,
 },
 yAxis: {
 min: 0,
 title: {
 text: ''
 },
 stackLabels: {
 enabled: true,
 style: {
 fontWeight: 'bold',

}
}
},
legend: {
align: 'left',
x: 0,
verticalAlign: 'left',
y: 15,
floating: true,
backgroundColor: 'white',
borderColor: '#CCC',
borderWidth: 1,
shadow: false
},
tooltip: {
formatter: function () {
return '<b>' + this.x + '</b><br/>' + this.series.name + ': ' + this.y + '<br/>' +
'Total: ' + this.point.stackTotal;
}
},
plotOptions: {
column: {
stacking: 'normal',
dataLabels: {
enabled: true,
color: 'white'
}
}
},
        series: [{
            name: '19-01-2013 ',
            data: [57]
        }, {
            name: '18-01-2013',
            data: [21]
        }, {
            name: '17-01-2013',
            data: [34]
        }]

        });
    });

});

1 个答案:

答案 0 :(得分:0)

所以我开始工作了。我基本上一步一步地添加代码来查看它的中断位置......它的工作原理。不知道会发生什么,但这里是代码:

<?php 

require_once 'config.php';

$response = file_get_contents('https://graph.facebook.com/'.FACEBOOK_PAGE_ID.'/insights    /page_views?access_token='.FACEBOOK_ACCESS_TOKEN);
$page = json_decode($response);

if ($page && $page->data) {

$test = $page->data[0]->values[2]->end_time; $newDate = date("d-m-Y", strtotime($test));  $newDate;
$gender = $page->data[0]->values[2]->value;  $gender;

$test2 = $page->data[0]->values[1]->end_time;  $newDate1 = date("d-m-Y", strtotime($test2)); $newDate1;
$gender1 = $page->data[0]->values[1]->value;  $gender1;

$test3 = $page->data[0]->values[0]->end_time;  $newDate2 = date("d-m-Y", strtotime($test3));  $newDate2;
$gender2 = $page->data[0]->values[0]->value;  $gender2;


}
header('Content-Type: application/json');

?>
 {
chart: {
renderTo: 'container',
height: 220,
type: 'column'
},
title: {
text: ''
},
xAxis: {
categories: ['Page Views'],
lineWidth: 0,
tickWidth: 0,
},
yAxis: {
min: 0,
title: {
text: ''
},
stackLabels: {
enabled: true,
style: {
fontWeight: 'bold',

}
}
},
legend: {
align: 'left',
x: 0,
verticalAlign: 'left',
y: 0,
floating: true,
backgroundColor: 'black',
borderColor: '#CCC',
borderWidth: 1,
shadow: false
},
tooltip: {
formatter: function () {
return '<b>' + this.x + '</b><br/>' + this.series.name + ': ' + this.y + '<br/>' +
'Total: ' + this.point.stackTotal;
}
},
plotOptions: {
column: {
stacking: 'normal',
dataLabels: {
enabled: true,
color: 'white'
}
}
},
series: [{
name: '<?php echo $newDate; ?> ',
data: [<?php echo  $gender; ?>]
}, {
name: '<?php echo $newDate1; ?>',
data: [<?php echo $gender1; ?>]
}, {
name: '<?php echo $newDate2;  ?>',
data: [<?php echo  $gender2;  ?>]
}]
}