From 5d73afddd0faa5ad549d2770917732e08b6da5e6 Mon Sep 17 00:00:00 2001 From: Joshua Seigler Date: Sat, 2 Jul 2016 01:02:57 -0400 Subject: [PATCH] added options for sparklines --- demo.php | 46 +++++++++++----- src/NeatCharts/LineChart.php | 100 ++++++++++++++++++----------------- src/NeatCharts/NeatChart.php | 8 +-- 3 files changed, 91 insertions(+), 63 deletions(-) diff --git a/demo.php b/demo.php index e90cfd8..408cbc7 100644 --- a/demo.php +++ b/demo.php @@ -1,5 +1,20 @@ @@ -27,18 +42,7 @@ require_once 'vendor/autoload.php';

SVG chart in svg tag

800, 'height'=>250, 'lineColor'=>'#F00', @@ -47,6 +51,24 @@ $chart = new NeatCharts\LineChart($chartData, [ 'fontSize'=>14 ]); echo $chart->render(); +?> +
Random generated data, loaded right in the page
+
+ +
+

SVG sparkline in svg tag

+
+ 100, + 'height'=>20, + 'lineColor'=>'#000', + 'smoothed'=>false, + 'fontSize'=>2, + 'yAxisEnabled'=>false, + 'xAxisEnabled'=>false +]); +echo $chart->render(); ?>
Random generated data, loaded right in the page
diff --git a/src/NeatCharts/LineChart.php b/src/NeatCharts/LineChart.php index d5f13d9..1c62f44 100644 --- a/src/NeatCharts/LineChart.php +++ b/src/NeatCharts/LineChart.php @@ -85,22 +85,59 @@ 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); + if ($this->options['yAxisEnabled']) { + $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']; + $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']; + + // Top and bottom grid lines + $gridLines = + 'M10,0 '.$this->width.',0 '. + ' M10,'.$this->height.','.$this->width.','.$this->height; + + // Top and bottom grid labels + $gridText = + ''.($this->labelFormat($this->yMax, $labelPrecision + 1)).'' . + ''.($this->labelFormat($this->yMin, $labelPrecision + 1)).''; + + // Main labels and grid lines + for ( + $labelY = $this->yMin - fmod($this->yMin, $labelInterval) + $labelInterval; // Start at the first "nice" Y value > min + $labelY < $this->yMax; // Keep going until max + $labelY += $labelInterval // Add Interval each iteration + ) { + $labelHeight = $this->transformY($labelY); + if ( // label is not too close to the min or max + $labelHeight < $this->height - 1.5 * $this->options['fontSize'] && + $labelHeight > $this->options['fontSize'] * 1.5 + ) { + $gridText .= ''.$this->labelFormat($labelY, $labelPrecision).''; + $gridLines .= ' M0,'.$labelHeight.' '.$this->width.','.$labelHeight; + } else if ( // label is too close + $labelHeight < $this->height - $this->options['fontSize'] * 0.75 && + $labelHeight > $this->options['fontSize'] * 0.75 + ) { + $gridLines .= ' M'.( // move grid line over when it's very close to the min or max label + $labelHeight < $this->height - $this->options['fontSize'] / 2 && $labelHeight > $this->options['fontSize'] / 2 ? 0 : $this->options['fontSize'] / 2 + ).','.$labelHeight.' '.$this->width.','.$labelHeight; + } + } + } else { + $this->width = $this->options['width'] - $this->padding['left'] - $this->padding['right']; + } $chartPoints = 'M'; $chartSplines = 'M'. @@ -126,39 +163,6 @@ namespace NeatCharts { } } - // Top and bottom grid lines - $gridLines = - 'M10,0 '.$this->width.',0 '. - ' M10,'.$this->height.','.$this->width.','.$this->height; - - // Top and bottom grid labels - $gridText = - ''.($this->labelFormat($this->yMax, $labelPrecision + 1)).'' . - ''.($this->labelFormat($this->yMin, $labelPrecision + 1)).''; - - // Main labels and grid lines - for ( - $labelY = $this->yMin - fmod($this->yMin, $labelInterval) + $labelInterval; // Start at the first "nice" Y value > min - $labelY < $this->yMax; // Keep going until max - $labelY += $labelInterval // Add Interval each iteration - ) { - $labelHeight = $this->transformY($labelY); - if ( // label is not too close to the min or max - $labelHeight < $this->height - 1.5 * $this->options['fontSize'] && - $labelHeight > $this->options['fontSize'] * 1.5 - ) { - $gridText .= ''.$this->labelFormat($labelY, $labelPrecision).''; - $gridLines .= ' M0,'.$labelHeight.' '.$this->width.','.$labelHeight; - } else if ( // label is too close - $labelHeight < $this->height - $this->options['fontSize'] * 0.75 && - $labelHeight > $this->options['fontSize'] * 0.75 - ) { - $gridLines .= ' M'.( // move grid line over when it's very close to the min or max label - $labelHeight < $this->height - $this->options['fontSize'] / 2 && $labelHeight > $this->options['fontSize'] / 2 ? 0 : $this->options['fontSize'] / 2 - ).','.$labelHeight.' '.$this->width.','.$labelHeight; - } - } - $chartID = rand(); $this->output = ' diff --git a/src/NeatCharts/NeatChart.php b/src/NeatCharts/NeatChart.php index a008ca3..fd46984 100644 --- a/src/NeatCharts/NeatChart.php +++ b/src/NeatCharts/NeatChart.php @@ -7,7 +7,9 @@ namespace NeatCharts { 'lineColor' => '#000', 'labelColor' => '#000', 'smoothed' => false, - 'fontSize' => 15 + 'fontSize' => 15, + 'yAxisEnabled'=>true, + 'xAxisEnabled'=>false ]; protected $width; @@ -66,8 +68,8 @@ namespace NeatCharts { } public abstract function setData($chartData); - public function render($inline = false) { - return ($inline ? '' : '') . $this->output; + public function render() { + return $this->output; } } }