PHPʵÏÖ¹ÉƱÇ÷ÊÆͼºÍÖùÐÎͼ

5年以前  |  阅读数:235 次  |  编程语言:PHP 

»uÓÚÇ¿´oµÄpchartÀa¿a¡£


    <?php

    /*
     * ¹ÉƱÇ÷ÊÆͼºÍÖuÐÎͼ
     * @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 Ç÷ÊÆͼ/ÖuÐÎͼ ĬÈÏÇ÷ÊÆͼ
     * @param txt 1/other ÏÔʾ/²»ÏÔʾ ÌaʾÎÄ×Ö Ä¬Èϲ»ÏÔʾ
     * @param lang hk/cn *±ÌaÖÐÎÄ/¼oÌaÖÐÎÄ Ä¬ÈÏ*±Ìa
     * @param id int ¹ÉƱ±aºÅ ±ØÌi
     * @param min int ×iСʱ¼a ĬÈÏÎÞ
     * @param max int ×i´oʱ¼a ĬÈÏÎÞ
     */

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

    //ÉeÖÃÓiÑÔ
    if (isset($_GET['lang'])) {
      $lang = $_GET['lang'] == 'cn' ? 'cn' : 'hk';
    } else {
      $lang = 'hk';
    } 

    $desc_tip = array(
      'hk' => array(
        'line' => array("×oÈÕÊÕ±Pƒr", "¹Éƒr"),
        'bar' => "¿‚³É½»Á¿£º"
      ),
      'cn' => array(
        'line' => array("×oÈÕÊÕÅ̼Û", "¹É¼Û"),
        'bar' => "×ܳɽ»Á¿£º"
      )
    );

    $id = isset($_GET['id']) ? (int)$_GET['id'] : 1; //¹ÉƱ±aÂe

    //Ìo¼þ
    $wheres = "where stock_no = ".$id;

    //×iСʱ¼a
    if (isset($_GET['min'])) {
      $wheres .= " and `created` >= ".(int)$_GET['min'];
    }
    //×i´oʱ¼a
    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";
    }

    //³oʼ»¯
    $line2 = array(); //¹É¼Û
    $bar = array(); //³É½»Á¿
    $times = array(); //ʱ¼a

    foreach ($results as $keys => $values) :
      $line2[] = $values['current_price'];
      $bar[] = $values['volume'];

      //Ö»ÏÔʾÕuµaµÄ±eÇ(C)
      if ($keys % 4 == 0) {
        $times[] = $values['created'];
      } else {
        $times[] = VOID;
      }

    endforeach;

    $l2counts = count($line2);

    $myData = new pData();

    //Èç¹uÊÇÏßÐÍͼ
    if ($type == "line") {

    //È¡¹ÉƱÃu³Æ 
      $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 = "ijij¹«Ë¾";

    //È¡³o×iÖµ
      $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;
      }

      //×oÈÕÊÕÅ̼Û
      $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);

      //ÉeÖÃĬÈÏ×ÖÌa
      $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);

    //»­¸ñ×ӺͱeÇ(C)
      $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();

      //ÉeÖÃLine1ΪÎÞЧ,ÔÙ»­µ×É«
      $myData->setSerieDrawable("Line1",FALSE);

    //»­ÇøÓoµ×Ïß
      $area_arr = array(
        "ForceTransparency"=>15, //͸Ã÷¶È
      );
      $myPicture->drawAreaChart($area_arr);

      //ÊÇ*ñÏÔʾÎÄ×Ö
      if ($showtxt) {
        //±eÌa
        $myPicture->drawText(200,30,$stock_name,array("FontName"=>$ttf_path, "FontSize"=>11,"Align"=>TEXT_ALIGN_BOTTOMMIDDLE)); 

        //ÉeÖÃ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);
      }

    //ÖuÐÎͼ
    } else {

      $myData->addPoints($bar, "Bar");
      $myData->setPalette("Bar",array("R"=>51,"G"=>114,"B"=>178)); //ÉeÖÃÖu×ÓµÄÑÕÉ«
      $myData->addPoints($times, "Times");
      $myData->setSerieDescription("Times","Time");
      $myData->setAbscissa("Times");
      $myData->setXAxisDisplay(AXIS_FORMAT_TIME,"H:i");

      $myPicture = new pImage(480, 200, $myData);

      //ÉeÖÃĬÈÏ×ÖÌa
      $myPicture->setFontProperties(array("FontName" => "fonts/en_us.ttf", "FontSize"=>6));
      $myPicture->Antialias = FALSE;

      $myPicture->setGraphArea(50,20,450,180);

      //Íø¸ñ¼°×ø±e
      $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));

      /* ¸²¸Ç»­°aÉ«
      $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; //×ܳɽ»Á¿³oʼ»¯
        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");

    //±£´aeÈÕÖ¾
    //file_put_contents("log.txt", json_encode($myData) . "\n");
    ?>
 相关文章:
PHP分页显示制作详细讲解
SSH 登录失败:Host key verification failed
获取IMSI
将二进制数据转为16进制以便显示
获取IMEI
文件下载
贪吃蛇
双位运算符
PHP自定义函数获取搜索引擎来源关键字的方法
Java生成UUID
发送邮件
年的日历图
提取后缀名
在Zeus Web Server中安装PHP语言支持
让你成为最历害的git提交人
Yii2汉字转拼音类的实例代码
再谈PHP中单双引号的区别详解
指定应用ID以获取对应的应用名称
Python 2与Python 3版本和编码的对比
php封装的page分页类完整实例