Php gd library plot Normal Distribution

Source: Internet
Author: User

<? PHP
Class draw {
Private $ im, $ Stu = false ;//
Protected $ sets = array (// configurable Parameters
"Size" => array (// image size
& Quot; width & quot; = & gt; 900,
"Height" = & gt; 700
),
"Position" => array (// dot position
"X" => 50,
"Y" => 50
),
"Lengths" => array (// X, Y axis length
"X" => 800,
& Quot; Y & quot; = & quot; 600 & quot;
),
"AVR" => array (// X, Y axis scale quantity
"X" => 20,
"Y" => 10
),
"Max" => array (// X, Y maximum value
"X" => 10000,
& Quot; Y & quot; = & quot; 1000 & quot;
),
"Width" => 1, // bar chart width
"Color" => array (// X, Y axis scale quantity
"Shapcolor" => array (0, 0), // bar chart color
"Xline" => array (0, 0), // X axis color
"Xlinek" => array (, 0), // X axis mark
"Xlinew" => array (0, 0), // X-axis text
"Yline" => array (0, 0), // y axis color
"Ylinek" => array (0, 0), // y-axis mark
"Ylinew" => array (0, 0), // X-axis text
"Background" => array (255,255,255) // background color
),

"Words" => array (// distance between text and XY axis
"Down" => 40,
"Left" => 40
),
);

Protected function drawarr ($ ARR) {// retrieves an array and draws a column chart.
If ($ this-> Stu = false ){
$ This-> prepare ();
}
$ Shape1 = $ this-> sets ['color'] ['shapcolor'];
$ Shape = imagecolorallocate ($ this-> im, $ shape1 [0], $ shape1 [1], $ shape1 [2]);
$ X = $ this-> sets ['position'] ['X'];
$ Y = $ this-> sets ['SIZE'] ['height']-$ this-> sets ['position'] ['y'];
$ H = $ this-> sets ['max '] ['y']/$ this-> sets ['lengths'] ['y'];
$ Counts = count ($ ARR );

$ Xlinew1 = $ this-> sets ['color'] ['xline'];
$ Xlinew = imagecolorallocate ($ this-> im, $ xlinew1 [0], $ xlinew1 [1], $ xlinew1 [2]);
For ($ I = 0; $ I <$ counts; $ I ++) {// cyclically draws a bar chart
$ X1 = $ arr [$ I] [0] + $ X;

If ($ arr [$ I] [1]/$ h) <= $ this-> sets ['lengths'] ['y']) {
Imagefilledrectangle ($ this-> im, $ X1, $ y, ($ X1 + $ this-> sets ['width']), ($ Y-$ arr [$ I] [1]/$ H), $ shape );
}
Else {
$ Arr1 = $ this-> sets ['lengths'] ['y'];
Imagefilledrectangle ($ this-> im, $ X1, $ y, ($ X1 + $ this-> sets ['width']), ($ Y-$ arr1 ), $ shape );
// Imagestring ($ this-> im, 2, $ X1, $ Y-$ num, $ arr [$ I] [1], $ ylinew); // write the value out of bounds
Imagestringup ($ this-> im, 2, $ X1, ($ Y-$ arr1), $ arr [$ I] [1], $ xlinew); // numeric value
}

}
}

Protected function done ($ address, $ name) {// output
Imagepng ($ this-> im, $ address. "/". $ name. ". PNG ");
Imagedestroy ($ this-> IM );
Return $ address. "/". $ name. ". PNG ";
}

Private function prepare () {// drawing preparation
$ X = $ this-> sets ['position'] ['X'];
$ Y = $ this-> sets ['SIZE'] ['height']-$ this-> sets ['position'] ['y'];
$ This-> im = imagecreate ($ this-> sets ['SIZE'] ['width'], $ this-> sets ['SIZE'] ['height']);
$ Black1 = $ this-> sets ['color'] ['background'];
$ Black = imagecolorallocate ($ this-> im, $ black1 [0], $ black1 [1], $ black1 [2]);
$ Xline1 = $ shape1 = $ this-> sets ['color'] ['xline'];
$ Xline = imagecolorallocate ($ this-> im, $ xline1 [0], $ xline1 [1], $ xline1 [2]);
$ Yline1 = $ shape1 = $ this-> sets ['color'] ['yline'];
$ Yline = imagecolorallocate ($ this-> im, $ yline1 [0], $ yline1 [1], $ yline1 [2]);
$ Yellow = imagecolorallocate ($ this-> im, 255,255, 0 );
Imageline ($ this-> im, $ X, $ y, ($ x + $ this-> sets ['lengths'] ['X']), $ y, $ xline); // draw the X axis
Imageline ($ this-> im, $ X, $ y, $ X, ($ Y-$ this-> sets ['lengths'] ['y']), $ yline); // draw the Y axis
$ This-> drawy ();
$ This-> drawx ();
$ This-> Stu = true;
}

