You are here

function yandex_metrics_reports_visits_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_visits_chart()
  2. 7.3 yandex_metrics_reports/yandex_metrics_reports.reports.inc \yandex_metrics_reports_visits_chart()
  3. 7.2 yandex_metrics_reports/yandex_metrics_reports.reports.inc \yandex_metrics_reports_visits_chart()

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

Parameters

string $counter_id:

string $filter:

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

Code

function yandex_metrics_reports_visits_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'],
  );
  if (isset($date_range['group'])) {
    $parameters['group'] = $date_range['group'];
  }
  $results = yandex_metrics_reports_retreive_data('/stat/traffic/summary', $parameters);
  $visits = json_decode($results
    ->getBody(TRUE));
  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();

  // If there is only one date point, we should duplicate for
  // building horizontal line.
  if (count($visits->data) == 1) {
    $z_point = $visits->data[0];
    $page_views[] = (int) $z_point->page_views;
    $visitors[] = (int) $z_point->visitors;
    $new_visitors[] = (int) $z_point->new_visitors;

    // Set date label to the middle.
    $position = 50;
  }
  else {
    $position = NULL;
  }
  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);
  $width = 500;
  $height = 250;
  $chart = array(
    '#chart_id' => 'chart_visits',
    '#title' => chart_title(t('Page Views, Visitors, New Visitors'), '000000', 15),
    '#type' => CHART_TYPE_LINE,
    '#size' => chart_size($width, $height),
    '#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;
    }
  }

  // Calculate max value for grid.
  $max_grid = _yandex_metrics_reports_calculate_max_grid(0, $max);
  $chart['#fluid_grid_adjust'] = array(
    '#max' => $max_grid,
  );
  $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');

  // Calculate grid step for X.
  $step_x_percent = _yandex_metrics_reports_calculate_grid_step_fixed(count($dates));

  // Calculate grid step for Y.
  list($step_y, $step_y_percent) = _yandex_metrics_reports_calculate_grid_step_fluid($max_grid, $height);

  // Count Y labels.
  $axis_y_count_labels = round($max_grid / $step_y, 0, PHP_ROUND_HALF_DOWN);

  // Draw Y labels.
  for ($i = 1; $i <= $axis_y_count_labels; $i++) {
    $chart['#mixed_axis_labels'][CHART_AXIS_Y_LEFT][0][] = chart_mixed_axis_label($i * $step_y, $i * $step_y_percent);
  }

  // Add grids to chart.
  $chart['#grid_lines'] = chart_grid_lines($step_x_percent, $step_y_percent);

  // Draw X labels.
  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), $position);
  }
  return theme('chart', array(
    'chart' => $chart,
  ));
}