Chart.class.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. <?php
  2. /**
  3. *+--------------------------------------------------
  4. * 图表管理类
  5. *+--------------------------------------------------
  6. *
  7. */
  8. class Chart {
  9. /**
  10. * @@生成三维饼图
  11. * @param string $title
  12. * @param array $data
  13. * @param int $size
  14. * @param int $height
  15. * @param int $width
  16. * @param array $legend
  17. */
  18. static function create3dpie($title,$data=array(),$size=40,$height=100,$width=80,$legend=array(),$slice=0){
  19. //包含相关的文件
  20. vendor("Jpgraph.jpgraph");
  21. vendor("Jpgraph.jpgraph_pie");
  22. vendor("Jpgraph.jpgraph_pie3d");
  23. // 创建图表
  24. $graph = new PieGraph($width,$height,"auto");
  25. $graph->SetShadow();
  26. // 设置标题
  27. $graph->title->Set(iconv("utf-8","gb2312",$title));
  28. //$graph->title->SetFont(FF_VERDANA,FS_BOLD,18);
  29. $graph->title->SetFont(FF_SIMSUN,FS_BOLD,18);
  30. $graph->title->SetColor("darkblue");
  31. $graph->legend->Pos(0.1,0.2);
  32. $graph->SetFrame(false,'#ffffff',0);//去掉周围的边框
  33. // Create 3D pie plot
  34. $p1 = new PiePlot3d($data);
  35. $p1->SetTheme("sand");
  36. $p1->SetCenter(0.4);
  37. $p1->SetSize($size);
  38. // Adjust projection angle
  39. $p1->SetAngle(70);
  40. // As a shortcut you can easily explode one numbered slice with
  41. if($slice==0)
  42. $p1->ExplodeSlice(3);
  43. // Setup the slice values
  44. $p1->value->SetFont(FF_ARIAL,FS_BOLD,10);
  45. //$p1->value->SetFont(FF_SIMSUN,FS_BOLD,11);
  46. $p1->value->SetColor("navy");
  47. $graph->legend->SetFont(FF_SIMSUN,FS_BOLD,8);
  48. //编码转化
  49. foreach ($legend as $k => $v) {
  50. $legend[$k] = iconv('utf-8', 'gb2312', $v);
  51. }
  52. $p1->SetLegends($legend);
  53. $graph->Add($p1);
  54. $graph->Stroke();
  55. }
  56. /**
  57. * 设置柱状图
  58. */
  59. static function createcolumnar($title,$data=array(),$size=40,$height=100,$width=80,$legend=array()){
  60. //载入文件
  61. vendor("Jpgraph.jpgraph");
  62. vendor("Jpgraph.jpgraph_bar");
  63. // Some data
  64. // Create the graph and setup the basic parameters
  65. $graph = new Graph($width,$height,'auto');
  66. $graph->img->SetMargin(40,30,40,40);
  67. $graph->SetScale("textint");
  68. $graph->SetShadow();
  69. $graph->SetFrame(false); // No border around the graph
  70. // Add some grace to the top so that the scale doesn't
  71. // end exactly at the max value.
  72. $graph->yaxis->scale->SetGrace(20);
  73. // Setup X-axis labels
  74. //编码转化
  75. foreach ($legend as $k => $v) {
  76. $legend[$k] = iconv('utf-8', 'gb2312', $v);
  77. }
  78. $graph->xaxis->SetTickLabels($legend);
  79. $graph->xaxis->SetFont(FF_SIMSUN);
  80. // Setup graph title ands fonts
  81. $graph->title->Set(iconv("utf-8","gb2312",$title));
  82. $graph->title->SetFont(FF_SIMSUN,FS_BOLD,11);
  83. //$graph->xaxis->title->Set("Year 2002");
  84. $graph->xaxis->title->SetFont(FF_FONT2,FS_BOLD);
  85. // Create a bar pot
  86. $bplot = new BarPlot($data);
  87. $bplot->SetFillColor("#0080C0");
  88. $bplot->SetWidth(0.3);
  89. //$bplot->SetShadow();
  90. // Setup the values that are displayed on top of each bar
  91. $bplot->value->Show();
  92. // Must use TTF fonts if we want text at an arbitrary angle
  93. $bplot->value->SetFont(FF_ARIAL,FS_BOLD);
  94. $bplot->value->SetAngle(0);
  95. // Black color for positive values and darkred for negative values
  96. $bplot->value->SetColor("black","darkred");
  97. $graph->Add($bplot);
  98. // Finally stroke the graph
  99. $graph->Stroke();
  100. }
  101. /**
  102. * 环形图
  103. */
  104. function createring($title,$data=array(),$size=40,$height=100,$width=80,$legend=array()){
  105. // Example of pie with center circle
  106. vendor("Jpgraph.jpgraph");
  107. vendor("Jpgraph.jpgraph_pie");
  108. // Some data
  109. //$data = array(50,28,25,27,30,30);
  110. // A new pie graph
  111. $graph = new PieGraph(700,350,'auto');
  112. //$graph->SetShadow();
  113. // Setup title
  114. $graph->title->Set(iconv("utf-8","gb2312","{$title}"));
  115. $graph->title->SetFont(FF_SIMSUN,FS_BOLD,14);
  116. $graph->title->SetMargin(2); // Add a little bit more margin from the top
  117. $graph->legend->Pos(0.1,0.1);
  118. $graph->SetFrame(false,'#ffffff',0);//去掉周围的边框
  119. // Create the pie plot
  120. $p1 = new PiePlotC($data);
  121. // Set size of pie
  122. $p1->SetTheme("sand");
  123. $p1->SetCenter(0.4);
  124. $p1->SetSize(0.35);
  125. // Label font and color setup
  126. $p1->value->SetFont(FF_ARIAL,FS_BOLD,10);
  127. $p1->value->SetColor('black');
  128. // Setup the title on the center circle
  129. $p1->midtitle->Set("");
  130. $p1->midtitle->SetFont(FF_SIMSUN,FS_NORMAL,10);
  131. // Set color for mid circle
  132. $p1->SetMidColor('white');
  133. // Use percentage values in the legends values (This is also the default)
  134. $p1->SetLabelType(PIE_VALUE_PER);
  135. $graph->legend->SetFont(FF_SIMSUN,FS_NORMAL,8);
  136. //编码转化
  137. foreach ($legend as $k => $v) {
  138. $legend[$k] = iconv('utf-8', 'gb2312', $v);
  139. }
  140. $p1->SetLegends($legend);
  141. // Add plot to pie graph
  142. $graph->Add($p1);
  143. // .. and send the image on it's marry way to the browser
  144. $graph->Stroke();
  145. }
  146. /**
  147. * 线图
  148. */
  149. function createmonthline($title,$data=array(),$size=40,$height=100,$width=80,$legend=array()){
  150. vendor("Jpgraph.jpgraph");
  151. vendor("Jpgraph.jpgraph_line");
  152. $labels = $legend;
  153. //编码转化
  154. foreach ($labels as $k => $v) {
  155. $labels[$k] = iconv('utf-8', 'gb2312', $v);
  156. }
  157. $data = $data;
  158. $graph = new Graph($width,$height,"auto");
  159. $graph->img->SetMargin(40,40,40,40);
  160. $graph->img->SetAntiAliasing();
  161. $graph->SetScale("textlin");
  162. $graph->SetShadow();
  163. $graph->title->Set(iconv('utf-8', 'gb2312',"{$title}"));
  164. $graph->title->SetFont(FF_SIMSUN,FS_NORMAL,14);
  165. $graph->SetFrame(false,'#ffffff',0);//去掉周围的边框
  166. $graph->xaxis->SetFont(FF_SIMSUN,FS_NORMAL,9);
  167. $graph->xaxis->SetTickLabels($labels);
  168. $graph->xaxis->SetLabelAngle(0);
  169. $p1 = new LinePlot($data);
  170. $p1->mark->SetType(MARK_FILLEDCIRCLE);
  171. $p1->mark->SetFillColor("#0080C0");
  172. $p1->mark->SetWidth(4);
  173. $p1->SetColor("#000000");
  174. $p1->SetCenter();
  175. $graph->Add($p1);
  176. $graph->Stroke();
  177. }
  178. /**
  179. * 横柱图
  180. *
  181. */
  182. function createhorizoncolumnar($title,$subtitle,$data=array(),$size=40,$height=100,$width=80,$legend=array()){
  183. vendor("Jpgraph.jpgraph");
  184. vendor("Jpgraph.jpgraph_bar");
  185. $datay = $data;
  186. $datax = $legend;
  187. //编码转化
  188. foreach ($datax as $k => $v) {
  189. $datax[$k] = iconv('utf-8', 'gb2312', $v);
  190. }
  191. // Size of graph
  192. $count = count($datay);
  193. $addheight = 0;
  194. if($count>10){
  195. $addheight = ($count-10)*20;
  196. }
  197. $height=$height+$addheight;
  198. // Set the basic parameters of the graph
  199. $graph = new Graph($width,$height,'auto');
  200. $graph->SetScale("textlin");
  201. // No frame around the image
  202. $graph->SetFrame(false);
  203. $graph->SetFrame(false,'#ffffff',0);//去掉周围的边框
  204. // Rotate graph 90 degrees and set margin
  205. $graph->Set90AndMargin(70,10,50,30);
  206. // Set white margin color
  207. $graph->SetMarginColor('white');
  208. // Use a box around the plot area
  209. $graph->SetBox();
  210. // Use a gradient to fill the plot area
  211. $graph->SetBackgroundGradient('white','white',GRAD_HOR,BGRAD_PLOT);
  212. // Setup title
  213. $graph->title->Set(iconv('utf-8', 'gb2312',"{$title}"));
  214. $graph->title->SetFont(FF_SIMSUN,FS_BOLD,12);
  215. $graph->subtitle->Set("(".iconv('utf-8', 'gb2312',$subtitle).")");
  216. $graph->subtitle->SetFont(FF_SIMSUN,FS_NORMAL,10);
  217. // Setup X-axis
  218. $graph->xaxis->SetTickLabels($datax);
  219. $graph->xaxis->SetFont(FF_SIMSUN,FS_NORMAL,10);
  220. // Some extra margin looks nicer
  221. $graph->xaxis->SetLabelMargin(10);
  222. // Label align for X-axis
  223. $graph->xaxis->SetLabelAlign('right','center');
  224. // Add some grace to y-axis so the bars doesn't go
  225. // all the way to the end of the plot area
  226. $graph->yaxis->scale->SetGrace(10);
  227. // We don't want to display Y-axis
  228. $graph->yaxis->Hide();
  229. // Now create a bar pot
  230. $bplot = new BarPlot($datay);
  231. //$bplot->SetShadow();
  232. //You can change the width of the bars if you like
  233. //$bplot->SetWidth(0.5);
  234. // Set gradient fill for bars
  235. $bplot->SetFillGradient('blue','#0080C0',GRAD_HOR);
  236. // We want to display the value of each bar at the top
  237. $bplot->value->Show();
  238. $bplot->value->SetFont(FF_ARIAL,FS_NORMAL,7);
  239. $bplot->value->SetAlign('left','center');
  240. $bplot->value->SetColor("black");
  241. $bplot->value->SetFormat('%.0f');
  242. //$bplot->SetValuePos('max');
  243. // Add the bar to the graph
  244. $graph->Add($bplot);
  245. // Add some explanation text
  246. $txt = new Text('');
  247. $txt->SetPos(130,399,'center','bottom');
  248. $txt->SetFont(FF_COMIC,FS_NORMAL,8);
  249. $graph->Add($txt);
  250. // .. and stroke the graph
  251. $graph->Stroke();
  252. }
  253. }