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


Posted in PHP onFebruary 07, 2015

基于强大的pchart类库。

<?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("昨日收??r", "股?r"),
    '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");
?>
PHP 相关文章推荐
php array_intersect比array_diff快(附详细的使用说明)
Jul 03 PHP
php中常用的预定义变量小结
May 09 PHP
php中的PHP_EOL换行符详细解析
Oct 26 PHP
PHP中4个加速、缓存扩展的区别和选用建议
Mar 12 PHP
PHP中mysqli_affected_rows作用行数返回值分析
Dec 26 PHP
PHP实现多维数组转字符串和多维数组转一维数组的方法
Aug 08 PHP
YII Framework教程之异常处理详解
Mar 14 PHP
注意!PHP 7中不要做的10件事
Sep 18 PHP
PHP让数组中有相同值的组成新的数组实例
Dec 31 PHP
PHP实现财务审核通过后返现金额到客户的功能
Jul 04 PHP
TP5框架实现签到功能的方法分析
Apr 05 PHP
PHP数组与字符串互相转换实例
May 05 PHP
PHP中的闭包(匿名函数)浅析
Feb 07 #PHP
PHP实现自动识别Restful API的返回内容类型
Feb 07 #PHP
PHP curl CURLOPT_RETURNTRANSFER参数的作用使用实例
Feb 07 #PHP
迁移PHP版本到PHP7
Feb 06 #PHP
php include类文件超时问题处理
Feb 06 #PHP
PHP的cURL库简介及使用示例
Feb 06 #PHP
PHP实用函数分享之去除多余的0
Feb 06 #PHP
You might like
PHP提取数据库内容中的图片地址并循环输出
2010/03/21 PHP
PHP中文件上传的一个问题
2010/09/04 PHP
php页面防重复提交方法总结
2013/11/25 PHP
PHP排序算法之归并排序(Merging Sort)实例详解
2018/04/21 PHP
PHP常用工具函数小结【移除XSS攻击、UTF8与GBK编码转换等】
2019/04/27 PHP
js函数般调用正则
2008/04/08 Javascript
如何判断元素是否为HTMLElement元素
2013/12/06 Javascript
node.js中的buffer.slice方法使用说明
2014/12/10 Javascript
jQuery控制Div拖拽效果完整实例分析
2015/04/15 Javascript
AngularJS单选框及多选框实现双向动态绑定
2016/01/13 Javascript
关于vue.extend和vue.component的区别浅析
2017/08/16 Javascript
开发Vue树形组件的示例代码
2017/12/21 Javascript
微信小程序项目实践之验证码倒计时功能
2018/07/18 Javascript
对layui中表单元素的使用详解
2018/08/15 Javascript
layui 对table中的数据进行转义的实例
2019/09/12 Javascript
Python判断文件和文件夹是否存在的方法
2015/05/21 Python
Python多线程下载文件的方法
2015/07/10 Python
Python创建对称矩阵的方法示例【基于numpy模块】
2017/10/12 Python
解决Pycharm下面出现No R interpreter defined的问题
2018/10/29 Python
selenium使用chrome浏览器测试(附chromedriver与chrome的对应关系表)
2018/11/29 Python
python getpass模块用法及实例详解
2019/10/07 Python
python图形开发GUI库pyqt5的详细使用方法及各控件的属性与方法
2020/02/14 Python
python连接mysql有哪些方法
2020/06/24 Python
Python如何爬取b站热门视频并导入Excel
2020/08/10 Python
浅析pandas随机排列与随机抽样
2021/01/22 Python
使用CSS3实现SVG路径描边动画效果入门教程
2019/10/21 HTML / CSS
SmartBuyGlasses美国官网:太阳眼镜和眼镜
2017/08/20 全球购物
澳大利亚领先的孕妇服装品牌:Mamaway
2018/08/14 全球购物
销售人员自我评价怎么写
2013/09/19 职场文书
妈妈的账单教学反思
2014/02/06 职场文书
春节联欢会策划方案
2014/05/16 职场文书
欢迎横幅标语
2014/06/17 职场文书
学校趣味运动会开幕词
2016/03/04 职场文书
MySQL 使用SQL语句修改表名的实现
2021/04/07 MySQL
Python中 range | np.arange | np.linspace三者的区别
2022/03/22 Python
《现实主义勇者的王国再建记》第三弹OST全曲试听片段公开
2022/04/04 日漫