. .

JpGraph: Klassenerweiterung „ErrorPlotValues“


Die Klasse „Plot“ bzw. „ErrorPlots“ von JpGraph bot nicht die Möglichkeit, die entsprechenden Werte darzustellen, die den Balken bestimmen.

Der Entwickelr von JpGraph konnte sich leider auf meine Anfrage nicht melden.

Daher gebe ich die Klassenerweiterung (recht spezifisch) hier bekannt:

<?PHP
/*
 * a small class extension for displaying percentage or absolute values
 * inside ErrorPlots
 * by Hofmann R. Julian // 16. Januar 2009
 *

    # new ErrorPlotValues(
    #    [array like for normal ErrorPlot], 
    #    [xValues array like for normal ErrorPlot],
    #    [FALSE = percentage, TRUE = absolute values], 
    #    [width of the upper and lower line]
    # );

   Usage example:

    $errplot1 = new ErrorPlotValues($differenzIntern, FALSE, FALSE, 4);
    $errplot1->SetColor("gray");
    $errplot1->SetWeight(1);
    $errplot1->SetCenter();
    $errplot1->value->Show();
    $errplot1->value->HideZero();
    $errplot1->value->SetFormat("%d%%");
    $errplot1->SetValuePos("top", "right");
    $errplot1->SetValueFont(FF_VERDANA, FS_BOLD, 5);

 */

class ErrorPlotValues extends Plot {
    private $breite = 0;
    private $absolut = FALSE;
    private $valign = "middle";
    private $halign = "center";
    private $FontSize = 5;

    function ErrorPlotValues(&$Ywerte, $Xwerte = FALSE, $absolut = FALSE, 
            $breite = 2) {
        $this->Plot($Ywerte, $Xwerte);
        $this->numpoints /= 2;
        $this->breite = $breite;
        $this->absolut = $absolut;
    }
    function SetValuePos($valign = "middle", $halign = "center") {
        $this->valign = $valign;
        $this->halign = $halign;
    }
    function SetValueFont($font = "FF_VERDANA", $style = "FS_BOLD", $size = 5) {
        $this->value->SetFont($font, $style, $size);
        $this->FontSize = $size;
    }
    function Stroke($img, $xscale, $yscale) {
        $numpoints = count($this->coords[0]) / 2;
        $img->SetColor($this->color);
        $img->SetLineWeight($this->weight);

        if (isset($this->coords[1])) {
            if (count($this->coords[1]) != $numpoints)
                JpGraphError::RaiseL(2003, count($this->coords[1]), $numpoints);
            else
                $exist_x = TRUE;
        } else
            $exist_x = FALSE;

        for ($i = 0; $i < $numpoints; ++$i) {
            $x = ($exist_x) ? $this->coords[1][$i] : $i;

            if (!is_numeric($x) || !is_numeric($this->coords[0][$i*2]) || 
                !is_numeric($this->coords[0][$i*2+1]))
                    continue;

            $wert = ($this->absolut == FALSE) ? 
                $this->getProzent($this->coords[0][$i*2], $this->coords[0][$i*2+1]) : 
                $this->getDifferenz($this->coords[0][$i*2], $this->coords[0][$i*2+1]);
            $wertShown = sprintf($this->value->format, $wert);

            $datenValpos_x = $datenValpos_y = NULL;
            $datenpos_x = $xscale->Translate($x);
            $datenpos_val1 = $yscale->Translate($this->coords[0][$i*2]);
            $datenpos_val2 = $yscale->Translate($this->coords[0][$i*2+1]);

            $datenPosDiff = $this->FontSize + ($this->value->margin / 2) + 1;

            switch ($this->halign) {
            case "right":
                $datenValpos_x = $datenpos_x + $this->breite + 
                    ((strlen((string) $wertShown) * $this->FontSize) / 2);
            break;
            case "left":
                $datenValpos_x = $datenpos_x - $this->breite - 
                    ((strlen((string) $wertShown) * $this->FontSize) / 2);
            break;
            case "center":
            default:
                $datenValpos_x = $datenpos_x;
            }
            switch ($this->valign) {
            case "top":
                if ($this->coords[0][$i*2] > 0)
                    $datenValpos_y = ($datenpos_val2 > $datenpos_val1) ? 
                        $datenpos_val1 : 
                        ($datenpos_val2 + 2 + ($datenPosDiff * 2));
                else
                    $datenValpos_y = ($datenpos_val1 > $datenpos_val2) ?
                        $datenpos_val2 :
                        ($datenpos_val1 + 2 + ($datenPosDiff * 2));
            break;
            case "bottom":
                if ($this->coords[0][$i*2+1] > 0)
                    $datenValpos_y = ($datenpos_val2 > $datenpos_val1) ? 
                        ($wert < 0 ? ($datenpos_val2 - 2 - ($datenPosDiff * 2)) : 
                            $datenpos_val2) : 
                        ($wert < 0 ? ($datenpos_val1 - 2 - ($datenPosDiff * 2)) : 
                            $datenpos_val1);
                else
                    $datenValpos_y = ($datenpos_val1 > $datenpos_val2) ?
                        ($wert < 0 ? ($datenpos_val1 - 2 - ($datenPosDiff * 2)) : 
                            $datenpos_val1) : 
                        ($wert < 0 ? ($datenpos_val2 - 2 - ($datenPosDiff * 2)) : 
                            $datenpos_val2);
            break;
            case "middle":
            default:
                $datenValpos_y = ($datenpos_val1 + $datenpos_val2) / 2;
                $datenValpos_y = ($wert > 0) ? $datenValpos_y + $datenPosDiff : 
                    $datenValpos_y - $datenPosDiff;
            }

            $img->Line($datenpos_x, $datenpos_val1, $datenpos_x, $datenpos_val2);
            $img->Line(
                $datenpos_x - $this->breite, $datenpos_val1,
                $datenpos_x + $this->breite, $datenpos_val1
            );
            $img->Line(
                $datenpos_x - $this->breite, $datenpos_val2,
                $datenpos_x + $this->breite, $datenpos_val2
            );
            $this->value->Stroke(
                $img,
                $wert,
                $datenValpos_x, 
                $datenValpos_y
            );
        }
        return TRUE;
    }
    function getProzent($val1, $val2) {
        if ($val1 == 0)
            return 0;
        return (($val2 - $val1) * 100 / $val1);
    }
    function getDifferenz($val1, $val2) {
        return ($val1 - $val2);
    }
}
/*
 * end of class 
 */

