Skip to content

Commit

Permalink
Create method to apply padding on chart
Browse files Browse the repository at this point in the history
  • Loading branch information
davaxi committed Dec 8, 2017
1 parent e34645f commit f88e587
Show file tree
Hide file tree
Showing 7 changed files with 160 additions and 23 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ $sparkline->setFormat('100x40');
$sparkline->setWidth(100);
$sparkline->setHeight(40);
// Apply padding
$sparkline->setPadding('10'); // > top: 10 | right: 10 | bottom: 10 | left: 10
$sparkline->setPadding('10 20'); // > top: 10 | right: 20 | bottom: 10 | left: 20
$sparkline->setPadding('10 20 30'); // > top: 10 | right: 20 | bottom: 30 | left: 20
$sparkline->setPadding('10 20 30 40'); // > top: 10 | right: 20 | bottom: 30 | left: 40
// Change background color (Default value #FFFFFF)
$sparkline->setBackgroundColorHex('#0f354b');
// or
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "davaxi/sparkline",
"description": "PHP Class (using GD) to generate sparklines",
"version": "1.1.0",
"version": "1.1.1",
"type": "library",
"keywords": [
"sparkline",
Expand Down
2 changes: 1 addition & 1 deletion composer.travis.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "davaxi/sparkline",
"description": "PHP Class (using GD) to generate sparklines",
"version": "1.1.0",
"version": "1.1.1",
"type": "library",
"keywords": [
"sparkline",
Expand Down
9 changes: 5 additions & 4 deletions src/Sparkline.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ class Sparkline
const MIN_DATA_LENGTH = 2;
const FORMAT_DIMENSION = 2;
const HEXADECIMAL_ALIAS_LENGTH = 3;
const CSS_PADDING_ONE = 1;
const CSS_PADDING_TWO = 2;
const CSS_PADDING_THREE = 3;
const CSS_PADDING = 4;

/**
* @var string
Expand Down Expand Up @@ -106,10 +110,7 @@ public function generate()
list($width, $height) = $this->getNormalizedSize();

$count = $this->getCount();
$step = $this->getStepWidth($width, $count);

$max = $this->getMaxValue();
list($polygon, $line) = $this->getChartElements($this->data, $max, $step);
list($polygon, $line) = $this->getChartElements($this->data);

$picture = new Picture($width, $height);
$picture->applyBackground($this->backgroundColor);
Expand Down
116 changes: 99 additions & 17 deletions src/Sparkline/FormatTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ trait FormatTrait
*/
protected $ratioComputing = 4;

/**
* @var array
*/
protected $padding = [
'top' => 0,
'right' => 0,
'bottom' => 0,
'left' => 0,
];

/**
* @param string $format (Width x Height)
*/
Expand Down Expand Up @@ -53,6 +63,21 @@ public function setHeight($height)
$this->height = (int)$height;
}

/**
* Set padding : format top right bottom left
* ex: 0 10 0 10.
*
* @param string $padding
*/
public function setPadding($padding)
{
list($top, $right, $bottom, $left) = $this->paddingStringToArray($padding);
$this->padding['top'] = $top;
$this->padding['right'] = $right;
$this->padding['bottom'] = $bottom;
$this->padding['left'] = $left;
}

/**
* @return int
*/
Expand All @@ -61,6 +86,35 @@ protected function getNormalizedHeight()
return $this->height * $this->ratioComputing;
}

/**
* @return int
*/
protected function getInnerHeight()
{
return $this->height - $this->padding['top'] - $this->padding['bottom'];
}

/**
* @return array
*/
protected function getNormalizedPadding()
{
return array_map(
function ($value) {
return $value * $this->ratioComputing;
},
$this->padding
);
}

/**
* @return int
*/
protected function getInnerNormalizedHeight()
{
return $this->getInnerHeight() * $this->ratioComputing;
}

/**
* @return int
*/
Expand All @@ -69,6 +123,22 @@ protected function getNormalizedWidth()
return $this->width * $this->ratioComputing;
}

/**
* @return int
*/
protected function getInnerWidth()
{
return $this->width - ($this->padding['left'] + $this->padding['right']);
}

/**
* @return int
*/
protected function getInnerNormalizedWidth()
{
return $this->getInnerWidth() * $this->ratioComputing;
}

/**
* @return array
*/
Expand All @@ -81,25 +151,37 @@ protected function getNormalizedSize()
}

/**
* @param $width
* @return array
*/
protected function getInnerNormalizedSize()
{
return [
$this->getInnerNormalizedWidth(),
$this->getInnerNormalizedHeight(),
];
}

