diff --git a/examples/README.md b/examples/README.md new file mode 100644 index 0000000..eac8ed5 --- /dev/null +++ b/examples/README.md @@ -0,0 +1,99 @@ +# JpGraph demo gallery + +A tiny, self-contained way to **spin up** this library and see it render real +charts. Each endpoint uses the Composer loader in `lib/JpGraph.php` and streams +a PNG generated on request. + +## Requirements + +- PHP with the **GD** extension (`php -m | grep gd`) +- No Composer install needed — the loader and library are vendored in `lib/` + +Tested on PHP 8.4. JpGraph 4.4.3 already handles the PHP 8.1+/9 deprecations on +the standard rendering paths, and this branch closes the remaining gaps (see the +PHP 9 note below). `_bootstrap.php` still mutes `E_DEPRECATED` for these +endpoints as a defensive measure, so a stray notice can never leak into the PNG +byte stream. + +## Run it + +From the repository root: + +```bash +php -S localhost:8000 -t examples +``` + +Then open for the gallery, or hit an endpoint directly: + +- — bar chart +- — two-series line chart +- — pie chart +- — wind rose + +## Files + +| File | What it does | +| ----------------- | ------------------------------------------------------- | +| `index.php` | HTML gallery that embeds the chart endpoints | +| `_bootstrap.php` | Shared loader + deprecation muting (`jpgraph_boot()`) | +| `bar.php` | Bar chart endpoint | +| `line.php` | Line chart endpoint (two series, legend, markers) | +| `pie.php` | Pie chart endpoint (percent labels, legend) | +| `windrose.php` | Wind rose endpoint (compass directions, speed ranges) | + +## Using the library in your own code + +```php +require 'lib/JpGraph.php'; + +JpGraph\JpGraph::load(); // core +JpGraph\JpGraph::module('bar'); // any module in lib/jpgraph/src/jpgraph_.php + +$graph = new Graph(400, 300); +$graph->SetScale('textlin'); +$graph->Add(new BarPlot([1, 2, 3, 4])); +$graph->Stroke(); // streams image/png, or pass a path to save +``` + +## PHP 9 compatibility + +The bundled **JpGraph 4.4.3** already resolves the main PHP 8.1+/9 changes on the +standard rendering paths: `strftime()` has been replaced, `imagefilledpolygon()` +is version-guarded via `CheckPHPVersion()`, and computed `float`→`int` +coordinates and dynamic properties are handled. Rendering ~20 chart types under +`error_reporting(E_ALL)` on PHP 8.4 produces **zero** deprecation notices. + +This branch closes the remaining removed-function gap, in `LanguageConv` +(`jpgraph_ttf.inc.php`) — the character-encoding paths that still call functions +PHP has removed: + +| Removed function | Replacement | Path | +| --- | --- | --- | +| `utf8_encode()` (removed in PHP 9.0) | guarded `mb_convert_encoding` / `iconv`, with a manual Latin-1 fallback | Hebrew text | +| `convert_cyr_string()` (removed in PHP 8.0) | guarded `mb_convert_encoding` / `iconv` | Cyrillic text | + +These reuse the guarded-`mb_convert_encoding` idiom the class already applies to +its EUC-JP handling, so behaviour is unchanged where the extensions are present. + +**Caveat:** full PHP 9 cleanliness of *every* code branch can't be proven +without a PHP 9 runtime, but all standard plot types — Windrose included — are +verified clean on PHP 8.4. + +## Windrose + +`WindroseGraph` `require_once`'d `jpgraph_glayout_vh.inc.php`, which was missing +from this fork, so *any* windrose script fatally errored at include time. That +file has been restored (`lib/jpgraph/src/jpgraph_glayout_vh.inc.php`): a +composite layout tree (`LayoutRect` base, with `LayoutHor`/`LayoutVert`) that +arranges several windrose plots in one graph, matching the interface +`jpgraph_windrose.php` expects (`WindroseGraph::Add()` accepts these alongside a +`WindrosePlot`, and each exposes `LayoutSize()`/`SetPos()`/`Stroke()`). Single +plots and the multi-plot `LayoutVert([LayoutHor(...), LayoutHor(...)])` form +both render correctly. + +Fonts: upstream windrose hardcoded the MS TrueType fonts (Verdana/Arial), which +aren't present on most systems, so it failed with a "font not readable" error +out of the box. Its font defaults now use the bundled DejaVu font +(`FF_DV_SANSSERIF`, which ships under `lib/jpgraph/src/fonts/`), so windrose +renders with no font setup at all. Call `SetFont()` (on the plot, its `scale`, +and the legend) to use a different font if you have one installed. diff --git a/examples/_bootstrap.php b/examples/_bootstrap.php new file mode 100644 index 0000000..6f5444b --- /dev/null +++ b/examples/_bootstrap.php @@ -0,0 +1,36 @@ +SetScale('textlin'); +$graph->SetMargin(45, 25, 45, 40); +$graph->title->Set('Weekly signups'); +$graph->subtitle->Set('JpGraph ' . trim(str_replace('Version:', '', @file_get_contents(__DIR__ . '/../lib/jpgraph/VERSION')))); +$graph->xaxis->SetTickLabels(['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']); +$graph->yaxis->title->Set('# signups'); + +$bar = new BarPlot($data); +$bar->SetFillColor('steelblue'); +$bar->SetColor('steelblue'); +$bar->SetWidth(0.6); +$bar->value->Show(); +$graph->Add($bar); + +$graph->Stroke(); diff --git a/examples/index.php b/examples/index.php new file mode 100644 index 0000000..ec69c46 --- /dev/null +++ b/examples/index.php @@ -0,0 +1,55 @@ + points at a chart endpoint that + * streams a freshly-rendered PNG from JpGraph. + */ +$version = trim(str_replace('Version:', '', @file_get_contents(__DIR__ . '/../lib/jpgraph/VERSION'))); +$charts = [ + 'bar.php' => 'Bar chart', + 'line.php' => 'Line chart (two series)', + 'pie.php' => 'Pie chart', + 'windrose.php' => 'Wind rose', +]; +?> + + + + + +JpGraph demo gallery + + + +
+

