graph.php 27 KB

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