You are here

function yandex_metrics_reports_traffic_hourly_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_traffic_hourly_chart()
  2. 7.3 yandex_metrics_reports/yandex_metrics_reports.reports.inc \yandex_metrics_reports_traffic_hourly_chart()
  3. 7.2 yandex_metrics_reports/yandex_metrics_reports.reports.inc \yandex_metrics_reports_traffic_hourly_chart()

The function generates chart with information about hourly traffic.

Parameters

string $counter_id:

string $filter:

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

Code

function yandex_metrics_reports_traffic_hourly_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/hourly', $parameters);
  $hourly_report = json_decode($results
    ->getBody(TRUE));
  if (empty($hourly_report->data)) {
    return t('There is no information about hourly traffic for the selected date range.');
  }
  $hours = array();
  $avg_visits = array();
  $denials = array();
  foreach ($hourly_report->data as $hour_data) {
    $hours[] = $hour_data->hours;
    $avg_visits[] = $hour_data->avg_visits;

    // Convert denials from percents.
    $denials[] = $hour_data->avg_visits * $hour_data->denial;
  }

  // Look up max value only in avg_visits because denials are always less.
  $max = max($avg_visits);
  $max_grid = _yandex_metrics_reports_calculate_max_grid(0, $max);
  $width = 500;
  $height = 350;
  $chart = array(
    '#chart_id' => 'chart_hourly',
    '#title' => chart_title(t('Hourly Traffic'), '000000', 15),
    '#type' => CHART_TYPE_BAR_V_GROUPED,
    '#size' => chart_size($width, $height),
    '#fluid_grid_adjust' => array(
      '#max' => $max_grid,
    ),
    '#legend_position' => CHART_LEGEND_BOTTOM,
    '#bar_size' => array(
      '#size' => 5,
      '#spacing' => 0,
    ),
  );
  $chart['#data']['avg_visits'] = $avg_visits;
  $chart['#data']['denials'] = $denials;
  $chart['#data_colors']['avg_visits'] = '4671D5';

  // Blue.
  $chart['#data_colors']['denials'] = 'FF4E40';

  // Red.
  $chart['#legends']['avg_visits'] = t('Visits per hour');
  $chart['#legends']['denials'] = t('Denials (visits with only one page view)');

  // Calculate grid step for X.
  $step_x_percent = _yandex_metrics_reports_calculate_grid_step_fixed(count($hours) + 1);

  // 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 ($hours as $hour) {
    list($label, ) = explode(':', $hour);
    $chart['#mixed_axis_labels'][CHART_AXIS_X_BOTTOM][1][] = chart_mixed_axis_label($label);
  }
  return theme('chart', array(
    'chart' => $chart,
  ));
}