服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - PHP教程 - PHP实现股票趋势图和柱形图

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

2020-09-02 15:14PHP教程网 PHP教程

这篇文章主要介绍了PHP实现股票趋势图和柱形图,本文效果基于pchart类库实现,给出实现代码和效果图,需要的朋友可以参考下

基于强大的pchart类库。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
<?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");
?>

 

延伸 · 阅读

精彩推荐