Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

859 lines
25KB

  1. <?php
  2. // mPDF 4.5.009
  3. define("FF_USERFONT", 15); // See jpgraph_ttf.inc.php for font IDs
  4. global $JpgUseSVGFormat;
  5. $JpgUseSVGFormat = true;
  6. //======================================================================================================
  7. // DELETE OLD GRAPH FILES FIRST - Housekeeping
  8. // First clear any files in directory that are >1 hrs old
  9. $interval = 3600;
  10. if ($handle = opendir(_MPDF_PATH . 'graph_cache')) {
  11. while (false !== ($file = readdir($handle))) {
  12. if (((filemtime(_MPDF_PATH . 'graph_cache/' . $file) + $interval) < time()) && ($file != "..") && ($file != ".")) {
  13. @unlink(_MPDF_PATH . 'graph_cache/' . $file); // mPDF 4.0
  14. }
  15. }
  16. closedir($handle);
  17. }
  18. //==============================================================================================================
  19. // LOAD GRAPHS
  20. include_once(_JPGRAPH_PATH . 'jpgraph.php');
  21. include_once(_JPGRAPH_PATH . 'jpgraph_line.php' );
  22. include_once(_JPGRAPH_PATH . 'jpgraph_log.php' );
  23. include_once(_JPGRAPH_PATH . 'jpgraph_scatter.php' );
  24. include_once(_JPGRAPH_PATH . 'jpgraph_regstat.php' );
  25. include_once(_JPGRAPH_PATH . 'jpgraph_pie.php');
  26. include_once(_JPGRAPH_PATH . 'jpgraph_pie3d.php');
  27. include_once(_JPGRAPH_PATH . 'jpgraph_bar.php');
  28. include_once(_JPGRAPH_PATH . 'jpgraph_radar.php');
  29. require_once __DIR__ . '/MpdfException.php';
  30. function print_graph($g, $pgwidth)
  31. {
  32. $splines = false;
  33. $bandw = false;
  34. $percent = false;
  35. $show_percent = false;
  36. $stacked = false;
  37. $h = false;
  38. $show_values = false;
  39. $hide_grid = false;
  40. $hide_y_axis = false;
  41. if (isset($g['attr']['TYPE']) && $g['attr']['TYPE']) {
  42. $type = strtolower($g['attr']['TYPE']);
  43. }
  44. if (!in_array($type, array('bar', 'horiz_bar', 'line', 'radar', 'pie', 'pie3d', 'xy', 'scatter'))) {
  45. $type = 'bar';
  46. } // Default=bar
  47. if (isset($g['attr']['STACKED']) && $g['attr']['STACKED']) {
  48. $stacked = true;
  49. } // stacked for bar or horiz_bar
  50. if (isset($g['attr']['SPLINES']) && $g['attr']['SPLINES'] && $type == 'xy') {
  51. $splines = true;
  52. } // splines for XY line graphs
  53. if (isset($g['attr']['BANDW']) && $g['attr']['BANDW']) {
  54. $bandw = true;
  55. } // black and white
  56. if (isset($g['attr']['LEGEND-OVERLAP']) && $g['attr']['LEGEND-OVERLAP']) {
  57. $overlap = true;
  58. } // avoid overlap of Legends over graph (line, bar, horiz_bar only)
  59. if (isset($g['attr']['PERCENT']) && $g['attr']['PERCENT'] && $type != 'xy' && $type != 'scatter') {
  60. $percent = true;
  61. } // Show data series as percent of total in series
  62. if (isset($g['attr']['SHOW-VALUES']) && $g['attr']['SHOW-VALUES']) {
  63. $show_values = true;
  64. } // Show the individual data values
  65. if (isset($g['attr']['HIDE-GRID']) && $g['attr']['HIDE-GRID']) {
  66. $hide_grid = true;
  67. } // Hide the y-axis gridlines
  68. if (isset($g['attr']['HIDE-Y-AXIS']) && $g['attr']['HIDE-Y-AXIS']) {
  69. $hide_y_axis = true;
  70. } // Hide the y-axis
  71. // Antialias: If true - better quality curves, but graph line will only be 1px even in PDF 300dpi
  72. // default=true for most except line and radar
  73. if (isset($g['attr']['ANTIALIAS']) && ($g['attr']['ANTIALIAS'] == '' || $g['attr']['ANTIALIAS'] == 0)) {
  74. $antialias = false;
  75. } else if (isset($g['attr']['ANTIALIAS']) && $g['attr']['ANTIALIAS'] > 0) {
  76. $antialias = true;
  77. } else if ($type == 'line' || $type == 'radar') {
  78. $antialias = false;
  79. } else {
  80. $antialias = true;
  81. }
  82. if ($g['attr']['DPI']) {
  83. $dpi = intval($g['attr']['DPI']);
  84. }
  85. if (!$dpi || $dpi < 50 || $dpi > 2400) {
  86. $dpi = 150;
  87. } // Default dpi 150
  88. $k = (0.2645 / 25.4 * $dpi);
  89. // mPDF 4.5.009
  90. global $JpgUseSVGFormat;
  91. if (isset($JpgUseSVGFormat) && $JpgUseSVGFormat) {
  92. $img_type = 'svg';
  93. $k = 1; // Overrides as Vector scale does not need DPI
  94. } else {
  95. $img_type = 'png';
  96. }
  97. if (isset($g['attr']['TITLE']) && $g['attr']['TITLE']) {
  98. $title = $g['attr']['TITLE'];
  99. }
  100. if (isset($g['attr']['LABEL-X']) && $g['attr']['LABEL-X']) {
  101. $xlabel = $g['attr']['LABEL-X'];
  102. } // NOT IMPLEMENTED??????
  103. if (isset($g['attr']['LABEL-Y']) && $g['attr']['LABEL-Y']) {
  104. $ylabel = $g['attr']['LABEL-Y'];
  105. }
  106. if (isset($g['attr']['AXIS-X']) && $g['attr']['AXIS-X']) {
  107. $xaxis = strtolower($g['attr']['AXIS-X']);
  108. }
  109. if (!in_array($xaxis, array('text', 'lin', 'linear', 'log'))) {
  110. $xaxis = 'text';
  111. } // Default=text
  112. if ($xaxis == 'linear') {
  113. $xaxis = 'lin';
  114. }
  115. if (isset($g['attr']['AXIS-Y']) && $g['attr']['AXIS-Y']) {
  116. $yaxis = strtolower($g['attr']['AXIS-Y']);
  117. }
  118. if (!in_array($yaxis, array('lin', 'linear', 'log', 'percent'))) {
  119. $yaxis = 'lin';
  120. } // Default=lin
  121. if ($yaxis == 'percent') {
  122. $show_percent = true;
  123. $yaxis = 'lin';
  124. } // Show percent sign on scales
  125. if ($yaxis == 'linear') {
  126. $yaxis = 'lin';
  127. }
  128. if ($splines) {
  129. $xaxis = 'lin';
  130. }
  131. $axes = $xaxis . $yaxis; // e.g.textlin, textlog, loglog, loglin, linlog (XY)
  132. // mPDF 4.0
  133. if (isset($g['attr']['cWIDTH']) && $g['attr']['cWIDTH']) {
  134. $w = ($g['attr']['cWIDTH'] / 0.2645);
  135. } // pixels
  136. if (isset($g['attr']['cHEIGHT']) && $g['attr']['cHEIGHT']) {
  137. $h = ($g['attr']['cHEIGHT'] / 0.2645);
  138. }
  139. if (isset($g['attr']['SERIES']) && strtolower($g['attr']['SERIES']) == 'rows') {
  140. $dataseries = 'rows';
  141. } else {
  142. $dataseries = 'cols';
  143. }
  144. // Defaults - define data
  145. $rowbegin = 2;
  146. $colbegin = 2;
  147. if ($type == 'scatter' || $type == 'xy') {
  148. if ($dataseries == 'rows') {
  149. $rowbegin = 1;
  150. } else {
  151. $colbegin = 1;
  152. }
  153. }
  154. $rowend = 0;
  155. $colend = 0;
  156. if (isset($g['attr']['DATA-ROW-BEGIN']) && ($g['attr']['DATA-ROW-BEGIN'] === '0' || $g['attr']['DATA-ROW-BEGIN'] > 0)) {
  157. $rowbegin = $g['attr']['DATA-ROW-BEGIN'];
  158. }
  159. if (isset($g['attr']['DATA-COL-BEGIN']) && ($g['attr']['DATA-COL-BEGIN'] === '0' || $g['attr']['DATA-COL-BEGIN'] > 0)) {
  160. $colbegin = $g['attr']['DATA-COL-BEGIN'];
  161. }
  162. if (isset($g['attr']['DATA-ROW-END']) && ($g['attr']['DATA-ROW-END'] === '0' || $g['attr']['DATA-ROW-END'] <> 0)) {
  163. $rowend = $g['attr']['DATA-ROW-END'];
  164. }
  165. if (isset($g['attr']['DATA-COL-END']) && ($g['attr']['DATA-COL-END'] === '0' || $g['attr']['DATA-COL-END'] <> 0)) {
  166. $colend = $g['attr']['DATA-COL-END'];
  167. }
  168. $nr = count($g['data']);
  169. $nc = 0;
  170. foreach ($g['data'] AS $r) {
  171. $cc = 0;
  172. foreach ($r AS $c) {
  173. $cc++;
  174. }
  175. $nc = max($nc, $cc);
  176. }
  177. if ($colend == 0) {
  178. $colend = $nc;
  179. } else if ($colend < 0) {
  180. $colend = $nc + $colend;
  181. }
  182. if ($rowend == 0) {
  183. $rowend = $nr;
  184. } else if ($rowend < 0) {
  185. $rowend = $nr + $rowend;
  186. }
  187. if ($colend < $colbegin) {
  188. $colend = $colbegin;
  189. }
  190. if ($rowend < $rowbegin) {
  191. $rowend = $rowbegin;
  192. }
  193. // if ($type == 'xy' || $type=='scatter') { $colstart=0; }
  194. // Get Data + Totals
  195. $data = array();
  196. $totals = array();
  197. for ($r = ($rowbegin - 1); $r < $rowend; $r++) {
  198. for ($c = ($colbegin - 1); $c < $colend; $c++) {
  199. if (isset($g['data'][$r][$c])) {
  200. $g['data'][$r][$c] = floatval($g['data'][$r][$c]);
  201. } else {
  202. $g['data'][$r][$c] = 0;
  203. }
  204. if ($dataseries == 'rows') {
  205. $data[($r + 1 - $rowbegin)][($c + 1 - $colbegin)] = $g['data'][$r][$c];
  206. $totals[($r + 1 - $rowbegin)] += $g['data'][$r][$c];
  207. } else {
  208. $data[($c + 1 - $colbegin)][($r + 1 - $rowbegin)] = $g['data'][$r][$c];
  209. if (isset($totals[($c + 1 - $colbegin)])) {
  210. $totals[($c + 1 - $colbegin)] += $g['data'][$r][$c];
  211. } else {
  212. $totals[($c + 1 - $colbegin)] = $g['data'][$r][$c];
  213. }
  214. }
  215. }
  216. }
  217. // PERCENT
  218. if ($percent && $type != 'pie' && $type != 'pie3d') {
  219. for ($r = 0; $r < count($data); $r++) {
  220. for ($c = 0; $c < count($data[$r]); $c++) {
  221. $data[$r][$c] = $data[$r][$c] / $totals[$r] * 100;
  222. }
  223. }
  224. }
  225. // Get Legends and labels
  226. $legends = array();
  227. $labels = array();
  228. $longestlegend = 0;
  229. $longestlabel = 0;
  230. if ($dataseries == 'cols') {
  231. if ($colbegin > 1) {
  232. for ($r = ($rowbegin - 1); $r < $rowend; $r++) {
  233. $legends[($r + 1 - $rowbegin)] = $g['data'][$r][0];
  234. $longestlegend = max($longestlegend, strlen($g['data'][$r][0]));
  235. }
  236. }
  237. if ($rowbegin > 1) {
  238. for ($c = ($colbegin - 1); $c < $colend; $c++) {
  239. $labels[($c + 1 - $colbegin)] = $g['data'][0][$c];
  240. $longestlabel = max($longestlabel, strlen($g['data'][0][$c]));
  241. }
  242. }
  243. } else if ($dataseries == 'rows') {
  244. if ($colbegin > 1) {
  245. for ($r = ($rowbegin - 1); $r < $rowend; $r++) {
  246. $labels[($r + 1 - $rowbegin)] = $g['data'][$r][0];
  247. $longestlabel = max($longestlabel, strlen($g['data'][$r][0]));
  248. }
  249. }
  250. if ($rowbegin > 1) {
  251. for ($c = ($colbegin - 1); $c < $colend; $c++) {
  252. $legends[($c + 1 - $colbegin)] = $g['data'][0][$c];
  253. $longestlegend = max($longestlegend, strlen($g['data'][0][$c]));
  254. }
  255. }
  256. }
  257. // Default sizes
  258. $defsize = array();
  259. $defsize['pie'] = array('w' => 600, 'h' => 300);
  260. $defsize['pie3d'] = array('w' => 600, 'h' => 300);
  261. $defsize['radar'] = array('w' => 600, 'h' => 300);
  262. $defsize['line'] = array('w' => 600, 'h' => 400);
  263. $defsize['xy'] = array('w' => 600, 'h' => 400);
  264. $defsize['scatter'] = array('w' => 600, 'h' => 400);
  265. $defsize['bar'] = array('w' => 600, 'h' => 400);
  266. $defsize['horiz_bar'] = array('w' => 600, 'h' => 500);
  267. // Use default ratios
  268. if ($w && !$h) {
  269. $h = $w * $defsize[$type]['h'] / $defsize[$type]['w'];
  270. }
  271. if ($h && !$w) {
  272. $w = $h * $defsize[$type]['w'] / $defsize[$type]['h'];
  273. }
  274. if (!$h && !$w) {
  275. $w = $defsize[$type]['w'];
  276. $h = $defsize[$type]['h'];
  277. }
  278. if (count($data) > 0 && $type) {
  279. $figure_file = "graph_cache/" . rand(11111, 999999999) . "." . $img_type;
  280. if ($bandw) {
  281. $colours = array('snow1', 'black', 'snow4', 'snow3', 'snow2', 'cadetblue4', 'cadetblue3', 'cadetblue1', 'bisque4', 'bisque2', 'beige');
  282. } else {
  283. $colours = array('cyan', 'darkorchid4', 'cadetblue3', 'khaki1', 'darkolivegreen2', 'cadetblue4', 'coral', 'cyan4', 'rosybrown3', 'wheat1');
  284. }
  285. $fills = array('navy', 'orange', 'red', 'yellow', 'purple', 'navy', 'orange', 'red', 'yellow', 'purple');
  286. $patterns = array(PATTERN_DIAG1, PATTERN_CROSS1, PATTERN_STRIPE1, PATTERN_DIAG3, PATTERN_CROSS2, PATTERN_DIAG2, PATTERN_DIAG4, PATTERN_CROSS3, PATTERN_CROSS4, PATTERN_STRIPE1);
  287. $markers = array(MARK_DIAMOND, MARK_SQUARE, MARK_CIRCLE, MARK_UTRIANGLE, MARK_DTRIANGLE, MARK_FILLEDCIRCLE, MARK_CROSS, MARK_STAR, MARK_X);
  288. // LEGENDS
  289. if ($type == 'pie' || $type == 'pie3d') {
  290. $graph = new PieGraph(($w * $k), ($h * $k));
  291. } else if ($type == 'radar') {
  292. $graph = new RadarGraph(($w * $k), ($h * $k));
  293. } else {
  294. $graph = new Graph(($w * $k), ($h * $k));
  295. }
  296. // mPDF 4.5.009
  297. // $graph->img->SetImgFormat($img_type) ;
  298. // if (strtoupper($img_type)=='JPEG') { $graph->img->SetQuality(90); }
  299. if ($antialias) {
  300. $graph->img->SetAntiAliasing();
  301. }
  302. $graph->SetShadow(true, 2 * $k);
  303. $graph->SetMarginColor("white");
  304. // TITLE
  305. $graph->title->Set($title);
  306. $graph->title->SetMargin(10 * $k);
  307. $graph->title->SetFont(FF_USERFONT, FS_BOLD, 11 * $k);
  308. $graph->title->SetColor("black");
  309. $graph->legend->SetLineSpacing(3 * $k);
  310. $graph->legend->SetMarkAbsSize(6 * $k);
  311. $graph->legend->SetFont(FF_USERFONT, FS_NORMAL, 8 * $k);
  312. // Set GRAPH IMAGE MARGINS
  313. if ($type == 'pie' || $type == 'pie3d') {
  314. $psize = 0.3;
  315. $pposxabs = ($w / 2);
  316. $pposy = 0.55;
  317. if ($longestlegend) { // if legend showing
  318. $pposxabs -= ((($longestlegend * 5) + 20) / 2);
  319. }
  320. $pposx = ($pposxabs / $w);
  321. $graph->legend->Pos(0.02, 0.5, 'right', 'center');
  322. } else if ($type == 'radar') {
  323. $psize = 0.5;
  324. $pposxabs = ($w / 2);
  325. $pposy = 0.55;
  326. if ($longestlabel) { // if legend showing
  327. $pposxabs -= ((($longestlabel * 5) + 20) / 2);
  328. }
  329. $pposx = ($pposxabs / $w);
  330. $graph->legend->Pos(0.02, 0.5, 'right', 'center');
  331. } else if ($type == 'xy' || $type == 'scatter') {
  332. $pml = 50;
  333. $pmr = 20;
  334. $pmt = 60;
  335. $pmb = 50;
  336. $xaxislblmargin = $pmb - 30;
  337. $yaxislblmargin = $pml - 15;
  338. $graph->legend->Pos(0.02, 0.1, 'right', 'top');
  339. } else if ($type == 'line' || $type == 'bar') {
  340. $pml = 50;
  341. $pmr = 20;
  342. $pmt = 60;
  343. $pmb = 50;
  344. $xlangle = 0;
  345. $ll = ($longestlegend * 5); // 45 degrees 8pt fontsize
  346. if ($ll > 5 || ($ll > 3 && count($data) > 10)) {
  347. $pmb = max($pmb, $ll + 30);
  348. $xlangle = 50;
  349. }
  350. $xaxislblmargin = $pmb - 30;
  351. $yaxislblmargin = $pml - 15;
  352. if ($longestlabel && !$overlap) { // if legend showing
  353. $pmr = ((($longestlabel * 5) + 40));
  354. }
  355. $graph->legend->Pos(0.02, 0.1, 'right', 'top');
  356. } else if ($type == 'horiz_bar') {
  357. $pml = 50;
  358. $pmr = 20;
  359. $pmt = 50;
  360. $pmb = 45;
  361. $ll = ($longestlegend * 6.5); // 8pt fontsize
  362. $pml = max($pml, $ll + 20);
  363. $xaxislblmargin = $pml - 20;
  364. $yaxislblmargin = $pmb - 15;
  365. if ($longestlabel && !$overlap) { // if legend showing
  366. $pmr = ((($longestlabel * 5) + 40));
  367. }
  368. $graph->legend->Pos(0.02, 0.1, 'right', 'top');
  369. }
  370. // DRAW THE GRAPHS
  371. if ($type == 'pie') {
  372. $p1 = new PiePlot($data[0]);
  373. $p1->SetSliceColors($colours);
  374. if ($show_values) {
  375. $p1->value->SetFont(FF_USERFONT, FS_NORMAL, 8 * $k);
  376. if ($percent) {
  377. $p1->SetLabelType(PIE_VALUE_PERADJ);
  378. } //PIE_VAL_PER = default
  379. else {
  380. $p1->SetLabelType(PIE_VALUE_ABS);
  381. }
  382. if ($percent || $show_percent) {
  383. $p1->value->SetFormat("%d%%");
  384. } else {
  385. $p1->value->SetFormat("%s");
  386. }
  387. // Enable and set policy for guide-lines. Make labels line up vertically
  388. $p1->SetGuideLines(true);
  389. $p1->SetGuideLinesAdjust(1.5);
  390. } else {
  391. $p1->value->Show(false);
  392. }
  393. $p1->SetLegends($legends);
  394. $p1->SetSize($psize);
  395. $p1->SetCenter($pposx, $pposy);
  396. if ($labels[0]) {
  397. $graph->subtitle->Set($labels[0]);
  398. $graph->subtitle->SetMargin(10 * $k);
  399. $graph->subtitle->SetFont(FF_USERFONT, FS_BOLD, 11 * $k);
  400. $graph->subtitle->SetColor("black");
  401. }
  402. $graph->Add($p1);
  403. } else if ($type == 'pie3d') {
  404. $p1 = new PiePlot3d($data[0]);
  405. $p1->SetSliceColors($colours);
  406. if ($show_values) {
  407. $p1->value->SetFont(FF_USERFONT, FS_NORMAL, 8 * $k);
  408. if ($percent) {
  409. $p1->SetLabelType(PIE_VALUE_PERADJ);
  410. } //PIE_VAL_PER = default
  411. else {
  412. $p1->SetLabelType(PIE_VALUE_ABS);
  413. }
  414. if ($percent || $show_percent) {
  415. $p1->value->SetFormat("%d%%");
  416. } else {
  417. $p1->value->SetFormat("%s");
  418. }
  419. } else {
  420. $p1->value->Show(false);
  421. }
  422. $p1->SetLegends($legends);
  423. $p1->SetEdge();
  424. $p1->SetSize($psize);
  425. $p1->SetCenter($pposx, $pposy);
  426. if ($labels[0]) {
  427. $graph->subtitle->Set($labels[0]);
  428. $graph->subtitle->SetMargin(10 * $k);
  429. $graph->subtitle->SetFont(FF_USERFONT, FS_BOLD, 11 * $k);
  430. $graph->subtitle->SetColor("black");
  431. }
  432. $graph->Add($p1);
  433. }
  434. // RADAR
  435. else if ($type == 'radar') {
  436. $graph->SetSize($psize);
  437. $graph->SetPos($pposx, $pposy);
  438. $graph->SetTitles($legends); // labels each axis
  439. $graph->axis->title->SetFont(FF_USERFONT, FS_NORMAL, 8 * $k);
  440. $graph->axis->title->SetMargin(5 * $k);
  441. $graph->axis->SetWeight(1 * $k);
  442. $graph->axis->HideLabels();
  443. $graph->axis->SetFont(FF_USERFONT, FS_NORMAL, 6 * $k);
  444. $graph->HideTickMarks();
  445. $group = array();
  446. foreach ($data AS $series => $dat) {
  447. $rdata = array();
  448. foreach ($data[$series] AS $row) {
  449. $rdata[] = $row;
  450. }
  451. if (count($rdata) < 3) {
  452. throw new MpdfException("ERROR::Graph::Cannot create a Radar Plot with less than 3 data points.");
  453. }
  454. // Create the radar plot
  455. $bplot = new RadarPlot($rdata);
  456. $bplot->mark->SetType($markers[$series]);
  457. $bplot->mark->SetFillColor($colours[$series]);
  458. $bplot->mark->SetWidth(3 * $k);
  459. $bplot->SetColor($colours[$series]);
  460. if ($series == 0) {
  461. $bplot->SetFillColor('lightred');
  462. } else {
  463. $bplot->SetFill(false);
  464. }
  465. $bplot->SetLineWeight(1 * $k);
  466. $bplot->SetLegend($labels[$series]);
  467. if ($bandw) {
  468. $bplot->SetShadow("gray5");
  469. }
  470. $graph->Add($bplot);
  471. }
  472. }
  473. // LINE
  474. else if ($type == 'line') {
  475. // Setup the graph.
  476. $graph->img->SetMargin($pml * $k, $pmr * $k, $pmt * $k, $pmb * $k); // LRTB
  477. $graph->SetScale($axes);
  478. $graph->yaxis->SetFont(FF_USERFONT, FS_NORMAL, 8 * $k);
  479. if ($ylabel) {
  480. $graph->yaxis->title->SetFont(FF_USERFONT, FS_NORMAL, 8 * $k);
  481. $graph->yaxis->SetTitle($ylabel, 'middle');
  482. $graph->yaxis->SetTitleMargin($yaxislblmargin * $k);
  483. }
  484. $graph->yaxis->SetLabelMargin(4 * $k);
  485. if ($percent || $show_percent) {
  486. $graph->yaxis->SetLabelFormat('%d%%');
  487. } // Percent
  488. // Show 0 label on Y-axis (default is not to show)
  489. $graph->yscale->ticks->SupressZeroLabel(true);
  490. if ($hide_y_axis) {
  491. $graph->yaxis->Hide();
  492. }
  493. if ($hide_grid) {
  494. $graph->ygrid->Show(false);
  495. }
  496. // Setup X-axis labels
  497. $graph->xaxis->SetFont(FF_USERFONT, FS_NORMAL, 8 * $k);
  498. $graph->xaxis->SetTickLabels($legends);
  499. $graph->xaxis->SetLabelAngle($xlangle);
  500. $graph->xaxis->SetLabelMargin(4 * $k);
  501. // X-axis title
  502. if ($xlabel) {
  503. $graph->xaxis->title->SetFont(FF_USERFONT, FS_NORMAL, 8 * $k);
  504. $graph->xaxis->SetTitle($xlabel, 'middle');
  505. $graph->xaxis->SetTitleMargin($xaxislblmargin * $k);
  506. }
  507. foreach ($data AS $series => $rdata) {
  508. $bplot = new LinePlot($rdata);
  509. $bplot->mark->SetType($markers[$series]);
  510. $bplot->mark->SetFillColor($colours[$series]);
  511. $bplot->mark->SetWidth(4 * $k);
  512. if ($show_values) {
  513. $bplot->value->Show(); // Not if scatter
  514. $bplot->value->SetMargin(6 * $k);
  515. $bplot->value->SetColor("darkred");
  516. $bplot->value->SetFont(FF_USERFONT, FS_NORMAL, 8 * $k);
  517. if ($percent || $show_percent) {
  518. $bplot->value->SetFormat('%d%%');
  519. } else {
  520. $bplot->value->SetFormat("%s");
  521. }
  522. }
  523. // Set color for each line
  524. $bplot->SetColor($colours[$series]);
  525. $bplot->SetWeight(2 * $k);
  526. $bplot->SetLegend($labels[$series]);
  527. if ($bandw) {
  528. $bplot->SetShadow("gray5");
  529. }
  530. // Indent the X-scale so the first and last point doesn't fall on the edges
  531. $bplot->SetCenter();
  532. $graph->Add($bplot);
  533. }
  534. }
  535. // XY or SCATTER
  536. else if ($type == 'xy' || $type == 'scatter') {
  537. // Setup the graph.
  538. $graph->img->SetMargin($pml * $k, $pmr * $k, $pmt * $k, $pmb * $k); // LRTB
  539. $graph->SetScale($axes);
  540. // Setup font for axis
  541. $graph->yaxis->SetFont(FF_USERFONT, FS_NORMAL, 8 * $k);
  542. // Y-axis title
  543. if ($labels[1]) {
  544. $graph->yaxis->title->SetFont(FF_USERFONT, FS_NORMAL, 8 * $k);
  545. $graph->yaxis->SetTitleMargin($yaxislblmargin * $k);
  546. $graph->yaxis->SetTitle($labels[1], 'middle');
  547. }
  548. $graph->yaxis->SetLabelMargin(4 * $k);
  549. if ($percent || $show_percent) {
  550. $graph->yaxis->SetLabelFormat('%d%%');
  551. } // Percent
  552. // Show 0 label on Y-axis (default is not to show)
  553. $graph->yscale->ticks->SupressZeroLabel(true);
  554. // Just let the maximum be autoscaled
  555. $graph->yaxis->scale->SetAutoMin(0);
  556. if ($hide_y_axis) {
  557. $graph->yaxis->Hide();
  558. }
  559. if ($hide_grid) {
  560. $graph->ygrid->Show(false);
  561. }
  562. // Setup X-axis labels
  563. $graph->xaxis->SetFont(FF_USERFONT, FS_NORMAL, 8 * $k);
  564. // mPDF 2.5 Corrects labelling of x-axis
  565. // $graph->xaxis->SetTickLabels($legends);
  566. $graph->xaxis->SetLabelAngle(50);
  567. $graph->xaxis->SetLabelMargin(4 * $k);
  568. // X-axis title
  569. if ($labels[0]) {
  570. $graph->xaxis->title->SetFont(FF_USERFONT, FS_NORMAL, 8 * $k);
  571. $graph->xaxis->SetTitleMargin($xaxislblmargin * $k);
  572. $graph->xaxis->SetTitle($labels[0], 'middle');
  573. }
  574. // Create the bar plot
  575. // SPLINES
  576. if ($splines && $type == 'xy') {
  577. $spline = new Spline($data[0], $data[1]);
  578. list($newx, $newy) = $spline->Get(100);
  579. } else {
  580. $newx = $data[0];
  581. $newy = $data[1];
  582. }
  583. if ($type == 'xy') {
  584. // LINE PLOT
  585. $bplot = new LinePlot($newy, $newx);
  586. // Set color for each line
  587. $bplot->SetColor($fills[0]);
  588. $bplot->SetWeight(4 * $k);
  589. if ($bandw) {
  590. $bplot->SetShadow("gray5");
  591. }
  592. $graph->Add($bplot);
  593. }
  594. // SCATTER PLOT
  595. $cplot = new ScatterPlot($data[1], $data[0]);
  596. $cplot->mark->SetType($markers[0]);
  597. $cplot->mark->SetFillColor($fills[0]);
  598. $cplot->mark->SetWidth(8 * $k);
  599. if ($show_values) {
  600. // mPDF 2.5
  601. if ($type == 'xy') {
  602. $cplot->value->Show();
  603. } // Not if scatter
  604. $cplot->value->SetMargin(8 * $k);
  605. $cplot->value->SetColor("darkred");
  606. $cplot->value->SetFont(FF_USERFONT, FS_NORMAL, 6 * $k);
  607. if ($percent || $show_percent) {
  608. $cplot->value->SetFormat('%d%%');
  609. } else {
  610. $cplot->value->SetFormat("%s");
  611. }
  612. }
  613. // Set color for each line
  614. $cplot->SetColor($fills[0]);
  615. $cplot->SetWeight(4 * $k);
  616. if ($bandw) {
  617. $cplot->SetShadow("gray5");
  618. }
  619. $graph->Add($cplot);
  620. }
  621. // BAR
  622. else if ($type == 'bar') {
  623. // Setup the graph.
  624. $graph->img->SetMargin($pml * $k, $pmr * $k, $pmt * $k, $pmb * $k); // LRTB
  625. $graph->SetScale($axes);
  626. // Setup y-axis
  627. $graph->yaxis->SetFont(FF_USERFONT, FS_NORMAL, 8 * $k);
  628. if ($hide_y_axis) {
  629. $graph->yaxis->Hide();
  630. }
  631. if ($hide_grid) {
  632. $graph->ygrid->Show(false);
  633. }
  634. $graph->yaxis->SetLabelMargin(4 * $k);
  635. if ($ylabel) {
  636. $graph->yaxis->title->SetFont(FF_USERFONT, FS_NORMAL, 8 * $k);
  637. $graph->yaxis->SetTitle($ylabel, 'middle');
  638. $graph->yaxis->SetTitleMargin($yaxislblmargin * $k);
  639. }
  640. // Show 0 label on Y-axis (default is not to show)
  641. $graph->yscale->ticks->SupressZeroLabel(false);
  642. // Setup X-axis labels
  643. $graph->xaxis->SetFont(FF_USERFONT, FS_NORMAL, 8 * $k);
  644. $graph->xaxis->SetTickLabels($legends);
  645. $graph->xaxis->SetLabelAngle($xlangle);
  646. $graph->xaxis->SetLabelMargin(4 * $k);
  647. // X-axis title
  648. if ($xlabel) {
  649. $graph->xaxis->title->SetFont(FF_USERFONT, FS_NORMAL, 8 * $k);
  650. $graph->xaxis->SetTitle($xlabel, 'middle');
  651. $graph->xaxis->SetTitleMargin($xaxislblmargin * $k);
  652. }
  653. $group = array();
  654. foreach ($data AS $series => $dat) {
  655. $rdata = array();
  656. foreach ($data[$series] AS $row) {
  657. $rdata[] = $row;
  658. }
  659. // Create the bar plot
  660. $bplot = new BarPlot($rdata);
  661. $bplot->SetWidth(0.6); // for SINGLE??
  662. // Setup color for gradient fill style
  663. if ($bandw) {
  664. $bplot->SetPattern($patterns[$series]);
  665. } else {
  666. $bplot->SetFillGradient($fills[$series], "#EEEEEE", GRAD_LEFT_REFLECTION);
  667. }
  668. // Set color for the frame of each bar
  669. $bplot->SetColor("darkgray");
  670. $bplot->SetLegend($labels[$series]);
  671. if ($bandw) {
  672. $bplot->SetShadow("gray5");
  673. }
  674. if ($show_values) {
  675. $bplot->value->Show();
  676. $bplot->value->SetMargin(6 * $k);
  677. $bplot->value->SetColor("darkred");
  678. $bplot->value->SetFont(FF_USERFONT, FS_NORMAL, 8 * $k);
  679. if ($percent || $show_percent) {
  680. $bplot->value->SetFormat('%d%%');
  681. } else {
  682. $bplot->value->SetFormat("%s");
  683. }
  684. }
  685. $group[] = $bplot;
  686. }
  687. if (count($data) == 1) {
  688. $graph->Add($group[0]);
  689. } else {
  690. // Create the grouped bar plot
  691. if ($stacked) {
  692. $gbplot = new AccBarPlot($group);
  693. } else {
  694. $gbplot = new GroupBarPlot($group);
  695. }
  696. $graph->Add($gbplot);
  697. }
  698. } else if ($type == 'horiz_bar') {
  699. $graph->SetScale($axes);
  700. $graph->Set90AndMargin($pml * $k, $pmr * $k, $pmt * $k, $pmb * $k); // LRTB
  701. // Setup y-axis
  702. $graph->yaxis->SetFont(FF_USERFONT, FS_NORMAL, 8 * $k);
  703. $graph->yaxis->SetLabelMargin(4 * $k);
  704. $graph->yaxis->SetPos('max'); // Intersect at top of x-axis i.e. y axis is at bottom
  705. // First make the labels look right
  706. $graph->yaxis->SetLabelAlign('center', 'top');
  707. if ($percent || $show_percent) {
  708. $graph->yaxis->SetLabelFormat('%d%%');
  709. }
  710. $graph->yaxis->SetLabelSide(SIDE_RIGHT);
  711. $graph->yaxis->scale->SetGrace(10); // sets 10% headroom
  712. if ($hide_y_axis) {
  713. $graph->yaxis->Hide();
  714. }
  715. if ($hide_grid) {
  716. $graph->ygrid->Show(false);
  717. }
  718. // The fix the tick marks
  719. $graph->yaxis->SetTickSide(SIDE_LEFT);
  720. if ($ylabel) {
  721. $graph->yaxis->title->SetFont(FF_USERFONT, FS_NORMAL, 8 * $k);
  722. $graph->yaxis->SetTitle($ylabel, 'middle');
  723. $graph->yaxis->SetTitleMargin($yaxislblmargin * $k);
  724. // Finally setup the title
  725. $graph->yaxis->SetTitleSide(SIDE_RIGHT);
  726. // To align the title to the right use :
  727. $graph->yaxis->title->Align('right');
  728. $graph->yaxis->title->SetAngle(0);
  729. }
  730. // Show 0 label on Y-axis (default is not to show)
  731. $graph->yscale->ticks->SupressZeroLabel(false);
  732. // Setup X-axis labels
  733. $graph->xaxis->SetFont(FF_USERFONT, FS_NORMAL, 8 * $k);
  734. $graph->xaxis->title->SetAngle(90);
  735. $graph->xaxis->SetTickLabels($legends);
  736. $graph->xaxis->SetLabelMargin(4 * $k);
  737. // X-axis title
  738. if ($xlabel) {
  739. $graph->xaxis->title->SetFont(FF_USERFONT, FS_NORMAL, 8 * $k);
  740. $graph->xaxis->SetTitleMargin($xaxislblmargin * $k);
  741. $graph->xaxis->SetTitle($xlabel, 'middle');
  742. }
  743. $group = array();
  744. foreach ($data AS $series => $dat) {
  745. $rdata = array();
  746. foreach ($data[$series] AS $row) {
  747. $rdata[] = $row;
  748. }
  749. // Create the bar pot
  750. $bplot = new BarPlot($rdata);
  751. $bplot->SetWidth(0.6); // for SINGLE??
  752. // Setup color for gradient fill style
  753. if ($bandw) {
  754. $bplot->SetPattern($patterns[$series]);
  755. } else {
  756. $bplot->SetFillGradient($fills[$series], "#EEEEEE", GRAD_LEFT_REFLECTION);
  757. }
  758. // Set color for the frame of each bar
  759. $bplot->SetColor("darkgray");
  760. $bplot->SetLegend($labels[$series]);
  761. if ($bandw) {
  762. $bplot->SetShadow("gray5");
  763. }
  764. if ($show_values) {
  765. $bplot->value->Show();
  766. $bplot->value->SetMargin(6 * $k);
  767. $bplot->value->SetColor("darkred");
  768. $bplot->value->SetFont(FF_USERFONT, FS_NORMAL, 8 * $k);
  769. if ($percent || $show_percent) {
  770. $bplot->value->SetFormat('%d%%');
  771. } else {
  772. $bplot->value->SetFormat("%s");
  773. }
  774. }
  775. $group[] = $bplot;
  776. }
  777. if (count($data) == 1) {
  778. $graph->Add($group[0]);
  779. } else {
  780. // Create the grouped bar plot
  781. if ($stacked) {
  782. $gbplot = new AccBarPlot($group);
  783. } else {
  784. $gbplot = new GroupBarPlot($group);
  785. }
  786. $graph->Add($gbplot);
  787. }
  788. }
  789. if ($graph) {
  790. $graph->Stroke(_MPDF_PATH . $figure_file);
  791. $srcpath = str_replace("\\", "/", dirname(__FILE__)) . "/";
  792. $srcpath .= $figure_file;
  793. return array('file' => $srcpath, 'w' => $w, 'h' => $h);
  794. }
  795. }
  796. return false;
  797. }