This article mainly introduces the implementation of the stock trend chart and column chart in PHP. the effect of this article is based on the pchart class library implementation, and provides the implementation code and. if you need it, you can refer to the powerful pchart class library based on.
<? Php/** stock trend chart and column chart * @ author: Skiychan
* @ 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 "; date_default_timezone_set ('Asia/Shanghai '); /** @ param type line/other trend chart/column chart default trend chart * @ param txt 1/other Display/do not show the prompt text is not displayed by default * @ param lang hk/cn traditional Chinese/ simplified Chinese default Traditional * @ param id int stock number required * @ param min int minimum time default no * @ param max int maximum time Mo Do not recognize */$ type = isset ($ _ GET ['type'])? $ _ GET ['type']: 'line'; $ showtxt = (isset ($ _ GET ['txt ']) & ($ _ GET ['txt '] = 1 ))? True: false; // set the language if (isset ($ _ GET ['Lang ']) {$ lang =$ _ GET ['Lang'] = 'cn '? 'Cn': 'HK ';} else {$ lang = 'HK ';} $ desc_tip = array ('HK '=> array ('line' => array ("accept yesterday", "shares "), 'bar' => "total transaction volume:"), 'cn' => array ('line' => array ("yesterday's closing price", "stock price "), 'bar' => "total transaction volume:"); $ id = isset ($ _ GET ['id'])? (Int) $ _ GET ['id']: 1; // stock code // condition $ wheres = "where stock_no = ". $ id; // minimum time if (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"; $ th = $ dbh-> prepare ("SELECT * FROM $ tb_name ". $ wheres); $……-> execute (); $ results = $……> 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 (); // transaction volume $ times = array (); // Time foreach ($ results as $ keys => $ values ): $ line2 [] = $ values ['current _ price']; $ bar [] = $ values ['Volume ']; // only display the entire vertex label if ($ keys % 4 = 0) {$ times [] = $ values ['created '];} else {$ times [] = VOID;} endforeach; $ l2counts = count ($ line2); $ myData = new pData (); // If it is a line chart if ($ type = "line ") {// Obtain the stock name $ stock_th = $ dbh-> prepare ("SELECT 'name' FROM 'tbl _ Stock' WHERE 'code' = {$ id }"); $ stock_something-> execute (); $ stock_info = $ stock_something-> fetch (PDO: FETCH_ASSOC); $ func_name = "zhconversion _". $ lang; // $ stock_name = $ func_name ($ stock_info ['name']); $ stock_name = "XX Company "; // retrieve the most value $ SQL = "SELECT MIN ('current _ price') xiao, MAX ('current _ price') da FROM $ tb_name $ wheres"; f Oreach ($ dbh-> query ($ SQL, PDO: FETCH_ASSOC) as $ row) {$ bottom = (int) $ row ['Xiao']-2; $ top = (int) $ row ['da'] + 2;} // yesterday's closing price $ l1s = array (); for ($ I = 1; $ I <= $ l2counts; $ I ++) {$ l1s [] = 130;} $ myData-> addPoints ($ l1s, "Line1 "); $ myData-> addPoints ($ line2, "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,300, $ myData ); // set the default font $ myPicture-> setFontProperties (array ("FontName" => "fonts/en_us.ttf", "FontSize" => 6 )); // background color // $ Settings = array ("StartR" => 219, "StartG" => 23 1, "StartB" => 139, "EndR" => 1, "EndG" => 138, "EndB" => 68, "Alpha" => 50 ); // $ myPicture-> drawGradientArea (480,300, 40,440,260, DIRECTION_VERTICAL, $ Settings); // draw a grid and tag $ myPicture-> setGraphArea (10 ); $ AxisBoundaries = array (0 => array ("Min" => $ bottom, "Max" => $ top); $ Settings = array ("Mode" => SCALE_MODE_MANUAL, "GridR" => 200, "GridG" => 200, "GridB" => 200, "XMargin" => 0, "YMargin" => 0, // "DrawX Lines "=> false," GridTicks "=> 3, // lattice density" ManualScale "=> $ AxisBoundaries,); $ myPicture-> drawScale ($ Settings ); // draw line/* $ line_arr = array ("ForceColor" => TRUE, "ForceR" => 0, "ForceG" => 0, "ForceB" => 255 ); $ myPicture-> drawLineChart ($ line_arr); */$ myPicture-> drawLineChart (); // you can specify the base color $ myData-> setSerieDrawable ("Line1 ", FALSE); // draw the bottom line of the area $ area_arr = array ("ForceTransparency" => 15, // transparency); $ MyPicture-> drawAreaChart ($ area_arr); // whether to display the text if ($ showtxt) {// Title $ myPicture-> drawText (, 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-> setFontProper Ties (array ("FontName" => $ ttf_path, "FontSize" => 8); $ tips = array ("Style" => LEGEND_NOBORDER, "Mode" => LEGEND_HORIZONTAL, "FontR" => 0, "FontG" => 0, "FontB" => 0,); $ myPicture-> drawLegend (20, 26, $ tips );} // column details} 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"); $ myD Ata-> setAbscissa ("Times"); $ myData-> setXAxisDisplay (AXIS_FORMAT_TIME, "H: I"); $ myPicture = new pImage (480,200, $ myData ); // set the default font $ myPicture-> setFontProperties (array ("FontName" => "fonts/en_us.ttf", "FontSize" => 6 )); $ myPicture-> Antialias = FALSE; $ myPicture-> setGraphArea (50, 20, 450,180); // Mesh and coordinates $ scaleSettings = array ("Mode" => SCALE_MODE_START0, "GridR" => 200, "GridG" => 200, "GridB" => 200); $ myPictu Re-> drawScale ($ scaleSettings);/* $ Palette = array (); for ($ I = 0; $ I <= $ l2counts; $ I ++) {$ Palette [$ I] = array ("R" => 74, "G" => 114, "B" => 178, "Alpha" => 100 );} // $ Palette = array ("0" => array ("R" => 74, "G" => 114, "B" => 178, "Alpha" => 100);/* overwrite the canvas 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," FontR "=> 0," FontG "=> 0," FontB "=> 0 ,); $ myPicture-> setFontProperties (array ("FontName" => $ ttf_path, "FontSize" => 9); $ calls = 0; // initialize foreach ($ bar as $ value) {$ balls + = $ value;} $ myData-> setSerieDescription ("Bar ", $ desc_tip [$ lang] ['bar']. $ balls); $ myPicture-> drawLegend (, 9, $ tips) ;}$ myPicture-> stroke (); // $ myPicture-> autoOutput ("image.png ");/ /Save the log // file_put_contents ("log.txt", json_encode ($ myData). "\ n");?>