nul
09-02-2005, 15:16
Jungs, ich brauch eure Hilfe; ich hab mir ff. Klasse geschrieben um dynamisch Diagramme erzeugen zu koennen:
<?php
class Diagram {
var $xsize; /* Die Breite des Bildes. */
var $ysize; /* Die Hoehe des Bildes. */
var $spacer; /* Der freie Bereich zwischen Bildrahmen und Diagram. */
var $diagram_heigth; /* Die Hoehe des Diagrams ($ysize-$spacer). */
var $diagram_width; /* Die Breite des Diagrams ($xsize-$spacer). */
var $cell_width; /* Die Breite eines Balken. */
var $val_arr; /* Die Werte fuer das Diagram. */
var $name_arr; /* Die Bezeichnung fuer die Balken. */
var $image; /* Das Bild selber. */
/* Konstruktor. */
function Diagram($x, $y, $sp, $varr, $narr) {
$this->xsize = $x;
$this->ysize = $y;
$this->spacer = $sp;
$this->val_arr = $varr;
$this->name_arr = $narr;
$this->control();
}
/* Kontroll-Methode zur Kontrolle des Bildaufbaus. */
function control() {
$this->calcDiagramValues();
$this->calcValues();
$this->createImage();
}
/* Berechnet die Werte fuer das Diagram. */
function calcDiagramValues() {
$x = $this->xsize - ( 2*$this->spacer );
$y = $this->ysize - ( 2*$this->spacer );
if ( $x < 0 || $y < 0 ) {
echo "Values incorrect; x=$x, y=$y<br>\n";
exit();
} else {
$this->diagram_width = $x;
$this->diagram_heigth = $y;
/* Anzahl der Balken im Diagram. */
$num_values = count($this->val_arr);
if ( $num_values <= 0 ) {
echo "Division by zero value.";
exit();
}
/* Die Breite des Diagrams ohne den Abstand ($spacer/2*2) links und rechts. */
$width = $x - $this->spacer;
/* Der freie Bereich zwischen den Balken. */
$balc = ($num_values - 1) * (1/2) * $this->spacer;
/* Der noch verbleibende Platz fuer die Balken. */
$width = $width - $balc;
/* Zum Schluss wird noch die Breite eines Balkens berechnet. */
$this->cell_width = ($width/$num_values);
if ( $this->cell_width <= 0 ) {
echo "No space for the diagram-items: $this->cell_width.";
exit();
}
}
}
/* Legt den Hintergrund fuer das Bild fest. */
function setImageBackground() {
$bg = imagecolorallocate($this->image, 204, 204, 204);
}
/* Die Linien im Diagram. */
function setDiagramLines() {
$black = imagecolorallocate($this->image,0,0,0); /* Die Farbe fuer die Linien. */
imageline($this->image, $this->spacer, $this->spacer, $this->spacer, $this->ysize-$this->spacer, $black); // verticale Linie
imageline($this->image, $this->spacer, $this->ysize-$this->spacer, $this->xsize-$this->spacer, $this->ysize-$this->spacer, $black); // horizontale Linie
}
/* Zeichnet die Balken ins Diagram ein. */
function setDiagram() {
$colors = array(); /* Array der Farben fuer die Balken. */
$colors[count($colors)] = imagecolorallocate($this->image, 7,0,211);
$colors[count($colors)] = imagecolorallocate($this->image, 255,0,0);
$colors[count($colors)] = imagecolorallocate($this->image, 12, 255, 0);
$space = $this->spacer + (1/2)*$this->spacer;
for ( $counter = 0; $counter < count($this->val_arr); $counter++ ) {
$color = $colors[$counter%count($colors)]; /* Die Farbe fuer den aktuellen Wert/Balken. */
$value = $this->val_arr[$counter]; /* Den Wert fuer den aktuellen Balken. */
$firstPoint = array(); /* Der linke obere Punkt. */
$secondPoint = array(); /* Der rechte untere Punkt. */
$firstPoint[0] = $space;
$firstPoint[1] = $this->ysize - ($this->spacer + $value);
$secondPoint[0] = $space+$this->cell_width;
$secondPoint[1] = $this->ysize - $this->spacer -1;
imagefilledrectangle($this->image, $firstPoint[0], $firstPoint[1], $secondPoint[0], $secondPoint[1], $color);
$space += $this->cell_width + (1/2)*$this->spacer;
}
}
/* Zeigt noch die Bezeichnungen fuer die Balken an. */
function setNames() {
$black = imagecolorallocate($this->image,0,0,0); /* Die Farbe fuer den Text. */
$x = $this->spacer + (1/2)*$this->spacer + $this->cell_width * (1/2);
$y = $this->ysize - $this->spacer*(0.9);
for ( $counter = 0; $counter < count($this->name_arr); $counter++ ) {
$name = $this->name_arr[$counter];
imagestring($this->image, 2, $x-strlen($name), $y, $name, $black);
$x += $this->spacer*(1/2)+$this->cell_width;
}
}
/* Zeichne das Bild. */
function createImage() {
$this->image = imagecreate($this->xsize, $this->ysize);
$this->setImageBackground();
$this->setDiagramLines();
$this->setDiagram();
$this->setNames();
header('Content-type: image/png');
imagepng($this->image);
imagedestroy($this->image);
}
/* Finde den maximalen Wert im Array. */
function findMaxValue() {
$max = 0;
for ( $counter = 0; $counter < count($this->val_arr); $counter++ ) {
$value = $this->val_arr[$counter];
if ( $value > $max ) {
$max = $value;
}
}
return $max;
}
/* Berechne die Werte, damit sie ins Diagramm passen. */
function calcValues() {
$max = $this->findMaxValue();
for ( $counter = 0; $counter < count($this->val_arr); $counter++ ) {
$this->val_arr[$counter] = $this->diagram_heigth * $this->val_arr[$counter]/$max;
}
}
}
?>
Die Klasse funktioniert einwandfrei wenn man die TestSeite wie ff. schreibt:
<?php include("../statistic/Diagram.php");
$arr = array();
$arr[count($arr)] = 4;
$arr[count($arr)] = 2;
$x = 200;
$y = 400;
$s = 15;
$a = array();
$a[0] = "blau";
$a[1] = "rot";
new Diagram($x,$y,$s,$arr,$a);
?>
Wenn ich aber nun einen HTML-Header dazu bringe wie hier:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Main Site</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<?php include("../statistic/Diagram.php");
$arr = array();
$arr[count($arr)] = 4;
$arr[count($arr)] = 2;
$x = 200;
$y = 400;
$s = 15;
$a = array();
$a[0] = "blau";
$a[1] = "rot";
new Diagram($x,$y,$s,$arr,$a);
?>
</body>
</html>
erhalte ich ff. Fehlermeldung:
Warning: Cannot modify header information - headers already sent by (output started at /var/www/include/tests/diaTest2.php:7) in /var/www/include/statistic/Diagram.php on line 130
‰PNG
Wie kann ich ein Bild anzeigen, ohne dass ich eine zweite Datei erzeugen muss, in der die Werte stehn?
Vielleicht hat jemand eine Hilfreiche Idee!!!
mfg nul
<?php
class Diagram {
var $xsize; /* Die Breite des Bildes. */
var $ysize; /* Die Hoehe des Bildes. */
var $spacer; /* Der freie Bereich zwischen Bildrahmen und Diagram. */
var $diagram_heigth; /* Die Hoehe des Diagrams ($ysize-$spacer). */
var $diagram_width; /* Die Breite des Diagrams ($xsize-$spacer). */
var $cell_width; /* Die Breite eines Balken. */
var $val_arr; /* Die Werte fuer das Diagram. */
var $name_arr; /* Die Bezeichnung fuer die Balken. */
var $image; /* Das Bild selber. */
/* Konstruktor. */
function Diagram($x, $y, $sp, $varr, $narr) {
$this->xsize = $x;
$this->ysize = $y;
$this->spacer = $sp;
$this->val_arr = $varr;
$this->name_arr = $narr;
$this->control();
}
/* Kontroll-Methode zur Kontrolle des Bildaufbaus. */
function control() {
$this->calcDiagramValues();
$this->calcValues();
$this->createImage();
}
/* Berechnet die Werte fuer das Diagram. */
function calcDiagramValues() {
$x = $this->xsize - ( 2*$this->spacer );
$y = $this->ysize - ( 2*$this->spacer );
if ( $x < 0 || $y < 0 ) {
echo "Values incorrect; x=$x, y=$y<br>\n";
exit();
} else {
$this->diagram_width = $x;
$this->diagram_heigth = $y;
/* Anzahl der Balken im Diagram. */
$num_values = count($this->val_arr);
if ( $num_values <= 0 ) {
echo "Division by zero value.";
exit();
}
/* Die Breite des Diagrams ohne den Abstand ($spacer/2*2) links und rechts. */
$width = $x - $this->spacer;
/* Der freie Bereich zwischen den Balken. */
$balc = ($num_values - 1) * (1/2) * $this->spacer;
/* Der noch verbleibende Platz fuer die Balken. */
$width = $width - $balc;
/* Zum Schluss wird noch die Breite eines Balkens berechnet. */
$this->cell_width = ($width/$num_values);
if ( $this->cell_width <= 0 ) {
echo "No space for the diagram-items: $this->cell_width.";
exit();
}
}
}
/* Legt den Hintergrund fuer das Bild fest. */
function setImageBackground() {
$bg = imagecolorallocate($this->image, 204, 204, 204);
}
/* Die Linien im Diagram. */
function setDiagramLines() {
$black = imagecolorallocate($this->image,0,0,0); /* Die Farbe fuer die Linien. */
imageline($this->image, $this->spacer, $this->spacer, $this->spacer, $this->ysize-$this->spacer, $black); // verticale Linie
imageline($this->image, $this->spacer, $this->ysize-$this->spacer, $this->xsize-$this->spacer, $this->ysize-$this->spacer, $black); // horizontale Linie
}
/* Zeichnet die Balken ins Diagram ein. */
function setDiagram() {
$colors = array(); /* Array der Farben fuer die Balken. */
$colors[count($colors)] = imagecolorallocate($this->image, 7,0,211);
$colors[count($colors)] = imagecolorallocate($this->image, 255,0,0);
$colors[count($colors)] = imagecolorallocate($this->image, 12, 255, 0);
$space = $this->spacer + (1/2)*$this->spacer;
for ( $counter = 0; $counter < count($this->val_arr); $counter++ ) {
$color = $colors[$counter%count($colors)]; /* Die Farbe fuer den aktuellen Wert/Balken. */
$value = $this->val_arr[$counter]; /* Den Wert fuer den aktuellen Balken. */
$firstPoint = array(); /* Der linke obere Punkt. */
$secondPoint = array(); /* Der rechte untere Punkt. */
$firstPoint[0] = $space;
$firstPoint[1] = $this->ysize - ($this->spacer + $value);
$secondPoint[0] = $space+$this->cell_width;
$secondPoint[1] = $this->ysize - $this->spacer -1;
imagefilledrectangle($this->image, $firstPoint[0], $firstPoint[1], $secondPoint[0], $secondPoint[1], $color);
$space += $this->cell_width + (1/2)*$this->spacer;
}
}
/* Zeigt noch die Bezeichnungen fuer die Balken an. */
function setNames() {
$black = imagecolorallocate($this->image,0,0,0); /* Die Farbe fuer den Text. */
$x = $this->spacer + (1/2)*$this->spacer + $this->cell_width * (1/2);
$y = $this->ysize - $this->spacer*(0.9);
for ( $counter = 0; $counter < count($this->name_arr); $counter++ ) {
$name = $this->name_arr[$counter];
imagestring($this->image, 2, $x-strlen($name), $y, $name, $black);
$x += $this->spacer*(1/2)+$this->cell_width;
}
}
/* Zeichne das Bild. */
function createImage() {
$this->image = imagecreate($this->xsize, $this->ysize);
$this->setImageBackground();
$this->setDiagramLines();
$this->setDiagram();
$this->setNames();
header('Content-type: image/png');
imagepng($this->image);
imagedestroy($this->image);
}
/* Finde den maximalen Wert im Array. */
function findMaxValue() {
$max = 0;
for ( $counter = 0; $counter < count($this->val_arr); $counter++ ) {
$value = $this->val_arr[$counter];
if ( $value > $max ) {
$max = $value;
}
}
return $max;
}
/* Berechne die Werte, damit sie ins Diagramm passen. */
function calcValues() {
$max = $this->findMaxValue();
for ( $counter = 0; $counter < count($this->val_arr); $counter++ ) {
$this->val_arr[$counter] = $this->diagram_heigth * $this->val_arr[$counter]/$max;
}
}
}
?>
Die Klasse funktioniert einwandfrei wenn man die TestSeite wie ff. schreibt:
<?php include("../statistic/Diagram.php");
$arr = array();
$arr[count($arr)] = 4;
$arr[count($arr)] = 2;
$x = 200;
$y = 400;
$s = 15;
$a = array();
$a[0] = "blau";
$a[1] = "rot";
new Diagram($x,$y,$s,$arr,$a);
?>
Wenn ich aber nun einen HTML-Header dazu bringe wie hier:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Main Site</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<?php include("../statistic/Diagram.php");
$arr = array();
$arr[count($arr)] = 4;
$arr[count($arr)] = 2;
$x = 200;
$y = 400;
$s = 15;
$a = array();
$a[0] = "blau";
$a[1] = "rot";
new Diagram($x,$y,$s,$arr,$a);
?>
</body>
</html>
erhalte ich ff. Fehlermeldung:
Warning: Cannot modify header information - headers already sent by (output started at /var/www/include/tests/diaTest2.php:7) in /var/www/include/statistic/Diagram.php on line 130
‰PNG
Wie kann ich ein Bild anzeigen, ohne dass ich eine zweite Datei erzeugen muss, in der die Werte stehn?
Vielleicht hat jemand eine Hilfreiche Idee!!!
mfg nul