width = max(1, min($width, self::$MaxWidth)); $this->buffer = $buffer ?? new Buffer(50); } public function getBuffer(): Buffer { return $this->buffer; } public function setMax(?float $max) { $this->max = $max; } public function getHistogram() { $max = ($this->max === null)?1.0:$this->max; $samples = $this->buffer->getMappedSamples(0.0, $max, 0, 8, $this->width); $output = null; if (count($samples) < $this->width) { $output .= str_repeat(" ", $this->width - count($samples)); } foreach ($samples as $sample) { if ($sample === null) { $output .= "\u{2a09}"; } else { $output .= $this->bars[floor($sample)]; } } return $output; } public function getMultiHistogram(int $lines): array { $max = ($this->max === null)?1.0:$this->max; $samples = $this->buffer->getMappedSamples(0.0, $max, 0, 8*$lines, $this->width); $result = []; $topmin = 8*($lines-1); for ($line = 0; $line < $lines; $line++) { $rowmin = $topmin - ($line * 8); $output = null; if (count($samples) < $this->width) { $output .= str_repeat(" ", $this->width - count($samples)); } foreach ($samples as $sample) { if ($sample === null) { $output .= "\u{2a09}"; } else { $remain = $sample - $rowmin; if ($remain <= 0) { $output.= " "; } else { $sample = min(8, $remain); $output .= $this->bars[floor($sample)]; } } } $result[] = $output; } return $result; } public function __toString() { return $this->getHistogram(); } }