You are here

function _yandex_metrics_geo_chart in Yandex.Metrics 6

Same name and namespace in other branches
  1. 7 yandex_metrics.module \_yandex_metrics_geo_chart()

The function generates pie chart with geographical information on visitors.

Parameters

string $counter_id:

string $filter:

2 calls to _yandex_metrics_geo_chart()
yandex_metrics_ajax in ./yandex_metrics.module
Menu callback; outputs content of one of the 4 reports. It is intended for AJAX calls.
yandex_metrics_report in ./yandex_metrics.module
Menu callback; displays a Summary page containing reports and charts.

File

./yandex_metrics.module, line 1132
The main code of Yandex.Metrics module.

Code

function _yandex_metrics_geo_chart($counter_id, $filter) {
  $output = '';
  $date_range = _yandex_metrics_filter_to_date_range($filter);
  $parameters = array(
    'id' => $counter_id,
    'date1' => $date_range['start_date'],
    'date2' => $date_range['end_date'],
  );
  $results = yandex_metrics_retreive_data('/stat/geo', $parameters);
  $geo = json_decode($results->data);
  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');
      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');
    $i++;
  }
  return chart_render($chart);
}