如何使用 fpdf php 创建双条形图?

2024-03-09

我在我的 php 项目中使用 FPDF。我希望在我的项目中拥有像上图这样的 PDF 版本双条形图。 FPDF 有一种方法可以创建饼图和条形图http://www.fpdf.org/en/script/script28.php http://www.fpdf.org/en/script/script28.php。但这不是我想要的双条形图。有人知道如何在 PHP 中使用 FPDF 创建双条形图吗? 非常感谢 !!!


可能你的意思是“柱状图”

似乎没有创建柱形图的方法,所以我尝试改编现有的柱形图。不幸的是我没有时间进一步开发它。

尝试这个(进行必要的更改):

<?php

require('diag/sector.php');

class PDF_Diag extends PDF_Sector {
    var $legends;
    var $wLegend;
    var $sum;
    var $NbVal;


    function ColumnChart($w, $h, $data, $format, $color=null, $maxVal=0, $nbDiv=4)
    {

        // RGB for color 0
        $colors[0][0] = 155;
        $colors[0][1] = 75;
        $colors[0][2] = 155;

        // RGB for color 1
        $colors[1][0] = 0;
        $colors[1][1] = 155;
        $colors[1][2] = 0;

        // RGB for color 2
        $colors[2][0] = 75;
        $colors[2][1] = 155;
        $colors[2][2] = 255;

        // RGB for color 3
        $colors[3][0] = 75;
        $colors[3][1] = 0;
        $colors[3][2] = 155;

        $this->SetFont('Courier', '', 10);
        $this->SetLegends($data,$format);

        // Starting corner (current page position where the chart has been inserted)
        $XPage = $this->GetX();
        $YPage = $this->GetY();
        $margin = 2; 

        // Y position of the chart
        $YDiag = $YPage + $margin;

        // chart HEIGHT
        $hDiag = floor($h - $margin * 2);

        // X position of the chart
        $XDiag = $XPage + $margin;

        // chart LENGHT
        $lDiag = floor($w - $margin * 3 - $this->wLegend);

        if($color == null)
            $color=array(155,155,155);
        if ($maxVal == 0) 
        {
            foreach($data as $val)
            {
                if(max($val) > $maxVal)
                {
                    $maxVal = max($val);
                }
            }
        }

        // define the distance between the visual reference lines (the lines which cross the chart's internal area and serve as visual reference for the column's heights)
        $valIndRepere = ceil($maxVal / $nbDiv);

        // adjust the maximum value to be plotted (recalculate through the newly calculated distance between the visual reference lines)
        $maxVal = $valIndRepere * $nbDiv;

        // define the distance between the visual reference lines (in milimeters)
        $hRepere = floor($hDiag / $nbDiv);

        // adjust the chart HEIGHT
        $hDiag = $hRepere * $nbDiv;

        // determine the height unit (milimiters/data unit)
        $unit = $hDiag / $maxVal;

        // determine the bar's thickness
        $lBar = floor($lDiag / ($this->NbVal + 1));
        $lDiag = $lBar * ($this->NbVal + 1);
        $eColumn = floor($lBar * 80 / 100);

        // draw the chart border
        $this->SetLineWidth(0.2);
        $this->Rect($XDiag, $YDiag, $lDiag, $hDiag);

        $this->SetFont('Courier', '', 10);
        $this->SetFillColor($color[0],$color[1],$color[2]);
        $i=0;
        foreach($data as $val) 
        {
            //Column
            $yval = $YDiag + $hDiag;
            $xval = $XDiag + ($i + 1) * $lBar - $eColumn/2;
            $lval = floor($eColumn/(count($val)));
            $j=0;
            foreach($val as $v)
            {
                $hval = (int)($v * $unit);
                $this->SetFillColor($colors[$j][0], $colors[$j][1], $colors[$j][2]);
                $this->Rect($xval+($lval*$j), $yval, $lval, -$hval, 'DF');
                $j++;
            }

            //Legend
            $this->SetXY($xval, $yval + $margin);
            $this->Cell($lval, 5, $this->legends[$i],0,0,'C');
            $i++;
        }

        //Scales
        for ($i = 0; $i <= $nbDiv; $i++) 
        {
            $ypos = $YDiag + $hRepere * $i;
            $this->Line($XDiag, $ypos, $XDiag + $lDiag, $ypos);
            $val = ($nbDiv - $i) * $valIndRepere;
            $ypos = $YDiag + $hRepere * $i;
            $xpos = $XDiag - $margin - $this->GetStringWidth($val);
            $this->Text($xpos, $ypos, $val);
        }
    }

    function SetLegends($data, $format)
    {
        $this->legends=array();
        $this->wLegend=0;
        $this->NbVal=count($data);
    }
}


$pdf = new PDF_Diag();
$pdf->AddPage();


$data[0] = array(470, 490, 90);
$data[1] = array(450, 530, 110);
$data[2] = array(420, 580, 100);


// Column chart
$pdf->SetFont('Arial', 'BIU', 12);
$pdf->Cell(210, 5, 'Chart Title', 0, 1, 'C');
$pdf->Ln(8);
$valX = $pdf->GetX();
$valY = $pdf->GetY();
$pdf->ColumnChart(110, 100, $data, null, array(255,175,100));
//$pdf->SetXY($valX, $valY);

$pdf->Output();


