Changeset View
Changeset View
Standalone View
Standalone View
src/applications/diffusion/view/DiffusionCoverageBarView.php
- This file was added.
<?php | |||||
final class DiffusionCoverageBarView extends AphrontBarView { | |||||
const MAX_WIDTH = 200; | |||||
const LOG_SCALE = 3; | |||||
private $isDirectory; | |||||
private $covered; | |||||
private $uncovered; | |||||
private $largestNeighbor; | |||||
protected function getDefaultColor() { | |||||
return parent::COLOR_AUTO_GOODNESS; | |||||
} | |||||
public function setIsDirectory($is_dir) { | |||||
$this->isDirectory = $is_dir; | |||||
return $this; | |||||
} | |||||
public function setCovered($covered) { | |||||
$this->covered = $covered; | |||||
return $this; | |||||
} | |||||
public function setUncovered($uncovered) { | |||||
$this->uncovered = $uncovered; | |||||
return $this; | |||||
} | |||||
public function setLargestNeighbor($largest_neighbor) { | |||||
$this->largestNeighbor = $largest_neighbor; | |||||
return $this; | |||||
} | |||||
protected function getTotal() { | |||||
return $this->covered + $this->uncovered; | |||||
} | |||||
protected function getRatio() { | |||||
return min($this->covered, $this->getTotal()) / $this->getTotal(); | |||||
} | |||||
public function render() { | |||||
$this->initBehavior('phabricator-tooltips'); | |||||
require_celerity_resource('aphront-tooltip-css'); | |||||
require_celerity_resource('aphront-bars'); | |||||
$scaled_total = log($this->getTotal() + 1, self::LOG_SCALE); | |||||
$scaled_maximum = log($this->largestNeighbor + 1, self::LOG_SCALE); | |||||
$bar_ratio = $scaled_total / $scaled_maximum; | |||||
$max_width = self::MAX_WIDTH; | |||||
$bar_width = $max_width * $bar_ratio; | |||||
$progress_width = $bar_width * $this->getRatio(); | |||||
$tooltip = hsprintf( | |||||
'%s/%s (%s%%)', | |||||
number_format($this->covered), | |||||
number_format($this->getTotal()), | |||||
sprintf('% 3d', 100 * $this->getRatio())); | |||||
$color = $this->getColor(); | |||||
return javelin_tag( | |||||
'div', | |||||
array( | |||||
'class' => "aphront-bar coverage color-{$color}", | |||||
'sigil' => 'has-tooltip', | |||||
'meta' => array('tip' => $tooltip), | |||||
), | |||||
array( | |||||
phutil_tag( | |||||
'div', | |||||
array( | |||||
'style' => "width: {$bar_width}px;", | |||||
), | |||||
phutil_tag( | |||||
'div', | |||||
array('style' => "width: {$progress_width}px;"), | |||||
'')), | |||||
)); | |||||
} | |||||
} |