/**
* @param $count
*
* @return float|int
*/
protected function getStepWidth($width, $count)
protected function getStepWidth($count)
{
return $width / ($count - 1);
$innerWidth = $this->getInnerNormalizedWidth();

return $innerWidth / ($count - 1);
}

/**
* @param array $data
* @param $height
* @param $max
*
* @return array
*/
protected function getDataForChartElements(array $data, $height, $max)
protected function getDataForChartElements(array $data, $height)
{
$max = $this->getMaxValue();
$minHeight = 1 * $this->ratioComputing;
$maxHeight = $height - $minHeight;
foreach ($data as $i => $value) {
Expand All @@ -108,7 +190,7 @@ protected function getDataForChartElements(array $data, $height, $max)
$value = 0;
}
if ($value > 0) {
$value = round($value / $max * $height);
$value = round(($value / $max) * $height);
}
$data[$i] = max($minHeight, min($value, $maxHeight));
}
Expand All @@ -118,32 +200,32 @@ protected function getDataForChartElements(array $data, $height, $max)

/**
* @param array $data
* @param $max
* @param $step
*
* @return array
*/
protected function getChartElements(array $data, $max, $step)
protected function getChartElements(array $data)
{
$count = count($data);
$height = $this->getNormalizedHeight();
$data = $this->getDataForChartElements($data, $height, $max);
$step = $this->getStepWidth($count);
$height = $this->getInnerNormalizedHeight();
$normalizedPadding = $this->getNormalizedPadding();
$data = $this->getDataForChartElements($data, $height);

$pictureX1 = $pictureX2 = 0;
$pictureY1 = $height - $data[0];
$pictureX1 = $pictureX2 = $normalizedPadding['left'];
$pictureY1 = $normalizedPadding['top'] + $height - $data[0];

$polygon = [];
$line = [];

// Initialize
$polygon[] = 0;
$polygon[] = $height + 50;
$polygon[] = $normalizedPadding['left'];
$polygon[] = $normalizedPadding['top'] + $height;
// First element
$polygon[] = $pictureX1;
$polygon[] = $pictureY1;
for ($i = 1; $i < $count; ++$i) {
$pictureX2 = $pictureX1 + $step;
$pictureY2 = $height - $data[$i];
$pictureY2 = $normalizedPadding['top'] + $height - $data[$i];

$line[] = [$pictureX1, $pictureY1, $pictureX2, $pictureY2];

Expand All @@ -155,7 +237,7 @@ protected function getChartElements(array $data, $max, $step)
}
// Last
$polygon[] = $pictureX2;
$polygon[] = $height + 50;
$polygon[] = $normalizedPadding['top'] + $height;

return [$polygon, $line];
}
Expand Down
36 changes: 36 additions & 0 deletions src/Sparkline/StyleTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,42 @@ protected function colorHexToRGB($color)
];
}

/**
* Formats:
* all
* vertical horizontal
* top horizontal bottom
* top right bottom left.
*
* @param string $padding
*
* @return array
*/
protected function paddingStringToArray($padding)
{
$parts = explode(' ', $padding);
switch (count($parts)) {
case static::CSS_PADDING_ONE:
$value = (float)$parts[0];

return [$value, $value, $value, $value];
break;
case static::CSS_PADDING_TWO:
$verticalValue = (float)$parts[0];
$horizontalValue = (float)$parts[1];

return [$verticalValue, $horizontalValue, $verticalValue, $horizontalValue];
case static::CSS_PADDING_THREE:
$parts[3] = $parts[1];

return $parts;
case static::CSS_PADDING:
return $parts;
default:
throw new \InvalidArgumentException('Invalid padding format');
}
}

/**
* @param $color
*
Expand Down
12 changes: 12 additions & 0 deletions tests/SparklineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,18 @@ public function testPoints()
unlink($path);
}

public function testMargin()
{
$path = __DIR__ . '/data/testGenerateMargin.png';
$this->sparkline->setFormat('250x250');
$this->sparkline->setData([-1, 2,4,5,6,10,7,8,5,7,7,11,8,6,9,11,9,13,14,12,16]);
$this->sparkline->addPoint(4, 4, '#6a737b');
$this->sparkline->setPadding("10 20 30 40");
$this->sparkline->save($path);
$this->assertFileExists($path);
unlink($path);
}

public function testSave()
{
$path = __DIR__ . '/data/testGenerateSave.png';
Expand Down

0 comments on commit f88e587

Please sign in to comment.