Fix number formatting in chart classes

Use explizit number formatting instead of implicit type conversions when
rendering SVGs, to prevent a change of the locale from breaking the charts.

fixes #6585
This commit is contained in:
Matthias Jentsch 2014-07-09 18:00:02 +02:00
parent e21b4243ca
commit ec3bafa026
11 changed files with 87 additions and 28 deletions

View File

@ -0,0 +1,46 @@
<?php
// {{{ICINGA_LICENSE_HEADER}}}
/**
* This file is part of Icinga 2 Web.
*
* Icinga 2 Web - Head for multiple monitoring backends.
* Copyright (C) 2013 Icinga Development Team
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* @copyright 2013 Icinga Development Team <info@icinga.org>
* @license http://www.gnu.org/licenses/gpl-2.0.txt GPL, version 2
* @author Icinga Development Team <info@icinga.org>
*/
// {{{ICINGA_LICENSE_HEADER}}}
namespace Icinga\Chart;
class Format
{
/**
* Format a number into a number-string as defined by the SVG-Standard
*
* @see http://www.w3.org/TR/SVG/types.html#DataTypeNumber
*
* @param $number
*
* @return string
*/
public static function formatSVGNumber($number)
{
return number_format($number, 1, '.', '');
}
}

View File

@ -83,8 +83,8 @@ class Circle extends Styleable implements Drawable
{ {
$coords = $ctx->toAbsolute($this->x, $this->y); $coords = $ctx->toAbsolute($this->x, $this->y);
$circle = $ctx->getDocument()->createElement('circle'); $circle = $ctx->getDocument()->createElement('circle');
$circle->setAttribute('cx', $coords[0]); $circle->setAttribute('cx', Format::formatSVGNumber($coords[0]));
$circle->setAttribute('cy', $coords[1]); $circle->setAttribute('cy', Format::formatSVGNumber($coords[1]));
$circle->setAttribute('r', 5); $circle->setAttribute('r', 5);
$circle->setAttribute('style', $this->getStyle()); $circle->setAttribute('style', $this->getStyle());
$this->applyAttributes($circle); $this->applyAttributes($circle);

View File

@ -101,10 +101,10 @@ class Line extends Styleable implements Drawable
list($x1, $y1) = $ctx->toAbsolute($this->xStart, $this->yStart); list($x1, $y1) = $ctx->toAbsolute($this->xStart, $this->yStart);
list($x2, $y2) = $ctx->toAbsolute($this->xEnd, $this->yEnd); list($x2, $y2) = $ctx->toAbsolute($this->xEnd, $this->yEnd);
$line = $doc->createElement('line'); $line = $doc->createElement('line');
$line->setAttribute('x1', $x1); $line->setAttribute('x1', Format::formatSVGNumber($x1));
$line->setAttribute('x2', $x2); $line->setAttribute('x2', Format::formatSVGNumber($x2));
$line->setAttribute('y1', $y1); $line->setAttribute('y1', Format::formatSVGNumber($y1));
$line->setAttribute('y2', $y2); $line->setAttribute('y2', Format::formatSVGNumber($y2));
$line->setAttribute('style', $this->getStyle()); $line->setAttribute('style', $this->getStyle());
$this->applyAttributes($line); $this->applyAttributes($line);
return $line; return $line;

View File

@ -30,8 +30,9 @@
namespace Icinga\Chart\Primitive; namespace Icinga\Chart\Primitive;
use \DOMElement; use DOMElement;
use \Icinga\Chart\Render\RenderContext; use Icinga\Chart\Render\RenderContext;
use Icinga\Chart\Format;
/** /**
* Drawable for creating a svg path element * Drawable for creating a svg path element
@ -177,6 +178,8 @@ class Path extends Styleable implements Drawable
if (!$this->isAbsolute) { if (!$this->isAbsolute) {
$point = $ctx->toAbsolute($point[0], $point[1]); $point = $ctx->toAbsolute($point[0], $point[1]);
} }
$point[0] = Format::formatSVGNumber($point[0]);
$point[1] = Format::formatSVGNumber($point[1]);
if ($lastPoint && $this->discrete) { if ($lastPoint && $this->discrete) {
$pathDescription .= sprintf($tpl, $point[0], $lastPoint[1]); $pathDescription .= sprintf($tpl, $point[0], $lastPoint[1]);
} }

View File

@ -29,8 +29,9 @@
namespace Icinga\Chart\Primitive; namespace Icinga\Chart\Primitive;
use \DOMElement; use DOMElement;
use \Icinga\Chart\Render\RenderContext; use Icinga\Chart\Render\RenderContext;
use Icinga\Chart\Format;
/** /**
* Component for drawing a pie slice * Component for drawing a pie slice
@ -138,15 +139,15 @@ class PieSlice extends Animatable implements Drawable
$yEnd = $y - ($r * cos($this->endRadian)); $yEnd = $y - ($r * cos($this->endRadian));
// Draw a straight line to the upper part of the arc // Draw a straight line to the upper part of the arc
$pathString .= 'L ' . $xStart . ' ' . $yStart; $pathString .= 'L ' . Format::formatSVGNumber($xStart) . ' ' . Format::formatSVGNumber($yStart);
// Instead of directly connecting the upper part of the arc (leaving a triangle), draw a bow with the radius // Instead of directly connecting the upper part of the arc (leaving a triangle), draw a bow with the radius
$pathString .= ' A ' . $r . ' ' . $r ; $pathString .= ' A ' . Format::formatSVGNumber($r) . ' ' . Format::formatSVGNumber($r);
// These are the flags for the bow, see the SVG path documentation for details // These are the flags for the bow, see the SVG path documentation for details
// http://www.w3.org/TR/SVG/paths.html#PathDataEllipticalArcCommands // http://www.w3.org/TR/SVG/paths.html#PathDataEllipticalArcCommands
$pathString .= ' 0 ' . (($this->endRadian - $this->startRadian > M_PI) ? '1' : '0 ') . ' 1'; $pathString .= ' 0 ' . (($this->endRadian - $this->startRadian > M_PI) ? '1' : '0 ') . ' 1';
// xEnd and yEnd are the lower point of the arc // xEnd and yEnd are the lower point of the arc
$pathString .= ' '.$xEnd . ' ' . $yEnd; $pathString .= ' ' . Format::formatSVGNumber($xEnd) . ' ' . Format::formatSVGNumber($yEnd);
return $pathString; return $pathString;
} }

View File

@ -31,6 +31,7 @@ namespace Icinga\Chart\Primitive;
use \DomElement; use \DomElement;
use \Icinga\Chart\Render\RenderContext; use \Icinga\Chart\Render\RenderContext;
use Icinga\Chart\Format;
/** /**
* Drawable representing the SVG rect element * Drawable representing the SVG rect element
@ -116,10 +117,10 @@ class Rect extends Animatable implements Drawable
if ($this->keepRatio) { if ($this->keepRatio) {
$ctx->ignoreRatio(); $ctx->ignoreRatio();
} }
$rect->setAttribute('x', $x); $rect->setAttribute('x', Format::formatSVGNumber($x));
$rect->setAttribute('y', $y); $rect->setAttribute('y', Format::formatSVGNumber($y));
$rect->setAttribute('width', $width); $rect->setAttribute('width', Format::formatSVGNumber($width));
$rect->setAttribute('height', $height); $rect->setAttribute('height', Format::formatSVGNumber($height));
$rect->setAttribute('style', $this->getStyle()); $rect->setAttribute('style', $this->getStyle());
$this->applyAttributes($rect); $this->applyAttributes($rect);

View File

@ -30,9 +30,10 @@
namespace Icinga\Chart\Primitive; namespace Icinga\Chart\Primitive;
use \DOMElement; use DOMElement;
use \DOMText; use DOMText;
use \Icinga\Chart\Render\RenderContext; use Icinga\Chart\Render\RenderContext;
use Icinga\Chart\Format;
/** /**
* Wrapper for the SVG text element * Wrapper for the SVG text element
@ -184,7 +185,7 @@ class Text extends Styleable implements Drawable
{ {
list($x, $y) = $ctx->toAbsolute($this->x, $this->y); list($x, $y) = $ctx->toAbsolute($this->x, $this->y);
$text = $ctx->getDocument()->createElement('text'); $text = $ctx->getDocument()->createElement('text');
$text->setAttribute('x', $x - 15); $text->setAttribute('x', Format::formatSVGNumber($x - 15));
$text->setAttribute( $text->setAttribute(
'style', 'style',
$this->getStyle() $this->getStyle()
@ -196,7 +197,7 @@ class Text extends Styleable implements Drawable
. 'text-anchor: ' . $this->alignment . 'text-anchor: ' . $this->alignment
); );
$text->setAttribute('y', $y); $text->setAttribute('y', Format::formatSVGNumber($y));
$text->appendChild(new DOMText($this->text)); $text->appendChild(new DOMText($this->text));
return $text; return $text;
} }

View File

@ -29,6 +29,8 @@
namespace Icinga\Chart\Render; namespace Icinga\Chart\Render;
use Icinga\Chart\Format;
/** /**
* Layout class encapsulating size, padding and margin information * Layout class encapsulating size, padding and margin information
*/ */
@ -147,7 +149,13 @@ class LayoutBox
$scaleX *= $this->getWidth()/100; $scaleX *= $this->getWidth()/100;
$scaleY *= $this->getHeight()/100; $scaleY *= $this->getHeight()/100;
return sprintf('translate(%s, %s) scale(%s, %s)', $translateX, $translateY, $scaleX, $scaleY); return sprintf(
'translate(%s, %s) scale(%s, %s)',
Format::formatSVGNumber($translateX),
Format::formatSVGNumber($translateY),
Format::formatSVGNumber($scaleX),
Format::formatSVGNumber($scaleY)
);
} }
/** /**

View File

@ -30,8 +30,7 @@
namespace Icinga\Chart\Render; namespace Icinga\Chart\Render;
use \DOMDocument; use DOMDocument;
use \Icinga\Util\Dimension;
/** /**
* Context for rendering, handles ratio based coordinate calculations. * Context for rendering, handles ratio based coordinate calculations.

View File

@ -107,9 +107,9 @@ class SVGRenderer
$ctx = $this->createRenderContext(); $ctx = $this->createRenderContext();
$svg = $this->document->createElement('svg'); $svg = $this->document->createElement('svg');
$svg->setAttribute('xmlns', 'http://www.w3.org/2000/svg'); $svg->setAttribute('xmlns', 'http://www.w3.org/2000/svg');
$svg->setATtribute('xmlns:xlink', 'http://www.w3.org/1999/xlink'); $svg->setAttribute('xmlns:xlink', 'http://www.w3.org/1999/xlink');
$svg->setAttribute('width', $this->width . '%'); $svg->setAttribute('width', $this->width . '%');
$svg->setAttribute('height', $this->width . '%'); $svg->setAttribute('height', $this->height . '%');
$svg->setAttribute( $svg->setAttribute(
'viewBox', 'viewBox',
sprintf( sprintf(

View File

@ -29,7 +29,7 @@
namespace Icinga\Chart\Unit; namespace Icinga\Chart\Unit;
use \Iterator; use Iterator;
/** /**
* Base class for Axis Units * Base class for Axis Units