【问题标题】:Wrong legend colours错误的图例颜色
【发布时间】:2018-08-21 12:01:32
【问题描述】:

我正在将一个非常旧的代码库从 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();

显然,图例颜色现在在主题(扩展 Theme 抽象类的 PHP 类)中进行了硬编码。

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

【问题讨论】:

    标签: php jpgraph


    【解决方案1】:

    最后我确实写了一个主题:

    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。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-05-08
      • 2018-08-23
      • 2016-12-29
      • 2016-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多