| 5866 |
eleazar |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
namespace LeadersLinked\Library;
|
|
|
4 |
|
|
|
5 |
use Fpdf\Fpdf;
|
|
|
6 |
use Amenadiel\JpGraph\Graph;
|
|
|
7 |
use Amenadiel\JpGraph\Plot;
|
|
|
8 |
|
|
|
9 |
class SurveyReport extends FPDF {
|
|
|
10 |
|
|
|
11 |
/**
|
|
|
12 |
* Header PDF
|
|
|
13 |
*/
|
|
|
14 |
function Header() {
|
|
|
15 |
$this->Image($_SERVER['DOCUMENT_ROOT'] . '/pdf/header_background.png', 10, 8, 190);
|
|
|
16 |
$this->Image($_SERVER['DOCUMENT_ROOT'] . '/pdf/header_logo.png', 130, 20, 60);
|
|
|
17 |
}
|
|
|
18 |
/**
|
|
|
19 |
* Header custom PDF
|
|
|
20 |
* @param string $headerFormName
|
|
|
21 |
* @param string $headerUsername
|
|
|
22 |
*/
|
|
|
23 |
function customHeader($headerFormName, $headerUsername) {
|
|
|
24 |
$s = utf8_decode(' Página: ' . $this->PageNo());
|
|
|
25 |
$this->SetFont('Arial', '', 10);
|
|
|
26 |
$this->SetY(40);
|
|
|
27 |
$this->Cell(190, 10, $s, 0, 0, 'R');
|
|
|
28 |
|
|
|
29 |
$this->SetFont('Arial', 'B', 15);
|
|
|
30 |
$this->SetY(50);
|
|
|
31 |
$this->Cell(180, 10, $headerFormName, 0, 0, 'C');
|
|
|
32 |
$this->setY($this->getY() + 8);
|
|
|
33 |
|
|
|
34 |
|
|
|
35 |
$this->SetFont('Arial', '', 10);
|
|
|
36 |
$this->Cell(180, 10, $headerUsername, 0, 0, 'C');
|
|
|
37 |
$this->setY($this->getY() + 10);
|
|
|
38 |
}
|
|
|
39 |
/**
|
|
|
40 |
* Footer PDF
|
|
|
41 |
*/
|
|
|
42 |
function Footer() {
|
|
|
43 |
$this->SetY(-30);
|
|
|
44 |
$this->Image($_SERVER['DOCUMENT_ROOT'] . '/pdf/footer_background.jpg', 10, $this->getY(), 190);
|
|
|
45 |
}
|
|
|
46 |
/**
|
|
|
47 |
* Create chart PDF
|
|
|
48 |
* @param string[] $labels
|
|
|
49 |
* @param float[] $values
|
|
|
50 |
* @param string $title
|
|
|
51 |
* @param string $filename
|
|
|
52 |
*/
|
| 6039 |
eleazar |
53 |
function pieChart($labels, $values, $title, $filename) {
|
| 5866 |
eleazar |
54 |
// We need some data
|
|
|
55 |
$datay = $values;
|
|
|
56 |
$datax = $labels;
|
|
|
57 |
|
|
|
58 |
// Setup the graph.
|
|
|
59 |
$graph = new Graph\PieGraph(400, 240);
|
|
|
60 |
$graph->clearTheme();
|
|
|
61 |
$graph->img->SetMargin(60, 20, 35, 75);
|
|
|
62 |
$graph->SetScale("textlin");
|
|
|
63 |
$graph->SetShadow();
|
|
|
64 |
|
|
|
65 |
// Set up the title for the graph
|
|
|
66 |
$graph->title->Set($title);
|
|
|
67 |
$graph->title->SetMargin(8);
|
|
|
68 |
$graph->title->SetColor("darkred");
|
|
|
69 |
|
|
|
70 |
// Show 0 label on Y-axis (default is not to show)
|
|
|
71 |
$graph->yscale->ticks->SupressZeroLabel(false);
|
|
|
72 |
|
|
|
73 |
// Setup X-axis labels
|
|
|
74 |
$graph->xaxis->SetTickLabels($datax);
|
|
|
75 |
$graph->xaxis->SetLabelAngle(50);
|
|
|
76 |
|
|
|
77 |
// Create the bar pot
|
|
|
78 |
$bplot = new Plot\PiePlot($datay);
|
| 6095 |
eleazar |
79 |
$bplot->SetLegends($datax);
|
| 5866 |
eleazar |
80 |
|
|
|
81 |
// Setup color for gradient fill style
|
|
|
82 |
// Set color for the frame of each bar
|
|
|
83 |
$bplot->SetColor("white");
|
|
|
84 |
$graph->Add($bplot);
|
|
|
85 |
|
|
|
86 |
|
| 6097 |
eleazar |
87 |
$graph->Stroke();
|
| 5866 |
eleazar |
88 |
}
|
|
|
89 |
|
| 6071 |
eleazar |
90 |
function BarDiagram($w, $h, $data, $format, $color=null, $maxVal=0, $nbDiv=4)
|
|
|
91 |
{
|
|
|
92 |
$this->SetFont('Courier', '', 10);
|
|
|
93 |
$this->SetLegends($data,$format);
|
|
|
94 |
|
|
|
95 |
$XPage = $this->GetX();
|
|
|
96 |
$YPage = $this->GetY();
|
|
|
97 |
$margin = 2;
|
|
|
98 |
$YDiag = $YPage + $margin;
|
|
|
99 |
$hDiag = floor($h - $margin * 2);
|
|
|
100 |
$XDiag = $XPage + $margin * 2 + $this->wLegend;
|
|
|
101 |
$lDiag = floor($w - $margin * 3 - $this->wLegend);
|
|
|
102 |
if($color == null)
|
|
|
103 |
$color=array(155,155,155);
|
|
|
104 |
if ($maxVal == 0) {
|
|
|
105 |
$maxVal = max($data);
|
|
|
106 |
}
|
|
|
107 |
$valIndRepere = ceil($maxVal / $nbDiv);
|
|
|
108 |
$maxVal = $valIndRepere * $nbDiv;
|
|
|
109 |
$lRepere = floor($lDiag / $nbDiv);
|
|
|
110 |
$lDiag = $lRepere * $nbDiv;
|
|
|
111 |
$unit = $lDiag / $maxVal;
|
|
|
112 |
$hBar = floor($hDiag / ($this->NbVal + 1));
|
|
|
113 |
$hDiag = $hBar * ($this->NbVal + 1);
|
|
|
114 |
$eBaton = floor($hBar * 80 / 100);
|
|
|
115 |
|
|
|
116 |
$this->SetLineWidth(0.2);
|
|
|
117 |
$this->Rect($XDiag, $YDiag, $lDiag, $hDiag);
|
|
|
118 |
|
|
|
119 |
$this->SetFont('Courier', '', 10);
|
|
|
120 |
$this->SetFillColor($color[0],$color[1],$color[2]);
|
|
|
121 |
$i=0;
|
|
|
122 |
foreach($data as $val) {
|
|
|
123 |
//Bar
|
|
|
124 |
$xval = $XDiag;
|
|
|
125 |
$lval = (int)($val * $unit);
|
|
|
126 |
$yval = $YDiag + ($i + 1) * $hBar - $eBaton / 2;
|
|
|
127 |
$hval = $eBaton;
|
|
|
128 |
$this->Rect($xval, $yval, $lval, $hval, 'DF');
|
|
|
129 |
//Legend
|
|
|
130 |
$this->SetXY(0, $yval);
|
|
|
131 |
$this->Cell($xval - $margin, $hval, $this->legends[$i],0,0,'R');
|
|
|
132 |
$i++;
|
|
|
133 |
}
|
|
|
134 |
|
|
|
135 |
//Scales
|
|
|
136 |
for ($i = 0; $i <= $nbDiv; $i++) {
|
|
|
137 |
$xpos = $XDiag + $lRepere * $i;
|
|
|
138 |
$this->Line($xpos, $YDiag, $xpos, $YDiag + $hDiag);
|
|
|
139 |
$val = $i * $valIndRepere;
|
|
|
140 |
$xpos = $XDiag + $lRepere * $i - $this->GetStringWidth($val) / 2;
|
|
|
141 |
$ypos = $YDiag + $hDiag - $margin;
|
|
|
142 |
$this->Text($xpos, $ypos, $val);
|
|
|
143 |
}
|
|
|
144 |
}
|
|
|
145 |
|
| 5866 |
eleazar |
146 |
}
|