diff --git a/GnuPlotBarChart.php b/GnuPlotBarChart.php new file mode 100644 index 0000000..50bb62b --- /dev/null +++ b/GnuPlotBarChart.php @@ -0,0 +1,139 @@ +values[$group][] = $value; + + return $this->values; + } + + /** + * Push a new $group, $group is a String name of group. + * + * Returns Int - group ID + */ + public function pushGroup($group){ + $this->groups[] = $group; + + end($this->groups); + return key($this->groups); + } + + /** + * Push a new $color for corresponding $group + */ + public function setColor($group, $color){ + $this->colors[$group] = $color; + } + + /** + * Push a $label of corresponding group + */ + public function pushLabel($label){ + $this->dataLabels[] = $label; + + return $this; + } + + public function get(){ + $this->sendInit(); + $this->sendCommand('set terminal png size '.$this->width.','.$this->height); + fflush($this->stdout); + $this->plot(); + + // Reading data, timeout=100ms + $result = ''; + $timeout = 100; + do { + stream_set_blocking($this->stdout, false); + $data = fread($this->stdout, 128); + $result .= $data; + usleep(5000); + $timeout -= 5; + } while ($timeout>0 || $data); + + return $result; + } + + public function plot($pieChart = false, $replot = false) { + if ($replot) { + $this->sendCommand('replot'); + } else { + $this->sendCommand('plot '.$this->getUsings()); + } + $this->plotted = true; + + $this->sendData(); + } + + protected function getUsings($pieChart = false) { + $n = count($this->groups); + if(!$n) + return false; + + $usings = array(); + + for($i = 0; $i < $n; $i++){ + $color = isset($this->colors[$i]) ? 'linecolor rgb "'.$this->colors[$i].'"' : ""; + $usings[] = "'-' u 2:xtic(1) ti col ".$color; + } + + return implode(', ', $usings); + } + + protected function sendData() { + foreach ($this->groups as $groupID => $groupName) + { + $this->sendCommand('"'.$groupName.'"'); + foreach ($this->values[$groupID] as $valueID => $value) { + $this->sendCommand('"'.$this->dataLabels[$valueID].'";'.$value); + } + $this->sendCommand('e'); + } + } + + protected function sendInit(){ + if(!sizeof($this->values)) + return false; + + $this->sendCommand('reset'); + + if ($this->title) { + $this->sendCommand('set title "'.$this->title.'"'); + } + + $this->sendCommand('set auto x'); + $this->sendCommand("set yrange [0:]"); + $this->sendCommand('set style data histogram'); + $this->sendCommand('set style histogram cluster gap 1'); + $this->sendCommand('set style fill solid border rgb "#333333"'); + $this->sendCommand('set border lw 2 lc rgb "#333333"'); + $this->sendCommand('set boxwidth 0.9'); + $this->sendCommand('set xtic scale 0 font ",12"'); + $this->sendCommand('set datafile separator ";"'); + } +} \ No newline at end of file diff --git a/README.md b/README.md index 136bc01..f955daa 100644 --- a/README.md +++ b/README.md @@ -85,6 +85,33 @@ run `realTime.php` for example: $plot->refresh(); ``` +GnuPlot BarChart USAGE +======= + +```php +require_once './GnuPlotBarChart.php'; + +$plot = new GnuPlotBarChart; +$plot->setWidth(500); +$plot->setHeight(500); +$plot->setGraphTitle("Exmple bar chart"); + +$g1 = $plot->pushGroup('Income'); +$plot->push($g1, 2); +$plot->push($g1, 1); + +$g2 = $plot->pushGroup('Sale'); +$plot->push($g2, 7); +$plot->push($g2, 4); + + +$plot->pushLabel("2013"); +$plot->pushLabel("2014"); + +header('Content-type: image/png'); +echo $plot->get(); +``` + API === @@ -109,4 +136,4 @@ License ======= `Gregwar\GnuPlot` is under MIT license - +`GnuPlotBarChart` is under MIT license