You are here

function _yandex_metrics_reports_calculate_grid_step_fluid in Yandex.Metrics 8.2

Same name and namespace in other branches
  1. 7.2 yandex_metrics_reports/yandex_metrics_reports.reports.inc \_yandex_metrics_reports_calculate_grid_step_fluid()

Helper function to calculate dynamic integer grids.

Parameters

int $max_grid Max grid value.:

int $axis_size Size of axis in px.:

int $min_step_px Min space between two grid lines in px.:

Return value

array array(0 => $step, 1 => $step_percent)

2 calls to _yandex_metrics_reports_calculate_grid_step_fluid()
yandex_metrics_reports_traffic_hourly_chart in yandex_metrics_reports/yandex_metrics_reports.reports.inc
The function generates chart with information about hourly traffic.
yandex_metrics_reports_visits_chart in yandex_metrics_reports/yandex_metrics_reports.reports.inc
The function generates chart with information about page views, visitors and new visitors.

File

yandex_metrics_reports/yandex_metrics_reports.reports.inc, line 490
Report callbacks for Yandex.Metrics Reports module.

Code

function _yandex_metrics_reports_calculate_grid_step_fluid($max_grid, $axis_size, $min_step_px = 25) {

  /**
   * Calculate smart grid steps for numeric values.
   *
   * We use manual method to synchronize labels and grid.
   * First, define min step as $min_step_px so grids are not too close to each other.
   * Calculate min step and round it to nice value as $step. 2 becomes 2,
   * 34 becomes 50, 148 becomes 150, 0.07 becomes 0.05.
   */
  $min_step_percent = 100 * $min_step_px / $axis_size;
  $min_step = $max_grid * $min_step_percent / 100;
  $k = log10($min_step);
  if ($min_step < 1) {
    $k_improved = round(abs($k)) + 1;
  }
  else {
    $k_improved = -round(abs($k)) + 1;
  }
  $level = pow(10, $k_improved);

  // Convert 0.07 to 7, 12 to 10, 148 to 14.8, etc. for smart rounding.
  $min_step = $min_step * $level;

  // Round min step.
  $step = round($min_step / 5) * 5;
  $step = !$step ? round($min_step) : $step;

  // Convert back to input range.
  $step = $step / $level;

  // Manually fix for 3 and 4.
  if ($step >= 3 && $step <= 4) {
    $step = 5;
  }

  // Convert min step back to percents.
  $step_percent = $step * 100 / $max_grid;
  return array(
    $step,
    $step_percent,
  );
}