| Server IP : 213.186.33.4 / Your IP : 216.73.216.193 Web Server : Apache System : Linux webm006.cluster103.gra.hosting.ovh.net 5.15.206-ovh-vps-grsec-zfs-classid #1 SMP Fri May 15 02:41:25 UTC 2026 x86_64 User : awebpaca ( 35430) PHP Version : 8.5.0 Disable Function : _dyuweyrj4,_dyuweyrj4r,dl MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : OFF | Pkexec : OFF Directory : /home/awebpaca/piwik/vendor/davaxi/sparkline/src/Sparkline/ |
Upload File : |
<?php
namespace Davaxi\Sparkline;
/**
* Trait DataTrait.
*/
trait DataTrait
{
/**
* @var int
* Base of value
*/
protected $base;
/**
* @var int
* Original value of chart
*/
protected $originValue = 0;
/**
* @var array
*/
protected $data = [
[0, 0],
];
/**
* @param $base
* Set base for values
*/
public function setBase($base)
{
$this->base = $base;
}
/**
* @param float $originValue
* Set origin value of chart
*/
public function setOriginValue($originValue)
{
$this->originValue = $originValue;
}
/**
* @param array $data,...
*/
public function setData()
{
$allSeries = func_get_args();
$this->data = [];
foreach ($allSeries as $data) {
$this->addSeries($data);
}
}
/**
* @param array $data
*/
public function addSeries($data)
{
$data = array_values($data);
$count = count($data);
if (!$count) {
$this->data[] = [0, 0];
return;
}
if ($count < static::MIN_DATA_LENGTH) {
$this->data[] = array_fill(0, 2, $data[0]);
return;
}
$this->data[] = $data;
}
/**
* @return int
*/
public function getSeriesCount()
{
return count($this->data);
}
/**
* @param int $seriesIndex
* @return array
*/
public function getNormalizedData($seriesIndex = 0)
{
$data = $this->data[$seriesIndex];
foreach ($data as $i => $value) {
$data[$i] = max(0, $value - $this->originValue);
}
return $data;
}
/**
* @param int $seriesIndex
* @return array
*/
public function getData($seriesIndex = 0)
{
return $this->data[$seriesIndex];
}
/**
* @param int $seriesIndex
* @return int
*/
public function getCount($seriesIndex = 0)
{
return count($this->data[$seriesIndex]);
}
/**
* @param int $seriesIndex
* @return array
*/
protected function getMaxValueWithIndex($seriesIndex = 0)
{
$max = max($this->data[$seriesIndex]);
$maxKeys = array_keys($this->data[$seriesIndex], $max);
$maxIndex = end($maxKeys);
if ($this->base) {
$max = $this->base;
}
return [$maxIndex, $max];
}
/**
* @param int $seriesIndex
* @return float
*/
protected function getMaxValue($seriesIndex = 0)
{
if ($this->base) {
return $this->base;
}
return max($this->data[$seriesIndex]);
}
/**
* TODO: this could be cached somehow
* @return float
*/
protected function getMaxValueAcrossSeries()
{
if ($this->base) {
return $this->base;
}
$maxes = array_map('max', $this->data);
return max($maxes);
}
protected function getMaxNumberOfDataPointsAcrossSerieses()
{
$counts = array_map('count', $this->data);
return max($counts);
}
/**
* @param int $seriesIndex
* @return array
*/
protected function getMinValueWithIndex($seriesIndex = 0)
{
$min = min($this->data[$seriesIndex]);
$minKey = array_keys($this->data[$seriesIndex], $min);
$minIndex = end($minKey);
return [$minIndex, $min];
}
/**
* @param int $seriesIndex
* @return array
*/
protected function getExtremeValues($seriesIndex = 0)
{
list($minIndex, $min) = $this->getMinValueWithIndex($seriesIndex);
list($maxIndex, $max) = $this->getMaxValueWithIndex($seriesIndex);
return [$minIndex, $min, $maxIndex, $max];
}
}