2013-01-21 86 views
0

我遇到了使用JSON數據填充我的高圖的問題。當我在網頁中執行腳本時,數據將填充圖表所需的特定區域,但是當我嘗試使用highcharts時,什麼都沒有出現。Highchart不會填充php JSON調用

當我擺脫我的PHP和插入一個靜態數字圖表將創建自己。

總結起來,當我運行我的php腳本(但正在生成數據)並且插入靜態數字並刪除我的php腳本時,Highcharts將不起作用。

$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] 
     }] 

     }); 
    }); 

}); 
+0

是否''file_get_contents''正常工作?看起來你在Facebook的URL中有一個空格 – ub3rst4r

+0

是的,我可以解析json數據並實時輸出它,只有當我應用highchart js時,一切都變得空白。但是,如果我擺脫了PHP,只是插入數據插槽中的數字,js的作品。我還有其他小工具,但這似乎給我帶來麻煩。感謝您花時間看這個。 – user1065905

+0

你得到任何javascript錯誤? $性別,$ gender1和$ gender2等於什麼? – Mark

回答

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; ?>] 
}] 
}