[class] Grafiekjes
Auteur: Rubdos - 14 februari 2009 - 18:11 - Gekeurd door: Koen - Hits: 5993 - Aantal punten: 4.50 (2 stemmen)
Voorbeeldje staat er bij, even uitleggen:
Versie 1 RC 1
class
graph :
-Methods:
addValue($value as array or point) -> value toevoegen, array van point, of point
parsecolor($img as resouce, $color as string) -> hexadecimale kleur naar gd-kleur
parse([$file as string]) -> zonder argumenten wordt de header image/png...
-vars:
$font: spreekt voor zich ;)
$textheight: hoeveel plaats dat de tekst krijgt
$colorPositive: positive waarden worden standaard groen, hexadecimale kleur!
$colorNegative: standaard rood
$width: een voor-zich-spreker
$height: idem...
$values: array met Points
class
point ($point as integer, $name as string) -> $point is het nummer, $name is een titel.
In de zelfde map als de class fontbestand uploaden!
Veel plezier, bugs te vermelden!
php source:
http://scriptso...graph.phps
php script:
http://scriptso....graph.php
Code:
<?php
class graph{
var $font = "ariali.ttf";
var $textheight = 90;
var $colorPositive = "#2FFF00";
var $colorNegative = "#FF0000";
var $width = 400;
var $height = 250;
var $values = array();
function addValue($value){
if(is_array($value)){
for($i=0;$i<count($value);$i++) $this->values[] = $value[$i];
}else{
$values[] = $value;
}
}
function parsecolor($img,$color){
$hex = ereg_replace("#", "", $color);
$color = array();
$color['r'] = hexdec(substr($hex, 0, 2));
$color['g'] = hexdec(substr($hex, 2, 2));
$color['b'] = hexdec(substr($hex, 4, 2));
return imagecolorallocate($img,$color['r'],$color['g'],$color['b']);
}
function parse($file = false){
$width = $this->width;
$height = $this->height;
$img = imagecreate($width,$height);
$bg = imagecolorallocate($img,255,255,255);
$clr = $this->parsecolor($img,$this->colorPositive);
$clrneg = $this->parsecolor($img,$this->colorNegative);
$barwidth = (($width - 40) / count($this->values)) - ( (($width - 40) / count($this->values)) / 4 * 3 );
$space = ( (($width - 40) / count($this->values)) / 4 * 3 );
$graphspace = $height - $this->textheight;
$highest = 1;
$lowest = 0;
foreach($this->values as $key => $value){ //hogerlager?
if($highest < $value->point){
$highest = $value->point;
}
if($lowest > $value->point){
$lowest = $value->point;
}
}
$highest += 1;
$lowest += -1;
$res = ($graphspace / ($highest + abs($lowest)));
$nulpoint = ($highest * $res + abs($lowest * $res)) + $lowest * $res;
$res2 = round($res);
$aantlijntjes = ($highest + abs($lowest));
if($res < 10){
$mod = 10 - $res;
}else{
$mod = 1;
}
for($i = 0;$i <= $aantlijntjes + 1;$i++){ //lijntjes tekenen, met de nummertjes ervoor
if((($i % $mod) == 1) OR ($highest - $i) == 0){
$x1 = 20;
$y1 = ($i * $res);
$x2 = $width - 2;
$y2 = ($i * $res);
imagestring($img,1,1,$y1-2,$highest - $i,$this->parsecolor($img,"#CACACA"));
if(($highest - $i)==0){
imageline($img,$x1,$y1,$x2,$y2, $this->parsecolor($img,"#000000"));
imagestring($img,1,1,$y1-2,$highest - $i,$this->parsecolor($img,"#000000"));
}else{
imageline($img,$x1,$y1,$x2,$y2, $this->parsecolor($img,"#CACACA"));
imagestring($img,1,1,$y1-2,$highest - $i,$this->parsecolor($img,"#CACACA"));
}
}
}
foreach($this->values as $key => $value){
$x1 = ($space / 2 + 40) + ($key * $barwidth) + ($key * $space);
if($value->point>0){
$y1 = $nulpoint - 1;
$currcol = $clr;
}else{
$y1 = $nulpoint + 1;
$currcol = $clrneg;
}
$x2 = ($space / 2 + 40) + ($key * $barwidth) + ($key * $space) + $barwidth;
$y2 = $nulpoint - ($value->point * $res);
imagefilledrectangle($img,$x1,$y1,$x2,$y2,$currcol);
$textpoints = imagettfbbox(12,45,$this->font,$value->name);
$textx = ($x1 + ($barwidth/2)) - ($textpoints[4] - $textpoints[0]);
$texty = ($graphspace + 5) - ($textpoints[5] - $textpoints[1]);
imagettftext($img,12,45,$textx,$texty,$this->parsecolor($img,"#CACACA"),$this->font,$value->name);
}
if($file){
imagepng($img,$file);
}else{
header("content-type: image/png");
imagepng($img);
}
}
}
class point{
var $point;
var $name;
function __construct($point,$name){
$this->point = $point;
$this->name = $name;
}
}
//VOORBEELD:
$gr = new graph();
$gr->addValue( array(new point(-23.5,"Juni"), new point(25,"Juli"), new point(40,"Augustus"), new point(-20,"September"), new point(23,"Oktober"), new point(21,"November") ) );
$gr->parse();
?>
<?php
class graph{
var $font = "ariali.ttf" ;
var $textheight = 90 ;
var $colorPositive = "#2FFF00" ;
var $colorNegative = "#FF0000" ;
var $width = 400 ;
var $height = 250 ;
function addValue( $value ) {
for ( $i = 0 ; $i < count( $value ) ; $i ++ ) $this -> values [ ] = $value [ $i ] ;
} else {
$values [ ] = $value ;
}
}
function parsecolor( $img , $color ) {
return imagecolorallocate( $img , $color [ 'r' ] , $color [ 'g' ] , $color [ 'b' ] ) ;
}
function parse( $file = false ) {
$width = $this -> width ;
$height = $this -> height ;
$img = imagecreate( $width , $height ) ;
$bg = imagecolorallocate( $img , 255 , 255 , 255 ) ;
$clr = $this -> parsecolor ( $img , $this -> colorPositive ) ;
$clrneg = $this -> parsecolor ( $img , $this -> colorNegative ) ;
$barwidth = ( ( $width - 40 ) / count ( $this -> values ) ) - ( ( ( $width - 40 ) / count ( $this -> values ) ) / 4 * 3 ) ; $space = ( ( ( $width - 40 ) / count ( $this -> values ) ) / 4 * 3 ) ; $graphspace = $height - $this -> textheight ;
$highest = 1 ;
$lowest = 0 ;
foreach ( $this -> values as $key => $value ) { //hogerlager?
if ( $highest < $value -> point ) {
$highest = $value -> point ;
}
if ( $lowest > $value -> point ) {
$lowest = $value -> point ;
}
}
$highest += 1 ;
$lowest += - 1 ;
$res = ( $graphspace / ( $highest + abs ( $lowest ) ) ) ; $nulpoint = ( $highest * $res + abs ( $lowest * $res ) ) + $lowest * $res ; $aantlijntjes = ( $highest + abs ( $lowest ) ) ; if ( $res < 10 ) {
$mod = 10 - $res ;
} else {
$mod = 1 ;
}
for ( $i = 0 ; $i <= $aantlijntjes + 1 ; $i ++ ) { //lijntjes tekenen, met de nummertjes ervoor
if ( ( ( $i % $mod ) == 1 ) OR ( $highest - $i ) == 0 ) {
$x1 = 20 ;
$y1 = ( $i * $res ) ;
$x2 = $width - 2 ;
$y2 = ( $i * $res ) ;
imagestring( $img , 1 , 1 , $y1 - 2 , $highest - $i , $this -> parsecolor ( $img , "#CACACA" ) ) ;
if ( ( $highest - $i ) == 0 ) {
imageline( $img , $x1 , $y1 , $x2 , $y2 , $this -> parsecolor ( $img , "#000000" ) ) ;
imagestring( $img , 1 , 1 , $y1 - 2 , $highest - $i , $this -> parsecolor ( $img , "#000000" ) ) ;
} else {
imageline( $img , $x1 , $y1 , $x2 , $y2 , $this -> parsecolor ( $img , "#CACACA" ) ) ;
imagestring( $img , 1 , 1 , $y1 - 2 , $highest - $i , $this -> parsecolor ( $img , "#CACACA" ) ) ;
}
}
}
foreach ( $this -> values as $key => $value ) {
$x1 = ( $space / 2 + 40 ) + ( $key * $barwidth ) + ( $key * $space ) ;
if ( $value -> point > 0 ) {
$y1 = $nulpoint - 1 ;
$currcol = $clr ;
} else {
$y1 = $nulpoint + 1 ;
$currcol = $clrneg ;
}
$x2 = ( $space / 2 + 40 ) + ( $key * $barwidth ) + ( $key * $space ) + $barwidth ;
$y2 = $nulpoint - ( $value -> point * $res ) ;
imagefilledrectangle( $img , $x1 , $y1 , $x2 , $y2 , $currcol ) ;
$textpoints = imagettfbbox( 12 , 45 , $this -> font , $value -> name ) ;
$textx = ( $x1 + ( $barwidth / 2 ) ) - ( $textpoints [ 4 ] - $textpoints [ 0 ] ) ;
$texty = ( $graphspace + 5 ) - ( $textpoints [ 5 ] - $textpoints [ 1 ] ) ;
imagettftext( $img , 12 , 45 , $textx , $texty , $this -> parsecolor ( $img , "#CACACA" ) , $this -> font , $value -> name ) ;
}
if ( $file ) {
imagepng( $img , $file ) ;
} else {
header ( "content-type: image/png" ) ; imagepng( $img ) ;
}
}
}
class point{
var $point ;
var $name ;
function __construct( $point , $name ) {
$this -> point = $point ;
$this -> name = $name ;
}
}
//VOORBEELD:
$gr = new graph( ) ;
$gr -> addValue ( array ( new point
( - 23.5 , "Juni" ) , new point
( 25 , "Juli" ) , new point
( 40 , "Augustus" ) , new point
( - 20 , "September" ) , new point
( 23 , "Oktober" ) , new point
( 21 , "November" ) ) ) ; $gr -> parse ( ) ;
?>
Download code (.txt)
Stemmen
Niet ingelogd.