This paper mainly introduces the example of PHP plotting stock trend chart and column chart. Through the Pchart class library to draw the stock trend chart and column chart, I hope to have the need for drawing friends to help.
Based on the powerful Pchart class library.
<?php/* * Stock Trend Chart and column chart * @author: Skiychan <developer@zzzzy.com> * @created: 02/05/2015 */include "Libs/pdata.class . php ", include" libs/pdraw.class.php "; include" libs/pimage.class.php "; include" database.php "; include" libs/ convert.php ";d ate_default_timezone_set (' Asia/shanghai ');/* * @param type Line/other trend/Column Chart default trend chart * @param txt 1/other display /do not show prompt text by default * @param lang hk/cn Traditional Chinese/Simplified Chinese default * @param ID int stock code required * @param min int minimum time default no * @param max int max time Default no/$type = Isset ($_get[' type ')? $_get[' type ': ' line '; $showtxt = (isset ($_get[' txt ') && ($_get[' txt '] = = 1))? true:false;//Set Language if (Isset ($_get[' lang ')) {$lang = $_get[' lang '] = = ' cn '? ' CN ': ' HK ';} else {$lang = ' HK ';} $desc _tip = Array (' HK ' = = Array (' line ' = = Array ("Price Yesterday", "stock price"), ' bar ' = ' Total Volume: ' ), ' cn ' = = Array (' line ' = = Array ("Yesterday close", "stock price"), ' bar ' = = ' Total Volume: ')); $id = Isset ($_get[' id ')? (int) $_get[' id ']: 1; Stock code//Condition $wheres = "where Stock_no =". $id;//Minimum timeif (Isset ($_get[' min '))) {$wheres. = "and ' Created ' >=". ( int) $_get[' min '];} Maximum time if (Isset ($_get[' Max ')) {$wheres. = "and ' Created ' <=". int) $_get[' Max ';} $wheres. = "ORDER by created", $sth = $dbh->prepare ("select * from $TB _name". $wheres), $sth->execute (), $results = $sth->fetchall (PDO::FETCH_ASSOC), if ($lang = = ' HK ') {$ttf _path = "Fonts/zh_ HK.TTC ";} else {$ttf _path = "Fonts/zh_cn.ttf";} Initialize $line2 = Array (); Stock price $bar = Array (); Volume $times = Array (); Time foreach ($results as $keys = + $values): $line 2[] = $values [' Current_price ']; $bar [] = $values [' volume ']; Show only the hour of the label if ($keys% = = 0) {$times [] = $values [' Created ']; } else {$times [] = VOID; }endforeach; $l 2counts = count ($line 2), $myData = new PData (),//If line graph if ($type = = "Lines") {//Take stock name $stock _sth = $DBH-&G T;prepare ("Select ' Name ' from ' tbl_stock ' WHERE ' code ' = {$id}"); $stock _sth->execute (); $stock _info = $stock _sth->fetch (PDO::FETCH_ASSOC); $func _name = "Zhconversion_". $lang; $stock _name = $func _name ($stock _info[' name '); $stock _name = "xxx Company";//Check out the maximum $sql = "Select MIN (' Current_price ') Xiao, MAX (' Current_price ') da from $TB _name $wheres"; foreach ($dbh->query ($sql, PDO::FETCH_ASSOC) as $row) {$bottom = (int) $row [' Xiao ']-2; $top = (int) $row [' da '] + 2; }//yesterday close price $l 1s = array (); for ($i = 1; $i <= $l 2counts; $i + +) {$l 1s[] = 130; } $myData->addpoints ($l 1s, "Line1"); $myData->addpoints ($line 2, "Line2"); $myData->setpalette ("Line1", Array ("R" =>51, "G" =>114, "B" =>178)); $myData->setpalette ("Line2", Array ("R" =>0, "G" =>255, "B" =>0)); $myData->setaxisposition (0, axis_position_right); $myData->addpoints ($times, "Times"); $myData->setseriedescription ("Times", "Time"); $myData->setabscissa ("Times"); $myData->setxaxisdisplay (Axis_format_time, "h:i"); $myPicture = new Pimage (480, $myData); Sets the default font $myPicture->setfontproperties (Array ("fontname" = "Fonts/en_us.ttf", "FontsiZe "= 6)); Background color//$Settings = Array ("STARTR" =>219, "STARTG" =>231, "STARTB" =>139, "Endr" =>1, "ENDG" =>138, "endb" = >68, "Alpha" =>50); $myPicture->drawgradientarea (0,0,480,300,direction_vertical, $Settings);//Draw grid and label $myPicture Setgrapharea (10, 40, 440, 260); $AxisBoundaries = Array (0 = = Array ("Min" = = $bottom, "Max" = $top)); $Settings = Array ("Mode" = = scale_mode_manual, "gridr" = +, "GRIDG" and "gridb" = 200, "Xmargin" = 0, "ymargin" = 0,//"Drawxlines" and False, "gridticks" = 3,//Lattice density "manualscale" = > $AxisBoundaries,); $myPicture->drawscale ($Settings);//Draw Line/* $line _arr = Array ("Forcecolor" = TRUE, "forcer" = 0, "Forc EG "+ 0," forceb "= 255); $myPicture->drawlinechart ($line _arr); */$myPicture->drawlinechart (); Set LINE1 to invalid, then draw the background $myData->setseriedrawable ("Line1", FALSE);//Draw area Bottom line $area _arr = Array ("Forcetransparency" =>15,//transparency); $myPicture->drawareachart ($area _arr); Whether to display the text if ($showtxt) {//title $myPicture->drawtext (200,30, $stock _name,array ("fontname" = $ttf _path, "FontSize" =>11, "Align" =>text_align_bottommiddle)); Set Line1 to valid $myData->setseriedrawable ("Line1", TRUE); $myData->setseriedescription ("Line1", $desc _tip[$lang] [' line '][0]); $myData->setseriedescription ("Line2", $desc _tip[$lang] [' line '][1]); $myPicture->setfontproperties (Array ("FontName" + $ttf _path, "FontSize" =>8)); $tips = Array ("Style" =>legend_noborder, "Mode" =>legend_horizontal, "Fontr" =>0, "Fontg" =>0, "Font B "=>0,); $myPicture->drawlegend (20,26, $tips); }//Column Chart} else {$myData->addpoints ($bar, "Bar"); $myData->setpalette ("Bar", Array ("R" =>51, "G" =>114, "B" =>178)); Set the color of the column $myData->addpoints ($times, "Times"); $myData->setseriedescription ("Times", "Time"); $myData->setabscissa ("Times"); $myData->SETXAXISDIsplay (Axis_format_time, "h:i"); $myPicture = new Pimage (480, $myData); Sets the default font $myPicture->setfontproperties (Array ("fontname" = "Fonts/en_us.ttf", "FontSize" =>6)); $myPicture->antialias = FALSE; $myPicture->setgrapharea (50,20,450,180); grid and coordinate $scaleSettings = Array ("Mode" = = scale_mode_start0, "Gridr" =>200, "GRIDG" =>200, "Gridb" => ; 200); $myPicture->drawscale ($scaleSettings); /* $Palette = array (); for ($i = 0; $i <= $l 2counts; $i + +) {$Palette [$i] = Array ("R" =>74, "G" =>114, "B" =>178, "Alpha" =>100); }//$Palette = Array ("0" =>array ("R" =>74, "G" =>114, "B" =>178, "Alpha" =>100)); /* Overlay artboard Color $barSetting = Array ("overridecolors" = = $Palette,); $myPicture->drawbarchart ($barSetting); */$myPicture->drawbarchart (); Whether to display the text if ($showtxt) {$tips = Array ("Style" =>legend_noborder, "Mode" =>legend_horizontal, "Fon TR "=>0," Fontg "=>0," Fontb "=>0,); $myPiCture->setfontproperties (Array ("fontname" = $ttf _path, "FontSize" =>9)); $alls = 0; Total volume initialization of foreach ($bar as $value) {$alls + = $value; } $myData->setseriedescription ("Bar", $desc _tip[$lang] [' Bar ']. $alls); $myPicture->drawlegend (300,9, $tips); }} $myPicture->stroke ();//$myPicture->autooutput ("image.png");//Save Log//file_put_contents ("Log.txt", Json_ Encode ($myData). "\ n");? >
Related recommendations:
PHP image operation class, support to generate thumbnails, add watermarks, upload thumbnails
PHP Image processing Class (easy to use)
Two powerful PHP image processing Classes 1_php Tutorial