Partage d'exemples de dessin de graphiques de tendance boursière et d'histogrammes avec PHP

*文
Libérer: 2023-03-18 12:06:02
original
1800 Les gens l'ont consulté

Cet article présente principalement des exemples de dessin de graphiques de tendance boursière et d'histogrammes avec PHP. Le dessin de graphiques de tendances boursières et de graphiques à colonnes est implémenté via la bibliothèque de classes pchart. J'espère que cela sera utile aux amis qui ont besoin de dessiner.

Basé sur la puissante bibliothèque de classes 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(&#39;Asia/Shanghai&#39;);

/*
 * @param type line/other 趋势图/柱形图 默认趋势图
 * @param txt 1/other 显示/不显示 提示文字 默认不显示
 * @param lang hk/cn 繁体中文/简体中文 默认繁体
 * @param id int 股票编号 必填
 * @param min int 最小时间 默认无
 * @param max int 最大时间 默认无
 */

$type = isset($_GET[&#39;type&#39;]) ? $_GET[&#39;type&#39;] : &#39;line&#39;;
$showtxt = (isset($_GET[&#39;txt&#39;]) && ($_GET[&#39;txt&#39;] == 1)) ? true : false;

//设置语言
if (isset($_GET[&#39;lang&#39;])) {
  $lang = $_GET[&#39;lang&#39;] == &#39;cn&#39; ? &#39;cn&#39; : &#39;hk&#39;;
} else {
  $lang = &#39;hk&#39;;
} 

$desc_tip = array(
  &#39;hk&#39; => array(
    &#39;line&#39; => array("昨日收盤價", "股價"),
    &#39;bar&#39; => "總成交量:"
  ),
  &#39;cn&#39; => array(
    &#39;line&#39; => array("昨日收盘价", "股价"),
    &#39;bar&#39; => "总成交量:"
  )
);

$id = isset($_GET[&#39;id&#39;]) ? (int)$_GET[&#39;id&#39;] : 1; //股票编码

//条件
$wheres = "where stock_no = ".$id;

//最小时间
if (isset($_GET[&#39;min&#39;])) {
  $wheres .= " and `created` >= ".(int)$_GET[&#39;min&#39;];
}
//最大时间
if (isset($_GET[&#39;max&#39;])) {
  $wheres .= " and `created` <= ".(int)$_GET[&#39;max&#39;];
}
$wheres .= " order by created";

$sth = $dbh->prepare("SELECT * FROM $tb_name " . $wheres);
$sth->execute();
$results = $sth->fetchAll(PDO::FETCH_ASSOC);

if ($lang == &#39;hk&#39;) {
  $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[&#39;current_price&#39;];
  $bar[] = $values[&#39;volume&#39;];

  //只显示整点的标签
  if ($keys % 4 == 0) {
    $times[] = $values[&#39;created&#39;];
  } 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[&#39;name&#39;]);
  $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[&#39;xiao&#39;] - 2;
    $top = (int)$row[&#39;da&#39;] + 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][&#39;line&#39;][0]);
    $myData->setSerieDescription("Line2",$desc_tip[$lang][&#39;line&#39;][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][&#39;bar&#39;].$alls);
    
    $myPicture->drawLegend(300,9,$tips);
  }

}

$myPicture->stroke();
//$myPicture->autoOutput("image.png");

//保存日志
//file_put_contents("log.txt", json_encode($myData) . "\n");
?>
Copier après la connexion

Recommandations associées :

Classe de manipulation d'images PHP, prend en charge la génération de vignettes, l'ajout d'un filigrane , téléchargez la vignette

classe de traitement d'image php (simple et facile à utiliser)

Deux puissantes classes de traitement d'images php 1_Tutoriel PHP

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal