/** * Render the text * * @param ezcGraphRenderer $renderer Renderer * @param ezcGraphBoundings $boundings Boundings for the axis * @return ezcGraphBoundings Remaining boundings */ public function render(ezcGraphRenderer $renderer, ezcGraphBoundings $boundings) { $height = (int) min(round($this->properties['maxHeight'] * ($boundings->y1 - $boundings->y0)), $this->properties['font']->maxFontSize + $this->padding * 2 + $this->margin * 2); switch ($this->properties['position']) { case ezcGraph::TOP: $textBoundings = new ezcGraphBoundings($boundings->x0, $boundings->y0, $boundings->x1, $boundings->y0 + $height); $boundings->y0 += $height + $this->properties['margin']; break; case ezcGraph::BOTTOM: $textBoundings = new ezcGraphBoundings($boundings->x0, $boundings->y1 - $height, $boundings->x1, $boundings->y1); $boundings->y1 -= $height + $this->properties['margin']; break; } $textBoundings = $renderer->drawBox($textBoundings, $this->properties['background'], $this->properties['border'], $this->properties['borderWidth'], $this->properties['margin'], $this->properties['padding']); $renderer->drawText($textBoundings, $this->properties['title'], ezcGraph::CENTER | ezcGraph::MIDDLE); return $boundings; }
/** * Render label text. * * Render the text of a single label, depending on the position, length and * rotation of the label. * * @param ezcGraphRenderer $renderer * @param ezcGraphChartElementAxis $axis * @param ezcGraphCoordinate $position * @param string $label * @param float $degTextAngle * @param float $labelLength * @param float $labelSize * @param float $lengthReducement * @return void */ protected function renderLabelText(ezcGraphRenderer $renderer, ezcGraphChartElementAxis $axis, ezcGraphCoordinate $position, $label, $degTextAngle, $labelLength, $labelSize, $lengthReducement) { switch (true) { case $degTextAngle >= 0 && $degTextAngle < 90 && ($axis->position === ezcGraph::LEFT || $axis->position === ezcGraph::RIGHT) || $degTextAngle >= 270 && $degTextAngle < 360 && ($axis->position === ezcGraph::TOP || $axis->position === ezcGraph::BOTTOM): $labelBoundings = new ezcGraphBoundings($position->x, $position->y, $position->x + abs($labelLength) - $lengthReducement, $position->y + $labelSize); $labelAlignement = ezcGraph::LEFT | ezcGraph::TOP; $labelRotation = $degTextAngle; break; case $degTextAngle >= 90 && $degTextAngle < 180 && ($axis->position === ezcGraph::LEFT || $axis->position === ezcGraph::RIGHT) || $degTextAngle >= 180 && $degTextAngle < 270 && ($axis->position === ezcGraph::TOP || $axis->position === ezcGraph::BOTTOM): $labelBoundings = new ezcGraphBoundings($position->x - abs($labelLength) + $lengthReducement, $position->y, $position->x, $position->y + $labelSize); $labelAlignement = ezcGraph::RIGHT | ezcGraph::TOP; $labelRotation = $degTextAngle - 180; break; case $degTextAngle >= 180 && $degTextAngle < 270 && ($axis->position === ezcGraph::LEFT || $axis->position === ezcGraph::RIGHT) || $degTextAngle >= 90 && $degTextAngle < 180 && ($axis->position === ezcGraph::TOP || $axis->position === ezcGraph::BOTTOM): $labelBoundings = new ezcGraphBoundings($position->x - abs($labelLength) + $lengthReducement, $position->y - $labelSize, $position->x, $position->y); $labelAlignement = ezcGraph::RIGHT | ezcGraph::BOTTOM; $labelRotation = $degTextAngle - 180; break; case $degTextAngle >= 270 && $degTextAngle < 360 && ($axis->position === ezcGraph::LEFT || $axis->position === ezcGraph::RIGHT) || $degTextAngle >= 0 && $degTextAngle < 90 && ($axis->position === ezcGraph::TOP || $axis->position === ezcGraph::BOTTOM): $labelBoundings = new ezcGraphBoundings($position->x, $position->y, $position->x - abs($labelLength) + $lengthReducement, $position->y + $labelSize); $labelAlignement = ezcGraph::LEFT | ezcGraph::TOP; $labelRotation = $degTextAngle; break; } $renderer->drawText($labelBoundings, $label, $labelAlignement, new ezcGraphRotation($labelRotation, $position)); }
/** * Render Axis labels * * Render labels for an axis. * * @param ezcGraphRenderer $renderer Renderer used to draw the chart * @param ezcGraphBoundings $boundings Boundings of the axis * @param ezcGraphCoordinate $start Axis starting point * @param ezcGraphCoordinate $end Axis ending point * @param ezcGraphChartElementAxis $axis Axis instance * @return void */ public function renderLabels(ezcGraphRenderer $renderer, ezcGraphBoundings $boundings, ezcGraphCoordinate $start, ezcGraphCoordinate $end, ezcGraphChartElementAxis $axis) { // receive rendering parameters from axis $steps = $axis->getSteps(); $this->steps = $steps; $axisBoundings = new ezcGraphBoundings($start->x, $start->y, $end->x, $end->y); // Determine normalized axis direction $this->direction = new ezcGraphVector($end->x - $start->x, $end->y - $start->y); $this->direction->unify(); $axisAngle = -$this->direction->angle(new ezcGraphVector(1, 0)); if ($this->outerGrid) { $gridBoundings = $boundings; } else { $gridBoundings = new ezcGraphBoundings($boundings->x0 + $renderer->xAxisSpace, $boundings->y0 + $renderer->yAxisSpace, $boundings->x1 - $renderer->xAxisSpace, $boundings->y1 - $renderer->yAxisSpace); } // Determine optimal angle if none specified if ($this->angle === null) { $minimumStepWidth = null; foreach ($steps as $nr => $step) { if ($minimumStepWidth === null || $step->width < $minimumStepWidth) { $minimumStepWidth = $step->width; } } $width = abs($axisBoundings->width * $minimumStepWidth * $this->direction->x + $axisBoundings->height * $minimumStepWidth * $this->direction->y); $height = abs($renderer->yAxisSpace * $this->direction->x + $renderer->xAxisSpace * $this->direction->y); $length = sqrt(pow($width, 2) + pow($height, 2)); $this->angle = rad2deg(acos($height / $length)); } // Determine additional required axis space by boxes $firstStep = reset($steps); $lastStep = end($steps); $textAngle = $axisAngle + deg2rad($this->angle) + ($axis->position & (ezcGraph::TOP | ezcGraph::BOTTOM) ? deg2rad(270) : deg2rad(90)); // Ensure angle between 0 and 360 degrees $degTextAngle = rad2deg($textAngle); while ($degTextAngle < 0) { $degTextAngle += 360.0; } $this->offset = ($this->angle < 0 ? -1 : 1) * ($axis->position & (ezcGraph::TOP | ezcGraph::LEFT) ? 1 : -1) * (1 - cos(deg2rad($this->angle * 2))); $axisSpaceFactor = abs(($this->direction->x == 0 ? 0 : $this->direction->x * $renderer->yAxisSpace / $axisBoundings->width) + ($this->direction->y == 0 ? 0 : $this->direction->y * $renderer->xAxisSpace / $axisBoundings->height)); $start = new ezcGraphCoordinate($start->x + max(0.0, $axisSpaceFactor * $this->offset) * ($end->x - $start->x), $start->y + max(0.0, $axisSpaceFactor * $this->offset) * ($end->y - $start->y)); $end = new ezcGraphCoordinate($end->x + min(0.0, $axisSpaceFactor * $this->offset) * ($end->x - $start->x), $end->y + min(0.0, $axisSpaceFactor * $this->offset) * ($end->y - $start->y)); $labelLength = sqrt(pow($renderer->xAxisSpace * $this->direction->y + $axisSpaceFactor * $this->offset * ($end->x - $start->x), 2) + pow($renderer->yAxisSpace * $this->direction->x + $axisSpaceFactor * $this->offset * ($end->y - $start->y), 2)); $this->offset *= $axisSpaceFactor; // Draw steps and grid foreach ($steps as $nr => $step) { $position = new ezcGraphCoordinate($start->x + ($end->x - $start->x) * $step->position * abs($this->direction->x), $start->y + ($end->y - $start->y) * $step->position * abs($this->direction->y)); $stepSize = new ezcGraphCoordinate(($end->x - $start->x) * $step->width, ($end->y - $start->y) * $step->width); // Calculate label boundings switch (true) { case $nr === 0: $labelSize = min(abs($renderer->xAxisSpace * 2 * $this->direction->x + $renderer->yAxisSpace * 2 * $this->direction->y), abs($step->width * $axisBoundings->width * $this->direction->x + $step->width * $axisBoundings->height * $this->direction->y)); break; case $step->isLast: $labelSize = min(abs($renderer->xAxisSpace * 2 * $this->direction->x + $renderer->yAxisSpace * 2 * $this->direction->y), abs($steps[$nr - 1]->width * $axisBoundings->width * $this->direction->x + $steps[$nr - 1]->width * $axisBoundings->height * $this->direction->y)); break; default: $labelSize = min(abs($step->width * $axisBoundings->width * $this->direction->x + $step->width * $axisBoundings->height * $this->direction->y), abs($steps[$nr - 1]->width * $axisBoundings->width * $this->direction->x + $steps[$nr - 1]->width * $axisBoundings->height * $this->direction->y)); break; } $labelSize = $labelSize * cos(deg2rad($this->angle)); $lengthReducement = min(abs(tan(deg2rad($this->angle)) * ($labelSize / 2)), abs($labelLength / 2)); switch (true) { case $degTextAngle >= 0 && $degTextAngle < 90 && ($axis->position === ezcGraph::LEFT || $axis->position === ezcGraph::RIGHT) || $degTextAngle >= 270 && $degTextAngle < 360 && ($axis->position === ezcGraph::TOP || $axis->position === ezcGraph::BOTTOM): $labelBoundings = new ezcGraphBoundings($position->x, $position->y, $position->x + abs($labelLength) - $lengthReducement, $position->y + $labelSize); $labelAlignement = ezcGraph::LEFT | ezcGraph::TOP; $labelRotation = $degTextAngle; break; case $degTextAngle >= 90 && $degTextAngle < 180 && ($axis->position === ezcGraph::LEFT || $axis->position === ezcGraph::RIGHT) || $degTextAngle >= 180 && $degTextAngle < 270 && ($axis->position === ezcGraph::TOP || $axis->position === ezcGraph::BOTTOM): $labelBoundings = new ezcGraphBoundings($position->x - abs($labelLength) + $lengthReducement, $position->y, $position->x, $position->y + $labelSize); $labelAlignement = ezcGraph::RIGHT | ezcGraph::TOP; $labelRotation = $degTextAngle - 180; break; case $degTextAngle >= 180 && $degTextAngle < 270 && ($axis->position === ezcGraph::LEFT || $axis->position === ezcGraph::RIGHT) || $degTextAngle >= 90 && $degTextAngle < 180 && ($axis->position === ezcGraph::TOP || $axis->position === ezcGraph::BOTTOM): $labelBoundings = new ezcGraphBoundings($position->x - abs($labelLength) + $lengthReducement, $position->y - $labelSize, $position->x, $position->y); $labelAlignement = ezcGraph::RIGHT | ezcGraph::BOTTOM; $labelRotation = $degTextAngle - 180; break; case $degTextAngle >= 270 && $degTextAngle < 360 && ($axis->position === ezcGraph::LEFT || $axis->position === ezcGraph::RIGHT) || $degTextAngle >= 0 && $degTextAngle < 90 && ($axis->position === ezcGraph::TOP || $axis->position === ezcGraph::BOTTOM): $labelBoundings = new ezcGraphBoundings($position->x, $position->y + $labelSize, $position->x + abs($labelLength) - $lengthReducement, $position->y); $labelAlignement = ezcGraph::LEFT | ezcGraph::BOTTOM; $labelRotation = $degTextAngle; break; } $renderer->drawText($labelBoundings, $step->label, $labelAlignement, new ezcGraphRotation($labelRotation, $position)); // major grid if ($axis->majorGrid) { $this->drawGrid($renderer, $gridBoundings, $position, $stepSize, $axis->majorGrid); } // major step $this->drawStep($renderer, $position, $this->direction, $axis->position, $this->majorStepSize, $axis->border); } }
/** * Render Axis labels * * Render labels for an axis. * * @param ezcGraphRenderer $renderer Renderer used to draw the chart * @param ezcGraphBoundings $boundings Boundings of the axis * @param ezcGraphCoordinate $start Axis starting point * @param ezcGraphCoordinate $end Axis ending point * @param ezcGraphChartElementAxis $axis Axis instance * @return void */ public function renderLabels(ezcGraphRenderer $renderer, ezcGraphBoundings $boundings, ezcGraphCoordinate $start, ezcGraphCoordinate $end, ezcGraphChartElementAxis $axis) { // receive rendering parameters from axis $steps = $axis->getSteps(); $this->steps = $steps; $axisBoundings = new ezcGraphBoundings($start->x, $start->y, $end->x, $end->y); // Determine normalized axis direction $this->direction = new ezcGraphVector($start->x - $end->x, $start->y - $end->y); $this->direction->unify(); if ($this->outerGrid) { $gridBoundings = $boundings; } else { $gridBoundings = new ezcGraphBoundings($boundings->x0 + $renderer->xAxisSpace * abs($this->direction->y), $boundings->y0 + $renderer->yAxisSpace * abs($this->direction->x), $boundings->x1 - $renderer->xAxisSpace * abs($this->direction->y), $boundings->y1 - $renderer->yAxisSpace * abs($this->direction->x)); } // Determine additional required axis space by boxes $firstStep = reset($steps); $lastStep = end($steps); $this->widthModifier = 1 + $firstStep->width / 2 + $lastStep->width / 2; // Draw steps and grid foreach ($steps as $nr => $step) { $position = new ezcGraphCoordinate($start->x + ($end->x - $start->x) * ($step->position + $step->width) / $this->widthModifier, $start->y + ($end->y - $start->y) * ($step->position + $step->width) / $this->widthModifier); $stepWidth = $step->width / $this->widthModifier; $stepSize = new ezcGraphCoordinate($axisBoundings->width * $stepWidth, $axisBoundings->height * $stepWidth); if ($this->showLabels) { // Calculate label boundings switch (true) { case abs($this->direction->x) > abs($this->direction->y) && $this->direction->x <= 0: $labelBoundings = new ezcGraphBoundings($position->x - $stepSize->x + $this->labelPadding, $position->y + $this->labelPadding, $position->x - $this->labelPadding, $position->y + $renderer->yAxisSpace - $this->labelPadding); $alignement = ezcGraph::CENTER | ezcGraph::TOP; break; case abs($this->direction->x) > abs($this->direction->y) && $this->direction->x > 0: $labelBoundings = new ezcGraphBoundings($position->x + $this->labelPadding, $position->y + $this->labelPadding, $position->x + $stepSize->x - $this->labelPadding, $position->y + $renderer->yAxisSpace - $this->labelPadding); $alignement = ezcGraph::CENTER | ezcGraph::TOP; break; case $this->direction->y <= 0: $labelBoundings = new ezcGraphBoundings($position->x - $renderer->xAxisSpace + $this->labelPadding, $position->y - $stepSize->y + $this->labelPadding, $position->x - $this->labelPadding, $position->y - $this->labelPadding); $alignement = ezcGraph::MIDDLE | ezcGraph::RIGHT; break; case $this->direction->y > 0: $labelBoundings = new ezcGraphBoundings($position->x - $renderer->xAxisSpace + $this->labelPadding, $position->y + $this->labelPadding, $position->x - $this->labelPadding, $position->y + $stepSize->y - $this->labelPadding); $alignement = ezcGraph::MIDDLE | ezcGraph::RIGHT; break; } $renderer->drawText($labelBoundings, $step->label, $alignement); } // major grid if ($axis->majorGrid) { $this->drawGrid($renderer, $gridBoundings, $position, $stepSize, $axis->majorGrid); } // major step $this->drawStep($renderer, $position, $this->direction, $axis->position, $this->majorStepSize, $axis->border); } }