PHP實(shí)現(xiàn)股票趨勢(shì)圖和柱形圖
基于強(qiáng)大的pchart類庫。
<?php /* * 股票趨勢(shì)圖和柱形圖 * @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"; date_default_timezone_set('Asia/Shanghai'); /* * @param type line/other 趨勢(shì)圖/柱形圖 默認(rèn)趨勢(shì)圖 * @param txt 1/other 顯示/不顯示 提示文字 默認(rèn)不顯示 * @param lang hk/cn 繁體中文/簡(jiǎn)體中文 默認(rèn)繁體 * @param id int 股票編號(hào) 必填 * @param min int 最小時(shí)間 默認(rèn)無 * @param max int 最大時(shí)間 默認(rèn)無 */ $type = isset($_GET['type']) ? $_GET['type'] : 'line'; $showtxt = (isset($_GET['txt']) && ($_GET['txt'] == 1)) ? true : false; //設(shè)置語言 if (isset($_GET['lang'])) { $lang = $_GET['lang'] == 'cn' ? 'cn' : 'hk'; } else { $lang = 'hk'; } $desc_tip = array( 'hk' => array( 'line' => array("昨日收盤價(jià)", "股價(jià)"), 'bar' => "總成交量:" ), 'cn' => array( 'line' => array("昨日收盤價(jià)", "股價(jià)"), 'bar' => "總成交量:" ) ); $id = isset($_GET['id']) ? (int)$_GET['id'] : 1; //股票編碼 //條件 $wheres = "where stock_no = ".$id; //最小時(shí)間 if (isset($_GET['min'])) { $wheres .= " and `created` >= ".(int)$_GET['min']; } //最大時(shí)間 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"; } //初始化 $line2 = array(); //股價(jià) $bar = array(); //成交量 $times = array(); //時(shí)間 foreach ($results as $keys => $values) : $line2[] = $values['current_price']; $bar[] = $values['volume']; //只顯示整點(diǎn)的標(biāo)簽 if ($keys % 4 == 0) { $times[] = $values['created']; } else { $times[] = VOID; } endforeach; $l2counts = count($line2); $myData = new pData(); //如果是線型圖 if ($type == "line") { //取股票名稱 $stock_sth = $dbh->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 = "某某公司"; //取出最值 $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; } //昨日收盤價(jià) $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); //設(shè)置默認(rèn)字體 $myPicture->setFontProperties(array("FontName" => "fonts/en_us.ttf", "FontSize" => 6)); //背景顏色 //$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); //畫格子和標(biāo)簽 $myPicture->setGraphArea(10, 40, 440, 260); $AxisBoundaries = array(0 => array("Min" => $bottom, "Max" => $top)); $Settings = array( "Mode" => SCALE_MODE_MANUAL, "GridR" => 200, "GridG" => 200, "GridB" => 200, "XMargin" => 0, "YMargin" => 0, //"DrawXLines" => false, "GridTicks" => 3, //格子密度 "ManualScale" => $AxisBoundaries, ); $myPicture->drawScale($Settings); //畫線 /* $line_arr = array( "ForceColor" => TRUE, "ForceR" => 0, "ForceG" => 0, "ForceB" => 255); $myPicture->drawLineChart($line_arr); */ $myPicture->drawLineChart(); //設(shè)置Line1為無效,再畫底色 $myData->setSerieDrawable("Line1",FALSE); //畫區(qū)域底線 $area_arr = array( "ForceTransparency"=>15, //透明度 ); $myPicture->drawAreaChart($area_arr); //是否顯示文字 if ($showtxt) { //標(biāo)題 $myPicture->drawText(200,30,$stock_name,array("FontName"=>$ttf_path, "FontSize"=>11,"Align"=>TEXT_ALIGN_BOTTOMMIDDLE)); //設(shè)置Line1為有效 $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,"FontB"=>0, ); $myPicture->drawLegend(20,26,$tips); } //柱形圖 } else { $myData->addPoints($bar, "Bar"); $myData->setPalette("Bar",array("R"=>51,"G"=>114,"B"=>178)); //設(shè)置柱子的顏色 $myData->addPoints($times, "Times"); $myData->setSerieDescription("Times","Time"); $myData->setAbscissa("Times"); $myData->setXAxisDisplay(AXIS_FORMAT_TIME,"H:i"); $myPicture = new pImage(480, 200, $myData); //設(shè)置默認(rèn)字體 $myPicture->setFontProperties(array("FontName" => "fonts/en_us.ttf", "FontSize"=>6)); $myPicture->Antialias = FALSE; $myPicture->setGraphArea(50,20,450,180); //網(wǎng)格及坐標(biāo) $scaleSettings = array( "Mode" => SCALE_MODE_START0, "GridR"=>200, "GridG"=>200, "GridB"=>200); $myPicture->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)); /* 覆蓋畫板色 $barSetting = array( "OverrideColors"=>$Palette, ); $myPicture->drawBarChart($barSetting); */ $myPicture->drawBarChart(); //是否顯示文字 if ($showtxt) { $tips = array( "Style"=>LEGEND_NOBORDER, "Mode"=>LEGEND_HORIZONTAL, "FontR"=>0,"FontG"=>0,"FontB"=>0, ); $myPicture->setFontProperties(array("FontName" => $ttf_path,"FontSize"=>9)); $alls = 0; //總成交量初始化 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"); //保存日志 //file_put_contents("log.txt", json_encode($myData) . "\n"); ?>
相關(guān)文章
PHP中讓json_encode不自動(dòng)轉(zhuǎn)義斜杠“/”的方法
這篇文章主要給大家分享了PHP中如何讓json_encode不自動(dòng)轉(zhuǎn)義斜杠“/”的兩種方法,文中給出了詳細(xì)的示例代碼,相信對(duì)大家具有一定的參考價(jià)值,需要的朋友們下面來一起看看吧。2017-02-02PHP基于pdo的數(shù)據(jù)庫操作類【可支持mysql、sqlserver及oracle】
這篇文章主要介紹了PHP基于pdo的數(shù)據(jù)庫操作類,可實(shí)現(xiàn)基本的數(shù)據(jù)庫連接、增刪改查、關(guān)閉連接等操作,還支持針對(duì)mysql、sqlserver及oracle等數(shù)據(jù)庫的操作,需要的朋友可以參考下2018-05-05php抽象類和接口知識(shí)點(diǎn)整理總結(jié)
這篇文章主要介紹了php抽象類和接口知識(shí)點(diǎn),整理總結(jié)了php抽象類與接口的概念、原理、操作技巧及相關(guān)使用注意事項(xiàng),需要的朋友可以參考下2019-08-08PHP函數(shù)spl_autoload_register()用法和__autoload()介紹
又是框架沖突導(dǎo)致__autoload()失效,用spl_autoload_register()重構(gòu)一下,問題解決2012-02-02PHP中函數(shù)rand和mt_rand的區(qū)別比較
mt_rand() 比rand() 快四倍,很多老的 libc 的隨機(jī)數(shù)發(fā)生器具有一些不確定和未知的特性而且很慢。PHP 的 rand() 函數(shù)默認(rèn)使用 libc 隨機(jī)數(shù)發(fā)生器。mt_rand() 函數(shù)是非正式用來替換它的。2012-12-12PHP global全局變量經(jīng)典應(yīng)用與注意事項(xiàng)分析【附$GLOBALS用法對(duì)比】
這篇文章主要介紹了PHP global全局變量經(jīng)典應(yīng)用與注意事項(xiàng),結(jié)合實(shí)例形式分析了php中g(shù)lobal全局變量的功能、使用方法及相關(guān)操作注意事項(xiàng),并附帶了$GLOBALS用法作為對(duì)比,需要的朋友可以參考下2019-07-07php構(gòu)造方法中析構(gòu)方法在繼承中的表現(xiàn)
這篇文章主要為大家詳細(xì)介紹了php構(gòu)造方法中析構(gòu)方法在繼承中的表現(xiàn),感興趣的小伙伴們可以參考一下2016-04-04