PHP object programming implementation 3D pie chart

zhaozj2021-02-08  205

Author: phpstudent

// Common function

/ / Convert angle to radians

Function Deg2arc ($ DegRees) {

Return ($ DegRees * (pi () / 180.0));

}

// RGB

Function GETRGB ($ color) {

$ R = ($ Color >> 16) & 0xFF;

$ G = ($ Color >> 8) & 0xff;

$ B = ($ color) & 0xff;

Return (Array ($ R, $ G, $ B));

}

/ / A value of X, Y point on the ellipse of elliptical heart (0, 0)

Function PIE_POINT ($ DEG, $ VA, $ VB) {

$ x = COS (DEG2ARC ($ DEG)) * $ VA;

$ y = sin (DEG2ARC ($ DEG)) * $ VB;

Return (Array ($ x, $ y);

}

// 3D pie chart

Class Pie3d {

VAR $ a; // Ellipse long half shaft

Var $ b; // Ellipse short half

Var $ dataArray; / / Data for each fan

Var $ colorRay; // Each sector color requires a pencil writing but not 0x in front

/ / For the edge and shadow as black

Function Pie3d ($ pa = 100, $ Pb = 60, $ sdata = "100, 200, 300, 400, 500", $ scolor = "EE00FF, DD0000, CCCCCC, CCFF00 ,00CCFF")

{

$ this-> a = $ pa;

$ this-> b = $ Pb;

$ this-> DataArray = Split (",", $ sdata);

$ this-> Colorarray = Split (",", $ scolor);

}

Function seta ($ V) {

$ this-> a = $ V;

}

Function Geta () {

Return $ this-> a;

}

Function setb ($ V) {

$ this-> b = $ V;

}

Function getb () {

Return $ this-> b;

}

Function setDataArray ($ V) {

$ this-> DataArray = Split (",", $ V);

}

Function GetDataArray ($ V) {

Return $ this-> DataArray;

}

Function setColorarray ($ V) {

$ this-> colorRay = Split (",", $ V);

}

Function getColorarray () {

Return $ this-> colorarray;

}

Function drawpie () {

$ image = imagecreate ($ this-> A * 2 40, $ this-> b * 2 40);

$ PIECENTERX = $ THIS-> A 10;

$ PIECENTERY = $ THIS-> B 10;

$ Doublea = $ this-> a * 2;

$ DoubleB = $ this-> b * 2;

List ($ R, $ G, $ B) = getRGB (0);

$ colorborder = ImageColoralLocate ($ image, $ R, $ G, $ B);

$ Datanumber = count ($ this-> DataArray);

// $ datatotal

For ($ I = 0; $ i <$ datanumber; $ i ) $ datatotal = $ this-> dataArray [$ I]; // Calculate data and // Fill the back

Imagefill ($ image, 0, 0, imagecolorallocate ($ image, 0xff, 0xff, 0xff);

/ *

** draw every sector

* /

$ DegRees = 0;

For ($ I = 0; $ i <$ datanumber; $ i ) {

$ StartDegrees = round ($ degrees);

$ DegRees = ($ this-> DataArray [$ I] / $ datatotal) * 360);

$ ENDDEGREES = Round ($ DegRees);

$ percent = Number_Format ($ this-> DataArray [$ I] / $ datatotal * 100, 1);

List ($ R, $ G, $ B) = GETRGB (HEXDEC ($ this-> Colorarray [$ I]));

$ Currentcolor = ImageColoralLocate ($ image, $ R, $ G, $ B);

IF ($ R> 60 and $ R <256) $ R = $ R-60;

IF ($ G> 60 and $ G <256) $ g = $ g-60;

IF ($ B> 60 and $ B <256) $ b = $ b-60;

$ CurrentdarkColor = ImageColoralLocate ($ image, $ R, $ G, $ B);

// Pavage arc

Imagearc ($ Image, $ PIECECENTERX, $ PIECENTERY, $ DOUBLEA, $ DoubleB, $ Startdegrees, $ ENDDEGREES, $ CURRENTCOLOR

// Painted straight line

List ($ ARCX, $ Arcy) = PIE_POINT ($ Startdegrees, $ this-> A, $ this-> b);

ImageLine ($ IMAGELINE ($ PIECENTERY, FLOOR ($ PIECENTERX $ ARCX), FLOOR ($ Piecentery $ Arcy), $ CurrentColor

// Painted straight line

List ($ ARCX, $ Arcy) = PIE_POINT ($ ENDDEGREES, $ THIS-> A, $ this-> b);

Imageline ($ PIECENTERY, CEIL ($ Piecenterx $ ARCX), CEIL ($ Piecentery $ Arcy), $ CurrentColor

// Fill the fan

$ Midpoint = round (($ ENDDEGREES - $ StartdeGrees / 2) $ startdegrees;

List ($ ARCX, $ Arcy) = PIE_POINT ($ MIDPOINT, $ THIS-> A * 3/4, $ THIS-> B * 3/4);

ImageFillToBorder ($ IMAGE, FLOOR ($ PIECECENTERX $ ARCX), Floor ($ PIECECENTERY $ Arcy), $ CurrentColor, $ CurrentColor;

ImagesTring ($ IMAGE, 2, FLOOR ($ PIECECENTERX $ ARCX-5), Floor ($ Piecentery $ ARCY-5), $ PERCENT. "%", $ colorborder;

// Draw a shadow

IF ($ startDegrees> = 0 and $ startdegrees <= 180) {if ($ ENDDEGREES <= 180) {

For ($ k = 1; $ k <15; $ k )

Imagearc ($ Image, $ Piecenterx, $ PIECENTERY $ K, $ DOUBLEA, $ DoubleB, $ StartdegRees, $ ENDDEGREES, $ CurrentDarkColor;

} else {

For ($ k = 1; $ k <15; $ k )

Imagearc ($ Image, $ Piecenterx, $ Piecentery $ K, $ DOUBLEA, $ DoubleB, $ Startdegreg, 180, $ CurrentDarkColor;

}

}

}

/ * The script has been born with an image.

** Now I need to send it to the browser, and important point is to send the header to the browser and let it know that it is a GIF file. Otherwise, you can only see a bunch of strange garbled

* /

// Output generated picture

Header ("Content-Type: Image / GIF");

Imagegif ($ image);

ImagesDestroy ($ image);

} // end drawpie ()

} // End Class

//achieve

$ objp = new poe3d ();

$ objp-> drawpie ();

?>

转载请注明原文地址:https://www.9cbs.com/read-2993.html

New Post(0)