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控制网页过期时间的代码
Sep 28 PHP
php daodb插入、更新与删除数据
Mar 19 PHP
php 输出双引号&quot;与单引号'的方法
May 09 PHP
(PHP实现)只使用++运算实现加法,减法,乘法,除法
Jun 27 PHP
php curl获取网页内容(IPV6下超时)的解决办法
Jul 16 PHP
php获取当前时间的毫秒数的方法
Jan 26 PHP
thinkphp模板继承实例简述
Nov 26 PHP
phpinfo()中Loaded Configuration File(none)的解决方法
Jan 16 PHP
PHP从数组中删除元素的四种方法实例
May 12 PHP
PHP中__set()实例用法和基础讲解
Jul 23 PHP
php实现QQ小程序发送模板消息功能
Sep 18 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/06/08 PHP
PHP中操作ini配置文件的方法
2013/04/25 PHP
php中的常用魔术方法汇总
2016/02/14 PHP
thinkPHP实现MemCache分布式缓存功能
2016/03/23 PHP
PHP中的浅复制与深复制的实例详解
2017/10/26 PHP
php微信开发之音乐回复功能
2018/06/14 PHP
php反序列化长度变化尾部字符串逃逸(0CTF-2016-piapiapia)
2020/02/15 PHP
js 编程笔记 无名函数
2011/06/28 Javascript
js解析xml字符串和xml文档实现原理及代码(针对ie与火狐)
2013/02/02 Javascript
简单谈谈JavaScript的同步与异步
2015/12/31 Javascript
JavaScript的设计模式经典之代理模式
2016/02/24 Javascript
url中的特殊符号有什么含义(推荐)
2016/06/17 Javascript
web打印小结
2017/01/11 Javascript
js阻止移动端页面滚动的两种方法
2017/01/25 Javascript
浅谈JavaScript find 方法不支持IE的问题
2017/09/28 Javascript
微信小程序自定义头部导航栏和导航栏背景图片 navigationStyle问题
2019/07/26 Javascript
Python简单进程锁代码实例
2015/04/27 Python
分享一下Python 开发者节省时间的10个方法
2015/10/02 Python
基于数据归一化以及Python实现方式
2018/07/11 Python
python pygame实现2048游戏
2018/11/20 Python
Python线程障碍对象Barrier原理详解
2019/12/02 Python
Python 实现一个计时器
2020/07/28 Python
python字典通过值反查键的实现(简洁写法)
2020/09/30 Python
HTML5 MiranaVideo播放器 (代码开源)
2010/06/11 HTML / CSS
联想马亚西亚官方网站:Lenovo Malaysia
2018/09/19 全球购物
Ajax实现页面无刷新留言效果
2021/03/24 Javascript
校园新闻广播稿
2014/01/10 职场文书
土木建筑学生自我评价
2014/01/14 职场文书
投标邀请书范文
2014/01/31 职场文书
教师评语大全
2014/04/28 职场文书
环卫工人节活动总结
2014/08/29 职场文书
七夕相亲活动策划方案
2014/08/31 职场文书
小学生国庆65周年演讲稿范文(2篇)
2014/09/21 职场文书
收入及婚姻状况证明
2014/11/20 职场文书
Django如何与Ajax交互
2021/04/29 Python
sql server 累计求和实现代码
2022/02/28 SQL Server