Fpdf представление в PHP - PullRequest
       12

Fpdf представление в PHP

0 голосов
/ 22 апреля 2020

Я создаю PDF в виде столбчатой ​​диаграммы и хотел бы внести некоторые изменения:

(a) Как ввести значение в верхней части каждого столбца - например, первый столбец при значении оси Y = 1, как записать значение сверху?

(б) Как объединить первые три столбца для представления, такого как "ab c"?

То, что я пробовал до сих пор, приведено ниже в коде, а также на рисунке. Я убедился, что SQL инъекций нет, поскольку страница защищена паролем.

<?php
ob_start();
include_once('fpdf/fpdf.php');

 $db = new PDO("mysql:host=localhost; dbname=db01;charset=utf8",'root','');

$pdf = new PDF();
$pdf->AliasNbPages();
$pdf->AddPage('L','A4',0);
//$pdf->Output();

//$pdf = new FPDF('p', 'mm', 'A4');
//$pdf -> AddPage();

//position
$chartX = 70;
$chartY = 90;

//dimension
$chartWidth = 150;
$chartHeight = 100;

//padding
$chartTopPadding = 10;
$chartLeftPadding = 20;
$chartBottonPadding = 20;
$chartRightPadding = 5;

//chartBox
$chartBoxX = $chartX+$chartLeftPadding;
$chartBoxY = $chartY+$chartTopPadding;
$chartBoxWidth = $chartWidth-$chartLeftPadding-$chartRightPadding;
$chartBoxHeight = $chartHeight - $chartTopPadding - $chartBottonPadding;

//barWidth
$barWidth = 10;

    $id=$_GET['id'];
    $sql = "SELECT * FROM Datas WHERE ID = '$id'";
    $stmt = $db->query($sql);

while($row = $stmt->fetch(PDO::FETCH_OBJ)){

//chartData
$data = Array(
        '1' => [
        'color' => [128,0,0],
        'value' => (int)$row->val1
        ],
        '2' => [
            'color' => [250,128,114],
            'value' => (int)$row->val2
        ],
        '3' => [
        'color' => [250,128,114],
        'value' => (int)$row->val3
        ],
        '4' => [
            'color' => [128,0,0],
            'value' => (int)$row->val4
        ],
        '5' => [
                'color' => [128,0,0],
                'value' => (int)$row->val5
        ],
        '6' => [
            'color' => [250,128,114],
            'value' => (int)$row->val6
        ],
        '7' => [
            'color' => [128,0,0],
            'value' => (int)$row->val7
        ],
        '8' => [
                'color' => [128,0,0],
                'value' => (int)$row->val8
        ],
        '9' => [
            'color' => [250,128,114],
            'value' => (int)$row->val9
        ],
        '10' => [
            'color' => [250,128,114],
            'value' => (int)$row->val10
        ],
        '11' => [
                'color' => [128,0,0],
                'value' => (int)$row->val11
        ],
        '12' => [
            'color' => [250,128,114],
            'value' => (int)$row->val12
        ],
    );
}

    //data max
    $dataMax = 0;
    foreach($data as $item){
        if($item['value']>$dataMax)$dataMax = $item['value'];
    }

    //data step - values on yaxis
    $dataStep = 2;

    //set font, line width and color
    $pdf->SetFont('Arial', '', 9);
    $pdf->SetLineWidth(0.2);
    $pdf->SetDrawColor(0);

    //chart boundary
    $pdf->Rect($chartX, $chartY, $chartWidth, $chartHeight);

    $pdf->Line(
            $chartBoxX,
            $chartBoxY,
            $chartBoxX,
            $chartBoxY+$chartBoxHeight
            );

    $pdf->Line(
            $chartBoxX,
            $chartBoxY+$chartBoxHeight,
            $chartBoxX+$chartBoxWidth,
            $chartBoxY+$chartBoxHeight
            );

    //calculating chart's y axis scale
    $yAxisUnits = $chartBoxHeight / $dataMax;

    //draw the vertical axis labels
    for($i=0; $i<=$dataMax; $i+=$dataStep){
        //y position
        $yAxisPos = $chartBoxY + ($yAxisUnits * $i);

        //draw y axis Lines

        $pdf->Line(
            $chartBoxX-2,
            $yAxisPos,
            $chartBoxX,
            $yAxisPos
            );

            //set cell pos y axis labels
            $pdf->SetXY($chartBoxX - $chartLeftPadding, $yAxisPos - 2);

            //write Labels
            $pdf->Cell($chartLeftPadding-4, 5, $dataMax-$i, 0, 0, 'R');
    }


    //horizontal axis
    //set cell position

    $pdf->SetXY($chartBoxX, $chartBoxY+$chartBoxHeight);

    //cell's width
    $xLabelWidth = $chartBoxWidth / count($data);

    //loop horizontal axis and draw the bars
    $barXPos = 0;

    foreach($data as $itemName => $item){

        //print the label
        $pdf -> Cell($xLabelWidth, 5, $itemName, 0, 0, 'C');

        //drawing the bar
        //bar color
        $pdf ->SetFillColor($item['color'][0], $item['color'][1], $item['color'][2]);
        //bar height
        $barHeight = $yAxisUnits * $item['value'];
        //bar x position
        $barX = ($xLabelWidth/2)+($xLabelWidth*$barXPos);
        $barX = $barX-($barWidth/2);
        $barX = $barX+$chartBoxX;
        //bar y position
        $barY = $chartBoxHeight - $barHeight;
        $barY = $barY + $chartBoxY;
        //draw the bar
        $pdf -> Rect($barX, $barY, $barWidth, $barHeight, 'DF');

        //increment $barXPos
        $barXPos++;
    }

$id=$_GET['id'];


$pdf->Output("D", "$id.pdf");
    ?>

enter image description here

...