adjusted label font placement

This commit is contained in:
Joshua Seigler 2016-07-01 17:56:38 -04:00
parent 733fb68f8b
commit a16b544a0d
2 changed files with 21 additions and 18 deletions

View file

@ -85,6 +85,23 @@ namespace NeatCharts {
$this->options['height'] = $this->height + $this->padding['top'] + $this->padding['bottom'];
}
$numLabels = 2 + ceil($this->height / $this->options['fontSize'] / 6);
$labelInterval = $this->yRange / $numLabels;
$labelModulation = 10 ** (1 + floor(-log($this->yRange / $numLabels, 10)));
// 0.1 here is a fudge factor so we get multiples of 2.5 a little more often
if (fmod($labelInterval * $labelModulation, 2.5) < fmod($labelInterval * $labelModulation, 2) + 0.1) {
$labelModulation /= 2.5;
} else {
$labelModulation /= 2;
}
$labelInterval = ceil($labelInterval * $labelModulation) / $labelModulation;
$labelPrecision = $this->getPrecision($labelInterval);
$this->padding['left'] = $this->options['fontSize'] * 0.6 * (
1 + max(1, ceil(log($this->yMax, 10))) + $this->getPrecision($labelInterval)
) + 10;
$this->width = $this->options['width'] - $this->padding['left'] - $this->padding['right'];
$chartPoints = 'M';
$chartSplines = 'M'.
$this->transformX($this->xMin).','.
@ -109,19 +126,6 @@ namespace NeatCharts {
}
}
$numLabels = 2 + ceil($this->height / $this->options['fontSize'] / 6);
$labelInterval = $this->yRange / $numLabels;
$labelModulation = 10 ** (1 + floor(-log($this->yRange / $numLabels, 10)));
// 0.1 here is a fudge factor so we get multiples of 2.5 a little more often
if (fmod($labelInterval * $labelModulation, 2.5) < fmod($labelInterval * $labelModulation, 2) + 0.1) {
$labelModulation /= 2.5;
} else {
$labelModulation /= 2;
}
$labelInterval = ceil($labelInterval * $labelModulation) / $labelModulation;
$labelPrecision = $this->getPrecision($labelInterval);
// Top and bottom grid lines
$gridLines =
'M10,0 '.$this->width.',0 '.
@ -156,8 +160,7 @@ namespace NeatCharts {
}
$chartID = rand();
$this->output = '<?xml version="1.0" standalone="no"?>
<svg viewBox="-'.( $this->padding['left'] ).' -'.( $this->padding['top'] ).' '.( $this->options['width'] ).' '.( $this->options['height'] ).'" width="'.( $this->options['width'] ).'" height="'.( $this->options['height'] ).'" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
$this->output = '<svg viewBox="-'.( $this->padding['left'] ).' -'.( $this->padding['top'] ).' '.( $this->options['width'] ).' '.( $this->options['height'] ).'" width="'.( $this->options['width'] ).'" height="'.( $this->options['height'] ).'" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<defs>
<marker id="SVGChart-markerCircle-'.( $chartID ).'" markerWidth="2" markerHeight="2" refX="1" refY="1" markerUnits="strokeWidth">
<circle cx="1" cy="1" r="1" style="stroke: none; fill:'.( $this->options['lineColor'] ).';" />
@ -181,7 +184,7 @@ namespace NeatCharts {
</g>
<g class="chart__gridLabels"
fill="'.( $this->options['labelColor'] ).'"
font-family="sans-serif"
font-family="monospace"
font-size="'.( $this->options['fontSize'] ).'px"
>
'.( $gridText ).'

View file

@ -66,8 +66,8 @@ namespace NeatCharts {
}
public abstract function setData($chartData);
public function render() {
return $this->output;
public function render($inline = false) {
return ($inline ? '' : '<?xml version="1.0" standalone="no"?>') . $this->output;
}
}
}