From d59dd86f1442c94deaf215358fd72a7da047e1dc Mon Sep 17 00:00:00 2001 From: Joshua Seigler Date: Sat, 4 Mar 2017 23:48:31 -0500 Subject: [PATCH] feat: add background fill, begin work on x-axis --- demo-as-image.php | 25 +++++++++++++---------- demo.php | 10 ++++++---- src/NeatCharts/BarChart.php | 7 ++++--- src/NeatCharts/CandlestickChart.php | 7 ++++--- src/NeatCharts/LineChart.php | 9 +++++---- src/NeatCharts/NeatChart.php | 31 +++++++++++++++++++---------- 6 files changed, 54 insertions(+), 35 deletions(-) diff --git a/demo-as-image.php b/demo-as-image.php index 6f4e65e..8c8800b 100644 --- a/demo-as-image.php +++ b/demo-as-image.php @@ -1,18 +1,23 @@ 500, 'height'=>400, 'lineColor'=>'#00F', diff --git a/demo.php b/demo.php index d8a04b4..5af4197 100644 --- a/demo.php +++ b/demo.php @@ -1,8 +1,10 @@
-

Line chart in svg tag, zero axis shown, filled

+

Line chart in svg tag, zero axis shown, filled, smoothed

250, 'lineColor'=>'#F00', 'labelColor'=>'#222', - 'smoothed'=>false, + 'smoothed'=>true, 'fontSize'=>14, 'yAxisZero'=>true, 'filled'=>true diff --git a/src/NeatCharts/BarChart.php b/src/NeatCharts/BarChart.php index a5b534b..076af25 100644 --- a/src/NeatCharts/BarChart.php +++ b/src/NeatCharts/BarChart.php @@ -9,9 +9,10 @@ namespace NeatCharts { 'markerColor' => '#000', 'labelColor' => '#000', 'fontSize' => 15, - 'yAxisEnabled'=>true, - 'xAxisEnabled'=>false, - 'yAxisZero'=>true + 'yAxisEnabled' => true, + 'xAxisEnabled' => false, + 'yAxisZero' => true, + 'background' => 'none' ]; parent::setOptions($options); } diff --git a/src/NeatCharts/CandlestickChart.php b/src/NeatCharts/CandlestickChart.php index 09bdd45..fdd1583 100644 --- a/src/NeatCharts/CandlestickChart.php +++ b/src/NeatCharts/CandlestickChart.php @@ -11,9 +11,10 @@ namespace NeatCharts { 'markerColor' => '#000', 'labelColor' => '#000', 'fontSize' => 15, - 'yAxisEnabled'=>true, - 'xAxisEnabled'=>false, - 'yAxisZero'=>false + 'yAxisEnabled' => true, + 'xAxisEnabled' => false, + 'yAxisZero' => false, + 'background' => 'none' ]; parent::setOptions($options); } diff --git a/src/NeatCharts/LineChart.php b/src/NeatCharts/LineChart.php index dc2ef77..b77bdaf 100644 --- a/src/NeatCharts/LineChart.php +++ b/src/NeatCharts/LineChart.php @@ -10,10 +10,11 @@ namespace NeatCharts { 'labelColor' => '#000', 'smoothed' => false, 'fontSize' => 15, - 'yAxisEnabled'=>true, - 'xAxisEnabled'=>false, - 'yAxisZero'=>false, - 'filled'=>false + 'yAxisEnabled' => true, + 'xAxisEnabled' => false, + 'yAxisZero' => false, + 'filled' => false, + 'background' => 'none' ]; parent::setOptions($options); } diff --git a/src/NeatCharts/NeatChart.php b/src/NeatCharts/NeatChart.php index 7b421f8..b783001 100644 --- a/src/NeatCharts/NeatChart.php +++ b/src/NeatCharts/NeatChart.php @@ -9,9 +9,10 @@ namespace NeatCharts { 'labelColor' => '#000', 'smoothed' => false, 'fontSize' => 15, - 'yAxisEnabled'=>true, - 'xAxisEnabled'=>false, - 'yAxisZero'=>false + 'yAxisEnabled' => true, + 'xAxisEnabled' => true, + 'yAxisZero' => false, + 'background' => 'none' ]; protected $width; @@ -24,6 +25,7 @@ namespace NeatCharts { protected $yMax; protected $yRange; protected $padding = ['top'=>10, 'right'=>10, 'bottom'=>10, 'left'=>10]; + protected $timeIntervals = [ 5 * 60, 60 * 60, 24 * 60 * 60, 28 * 60 * 60 ]; // 5 min, 1 hr, 24 hr, 1 month protected function labelFormat($float, $places, $minPlaces = 0) { $value = number_format($float, max($minPlaces, $places)); @@ -31,7 +33,6 @@ namespace NeatCharts { return (strpos($value, '.') === false ? $value . '.' : $value); } - /* Transform data coords to chart coords */ /* Transform data coords to chart coords */ protected function transformX($x) { return round( @@ -49,7 +50,7 @@ namespace NeatCharts { if (!is_numeric($value)) { return false; } $decimal = $value - floor($value); //get the decimal portion of the number if ($decimal == 0) { return 0; } //if it's a whole number - $precision = strlen(trim(number_format($decimal,10),'0')) - 1; //-2 to account for '0.' + $precision = strlen(trim(number_format($decimal,10),'0')) - 1; //-1 to account for '0.' return $precision; } @@ -78,21 +79,23 @@ namespace NeatCharts { protected function buildGridLabelXML() { $this->width = $this->options['width'] - $this->padding['left'] - $this->padding['right']; $this->height = $this->options['height'] - $this->padding['top'] - $this->padding['bottom']; - if ($this->options['yAxisEnabled'] || $this->options['xAxisEnabled']) { - $numLabels = 2 + ceil($this->height / $this->options['fontSize'] / 6); + if ($this->options['yAxisEnabled']) { + $numLabels = 4 + ceil($this->height / $this->options['fontSize'] / 4); $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) { + // 1 here is a fudge factor so we get multiples of 2.5 more often + if (fmod($labelInterval * $labelModulation, 2.5) < fmod($labelInterval * $labelModulation, 2) + 1) { $labelModulation /= 2.5; } else { $labelModulation /= 2; } $labelInterval = ceil($labelInterval * $labelModulation) / $labelModulation; $labelPrecision = $this->getPrecision($labelInterval); + $digitsLeft = max(1, ceil(log($this->yMax, 10))); + $commas = max(0, floor(($digitsLeft - 1) / 3)); $this->padding['left'] = $this->options['fontSize'] * 0.65 * ( - 2.5 + max(1, ceil(log($this->yMax, 10))) + $this->getPrecision($labelInterval) + 2.5 + $digitsLeft + $commas + $this->getPrecision($labelInterval) ); $this->width = $this->options['width'] - $this->padding['left'] - $this->padding['right']; @@ -130,8 +133,14 @@ namespace NeatCharts { } return ' +