?>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使用 fpdf php 创建双条形图? 的相关文章

  • 使用 crypt() 加密

    我目前正在做一个非常安全的登录系统 但我是 crypt 函数的新手 需要一些快速帮助 我在注册过程中使用 crypt 加密密码字符串并将其保存到数据库中 但是 我如何在登录过程中解密密钥 或者我应该怎么做 或者是否可以对提交的密码字符串进行
  • 如何解析cURL返回的header?

    我正在尝试使用 cURL 与 API 进行通信 其中一种方法要求我传递ININ ICWS CSRF Token标题 即WAhtYWxoYXlla1dBY2NvUkRJWCQxZmUxZWFhZS0xZTE0LTQyNGYtYjdhZS0zN
  • 重定向而不是 404 错误页面 - 状态代码不起作用 (Nginx)

    我目前正在迁移到 nginx 服务器 我尝试将其放入名为的 404 ErrorDocument 中404 php 如果我现在尝试访问http mydomain com 404 php 这按预期工作 它将我重定向到 Google 但是一旦我尝
  • TCPDF / FPDI 可以接受 PDF 作为字符串吗?

    是否可以将 TCPDF 或 FPDI PDF 作为字符串提供 我有一个传入的 PDF 数组作为字符串 但无法写入磁盘 我在文档中找不到与此相关的任何内容 如果没有 是否有一种有效的方法来从内存或作为对象存储 读取这些 PDF 将它们喂给 F
  • 在 PHP 中拆分 XML

    我有一个带有根元素和多个项目子元素的合并 xml 像这样的东西
  • 如何在 PHP 中将默认日期设置为波斯日期?

    如何在 PHP 中将默认日期设置为波斯日期 说吧 如果我echo这个功能date Y m d 然后它会显示2018 03 05但我想要1396 12 14波斯日期 请检查 http php net manual en intldatefor
  • $_REQUEST、$_GET、$_POST 哪一个最快?

    这些代码中哪一个会更快 temp REQUEST s or if isset GET s temp GET s else temp POST s REQUEST http php net manual en reserved variabl
  • 在 php 和 mysql 中使用 utf8mb4

    我读过 mysql gt 5 5 3 完全支持每个可能的字符 如果您使用编码utf8mb4对于某个表 列http mathiasbynens be notes mysql utf8mb4 http mathiasbynens be note
  • 如何在 Laravel 5 中的视图模板上显示会话数据

    我正在尝试在 Laravel 5 中的视图模板上显示会话数据 但是它似乎没有显示任何内容 这是我用来设置会话的代码 Session set bookingConfirmed BookingDates where id Session get
  • filter_input() 何时删除 POST 变量的斜杠?

    我创建了一个小型 PHP 脚本 它在 PHP 5 2 17 的服务器上运行magic quotes gpc指令已启用 我没有对 php ini 文件的写访问权限 并且我想从用户输入中删除所有斜杠 即使magic quotes gpc指令被关
  • 使用php插入sql数据库时出错

    我有一个带有 MySQL 插入查询的程序 sql INSERT INTO people person id name username password email salt VALUES person id name username p
  • PHP 和 MySQL 的重音字符错误

    我的问题是 直接通过 PHP 编写的内容是正确重音的 但是当重音单词来自 MySQL 时 字母会像这样 我尝试使用html charset as ISO 8859 1它修复了 MySQL 字母 但破坏了其他字母 解决这一切的一种方法是设置我
  • 如何在 WordPress/WooCommerce 3+ 中向评论表单添加自定义字段

    我正在尝试在产品评论中添加 电话 字段 WooCommerce 3 针对未注册用户 来宾 电话号码只能由管理员在管理面板中看到 电话字段需要填写 Required 我尝试了这段代码 但这不起作用 function true phone nu
  • MySQL 中布尔值的 TINYINT 与 ENUM(0, 1)

    MyISAM 表和 MySQL 5 1 中具有 0 和 1 值的 Tinyint 或 ENUM 0 1 哪个更好 您可以使用BIT 1 如中提到的MySQL 5 1 参考 http dev mysql com doc refman 5 1
  • 在 PHP 中使用数组来比较用户名/密码

    我有以下 php 脚本 其中有一个用户名和密码 Username user1 Password pass1 if isset POST submitform Clean up the input values foreach POST as
  • 使用 mupdf android 库导航到特定页面

    我如何使用 muPDF 库导航到特定页面 或者有没有办法让图书馆不记得我最后在那个pdf文件中浏览的是哪一页 Uri uri Uri parse path Intent intent new Intent MainActivity getC
  • 如何在启用嵌入时间戳和 LTV 的情况下签署 PDF?

    我正在尝试签署启用了时间戳和 LTV 的 pdf 以便它在 Adob e Reader 中显示如下 在英语中 这意味着 签名包含嵌入的时间戳 和 签名启用了 LTV 这是我正在使用的代码 PrivateKey pk get pk from
  • 表单提交后显示 $_FILES['image']

    提交表单后如何显示上传的图片 提交表单后 它将是一个预览页面 因此我不会在 MySQLet 中存储图像类型 BLOB 如何显示 FILES image
  • PHP 中的坏词过滤器?

    我正在用 PHP 编写一个坏词过滤器 我在数组中有一个坏词列表 方法 clean text 的写法如下 public static function cleanse text originalstring if self is sorted
  • 在 PHP 命令行上显示完整的堆栈跟踪

    Problem 我的 PHP 堆栈跟踪缩写为 Stack trace 0 www html table app create php 128 SoapClient gt call call Array 1 www html table ap

随机推荐