graph.php 27 KB

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