JpGraph demo gallery

+

Live charts rendered by JpGraph + on PHP . Each image is generated on request.

+
+
+ $title): ?> +
+ <?= htmlspecialchars($title) ?> +
+ PNG → +
+
+ +
+ + diff --git a/examples/line.php b/examples/line.php new file mode 100644 index 0000000..878767f --- /dev/null +++ b/examples/line.php @@ -0,0 +1,36 @@ +SetScale('textlin'); +$graph->SetMargin(50, 30, 45, 45); +$graph->title->Set('Monthly active users'); +$graph->xaxis->SetTickLabels($months); +$graph->yaxis->title->Set('thousands'); +$graph->legend->SetPos(0.03, 0.06, 'right', 'top'); + +$l1 = new LinePlot($thisYear); +$l1->SetColor('firebrick'); +$l1->SetWeight(2); +$l1->mark->SetType(MARK_FILLEDCIRCLE); +$l1->mark->SetFillColor('firebrick'); +$l1->mark->SetSize(3); +$l1->SetLegend('This year'); +$graph->Add($l1); + +$l2 = new LinePlot($lastYear); +$l2->SetColor('gray'); +$l2->SetWeight(1); +$l2->SetStyle('dashed'); +$l2->SetLegend('Last year'); +$graph->Add($l2); + +$graph->Stroke(); diff --git a/examples/pie.php b/examples/pie.php new file mode 100644 index 0000000..d77b647 --- /dev/null +++ b/examples/pie.php @@ -0,0 +1,26 @@ +title->Set('Browser share'); +// Legend along the bottom so it never overlaps the pie. +$graph->legend->SetPos(0.5, 0.97, 'center', 'bottom'); +$graph->legend->SetColumns(5); + +$pie = new PiePlot($data); +$pie->SetLegends($labels); +$pie->SetCenter(0.5, 0.5); +$pie->SetSize(0.32); +$pie->SetLabelType(PIE_VALUE_PER); +$pie->value->Show(); +$pie->value->SetFormat('%d%%'); +$graph->Add($pie); + +$graph->Stroke(); diff --git a/examples/windrose.php b/examples/windrose.php new file mode 100644 index 0000000..8959629 --- /dev/null +++ b/examples/windrose.php @@ -0,0 +1,37 @@ + [1.5, 1.0, 0.8, 0.4], + 'ne' => [1.0, 0.8, 0.5, 0.3], + 'e' => [1.2, 1.5, 0.9, 0.4], + 'se' => [1.0, 1.2, 0.8, 0.3], + 's' => [2.0, 2.5, 1.5, 0.5], + 'sw' => [3.0, 4.0, 3.5, 2.0], + 'w' => [4.0, 4.5, 3.5, 2.0], + 'nw' => [2.5, 3.0, 2.0, 1.0], +]; + +$graph = new WindroseGraph(500, 400); +$graph->title->Set('Wind rose — frequency by direction'); +$graph->title->SetFont(FF_DV_SANSSERIF, FS_BOLD, 12); + +$wp = new WindrosePlot($data); +$wp->SetType(WINDROSE_TYPE8); +$wp->SetFontColor('darkgray'); +// Speed ranges (knots) -> legend labels + colours (3 ranges after the calm centre). +$wp->SetRanges([0, 5, 10, 15]); +$wp->SetRangeColors(['#4575b4', '#91bfdb', '#fdae61']); +$graph->Add($wp); + +$graph->Stroke(); diff --git a/lib/jpgraph/src/jpgraph_glayout_vh.inc.php b/lib/jpgraph/src/jpgraph_glayout_vh.inc.php new file mode 100644 index 0000000..663ac1d --- /dev/null +++ b/lib/jpgraph/src/jpgraph_glayout_vh.inc.php @@ -0,0 +1,143 @@ +Stroke($graph) on whatever was added. +// +// The layout is a composite tree: a WindrosePlot is a "leaf" +// (its LayoutSize() returns 1) and LayoutHor / LayoutVert are +// container nodes that split a rectangular region of the image +// among their children -- horizontally or vertically, +// proportional to each child's LayoutSize() -- and then stroke +// them. Children only need the small layout protocol: +// LayoutSize(), SetPos($x,$y) and Stroke($graph); nested +// layouts additionally accept SetRegion(). +// +// Regions and positions are expressed as fractions (0..1) of +// the image, which is exactly what WindrosePlot::Stroke() +// expects for its centre (iX,iY) coordinates. +//======================================================================== + +class LayoutRect { + protected $iObj; // child nodes (plots or nested layouts) + protected $iX = 0.0, $iY = 0.0, $iW = 1.0, $iH = 1.0; // region, as fractions of the image + protected $iTitleInset = true; // reserve top space for the graph title + + function __construct($aObjArray) { + if( ! is_array($aObjArray) ) { + $aObjArray = array($aObjArray); + } + $this->iObj = array_values($aObjArray); + } + + // Total number of leaf plots in this subtree. A parent layout uses this + // to proportion the space handed to each child along its own axis. + function LayoutSize() { + $s = 0; + foreach( $this->iObj as $o ) { + $s += $o->LayoutSize(); + } + return $s > 0 ? $s : 1; + } + + // Assign the rectangular region (image fractions) this node may use. + // Called by a parent layout before Stroke(). + function SetRegion($aX, $aY, $aW, $aH) { + $this->iX = $aX; $this->iY = $aY; + $this->iW = $aW; $this->iH = $aH; + $this->iTitleInset = false; // a nested node inherits an already-inset region + } + + // Position (and stroke) a single child inside the given sub-region. + // Nested layouts receive the region; leaf plots receive their centre. + protected function _strokeChild($aChild, $aGraph, $x, $y, $w, $h) { + if( $aChild instanceof LayoutRect ) { + $aChild->SetRegion($x, $y, $w, $h); + $aChild->Stroke($aGraph); + } + else { + // Leaf plot: centre it in the cell (fraction of the whole image). + $aChild->SetPos($x + $w / 2, $y + $h / 2); + $aChild->Stroke($aGraph); + } + } + + // Lay children out along one axis, proportional to their LayoutSize(). + protected function _layout($aGraph, $aHoriz) { + $this->_applyTitleInset($aGraph); + $total = $this->LayoutSize(); + if( $total <= 0 ) { + return; + } + if( $aHoriz ) { + $cx = $this->iX; + foreach( $this->iObj as $child ) { + $cw = $this->iW * ($child->LayoutSize() / $total); + $this->_strokeChild($child, $aGraph, $cx, $this->iY, $cw, $this->iH); + $cx += $cw; + } + } + else { + $cy = $this->iY; + foreach( $this->iObj as $child ) { + $ch = $this->iH * ($child->LayoutSize() / $total); + $this->_strokeChild($child, $aGraph, $this->iX, $cy, $this->iW, $ch); + $cy += $ch; + } + } + } + + // For the top-level node only, shrink the region from the top so the + // sub-plots do not overlap the graph title. + protected function _applyTitleInset($aGraph) { + if( ! $this->iTitleInset ) { + return; // nested node: region already accounts for the title + } + $this->iTitleInset = false; // apply once + if( ! isset($aGraph->title) || ! is_object($aGraph->title) ) { + return; + } + $t = isset($aGraph->title->t) ? $aGraph->title->t : ''; + if( $t === '' || $t === null ) { + return; + } + $img = $aGraph->img; + $th = 0; + if( method_exists($aGraph->title, 'GetTextHeight') ) { + $th = (float) $aGraph->title->GetTextHeight($img); + } + if( $th <= 0 ) { + $th = 20; // conservative default when the height can't be measured + } + $inset = ($th + 12) / max(1, $img->height); + $inset = min(0.25, max(0.0, $inset)); + if( $inset > 0 ) { + $this->iY = $inset; + $this->iH = 1.0 - $inset; + } + } + + // A bare LayoutRect behaves like a horizontal row. + function Stroke($aGraph) { + $this->_layout($aGraph, true); + } +} + +// Arrange children left-to-right. +class LayoutHor extends LayoutRect { + function Stroke($aGraph) { + $this->_layout($aGraph, true); + } +} + +// Arrange children top-to-bottom. +class LayoutVert extends LayoutRect { + function Stroke($aGraph) { + $this->_layout($aGraph, false); + } +} diff --git a/lib/jpgraph/src/jpgraph_ttf.inc.php b/lib/jpgraph/src/jpgraph_ttf.inc.php index 5a1bc3e..b41a759 100755 --- a/lib/jpgraph/src/jpgraph_ttf.inc.php +++ b/lib/jpgraph/src/jpgraph_ttf.inc.php @@ -169,10 +169,10 @@ function Convert($aTxt,$aFF) { return $unistring; } elseif( LANGUAGE_CYRILLIC ) { if( CYRILLIC_FROM_WINDOWS && (!defined('LANGUAGE_CHARSET') || stristr(LANGUAGE_CHARSET, 'windows-1251')) ) { - $aTxt = convert_cyr_string($aTxt, "w", "k"); + $aTxt = LanguageConv::cyr_convert($aTxt, "w", "k"); } if( !defined('LANGUAGE_CHARSET') || stristr(LANGUAGE_CHARSET, 'koi8-r') || stristr(LANGUAGE_CHARSET, 'windows-1251')) { - $isostring = convert_cyr_string($aTxt, "k", "i"); + $isostring = LanguageConv::cyr_convert($aTxt, "k", "i"); $unistring = LanguageConv::iso2uni($isostring); } else { @@ -256,7 +256,55 @@ public static function heb_iso2uni($isoline) { $c=ord( substr($isoline,$i,1) ); $o .= ($c > 223) && ($c < 251) ? '&#'.(1264+$c).';' : chr($c); } - return utf8_encode($o); + return LanguageConv::utf8_encode_compat($o); + } + + // PHP 9 removes utf8_encode(); encode an ISO-8859-1 (Latin-1) string as + // UTF-8 via mbstring/iconv, falling back to a manual encoder if neither is + // available or the conversion fails (they return false on error). + private static function utf8_encode_compat($aData) { + if( function_exists('mb_convert_encoding') ) { + $r = mb_convert_encoding($aData, 'UTF-8', 'ISO-8859-1'); + if( $r !== false ) { + return $r; + } + } + if( function_exists('iconv') ) { + $r = iconv('ISO-8859-1', 'UTF-8', $aData); + if( $r !== false ) { + return $r; + } + } + $o = ''; + for( $i = 0, $n = strlen($aData); $i < $n; ++$i ) { + $c = ord($aData[$i]); + $o .= $c < 0x80 ? chr($c) : chr(0xC0 | ($c >> 6)) . chr(0x80 | ($c & 0x3F)); + } + return $o; + } + + // PHP 8.0 removes convert_cyr_string(); convert between Cyrillic charsets + // (single-letter codes as used by the old function) via mbstring/iconv. + // If neither is available or the conversion fails (they return false), + // return the text unchanged rather than a non-string. + private static function cyr_convert($aTxt, $aFrom, $aTo) { + $map = array('w'=>'Windows-1251', 'k'=>'KOI8-R', 'i'=>'ISO-8859-5', + 'a'=>'CP866', 'd'=>'CP866', 'm'=>'x-mac-cyrillic'); + $from = isset($map[$aFrom]) ? $map[$aFrom] : 'Windows-1251'; + $to = isset($map[$aTo]) ? $map[$aTo] : 'ISO-8859-5'; + if( function_exists('mb_convert_encoding') ) { + $r = mb_convert_encoding($aTxt, $to, $from); + if( $r !== false ) { + return $r; + } + } + if( function_exists('iconv') ) { + $r = iconv($from, $to, $aTxt); + if( $r !== false ) { + return $r; + } + } + return $aTxt; } } diff --git a/lib/jpgraph/src/jpgraph_windrose.php b/lib/jpgraph/src/jpgraph_windrose.php index 017dd38..2f46037 100755 --- a/lib/jpgraph/src/jpgraph_windrose.php +++ b/lib/jpgraph/src/jpgraph_windrose.php @@ -49,8 +49,8 @@ class WindrosePlotScale extends stdClass { private $iNumCirc=3; public $iMaxNum=0; private $iLblFmt='%.0f%%'; - public $iFontFamily=FF_VERDANA,$iFontStyle=FS_NORMAL,$iFontSize=10; - public $iZFontFamily=FF_ARIAL,$iZFontStyle=FS_NORMAL,$iZFontSize=10; + public $iFontFamily=FF_DV_SANSSERIF,$iFontStyle=FS_NORMAL,$iFontSize=10; + public $iZFontFamily=FF_DV_SANSSERIF,$iZFontStyle=FS_NORMAL,$iZFontSize=10; public $iFontColor='black',$iZFontColor='black'; private $iFontFrameColor=false, $iFontBkgColor=false; private $iLblZeroTxt=null; @@ -275,9 +275,9 @@ function StrokeLabels($aImg,$xc,$yc,$ri,$rr) { class LegendStyle { public $iLength = 40, $iMargin = 20 , $iBottomMargin=5; public $iCircleWeight=2, $iCircleRadius = 18, $iCircleColor='black'; - public $iTxtFontFamily=FF_VERDANA,$iTxtFontStyle=FS_NORMAL,$iTxtFontSize=8; - public $iLblFontFamily=FF_VERDANA,$iLblFontStyle=FS_NORMAL,$iLblFontSize=8; - public $iCircleFontFamily=FF_VERDANA,$iCircleFontStyle=FS_NORMAL,$iCircleFontSize=8; + public $iTxtFontFamily=FF_DV_SANSSERIF,$iTxtFontStyle=FS_NORMAL,$iTxtFontSize=8; + public $iLblFontFamily=FF_DV_SANSSERIF,$iLblFontStyle=FS_NORMAL,$iLblFontSize=8; + public $iCircleFontFamily=FF_DV_SANSSERIF,$iCircleFontStyle=FS_NORMAL,$iCircleFontSize=8; public $iLblFontColor='black',$iTxtFontColor='black',$iCircleFontColor='black'; public $iShow=true; public $iFormatString='%.1f'; @@ -395,7 +395,7 @@ class WindrosePlot extends stdClass { private $iRangeStyle = RANGE_OVERLAPPING ; public $iCenterSize=60; private $iType = WINDROSE_TYPE16; - public $iFontFamily=FF_VERDANA,$iFontStyle=FS_NORMAL,$iFontSize=10; + public $iFontFamily=FF_DV_SANSSERIF,$iFontStyle=FS_NORMAL,$iFontSize=10; public $iFontColor='darkgray'; private $iRadialGridStyle='longdashed'; private $iAllDirectionLabels = array('E','ENE','NE','NNE','N','NNW','NW','WNW','W','WSW','SW','SSW','S','SSE','SE','ESE'); @@ -1453,11 +1453,11 @@ function __construct($width=300,$height=200,$cachedName="",$timeout=0,$inline=1) $this->posx=$width/2; $this->posy=$height/2; $this->SetColor('white'); - $this->title->SetFont(FF_VERDANA,FS_NORMAL,12); + $this->title->SetFont(FF_DV_SANSSERIF,FS_NORMAL,12); $this->title->SetMargin(8); - $this->subtitle->SetFont(FF_VERDANA,FS_NORMAL,10); + $this->subtitle->SetFont(FF_DV_SANSSERIF,FS_NORMAL,10); $this->subtitle->SetMargin(0); - $this->subsubtitle->SetFont(FF_VERDANA,FS_NORMAL,8); + $this->subsubtitle->SetFont(FF_DV_SANSSERIF,FS_NORMAL,8); $this->subsubtitle->SetMargin(0); }