You are here

function _yandex_metrics_sources_chart in Yandex.Metrics 6

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

The function generates pie chart with traffic sources summary.

Parameters

string $counter_id:

string $filter:

2 calls to _yandex_metrics_sources_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 405
The main code of Yandex.Metrics module.

Code

function _yandex_metrics_sources_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/sources/summary', $parameters);
  $summary = json_decode($results->data);
  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');
    $i++;
  }
  return chart_render($chart);
}