2013-07-27 127 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(); ?>