You are here

public function GetWidthPos::getWidthAndPosition in Length Indicator 8

Gets the widths and positions for the indicator template.

Parameters

int $optimin: The optimum minimum.

int $optimax: The optimum maximum. This must be larger than $optimin. This is validated on the widget settings form.

int $tolerance: The tolerance. This must be smaller than $optimin. This is validated on the widget settings form.

Return value

array The widths and positions of the indicators.

File

src/GetWidthPos.php, line 27

Class

GetWidthPos
Class GetWidthPos.

Namespace

Drupal\length_indicator

Code

public function getWidthAndPosition($optimin, $optimax, $tolerance) {
  $indicators = [];
  $min = $optimin - $tolerance;
  $max = $optimax + $tolerance;
  $total = $max + $min;
  $width = $min / $total * 100;
  $indicators[0]['width'] = $width;
  $indicators[0]['pos'] = 0;
  $indicators[0]['class'] = 'length-indicator__indicator--bad';

  // Adding +1 to make max inclusive.
  $indicators[4]['width'] = $width;
  $indicators[4]['pos'] = $max + 1;
  $indicators[4]['class'] = 'length-indicator__indicator--bad';
  $last = $width;
  $width = $optimin / $total * 100;
  $indicators[1]['width'] = $width - $last;
  $indicators[1]['pos'] = $min;
  $indicators[1]['class'] = 'length-indicator__indicator--ok';
  $last = $width;
  $width = $optimax / $total * 100;
  $indicators[2]['width'] = $width - $last;
  $indicators[2]['pos'] = $optimin;
  $indicators[2]['class'] = 'length-indicator__indicator--good';
  $last = $width;
  $width = $max / $total * 100;

  // Adding +1 to make optimax inclusive.
  $indicators[3]['width'] = $width - $last;
  $indicators[3]['pos'] = $optimax + 1;
  $indicators[3]['class'] = 'length-indicator__indicator--ok';
  ksort($indicators);
  return $indicators;
}