当前位置:Gxlcms > PHP教程 > PHP实现股票趋势图和柱形图_PHP

PHP实现股票趋势图和柱形图_PHP

时间:2021-07-01 10:21:17 帮助过:77人阅读

基于强大的pchart类库。

  1. <?php
  2. /*
  3. * 股票趋势图和柱形图
  4. * @author: Skiychan <developer@zzzzy.com>
  5. * @created: 02/05/2015
  6. */
  7. include "libs/pData.class.php";
  8. include "libs/pDraw.class.php";
  9. include "libs/pImage.class.php";
  10. include "database.php";
  11. include "libs/convert.php";
  12. date_default_timezone_set('Asia/Shanghai');
  13. /*
  14. * @param type line/other 趋势图/柱形图 默认趋势图
  15. * @param txt 1/other 显示/不显示 提示文字 默认不显示
  16. * @param lang hk/cn 繁体中文/简体中文 默认繁体
  17. * @param id int 股票编号 必填
  18. * @param min int 最小时间 默认无
  19. * @param max int 最大时间 默认无
  20. */
  21. $type = isset($_GET['type']) ? $_GET['type'] : 'line';
  22. $showtxt = (isset($_GET['txt']) && ($_GET['txt'] == 1)) ? true : false;
  23. //设置语言
  24. if (isset($_GET['lang'])) {
  25. $lang = $_GET['lang'] == 'cn' ? 'cn' : 'hk';
  26. } else {
  27. $lang = 'hk';
  28. }
  29. $desc_tip = array(
  30. 'hk' => array(
  31. 'line' => array("昨日收盤價", "股價"),
  32. 'bar' => "總成交量:"
  33. ),
  34. 'cn' => array(
  35. 'line' => array("昨日收盘价", "股价"),
  36. 'bar' => "总成交量:"
  37. )
  38. );
  39. $id = isset($_GET['id']) ? (int)$_GET['id'] : 1; //股票编码
  40. //条件
  41. $wheres = "where stock_no = ".$id;
  42. //最小时间
  43. if (isset($_GET['min'])) {
  44. $wheres .= " and `created` >= ".(int)$_GET['min'];
  45. }
  46. //最大时间
  47. if (isset($_GET['max'])) {
  48. $wheres .= " and `created` <= ".(int)$_GET['max'];
  49. }
  50. $wheres .= " order by created";
  51. $sth = $dbh->prepare("SELECT * FROM $tb_name " . $wheres);
  52. $sth->execute();
  53. $results = $sth->fetchAll(PDO::FETCH_ASSOC);
  54. if ($lang == 'hk') {
  55. $ttf_path = "fonts/zh_hk.ttc";
  56. } else {
  57. $ttf_path = "fonts/zh_cn.ttf";
  58. }
  59. //初始化
  60. $line2 = array(); //股价
  61. $bar = array(); //成交量
  62. $times = array(); //时间
  63. foreach ($results as $keys => $values) :
  64. $line2[] = $values['current_price'];
  65. $bar[] = $values['volume'];
  66. //只显示整点的标签
  67. if ($keys % 4 == 0) {
  68. $times[] = $values['created'];
  69. } else {
  70. $times[] = VOID;
  71. }
  72. endforeach;
  73. $l2counts = count($line2);
  74. $myData = new pData();
  75. //如果是线型图
  76. if ($type == "line") {
  77. //取股票名称
  78. $stock_sth = $dbh->prepare("SELECT `name` FROM `tbl_stock` WHERE `code` = {$id}");
  79. $stock_sth->execute();
  80. $stock_info = $stock_sth->fetch(PDO::FETCH_ASSOC);
  81. $func_name = "zhconversion_".$lang;
  82. //$stock_name = $func_name($stock_info['name']);
  83. $stock_name = "某某公司";
  84. //取出最值
  85. $sql = "SELECT MIN(`current_price`) xiao, MAX(`current_price`) da FROM $tb_name $wheres";
  86. foreach ($dbh->query($sql, PDO::FETCH_ASSOC) as $row) {
  87. $bottom = (int)$row['xiao'] - 2;
  88. $top = (int)$row['da'] + 2;
  89. }
  90. //昨日收盘价
  91. $l1s = array();
  92. for ($i = 1; $i <= $l2counts; $i++) {
  93. $l1s[] = 130;
  94. }
  95. $myData->addPoints($l1s, "Line1");
  96. $myData->addPoints($line2, "Line2");
  97. $myData->setPalette("Line1",array("R"=>51,"G"=>114,"B"=>178));
  98. $myData->setPalette("Line2",array("R"=>0,"G"=>255,"B"=>0));
  99. $myData->setAxisPosition(0, AXIS_POSITION_RIGHT);
  100. $myData->addPoints($times, "Times");
  101. $myData->setSerieDescription("Times","Time");
  102. $myData->setAbscissa("Times");
  103. $myData->setXAxisDisplay(AXIS_FORMAT_TIME,"H:i");
  104. $myPicture = new pImage(480, 300, $myData);
  105. //设置默认字体
  106. $myPicture->setFontProperties(array("FontName" => "fonts/en_us.ttf", "FontSize" => 6));
  107. //背景颜色
  108. //$Settings = array("StartR"=>219, "StartG"=>231, "StartB"=>139, "EndR"=>1, "EndG"=>138, "EndB"=>68, "Alpha"=>50);
  109. //$myPicture->drawGradientArea(0,0,480,300,DIRECTION_VERTICAL,$Settings);
  110. //画格子和标签
  111. $myPicture->setGraphArea(10, 40, 440, 260);
  112. $AxisBoundaries = array(0 => array("Min" => $bottom, "Max" => $top));
  113. $Settings = array(
  114. "Mode" => SCALE_MODE_MANUAL,
  115. "GridR" => 200,
  116. "GridG" => 200,
  117. "GridB" => 200,
  118. "XMargin" => 0,
  119. "YMargin" => 0,
  120. //"DrawXLines" => false,
  121. "GridTicks" => 3, //格子密度
  122. "ManualScale" => $AxisBoundaries,
  123. );
  124. $myPicture->drawScale($Settings);
  125. //画线
  126. /*
  127. $line_arr = array(
  128. "ForceColor" => TRUE,
  129. "ForceR" => 0,
  130. "ForceG" => 0,
  131. "ForceB" => 255);
  132. $myPicture->drawLineChart($line_arr); */
  133. $myPicture->drawLineChart();
  134. //设置Line1为无效,再画底色
  135. $myData->setSerieDrawable("Line1",FALSE);
  136. //画区域底线
  137. $area_arr = array(
  138. "ForceTransparency"=>15, //透明度
  139. );
  140. $myPicture->drawAreaChart($area_arr);
  141. //是否显示文字
  142. if ($showtxt) {
  143. //标题
  144. $myPicture->drawText(200,30,$stock_name,array("FontName"=>$ttf_path, "FontSize"=>11,"Align"=>TEXT_ALIGN_BOTTOMMIDDLE));
  145. //设置Line1为有效
  146. $myData->setSerieDrawable("Line1",TRUE);
  147. $myData->setSerieDescription("Line1",$desc_tip[$lang]['line'][0]);
  148. $myData->setSerieDescription("Line2",$desc_tip[$lang]['line'][1]);
  149. $myPicture->setFontProperties(array("FontName" => $ttf_path,"FontSize"=>8));
  150. $tips = array(
  151. "Style"=>LEGEND_NOBORDER,
  152. "Mode"=>LEGEND_HORIZONTAL,
  153. "FontR"=>0,"FontG"=>0,"FontB"=>0,
  154. );
  155. $myPicture->drawLegend(20,26,$tips);
  156. }
  157. //柱形图
  158. } else {
  159. $myData->addPoints($bar, "Bar");
  160. $myData->setPalette("Bar",array("R"=>51,"G"=>114,"B"=>178)); //设置柱子的颜色
  161. $myData->addPoints($times, "Times");
  162. $myData->setSerieDescription("Times","Time");
  163. $myData->setAbscissa("Times");
  164. $myData->setXAxisDisplay(AXIS_FORMAT_TIME,"H:i");
  165. $myPicture = new pImage(480, 200, $myData);
  166. //设置默认字体
  167. $myPicture->setFontProperties(array("FontName" => "fonts/en_us.ttf", "FontSize"=>6));
  168. $myPicture->Antialias = FALSE;
  169. $myPicture->setGraphArea(50,20,450,180);
  170. //网格及坐标
  171. $scaleSettings = array(
  172. "Mode" => SCALE_MODE_START0,
  173. "GridR"=>200,
  174. "GridG"=>200,
  175. "GridB"=>200);
  176. $myPicture->drawScale($scaleSettings);
  177. /*
  178. $Palette = array();
  179. for ($i = 0; $i <= $l2counts; $i++) {
  180. $Palette[$i] = array("R"=>74,"G"=>114,"B"=>178,"Alpha"=>100);
  181. }
  182. //$Palette = array("0"=>array("R"=>74,"G"=>114,"B"=>178,"Alpha"=>100));
  183. /* 覆盖画板色
  184. $barSetting = array(
  185. "OverrideColors"=>$Palette,
  186. );
  187. $myPicture->drawBarChart($barSetting);
  188. */
  189. $myPicture->drawBarChart();
  190. //是否显示文字
  191. if ($showtxt) {
  192. $tips = array(
  193. "Style"=>LEGEND_NOBORDER,
  194. "Mode"=>LEGEND_HORIZONTAL,
  195. "FontR"=>0,"FontG"=>0,"FontB"=>0,
  196. );
  197. $myPicture->setFontProperties(array("FontName" => $ttf_path,"FontSize"=>9));
  198. $alls = 0; //总成交量初始化
  199. foreach ($bar as $value) {
  200. $alls += $value;
  201. }
  202. $myData->setSerieDescription("Bar", $desc_tip[$lang]['bar'].$alls);
  203. $myPicture->drawLegend(300,9,$tips);
  204. }
  205. }
  206. $myPicture->stroke();
  207. //$myPicture->autoOutput("image.png");
  208. //保存日志
  209. //file_put_contents("log.txt", json_encode($myData) . "\n");
  210. ?></developer@zzzzy.com>

人气教程排行