You are here

function yandex_metrics_reports_geo_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_geo_chart()
  2. 7.3 yandex_metrics_reports/yandex_metrics_reports.reports.inc \yandex_metrics_reports_geo_chart()
  3. 7.2 yandex_metrics_reports/yandex_metrics_reports.reports.inc \yandex_metrics_reports_geo_chart()

The function generates pie chart with geographical information on visitors.

Parameters

string $counter_id:

string $filter:

1 string reference to 'yandex_metrics_reports_geo_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 256
Report callbacks for Yandex.Metrics Reports module.

Code

function yandex_metrics_reports_geo_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/geo', $parameters);
  $geo = json_decode($results
    ->getBody(TRUE));
  if (empty($geo->data)) {
    return t('There is no information about geography of visits for the selected date range.');
  }
  $chart = array(
    '#chart_id' => 'chart_geo',
    '#title' => chart_title(t('Geography of Visits'), '000000', 15),
    '#type' => CHART_TYPE_PIE,
    '#size' => chart_size(500, 230),
    '#adjust_resolution' => TRUE,
  );
  $total_visits = $geo->totals->visits;

  // Exclude unknown visits.
  foreach ($geo->data as $key => $value) {
    if ($value->name == "Не определено") {
      $total_visits -= $value->visits;
      unset($geo->data[$key]);
      break;
    }
  }
  $i = 1;
  $sum_visits = 0;
  foreach ($geo->data as $value) {
    $visits = (int) $value->visits;
    if ($i > 10) {
      $others_visits = $total_visits - $sum_visits;
      $chart['#data'][] = $others_visits;
      $chart['#labels'][] = t('Others') . ' (' . round($others_visits * 100 / $total_visits, 1) . '%' . ')';
      $chart['#data_colors'][] = chart_unique_color('geo_' . $i, 'yandex_metrics_reports');
      break;
    }
    $sum_visits += $visits;
    $name = check_plain($value->name);
    $chart['#data'][] = $visits;
    $chart['#labels'][] = $name . ' (' . round($visits * 100 / $total_visits, 1) . '%' . ')';
    $chart['#data_colors'][] = chart_unique_color('geo_' . $i, 'yandex_metrics_reports');
    $i++;
  }
  return theme('chart', array(
    'chart' => $chart,
  ));
}