public function Reports::sources_chart in Yandex.Metrics 8.2
The function generates pie chart with traffic sources summary.
Parameters
string $this->filter:
File
- yandex_metrics_reports/
src/ Reports.php, line 29 - Contains \Drupal\yandex_metrics_reports\YandexMetricsReports.
Class
- Reports
- Reports manager for YandexMetrics module.
Namespace
Drupal\yandex_metrics_reportsCode
public function sources_chart() {
$date_range = _yandex_metrics_reports_filter_to_date_range($this->filter);
$parameters = array(
'id' => $this->counter_id,
'date1' => $date_range['start_date'],
'date2' => $date_range['end_date'],
);
$results = yandex_metrics_reports_retreive_data('/stat/sources/summary', $parameters);
$summary = json_decode($results
->getBody(TRUE));
if (empty($summary->data)) {
return t('There is no information about traffic sources for the selected date range.');
}
$sum = $summary->totals->visits;
$i = 1;
foreach ($summary->data as $value) {
$name = String::checkPlain($value->name);
$data[] = array(
'legends' => $i . '. ' . $name . ' (' . round($value->visits * 100 / $sum) . '%' . ')',
'visits' => $value->visits,
);
$i++;
}
$chart = array(
'#theme' => 'visualization',
'#options' => array(
'title' => t('Traffic Sources'),
'width' => 500,
'height' => 200,
'fields' => array(
'legends' => array(
'label' => 'legends',
'enabled' => TRUE,
),
'visits' => array(
'label' => 'visits',
'enabled' => TRUE,
),
),
'xAxis' => array(
'labelField' => 'legends',
),
'data' => $data,
'type' => 'pie',
),
);
return drupal_render($chart);
}