/** * Draw the data point text on the graph * * @param array $dataPoints * @param array $xAxis * @param array $yAxis * @param string $type * @param array $points * @param int $skip * @return void */ protected function drawDataText($dataPoints, $xAxis, $yAxis, $type, $points = null, $skip = 1) { $font = $this->graph->getFont(); $fontSize = $this->graph->getFontSize(); $fontColor = $this->graph->getFontColor(); $reverseFontColor = $this->graph->getReverseFontColor(); $fillColor = $this->graph->getFillColor(); switch ($type) { // Draw data point text on a line graph. case 'line': $this->graph->adapter()->setFillColor($fontColor); $prevY = null; $nextY = null; $start = $skip; for ($i = $start; $i < count($dataPoints); $i++) { $strSize = strlen($dataPoints[$i][0]) * $fontSize / 8; $x = ($dataPoints[$i][0] - $dataPoints[0][0]) / $points->xRange * $points->xLength + $points->zeroPoint['x'] - $strSize; $y = $points->yOffset - ($dataPoints[$i][1] - $dataPoints[0][1]) / $points->yRange * $points->yLength; if ($i < count($dataPoints) - 1) { if (null !== $prevY) { $nextY = $points->yOffset - ($dataPoints[$i + 1][1] - $dataPoints[0][1]) / $points->yRange * $points->yLength; } if ($this->graph->adapter() instanceof \Pop\Pdf\Pdf) { if (null !== $prevY && $y < $nextY && $y < $prevY) { $y -= $fontSize * 2; if (null !== $fillColor) { $revColor = null !== $reverseFontColor ? $reverseFontColor : new \Pop\Color\Space\Rgb(255, 255, 255); $this->graph->adapter()->setFillColor($revColor); } } else { if (null !== $prevY && $y < $nextY && $y > $prevY || null === $prevY && $y > $nextY) { $x -= $strSize * 2; } else { if (null !== $prevY && $y > $nextY && $y < $prevY || null === $prevY && $y < $nextY) { $x += $strSize * 2; } } } } else { if (null !== $prevY && $y > $nextY && $y > $prevY) { $y += $fontSize * 2; if (null !== $fillColor) { $revColor = null !== $reverseFontColor ? $reverseFontColor : new \Pop\Color\Space\Rgb(255, 255, 255); $this->graph->adapter()->setFillColor($revColor); } } else { if (null !== $prevY && $y > $nextY && $y < $prevY || null === $prevY && $y > $nextY) { $x -= $strSize * 2; } else { if (null !== $prevY && $y < $nextY && $y > $prevY || null === $prevY && $y < $nextY) { $x += $strSize * 2; } } } } } if (null !== $font) { if ($this->graph->adapter() instanceof \Pop\Pdf\Pdf) { $this->graph->adapter()->addText($x, $y + $fontSize / 2, $fontSize, $dataPoints[$i][1], $this->graph->getFonts($this->graph->getFont())); } else { $this->graph->adapter()->text($dataPoints[$i][1], $fontSize, $x, $y - $fontSize / 2, $this->graph->getFonts($this->graph->getFont())); } } else { if ($this->graph->adapter() instanceof \Pop\Pdf\Pdf) { $this->graph->adapter()->addFont('Arial'); $this->graph->adapter()->addText($x, $y + $fontSize / 2, $fontSize, $dataPoints[$i][1], 'Arial'); } else { $this->graph->adapter()->text($dataPoints[$i][1], $fontSize, $x, $y - $fontSize / 2); } } $prevY = $y; $this->graph->adapter()->setFillColor($fontColor); } break; // Draw data point text on a vertical bar graph. // Draw data point text on a vertical bar graph. case 'vBar': $this->graph->adapter()->setFillColor($fontColor); $realXDiv = ($points->xLength - $this->graph->getBarWidth() * 2) / (count($xAxis) - 1); for ($i = 0; $i < count($dataPoints); $i++) { $strSize = strlen($dataPoints[$i]) * $fontSize / 4; $x = $realXDiv * ($i + 1) - $this->graph->getBarWidth() / 1.75 + $this->graph->getBarWidth() / 2 - $strSize; $y = $points->yOffset - $dataPoints[$i] / $points->yRange * $points->yLength; if (null !== $font) { if ($this->graph->adapter() instanceof \Pop\Pdf\Pdf) { $this->graph->adapter()->addText($x, $y + $fontSize / 2, $fontSize, $dataPoints[$i], $this->graph->getFonts($this->graph->getFont()), $fontSize); } else { $this->graph->adapter()->text($dataPoints[$i], $fontSize, $x, $y - $fontSize / 2, $this->graph->getFonts($this->graph->getFont())); } } else { if ($this->graph->adapter() instanceof \Pop\Pdf\Pdf) { $this->graph->adapter()->addFont('Arial'); $this->graph->adapter()->addText($x, $y + $fontSize / 2, $fontSize, $dataPoints[$i], 'Arial'); } else { $this->graph->adapter()->text($dataPoints[$i], $fontSize, $x, $y - $fontSize / 2); } } } break; // Draw data point text on a horizontal bar graph. // Draw data point text on a horizontal bar graph. case 'hBar': $this->graph->adapter()->setFillColor($fontColor); if ($this->graph->adapter() instanceof \Pop\Pdf\Pdf) { $realYDiv = ($points->yLength + $this->graph->getBarWidth() * 2) / (count($yAxis) - 1); } else { $realYDiv = ($points->yLength - $this->graph->getBarWidth() * 2) / (count($yAxis) - 1); } $len = count($dataPoints); for ($i = 0; $i < $len; $i++) { if ($this->graph->adapter() instanceof \Pop\Pdf\Pdf) { $y = $points->zeroPoint['y'] - $realYDiv * $i + $this->graph->getBarWidth() / 5 + $this->graph->getBarWidth() / 2 - $fontSize / 2; } else { $y = $points->yLength - $realYDiv * ($i + 1) + $this->graph->getBarWidth() * 1.1 + $this->graph->getBarWidth() / 2 + $fontSize / 2; } $x = $dataPoints[$i] / $points->xRange * $points->xLength + $points->zeroPoint['x'] + $fontSize / 2; if (null !== $font) { if ($this->graph->adapter() instanceof \Pop\Pdf\Pdf) { $this->graph->adapter()->addText($x, $y + $fontSize / 2, $fontSize, $dataPoints[$i], $this->graph->getFonts($this->graph->getFont())); } else { $this->graph->adapter()->text($dataPoints[$i], $fontSize, $x, $y - $fontSize / 2, $this->graph->getFonts($this->graph->getFont())); } } else { if ($this->graph->adapter() instanceof \Pop\Pdf\Pdf) { $this->graph->adapter()->addFont('Arial'); $this->graph->adapter()->addText($x, $y + $fontSize / 2, $fontSize, $dataPoints[$i], 'Arial'); } else { $this->graph->adapter()->text($dataPoints[$i], $fontSize, $x, $y - $fontSize / 2); } } } break; // Draw data point text on a pie chart. // Draw data point text on a pie chart. case 'pie': for ($i = 0; $i < count($dataPoints); $i++) { $newMidX = $xAxis[$i]['x']; $newMidY = $xAxis[$i]['y']; $this->graph->adapter()->setFillColor($fontColor); if ($this->graph->adapter() instanceof \Pop\Pdf\Pdf) { // Text not supported on PDF pie charts yet due to clipping path issues. } else { switch ($yAxis[$i]) { case 1: $textX = $newMidX + $fontSize * 1.5; $textY = $newMidY + $fontSize * 1.5; break; case 2: $textX = $newMidX - $fontSize * 1.5; $textY = $newMidY + $fontSize * 1.5; break; case 3: $textX = $newMidX - $fontSize * 1.5; $textY = $newMidY - $fontSize * 1.5; break; case 4: $textX = $newMidX + $fontSize * 1.5; $textY = $newMidY - $fontSize * 1.5; break; } $this->graph->adapter()->text($dataPoints[$i][0] . '%', $fontSize, $textX, $textY, $this->graph->getFonts($this->graph->getFont())); } $this->graph->adapter()->setFillColor($dataPoints[$i][1]); } break; } }
public function testSetBarWidth() { $g = new Graph($this->imageOptions); $g->setBarWidth(5); $this->assertEquals(5, $g->getBarWidth()); }