Private function drawy () {// cyclically draws the Y axis Scale
$ Kedu = (INT) ($ this-> sets ['lengths'] ['y']/$ this-> sets ['avr '] ['y']);
$ Kedu1 = (INT) ($ this-> sets ['max '] ['y']/$ this-> sets ['avr'] ['y']);
$ Ylinew1 = $ this-> sets ['color'] ['ylinew '];
$ Ylinew = imagecolorallocate ($ this-> im, $ ylinew1 [0], $ ylinew1 [1], $ ylinew1 [2]);
$ Ylinek1 = $ this-> sets ['color'] ['ylinek '];
$ Ylinek = imagecolorallocate ($ this-> im, $ ylinek1 [0], $ ylinek1 [1], $ ylinek1 [2]);
$ X = $ this-> sets ['position'] ['X'];
$ X1 = $ this-> sets ['position'] ['X']-$ this-> sets ['word'] ['left'];
$ Y = $ this-> sets ['SIZE'] ['height']-$ this-> sets ['position'] ['y'];
For ($ I = 0; $ I <= $ this-> sets ['avr '] ['y']; $ I ++ ){
$ Num = $ I * $ kedu;
Imagestring ($ this-> im, 2, $ X1, $ Y-$ num, $ kedu1 * $ I, $ ylinew); // Value
Imageline ($ this-> im, $ x + 10, $ Y-$ num, $ X, $ Y-$ num, $ ylinek); // draw a label
}
}

Private function drawx () {// cyclically draws the X axis Scale
$ Kedu = (INT) ($ this-> sets ['lengths'] ['X']/$ this-> sets ['avr '] ['X']);
$ Kedu1 = (INT) ($ this-> sets ['max '] ['X']/$ this-> sets ['avr'] ['X']);
$ Xlinew1 = $ this-> sets ['color'] ['xline'];
$ Xlinew = imagecolorallocate ($ this-> im, $ xlinew1 [0], $ xlinew1 [1], $ xlinew1 [2]);
$ Xlinek1 = $ this-> sets ['color'] ['xlinek '];
$ Xlinek = imagecolorallocate ($ this-> im, $ xlinek1 [0], $ xlinek1 [1], $ xlinek1 [2]);
$ X = $ this-> sets ['position'] ['X'];
$ Y = $ this-> sets ['SIZE'] ['height']-$ this-> sets ['position'] ['y'];
$ Y1 = $ Y + $ this-> sets ['word'] ['low'];

For ($ I = 1; $ I <= $ this-> sets ['avr '] ['X']; $ I ++ ){
$ Num = $ I * $ kedu;
Imagestringup ($ this-> im, 2, $ num + $ X, $ Y1, $ kedu1 * $ I, $ xlinew); // Value
Imageline ($ this-> im, $ x + $ num, $ y-10, $ x + $ num, $ y, $ xlinek); // draw a label
}
}
}
?>

<? PHP
Include 'draw. php ';
Class idraw extends draw {
Private $ arr = array ();
Private $ arr1 = array ();
Private $ arrt = array ();

Function Putin ($ ARR ){
$ Counts = count ($ ARR );
$ AVR = $ this-> sets ['Max'] ['X']/($ this-> sets ['lengths'] ['X']/$ this-> sets ['width']);
For ($ I = 0; $ I <$ counts; $ I ++ ){
$ Mod = (INT) ($ arr [$ I] [0]/$ avr );
If (! In_array ($ mod, $ this-> ARR )){
Array_push ($ this-> arr, $ mod );
Array_push ($ this-> arr1, $ arr [$ I] [1]);
}
Else {
$ Key = array_search ($ mod, $ this-> ARR );
$ This-> arr1 [$ key] = ($ this-> arr1 [$ key] + $ arr [$ I] [1]);
}
}
// Echo $ this-> arr1 [0];
}

Function getout ($ address, $ name ){
$ This-> arrt ();
$ This-> drawarr ($ this-> arrt );
Return $ this-> done ($ address, $ name );
}

Private function arrt (){
$ Counts = count ($ this-> ARR );
For ($ I = 0; $ I <$ counts; $ I ++ ){
$ Arr = array ($ this-> arr [$ I], $ this-> arr1 [$ I]);
Array_push ($ this-> arrt, $ ARR );
}
}

}

TestingCode:

<? PHP

Include 'idraw. php ';

$ Arr = array (
Arrays (130,100 ),
Arrays (131,100 ),
Arrays (465,105 ),
Arrays (9997,200 ),
Arrays (5000,600 ),
Arrays (7000,200 ),
Arrays (6000,600 ),
Arrays (8000,400 ),

);

$ Arr1 = array (
Arrays (130,100 ),
Arrays (8001,900 ),
Array (8500,1800 ),
);


$ Draw1 = new idraw ();
$ Draw1-> Putin ($ ARR );
$ Draw1-> Putin ($ arr1 );
$ Draw1-> getout ("image", "yes1 ");
Echo " ";
/// Home/M/dev/CVS/imrobot5/spider2/newshive/admin
 
 
 
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.