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 相关文章推荐
风格模板初级不完全修改教程
Oct 09 PHP
约瑟夫环问题的PHP实现 使用PHP数组内部指针操作函数
Oct 12 PHP
header跳转和include包含问题详解
Sep 08 PHP
php网页病毒清除类
Dec 08 PHP
PHP开发注意事项总结
Feb 04 PHP
PHP+JS实现大规模数据提交的方法
Jul 02 PHP
php $_SESSION会员登录实例分享
Jan 19 PHP
php基于mcrypt_encrypt和mcrypt_decrypt实现字符串加密解密的方法
Jul 12 PHP
PHP基于mssql扩展远程连接MSSQL的简单实现方法
Oct 08 PHP
php与python实现的线程池多线程爬虫功能示例
Oct 12 PHP
ThinkPHP框架实现的邮箱激活功能示例
Jun 15 PHP
php的无刷新操作实现方法分析
Feb 28 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实现小写金额转换大写金额的代码(精确到分)
2012/01/10 PHP
php获取网页标题和内容函数(不包含html标签)
2014/02/03 PHP
destoon首页调用求购供应信息的地区名称的方法
2014/08/21 PHP
PHP利用MySQL保存session的实现思路及示例代码
2014/09/09 PHP
PHP计算数组中值的和与乘积的方法(array_sum与array_product函数)
2016/04/01 PHP
在PHP 7下安装Swoole与Yar,Yaf的方法教程
2017/06/02 PHP
基于jquery的图片懒加载js
2010/06/30 Javascript
jQuery Tools tooltip使用说明
2012/07/14 Javascript
js选取多个或单个元素的实现代码(用class)
2012/08/22 Javascript
Extjs4 关于Store的一些操作(加载/回调/添加)
2013/04/18 Javascript
jquery插件开发注意事项小结
2013/06/04 Javascript
JS实现禁止用户使用Ctrl+鼠标滚轮缩放网页的方法
2017/04/28 Javascript
Angularjs实现页面模板清除的方法
2018/07/20 Javascript
NodeJs实现简单的爬虫功能案例分析
2018/12/05 NodeJs
javascript设计模式 ? 享元模式原理与用法实例分析
2020/04/15 Javascript
JS原型对象操作实例分析
2020/06/06 Javascript
Vue watch响应数据实现方法解析
2020/07/10 Javascript
vue radio单选框,获取当前项(每一项)的value值操作
2020/09/10 Javascript
Vue 打包的静态文件不能直接运行的原因及解决办法
2020/11/19 Vue.js
Python 拷贝对象(深拷贝deepcopy与浅拷贝copy)
2008/09/06 Python
python实现查询苹果手机维修进度
2015/03/16 Python
Python使用cx_Freeze库生成msi格式安装文件的方法
2018/07/10 Python
简单分析python的类变量、实例变量
2019/08/23 Python
python批量替换文件名中的共同字符实例
2020/03/05 Python
pandas按条件筛选数据的实现
2021/02/20 Python
美国著名手表网站:Timepiece
2017/11/15 全球购物
New Balance加拿大官方网站:运动鞋和健身服装
2018/11/19 全球购物
体育教育个人自荐信范文
2013/12/01 职场文书
户籍证明的格式
2014/01/13 职场文书
结婚喜宴家长答谢词
2014/01/15 职场文书
物理系毕业生自荐书范文
2014/02/22 职场文书
80后职场人的职业生涯规划
2014/03/08 职场文书
企业趣味活动方案
2014/08/21 职场文书
离婚协议书怎么写2014
2014/09/30 职场文书
党的群众路线教育实践活动调研报告
2014/11/03 职场文书
CSS实现渐变色边框(Gradient borders)的5种方法
2022/03/25 HTML / CSS