You are here

function _yandex_metrics_visits_chart in Yandex.Metrics 6

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

The function generates bar chart with information about page views, visitors and new visitors.

Parameters

string $counter_id:

string $filter:

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

Code

function _yandex_metrics_visits_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'],
  );
  if (isset($date_range['group'])) {
    $parameters['group'] = $date_range['group'];
  }
  $results = yandex_metrics_retreive_data('/stat/traffic/summary', $parameters);
  $visits = json_decode($results->data);
  if (empty($visits->data)) {
    return t('There is no information about page views and visitors for the selected date range.');
  }
  $dates = array();
  $page_views = array();
  $visitors = array();
  $new_visitors = array();
  foreach ($visits->data as $value) {
    $dates[] = check_plain($value->date);
    $page_views[] = (int) $value->page_views;
    $visitors[] = (int) $value->visitors;
    $new_visitors[] = (int) $value->new_visitors;
  }
  $dates = array_reverse($dates);
  $page_views = array_reverse($page_views);
  $visitors = array_reverse($visitors);
  $new_visitors = array_reverse($new_visitors);
  $chart = array(
    '#chart_id' => 'chart_visits',
    '#title' => chart_title(t('Page Views, Visitors, New Visitors'), '000000', 15),
    '#type' => CHART_TYPE_BAR_V_GROUPED,
    '#size' => chart_size(500, 250),
    '#bar_size' => chart_bar_size(17, 3),
    '#adjust_resolution' => TRUE,
    '#legend_position' => CHART_LEGEND_BOTTOM,
  );
  $chart['#data']['views'] = $page_views;
  $chart['#data']['visitors'] = $visitors;
  $chart['#data']['new_visitors'] = $new_visitors;
  $max = 0;
  foreach ($chart['#data'] as $data) {
    $max_new = max($data);
    if ($max_new > $max) {
      $max = $max_new;
    }
  }
  $chart['#data_colors']['views'] = '0000FF';

  // Blue.
  $chart['#data_colors']['visitors'] = '008000';

  // Green.
  $chart['#data_colors']['new_visitors'] = 'FF0000';

  // Red.
  $chart['#legends']['views'] = t('Page Views');
  $chart['#legends']['visitors'] = t('Visitors');
  $chart['#legends']['new_visitors'] = t('New Visitors');
  $chart['#mixed_axis_labels'][CHART_AXIS_Y_LEFT][0][] = chart_mixed_axis_range_label(0, $max);
  foreach ($dates as $date) {
    $timestamp = strtotime($date);
    $chart['#mixed_axis_labels'][CHART_AXIS_X_BOTTOM][1][] = chart_mixed_axis_label(date('d.m.y', $timestamp));
  }
  return chart_render($chart);
}