I'm upgrading a very old codebase from JpGraph/2.3 to 4.2.0. The biggest issue is that legend colours no longer match graph colours:
$good = new BarPlot([10, 12, 11, 9]);
$good->SetLegend('Good');
$good->SetFillGradient('#089B81', '#089B81:1.2', GRAD_LEFT_REFLECTION);
$bad = new BarPlot([4, 5, 2, 8]);
$bad->SetLegend('Bad');
$bad->SetFillGradient('#9B0829', '#9B0829:1.2', GRAD_LEFT_REFLECTION);
$both = new AccBarPlot([$good, $bad]);
$graph = new Graph(400, 300, 'auto');
$graph->SetScale('textlin');
$graph->Add($both);
$graph->Stroke();
Apparently, legend colours are now hard-coded in themes (PHP classes that extend the Theme
abstract class).
Is there a way to either set legend colours manually or make it automatic as in version 2? I have several plots that use different colours and writing a theme for each colour combination looks like overkill.
In the end I did write a theme:
class MyJpGraphTheme extends UniversalTheme
{
function GetColorList()
{
$colors = array();
if (isset($this->graph->plots)) {
foreach ($this->graph->plots as $level1) {
if (!isset($level1->plots)) {
continue;
}
foreach ($level1->plots as $level2) {
$colors[] = $level2->color;
}
}
}
return $colors ? $colors : parent::GetColorList();
}
}
Please don't do this at home. It works for me because the color
property gets actually populated in my real codebase, something I don't think that happens in the simplified test case I shared. This is just an ugly hack.