php, arrays, strings and jpgraph error

35 views
Skip to first unread message

Nebojša Subanović

unread,
Oct 27, 2023, 4:24:27 AM10/27/23
to JpGraph
Hello folks!
I got an error response: 
Fatal error: Uncaught TypeError: preg_split(): Argument #2 ($subject) must be of type string, array given in /var/www/html/jpgraph/src/gd_image.inc.php:336 Stack trace: #0 /var/www/html/jpgraph/src/gd_image.inc.php(336): preg_split() #1 /var/www/html/jpgraph/src/jpgraph_legend.inc.php(196): Image->GetTextWidth() #2 /var/www/html/jpgraph/src/jpgraph.php(2244): Legend->Stroke() #3 /var/www/html/php_skola/php_statistika.php(50): Graph->Stroke() #4 {main} thrown in /var/www/html/jpgraph/src/gd_image.inc.php on line 336
Any idea how to fix it? I deal with arrays, should I convert array into string?

Thanks in advance!

w1djm...@gmail.com

unread,
Oct 27, 2023, 5:56:10 AM10/27/23
to JpGraph
What version of JPGraph are you using? Please post the simplest example you can, with data, that shows the failure.

Nebojša Subanović

unread,
Oct 27, 2023, 6:15:31 AM10/27/23
to JpGraph
It is Version: v4.4.2 and here's the line that cause the error:

$lineplot->SetLegend(['Mean', '2.5th Percentile', '97.5th Percentile']);

If I put :

$lineplot->SetLegend('Mean, 2.5th Percentile, 97.5th Percentile');

it just plot this string as the one line in legend.

Here are code lines that create the graph:

// Create a line plot for your data points
$data = [$mean, $percentile_2_5, $percentile_97_5];
$lineplot = new LinePlot($data);

w1djm...@gmail.com

unread,
Oct 27, 2023, 6:41:20 AM10/27/23
to JpGraph
Create a lineplot for your data (the X and Y values), add it to your graph, then set the legend for that lineplot. Continue to build and add lineplots and set the legend of other data you may have. Setting a legend only allows a single string value. Since it appears as though you have multiple things to plot the method I mention will work just fine. I have graphs with more than 10 plots on them, all with different legends.

Nebojša Subanović

unread,
Oct 27, 2023, 7:11:50 AM10/27/23
to JpGraph
Thanks! It works!
Reply all
Reply to author
Forward
0 new messages