Commit c39f00f80d05a07eaf5358da87b7bc1b03d7fa8c

Authored by Alexey Boroda
1 parent e01541b2

-Am charts ready

backend/models/Analytics.php
@@ -2,13 +2,21 @@ @@ -2,13 +2,21 @@
2 namespace backend\models; 2 namespace backend\models;
3 3
4 use yii\base\Model; 4 use yii\base\Model;
5 - use yii\helpers\ArrayHelper;  
6 - use yii\helpers\VarDumper;  
7 5
  6 + /**
  7 + * Class Analytics
  8 + *
  9 + * @package backend\models
  10 + */
8 class Analytics extends Model 11 class Analytics extends Model
9 { 12 {
10 public $viewId; 13 public $viewId;
11 - 14 +
  15 + /**
  16 + * Preparing and querying data
  17 + *
  18 + * @return \Google_Service_AnalyticsReporting_GetReportsResponse
  19 + */
12 private function executeQuery() 20 private function executeQuery()
13 { 21 {
14 $client = new \Google_Client(); 22 $client = new \Google_Client();
@@ -104,7 +112,12 @@ @@ -104,7 +112,12 @@
104 112
105 return $response; 113 return $response;
106 } 114 }
107 - 115 +
  116 + /**
  117 + * Generates data for view
  118 + *
  119 + * @return array
  120 + */
108 public function generateData() 121 public function generateData()
109 { 122 {
110 $reports = $this->executeQuery(); 123 $reports = $this->executeQuery();
@@ -134,7 +147,7 @@ @@ -134,7 +147,7 @@
134 147
135 $dimensions = $row->getDimensions(); 148 $dimensions = $row->getDimensions();
136 for ($i = 0; $i < count($dimensionHeaders) && $i < count($dimensions); $i++) { 149 for ($i = 0; $i < count($dimensionHeaders) && $i < count($dimensions); $i++) {
137 - $data[ 'plot' ][ $rowIndex ][ 'day' ] = $dimensions[ $i ]; 150 + $data[ 'plot' ][ $rowIndex ][ 'day' ] = date('d-m-Y', strtotime($dimensions[ $i ]));
138 } 151 }
139 152
140 $metrics = $row->getMetrics(); 153 $metrics = $row->getMetrics();
@@ -155,7 +168,8 @@ @@ -155,7 +168,8 @@
155 } 168 }
156 169
157 /** 170 /**
158 - * Generating data for 171 + * Generating data for tabs,
  172 + * with browser, city and country statistics
159 */ 173 */
160 $data2 = []; 174 $data2 = [];
161 175
backend/views/site/analytics.php
@@ -8,7 +8,7 @@ @@ -8,7 +8,7 @@
8 */ 8 */
9 9
10 use artbox\gentelella\widgets\XPanel; 10 use artbox\gentelella\widgets\XPanel;
11 - use dosamigos\highcharts\HighCharts; 11 + use speixoto\amcharts\Widget;
12 use yii\bootstrap\Tabs; 12 use yii\bootstrap\Tabs;
13 use yii\web\View; 13 use yii\web\View;
14 14
@@ -71,6 +71,9 @@ @@ -71,6 +71,9 @@
71 'type' => 'serial', 71 'type' => 'serial',
72 'dataProvider' => $data[ 'plot' ], 72 'dataProvider' => $data[ 'plot' ],
73 'categoryField' => 'day', 73 'categoryField' => 'day',
  74 + 'categoryAxis' => [
  75 + 'labelFrequency' => 5,
  76 + ],
74 'graphs' => [ 77 'graphs' => [
75 [ 78 [
76 "balloon" => [ 79 "balloon" => [
@@ -87,8 +90,8 @@ @@ -87,8 +90,8 @@
87 "title" => "red line", 90 "title" => "red line",
88 "useLineColorForBulletBorder" => true, 91 "useLineColorForBulletBorder" => true,
89 "valueField" => "users", 92 "valueField" => "users",
90 - "balloonText" => "<span style='font-size:18px;'>[[users]]</span>",  
91 - "lineColors" => '#9ABCC3', 93 + "balloonText" => "<span style='font-size:18px;'>Users: [[users]]</span>",
  94 + "lineColor" => '#9ABCC3',
92 ], 95 ],
93 [ 96 [
94 "balloon" => [ 97 "balloon" => [
@@ -105,13 +108,23 @@ @@ -105,13 +108,23 @@
105 "title" => "red line", 108 "title" => "red line",
106 "useLineColorForBulletBorder" => true, 109 "useLineColorForBulletBorder" => true,
107 "valueField" => "sessions", 110 "valueField" => "sessions",
108 - "balloonText" => "<span style='font-size:18px;'>[[sessions]]</span>",  
109 - "lineColors" => '#A8E3D6', 111 + "balloonText" => "<span style='font-size:18px;'>Sessions: [[sessions]]</span>",
  112 + "lineColor" => '#A8E3D6',
110 ], 113 ],
111 ], 114 ],
  115 + 'chartCursor' => [
  116 + "pan" => true,
  117 + "valueLineEnabled" => true,
  118 + "valueLineBalloonEnabled" => true,
  119 + "cursorAlpha" => 1,
  120 + "cursorColor" => "#1ABB9C",
  121 + "limitToGraph" => "g1",
  122 + "valueLineAlpha" => 0.2,
  123 + "valueZoomable" => true,
  124 + ],
112 ]; 125 ];
113 126
114 - echo speixoto\amcharts\Widget::widget( 127 + echo Widget::widget(
115 [ 128 [
116 'chartConfiguration' => $chartConfiguration, 129 'chartConfiguration' => $chartConfiguration,
117 'width' => '100%', 130 'width' => '100%',
@@ -134,50 +147,30 @@ @@ -134,50 +147,30 @@
134 ); ?> 147 ); ?>
135 148
136 <?php 149 <?php
137 - echo HighCharts::widget(  
138 - [  
139 - 'clientOptions' => [  
140 - 'exporting' => [  
141 - 'enabled' => false,  
142 - ],  
143 - 'colors' => [  
144 - '#9ABCC3',  
145 - '#A8E3D6',  
146 - ],  
147 - 'chart' => [  
148 - 'plotBackgroundColor' => null,  
149 - 'plotBorderWidth' => null,  
150 - 'plotShadow' => false,  
151 - 'type' => 'pie',  
152 - ],  
153 - 'title' => [  
154 - 'text' => 'Analytics',  
155 - ],  
156 - 'series' => [  
157 - [  
158 - 'name' => 'Sessions',  
159 - 'data' => [  
160 - [  
161 - 'name' => 'New visitor',  
162 - 'y' => round(intval($data[ 'new' ]), 2),  
163 - ],  
164 - [  
165 - 'name' => 'Returning Visitor',  
166 - 'y' => 100 - round(intval($data[ 'new' ]), 2),  
167 - ],  
168 - ],  
169 - ],  
170 - ],  
171 - 'credits' => [  
172 - 'enabled' => false,  
173 - ],  
174 - 'plotOptions' => [  
175 - 'pie' => [  
176 - 'allowPointSelect' => true,  
177 - 'cursor' => 'pointer',  
178 - ],  
179 - ], 150 + $chartConfiguration = [
  151 + "type" => "pie",
  152 + "titleField" => "category",
  153 + "valueField" => "column-1",
  154 + "dataProvider" => [
  155 + [
  156 + "category" => "New Visitor",
  157 + "column-1" => round(intval($data[ 'new' ]), 2),
180 ], 158 ],
  159 + [
  160 + "category" => "Returning Visitor",
  161 + "column-1" => 100 - round(intval($data[ 'new' ]), 2),
  162 + ],
  163 + ],
  164 + "colors" => [
  165 + "#A8E3D6",
  166 + "#9ABCC3",
  167 + ],
  168 + ];
  169 +
  170 + echo Widget::widget(
  171 + [
  172 + 'chartConfiguration' => $chartConfiguration,
  173 + 'width' => '100%',
181 ] 174 ]
182 ); 175 );
183 ?> 176 ?>
@@ -239,4 +232,5 @@ @@ -239,4 +232,5 @@
239 232
240 </div> 233 </div>
241 234
242 -</div>  
243 \ No newline at end of file 235 \ No newline at end of file
  236 +</div>
  237 +
backend/views/site/stab.php 0 → 100644
  1 +<?php
  2 + use speixoto\amcharts\AmChartAsset;
  3 + use yii\web\View;
  4 +
  5 + /**
  6 + * @var View $this
  7 + */
  8 +
  9 + AmChartAsset::register($this);
  10 +
  11 + $js = <<<JS
  12 + $.ajax({
  13 + url: "site/analytics",
  14 + type: "GET",
  15 + success: function(data) {
  16 + console.log(data);
  17 + $('#stab').replaceWith(data);
  18 + }
  19 + });
  20 +JS;
  21 +
  22 + $this->registerJs($js, View::POS_READY);
  23 +?>
  24 +
  25 +<div id="stab"></div>
  26 +