Display inline pie-charts also in views exported to PDF

fixes #6463
This commit is contained in:
Johannes Meyer 2014-12-15 13:55:20 +01:00
parent b307fe2791
commit bca28a5ae2
6 changed files with 72 additions and 22 deletions

View File

@ -92,7 +92,7 @@ if (in_array($path, $special)) {
header('Content-Type: image/svg+xml');
$pie = new PieChart();
$pie->initFromRequest();
echo $pie->render();
$pie->toSvg();
} elseif ($path === 'png/chart.php') {
if (!array_key_exists('data', $_GET)) {

View File

@ -4,7 +4,7 @@
namespace Icinga\Chart;
use Exception;
use Imagick;
use Icinga\Chart\Legend;
use Icinga\Chart\Palette;
use Icinga\Chart\Primitive\Drawable;
@ -110,12 +110,35 @@ abstract class Chart implements Drawable
return $this->renderer->render();
}
/**
* Return this graph rendered as PNG
*
* @param int $width The width of the PNG in pixel
* @param int $height The height of the PNG in pixel
*
* @return string A PNG binary string
*
* @throws IcingaException In case ImageMagick is not available
*/
public function toPng($width, $height)
{
if (! class_exists('Imagick')) {
throw new IcingaException('Cannot render PNGs without ImageMagick');
}
$image = new Imagick();
$image->readImageBlob($this->render());
$image->setImageFormat('png24');
$image->resizeImage($width, $height, imagick::FILTER_LANCZOS, 1);
return $image;
}
/**
* Align the chart to the top left corner instead of centering it
*
* @param bool $align
*/
public function alignTopLeft ($align = true)
public function alignTopLeft($align = true)
{
$this->align = $align;
}

View File

@ -5,17 +5,13 @@
namespace Icinga\Chart\Inline;
use Icinga\Chart\PieChart as PieChartRenderer;
use Imagick;
use Exception;
use Icinga\Exception\IcingaException;
/**
* Draw an inline pie-chart directly from the available request parameters.
*/
class PieChart extends Inline
{
public function render($output = true)
protected function getChart()
{
$pie = new PieChartRenderer();
$pie->alignTopLeft();
@ -23,23 +19,24 @@ class PieChart extends Inline
$pie->drawPie(array(
'data' => $this->data, 'colors' => $this->colors, 'labels' => $this->labels
));
return $pie;
}
public function toSvg($output = true)
{
if ($output) {
echo $pie->render();
echo $this->getChart()->render();
} else {
return $pie->render();
return $this->getChart()->render();
}
}
public function toPng()
public function toPng($output = true)
{
if (! class_exists('Imagick')) {
// TODO: This is quick & dirty. 404?
throw new IcingaException('Cannot render PNGs without Imagick');
if ($output) {
echo $this->getChart()->toPng($this->width, $this->height);
} else {
return $this->getChart()->toPng($this->width, $this->height);
}
$image = new Imagick();
$image->readImageBlob($this->render(false));
$image->setImageFormat('png24');
$image->resizeImage($this->width, $this->height, imagick::FILTER_LANCZOS, 1);
echo $image;
}
}

View File

@ -49,7 +49,6 @@ class Pdf extends DOMPDF
$html = $layout->render();
$imgDir = Url::fromPath('img');
$html = preg_replace('~src="' . $imgDir . '/~', 'src="' . Icinga::app()->getBootstrapDirectory() . '/img/', $html);
$html = preg_replace('~src="/svg/chart.php(.*)"~', 'class="icon" src="http://master1.com/png/chart.php$1"', $html);
$this->load_html($html);
$this->render();
$this->stream(

View File

@ -4,10 +4,12 @@
namespace Icinga\Web\Widget\Chart;
use Icinga\Chart\PieChart;
use Icinga\Web\Widget\AbstractWidget;
use Icinga\Web\Url;
use Icinga\Util\Format;
use Icinga\Application\Logger;
use Icinga\Exception\IcingaException;
/**
* A SVG-PieChart intended to be displayed as a small icon next to labels, to offer a better visualization of the
@ -375,6 +377,22 @@ EOD;
*/
public function render()
{
if ($this->view()->layout()->getLayout() === 'pdf') {
$pie = new PieChart();
$pie->alignTopLeft();
$pie->disableLegend();
$pie->drawPie(array(
'data' => $this->data, 'colors' => $this->colors, 'labels' => $this->labels
));
try {
$png = $pie->toPng($this->width, $this->height);
return '<img src="data:image/png;base64,' . base64_encode($png) . '" />';
} catch (IcingaException $_) {
return '';
}
}
$template = $this->template;
$template = str_replace('{url}', $this->url, $template);

View File

@ -461,8 +461,8 @@ class WebWizard extends Wizard implements SetupWizard
mt('setup', 'PHP Module: GD'),
mt(
'setup',
'In case you want icons and graphs being exported to PDF'
. ' as well, you\'ll need the GD extension for PHP.'
'In case you want icons being exported to PDF as'
. ' well, you\'ll need the GD extension for PHP.'
),
Platform::extensionLoaded('gd'),
Platform::extensionLoaded('gd') ? mt('setup', 'The PHP module GD is available') : (
@ -470,6 +470,19 @@ class WebWizard extends Wizard implements SetupWizard
)
);
$requirements->addOptional(
mt('setup', 'PHP Module: Imagick'),
mt(
'setup',
'In case you want graphs being exported to PDF as well'
. ', you\'ll need the ImageMagick extension for PHP.'
),
Platform::extensionLoaded('imagick'),
Platform::extensionLoaded('imagick') ? mt('setup', 'The PHP module Imagick is available') : (
mt('setup', 'The PHP module Imagick is missing')
)
);
$requirements->addOptional(
mt('setup', 'PHP Module: PDO-MySQL'),
mt(