You are here

function yandex_metrics_reports_sources_chart in Yandex.Metrics 8.2

Same name and namespace in other branches
  1. 8.3 yandex_metrics_reports/yandex_metrics_reports.reports.inc \yandex_metrics_reports_sources_chart()
  2. 7.3 yandex_metrics_reports/yandex_metrics_reports.reports.inc \yandex_metrics_reports_sources_chart()
  3. 7.2 yandex_metrics_reports/yandex_metrics_reports.reports.inc \yandex_metrics_reports_sources_chart()

The function generates pie chart with traffic sources summary.

Parameters

string $counter_id:

string $filter:

1 string reference to 'yandex_metrics_reports_sources_chart'
yandex_metrics_reports_yandex_metrics_reports_list in yandex_metrics_reports/yandex_metrics_reports.module
Implements hook_yandex_metrics_reports_list().

File

yandex_metrics_reports/yandex_metrics_reports.reports.inc, line 54
Report callbacks for Yandex.Metrics Reports module.

Code

function yandex_metrics_reports_sources_chart($counter_id, $filter) {
  $output = '';
  $date_range = _yandex_metrics_reports_filter_to_date_range($filter);
  $parameters = array(
    'id' => $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.');
  }
  $chart = array(
    '#chart_id' => 'chart_sources',
    '#title' => chart_title(t('Traffic Sources'), '000000', 15),
    '#type' => CHART_TYPE_PIE,
    '#size' => chart_size(500, 200),
    '#adjust_resolution' => TRUE,
  );
  $sum = $summary->totals->visits;
  $i = 1;
  foreach ($summary->data as $value) {
    $name = check_plain($value->name);
    $chart['#data'][] = (int) $value->visits;
    $chart['#labels'][] = $i;
    $chart['#legends'][] = $i . '. ' . $name . ' (' . round($value->visits * 100 / $sum) . '%' . ')';
    $chart['#data_colors'][] = chart_unique_color('sources_' . $i, 'yandex_metrics_reports');
    $i++;
  }
  return theme('chart', array(
    'chart' => $chart,
  ));
}