图例颜色错误

Wrong legend colours

我正在将一个非常旧的代码库从 JpGraph/2.3 升级到 4.2.0。最大的问题是图例颜色不再匹配图表颜色:

$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();

显然,图例颜色现在已硬编码在主题中(PHP class 扩展 Theme 摘要 class 的元素)。

有没有办法手动设置图例颜色或像版本 2 那样自动设置图例颜色?我有几个使用不同颜色的图,为每种颜色组合写一个主题看起来有点矫枉过正。

最后我写了一个主题:

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();
    }
}

请不要在家里这样做。它对我有用,因为 color 属性 实际上填充在我的真实代码库中,我认为在我共享的简化测试用例中不会发生这种情况。 这只是一个丑陋的 hack。