2013-07-27 167 views
0

我使用oocharts api來獲取我的谷歌分析數據爲一個簡單的圖表繪製在我的內容管理系統。 api吐出json,我需要將其轉換成谷歌兼容行
["Jun 27", 3], .etc其中3是當天的查看次數。oocharts谷歌anayltics數據谷歌圖表

https://api.oocharts.com/v1/query.jsonp?query=visits-by-date&key=37e6fe024f36bbbb1661f4e68872862de98da594&start=30d

將這些信息處理成圖表友好的信息與Ajax或PHP的最佳方式是什麼?

+1

OOcharts API已更新爲您完成所有繪圖。查看[docs](http://docs.oocharts.com),並告訴我們您是否有問題。但是,如果您想使用自己的自定義代碼,我可以幫助您。請讓我知道。 –

回答

0
<?php 
// ----------------------SOP----------------------------------- 
require ('framework/framework.php'); 
page_protect(); 
// ------------------------------------------------------------ 
if (isset($_GET['ga_length'])) { 
    $default = $_GET['ga_length']; 
} else { 
    $default = '1m'; 
} 
$len = array(
    "1m" => "1 month", 
    "2m" => "2 months", 
    "3m" => "3 months", 
    "4m" => "4 months", 
    "5m" => "5 months" 
); 
// params 
$length = $default; 
$query_stub = 'visits-by-date'; 
$key = '37e6fe024f36bbbb1661f4e68872862de98da594'; 
$url_api = "https://api.oocharts.com/v1/query.jsonp?query={$query_stub}&key={$key}&start={$length}"; 
$json = file_get_contents($url_api); 
// get json 
if ($json && GA_OO_ACTIVE) { 
    $data = json_decode($json, TRUE); 
    $rows = $data['rows']; 
    foreach ($rows as $row) { 
     $info .= '["' . get_array_value_from_key($row[0], $months, true) . ' ' . $row[1] . '", ' . $row[2] . '],'; 
    } 
    $date_title = date('F d, Y', strtotime('-' . get_array_value_from_key($default, $len, true))) . ' through ' . date('F d, Y'); 
?> 
<?php head(); ?> 
<script type="text/javascript" src="https://www.google.com/jsapi"></script> 
<script type="text/javascript"> 
google.load("visualization", "1", {packages:["corechart"]}); 
google.setOnLoadCallback(drawChart); 
function drawChart() { 
    var data = new google.visualization.DataTable(); 
    data.addColumn('string', 'Day'); 
    data.addColumn('number', 'Pageviews'); 
    data.addRows([ <?php echo $info; ?> ]); 
    var chart = new google.visualization.AreaChart(document.getElementById('chart')); 
    chart.draw(data, {width: '100%', height: 180, title: 'Site views <?php echo $date_title; ?>', 
      colors:['#666666','#333333'], 
      areaOpacity: 0.1, 
      hAxis: {textPosition: 'in', showTextEvery: 5, slantedText: false, textStyle: { color: '', fontSize: 10 } }, 
      pointSize: 5, 
      legend: 'none', 
      chartArea:{left:0,top:30,width:"100%",height:"100%"} 
    }); 
} 
</script> 
<?php body(); ?> 
<div style="min-height:220px;"> 
<?php 
echo '<h2>Projects</h2>'; 
$t1 = array('projects'); 
publishCountTotal($t1); 
echo '<h2>Testimonials</h2>'; 
$t2 = array('testimonials'); 
publishCountTotal($t2); 
echo '<h2>Users</h2>'; 
echo totalUserPending(); 
?> 
</div> 
<h2>Google Analytics</h2> 
<form id="ga_length_frm" class="form-horizontal" method="get"> 
<span style="margin-right:5px;">Go back...</span> 
<select class="select-ms" name="ga_length" onchange="this.form.submit();"> 
    <?php echo arrayToSelect($len, $default); ?> 
</select> 
</form> 
<div id="chart"></div> 
<?php } else { head(); body(); echo '<div class="ec-messages messages-error">JSON file get contents error!</div>'; } ?> 
<?php footer(); ?>