0

我正在使用 oocharts api 来获取我的谷歌分析数据,以便在我的内容管理系统中绘制一个简单的图表。api 吐出 json,我需要将其转换为与 google 兼容的行,当天的视图计数在
["Jun 27", 3], .etc哪里。3

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

使用 ajax 或 php 将此信息处理为图表友好信息的最佳方法是什么?

4

1 回答 1

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(); ?>
于 2013-07-27T18:25:43.720 回答