You are here

function Reports::hourly_chart in Yandex.Metrics 8.2

The function generates chart with information about hourly traffic.

File

yandex_metrics_reports/src/Reports.php, line 234
Contains \Drupal\yandex_metrics_reports\YandexMetricsReports.

Class

Reports
Reports manager for YandexMetrics module.

Namespace

Drupal\yandex_metrics_reports

Code

function hourly_chart() {
  $date_range = _yandex_metrics_reports_filter_to_date_range($this->filter);
  $parameters = array(
    'id' => $this->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.');
  }
  foreach ($hourly_report->data as $hour_data) {
    $data[] = array(
      'hours' => $hour_data->hours,
      'avg_visits' => $hour_data->avg_visits,
      // Convert denials from percents.
      'denials' => $hour_data->avg_visits * $hour_data->denial,
    );
  }
  $chart = array(
    '#theme' => 'visualization',
    '#options' => array(
      'title' => t('Hourly Traffic'),
      'width' => 500,
      'height' => 350,
      'fields' => array(
        'hours' => array(
          'label' => 'hours',
          'enabled' => FALSE,
        ),
        'avg_visits' => array(
          'label' => 'avg_visits',
          'enabled' => TRUE,
        ),
        'denials' => array(
          'label' => 'denials',
          'enabled' => TRUE,
        ),
      ),
      'xAxis' => array(
        'labelField' => 'hours',
      ),
      'data' => $data,
      'type' => 'column',
    ),
  );
  return drupal_render($chart);
}