function printAbsoluteValue($val) {
    return ((string) abs($val));
}
function printAbsoluteEuroValue($val) {
    return ((string) abs($val)." E");
}
function fehlerkostenGesamt($intern, $extern, $monat, $legende, $legende2, 
        $datei) {
    $graph = new Graph(800, 500, "auto");
    $graph->SetScale("textlin");
    $graph->SetMargin(44, 36, 50, 70);
    $graph->SetMarginColor("#EBEBEB");
    $graph->title->SetFont(FF_VERDANA,FS_BOLD, 9);
    $graph->title->SetMargin(8);
    $graph->title->SetColor("black");
    $graph->title->Set("Fehlerkostenentwicklung der letzten 12 Monate");

    $graph->xaxis->SetFont(FF_VERDANA, FS_BOLD, 7);
    $graph->yaxis->SetFont(FF_VERDANA, FS_BOLD, 7);

    $graph->yaxis->SetLabelMargin(20);
    $graph->xaxis->SetLabelMargin(12);
    $graph->xaxis->HideZeroLabel();
    $graph->xaxis->SetPos("min");
    $graph->xaxis->HideTicks();

    $graph->xaxis->SetTickLabels($legende);

    $graph->xgrid->Show(false, false);
    $graph->ygrid->Show(true, false);
    $graph->ygrid->SetColor("#F4F4F4");
    $graph->ygrid->SetWeight(1); 

    $graph->yaxis->SetLabelAlign("center", "center");

    $graph->yaxis->SetColor("#D5552A");
    $graph->yaxis->HideZeroLabel();
    $graph->yaxis->HideLastTickLabel();
    $graph->yaxis->HideFirstTickLabel();

    $graph->legend->Pos(0.5, 0.97, "center", "bottom");
    $graph->legend->SetLayout(LEGEND_HOR);
    $graph->legend->SetColor("darkblue");
    $graph->legend->SetFont(FF_VERDANA, FS_BOLD, 7);
    $graph->legend->SetFillColor("white");
    $graph->legend->SetShadow(FALSE);

    $line1 = new PlotLine(HORIZONTAL, 0, "#0D6D00", 1);

    $t1 = new Text("intern");
    $t1->SetPos(0.965, 0.28);
    $t1->SetOrientation(270);
    $t1->SetFont(FF_VERDANA, FS_BOLD, 9);

    $t2 = new Text("extern");
    $t2->SetPos(0.965, 0.60);
    $t2->SetOrientation(270);
    $t2->SetFont(FF_VERDANA, FS_BOLD, 9);

    for ($k = 0; $k < count($intern); $k++) {
        for ($z = 0; $z < count($intern[$k]); $z++)
            $gesamtIntern[$z] += $intern[$k][$z];
    }
    for ($k = 0; $k < count($extern); $k++) {
        for ($z = 0; $z < count($extern[$k]); $z++)
            $gesamtExtern[$z] += $extern[$k][$z];
    }

    foreach ($gesamtIntern as $index => $gesamt) {
        $differenzIntern[] = $gesamtIntern[($index - 1)];
        $differenzIntern[] = $gesamtIntern[($index + 1)];
    }
    foreach ($gesamtExtern as $index => $gesamt) {
        $differenzExtern[] = $gesamtExtern[($index - 1)];
        $differenzExtern[] = $gesamtExtern[($index + 1)];
    }
    foreach ($gesamtIntern as $value)
        if ($max < $value)
            $max = $value;
    foreach ($gesamtExtern as $value)
        if ($max < abs($value))
            $max = abs($value);
    foreach ($gesamtIntern as $index => $gesamt)
        if ($index % 2) {
            $gesamtIntern[$index] = ($gesamtIntern[$index - 1] + 
                $gesamtIntern[$index + 1]) / 2;
            $valueIntern[$index] = $gesamtIntern[$index];
        } else
            $valueIntern[$index] = 0;
    foreach ($gesamtExtern as $index => $gesamt)
        if ($index % 2)
            $gesamtExtern[$index] = ($gesamtExtern[$index - 1] + 
                $gesamtExtern[$index + 1]) / 2;

    $max += $max / 20;

    $uline1 = new PlotLine(HORIZONTAL, $max, "#0D6D00", 0);
    $lline1 = new PlotLine(HORIZONTAL, 0 - $max, "#0D6D00", 0);

    $bplot1 = new BarPlot($intern[0]);
    $bplot1->SetFillColor("#E16E15");
    $bplot2 = new BarPlot($intern[1]);
    $bplot2->SetFillColor("#6F39ED");
    $bplot3 = new BarPlot($intern[2]);
    $bplot3->SetFillColor("#18A827");

    $bplot4 = new BarPlot($extern[0]);
    $bplot4->SetFillColor("#E16E15");
    $bplot5 = new BarPlot($extern[1]);
    $bplot5->SetFillColor("#6F39ED");
    $bplot6 = new BarPlot($extern[2]);
    $bplot6->SetFillColor("#18A827");

    $errplot1 = new ErrorPlotValues($differenzIntern, FALSE, FALSE, 4);
    $errplot1->SetColor("gray");
    $errplot1->SetValuePos("top", "right");
    $errplot1->SetWeight(1);
    $errplot1->SetCenter();
    $errplot1->SetValueFont(FF_VERDANA, FS_BOLD, 5);
    $errplot1->value->Show();
    $errplot1->value->HideZero();
    $errplot1->value->SetFormat("%d%%");

    $errplot2 = new ErrorPlotValues($differenzExtern, FALSE, FALSE, 4);
    $errplot2->SetColor("gray");
    $errplot2->SetValuePos("bottom", "right");
    $errplot2->SetWeight(1);
    $errplot2->SetCenter();
    $errplot2->SetValueFont(FF_VERDANA, FS_BOLD, 5);
    $errplot2->value->Show();
    $errplot2->value->HideZero();
    $errplot2->value->SetFormat("%d%%");

    $lineplot1 = new LinePlot($gesamtIntern);
    $lineplot1->SetFillColor("#D5552A@0.8");
    $lineplot1->SetColor("#D5552A");
    $lineplot1->SetWeight(0);

    $lineplot2 = new LinePlot($gesamtExtern);
    $lineplot2->SetFillColor("#769FEB@0.8");
    $lineplot2->SetColor("#769FEB");
    $lineplot2->SetWeight(0);

    $abplot1 = new AccBarPlot(array($bplot1, $bplot2, $bplot3));
    $abplot1->SetWidth(0.4);
    $abplot1->SetAlign("right");
    $abplot1->value->Show();
    $abplot1->value->SetFont(FF_VERDANA, FS_BOLD, 6);
    $abplot1->value->SetColor("black");
    $abplot1->value->SetFormat("%d E");
    $abplot1->value->HideZero();
    $abplot1->value->SetFormatCallback("printAbsoluteEuroValue");

    $abplot2 = new AccBarPlot(array($bplot4, $bplot5, $bplot6));
    $abplot2->SetWidth(0.4);
    $abplot2->value->Show();
    $abplot2->value->SetFont(FF_VERDANA, FS_BOLD, 6);
    $abplot2->value->SetColor("black");
    $abplot2->value->SetFormat("%d E");
    $abplot2->value->HideZero();
    $abplot2->value->SetFormatCallback("printAbsoluteEuroValue");

    $graph->Add($uline1);
    $graph->Add($lline1);
    $graph->Add($lineplot1);
    $graph->Add($lineplot2);
    $graph->Add($abplot1);
    $graph->Add($abplot2);

    $graph->Add($errplot1);
    $graph->Add($errplot2);

    $graph->Add($line1);
    $graph->AddText($t1);
    $graph->AddText($t2);

    $graph->Stroke("include/tmp/jpgraph/{$datei}");
}
?>

Hinterlasse eine Antwort

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind markiert *

Du kannst folgende HTML-Tags benutzen: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>