PHP实现股票趋势图和柱形图
程序员文章站
2022-06-03 16:37:16
基于强大的类库。
基于强大的类库。
<?php /* * 股票趋势图和柱形图 * @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 趋势图/柱形图 默认趋势图 * @param txt 1/other 显示/不显示 提示文字 默认不显示 * @param lang hk/cn 繁体中文/简体中文 默认繁体 * @param id int 股票编号 必填 * @param min int 最小时间 默认无 * @param max int 最大时间 默认无 */ $type = isset($_get['type']) ? $_get['type'] : 'line'; $showtxt = (isset($_get['txt']) && ($_get['txt'] == 1)) ? true : false; //设置语言 if (isset($_get['lang'])) { $lang = $_get['lang'] == 'cn' ? 'cn' : 'hk'; } else { $lang = 'hk'; } $desc_tip = array( 'hk' => array( 'line' => array("昨日收盤價", "股價"), 'bar' => "總成交量:" ), 'cn' => array( 'line' => array("昨日收盘价", "股价"), 'bar' => "总成交量:" ) ); $id = isset($_get['id']) ? (int)$_get['id'] : 1; //股票编码 //条件 $wheres = "where stock_no = ".$id; //最小时间 if (isset($_get['min'])) { $wheres .= " and `created` >= ".(int)$_get['min']; } //最大时间 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(); //股价 $bar = array(); //成交量 $times = array(); //时间 foreach ($results as $keys => $values) : $line2[] = $values['current_price']; $bar[] = $values['volume']; //只显示整点的标签 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; } //昨日收盘价 $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); //设置默认字体 $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); //画格子和标签 $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(); //设置line1为无效,再画底色 $mydata->setseriedrawable("line1",false); //画区域底线 $area_arr = array( "forcetransparency"=>15, //透明度 ); $mypicture->drawareachart($area_arr); //是否显示文字 if ($showtxt) { //标题 $mypicture->drawtext(200,30,$stock_name,array("fontname"=>$ttf_path, "fontsize"=>11,"align"=>text_align_bottommiddle)); //设置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)); //设置柱子的颜色 $mydata->addpoints($times, "times"); $mydata->setseriedescription("times","time"); $mydata->setabscissa("times"); $mydata->setxaxisdisplay(axis_format_time,"h:i"); $mypicture = new pimage(480, 200, $mydata); //设置默认字体 $mypicture->setfontproperties(array("fontname" => "fonts/en_us.ttf", "fontsize"=>6)); $mypicture->antialias = false; $mypicture->setgrapharea(50,20,450,180); //网格及坐标 $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"); ?>
上一篇: 蕨菜是贡菜吗?快了解,别再傻傻分不清了
下一篇: 曹丕娶庶母遗臭万年,他为什么要这么做?