jpgraph 生成类型数据以图表走势图的形式表现出来

  1. <?php
  2. /**
  3. * ecshop jpgraph图表类库
  4. * ==============================================================
  5. * 利用开源的jpgraph库实现对各类型数据以图表,走势图的形式表现出来。
  6. * ==============================================================
  7. **/
  8. class cls_jpgraph
  9. {
  10. var $db = null;
  11. var $ydata = array();
  12. var $width = 350;
  13. var $height = 250;
  14. var $graph = ''; //图形对象
  15. var $piegraph = ''; //丙状图形
  16. var $lineplot = ''; //线性对象
  17. var $barpot = ''; //柱状对象
  18. var $gbplat = ''; //柱状组对象
  19. var $txt = ''; //文本对象
  20. var $p1 = ''; //丙状图对象
  21. var $scale = ''; //图表类型? (textlin,textlog,intlin)
  22. var $yscale = ''; // (log,)
  23. var $xgrid = false; //x轴网格显示
  24. var $ygrid = false; //y轴网格显示
  25. var $title = ''; //标题
  26. var $subtitle = ''; //子标题(一般为日期)
  27. var $xaxis = ''; //x轴名称
  28. var $yaxis = ''; //y轴名称
  29. /*margin position*/
  30. var $left = 0;
  31. var $right = 0;
  32. var $top = 0;
  33. var $bottom = 0;
  34. /**
  35. *构造函数
  36. */
  37. function cls_jpgraph($width=350,$height=250)
  38. {
  39. $this->width = $width;
  40. $this->height = $height;
  41. $this->graph = new graph($this->width,$this->height);
  42. }
  43. /**
  44. * 图表类库的构造函数
  45. *
  46. */
  47. /*
  48. function __construct($parms,$width,$height)
  49. {
  50. cls_jpgraph($parms,$width,$height);
  51. }
  52. */
  53. /*图片基本信息设置*/
  54. function set_jpgraph($scale='intlin',$title='',$subtitle='',$bgcolor='',$xaxis='',
  55. $yaxis='',$xgr') {
  56. //开源代码phpfensi.com
  57. $this->scale = $scale;
  58. $this->title = $title;
  59. $this->subtitle = $subtitle;
  60. $this->xaxis = $xaxis;
  61. $this->yaxis = $yaxis;
  62. $this->xgrid = $xgrid;
  63. $this->ygrid = $ygrid;
  64. if(!emptyempty($scale)) {
  65. $this->graph->setscale($this->scale);
  66. }
  67. else {
  68. $this->graph->setscale('intlin');
  69. }
  70. $this->graph->xgrid->show($this->xgrid,$this->xgrid);
  71. $this->graph->ygrid->show($this->ygrid,$this->ygrid);
  72. if(!emptyempty($bgcolor)) {
  73. $this->graph->setmargincolor($bgcolor);
  74. }
  75. /*如果手工设置了图片位置*/
  76. if(is_array($margin)) {
  77. while(list($key,$val) = each($margin)) {
  78. $this->$key = $val;
  79. }
  80. $this->graph->setmargin($this->left,$this->right,$this->top,$this->bottom);
  81. }
  82. if(!emptyempty($this->title)) {
  83. $this->graph->title->set($this->title);
  84. }
  85. if(!emptyempty($this->subtitle)) {
  86. $this->graph->subtitle->set($this->subtitle);
  87. }
  88. else {
  89. $this->graph->subtitle->set('('.date('y-m-d').')'); //默认子标题设置为当前日期
  90. }
  91. if(!emptyempty($this->xaxis)) {
  92. $this->graph->xaxis->title->set($this->xaxis);
  93. }
  94. if(!emptyempty($this->yaxis)) {
  95. $this->graph->yaxis->title->set($this->yaxis);
  96. }
  97. }
  98. /*创建线性关系图表(linear plot)*/
  99. function create_lineplot($parms,$color='black',$weight=1)
  100. {
  101. $this->ydata = $parms;
  102. $this->lineplot = new lineplot($this->ydata);
  103. $this->lineplot->setcolor($color);
  104. $this->lineplot->setweight($weight);
  105. return $this->lineplot;
  106. }
  107. /*创建柱状图表(bar pot)*/
  108. function create_barpot($parms,$color='black',$width='0')
  109. {
  110. $this->ydata = $parms;
  111. $this->barpot = new barplot($this->ydata);
  112. $this->barpot->setfillcolor($color);
  113. if(!emptyempty($width)) {
  114. $this->barpot->setwidth($width);
  115. }
  116. return $this->barpot;
  117. }
  118. /*创建数据柱状图表组*/
  119. function create_bargroup($plotarr,$width='0.8')
  120. {
  121. $this->gbplot = new groupbarplot($plotarr);
  122. $this->gbplot->setwidth($width);
  123. return $this->gbplot;
  124. }
  125. /*创建文本内容*/
  126. function create_text($str,$postion='',$color='black')
  127. {
  128. $this->txt = new text($str);
  129. if(is_array($postion)) {
  130. while(list($key,$val) = each($postion)) {
  131. $this->$key = $val;
  132. }
  133. $this->txt->setpos($this->left,$this->top);
  134. }
  135. else {
  136. $this->txt->setpos(10,20);
  137. }
  138. $this->txt->setcolor($color);
  139. $this->graph->add($this->txt);
  140. }
  141. /*创建丙状图表*/
  142. function create_pie($parms,$title,$type='3d',$size='0.5',$center='0.5',$width='350',$height='250')
  143. {
  144. $this->width = $width;
  145. $this->height = $height;
  146. $this->piegraph = new piegraph(300,200);
  147. $this->piegraph->setshadow();
  148. $this->piegraph->title->set($title);
  149. if('3d' != $type) {
  150. $this->p1 = new pieplot($parms);
  151. $this->piegraph->add($this->p1);
  152. }
  153. else {
  154. $this->p1 = new pieplot3d($parms);
  155. $this->p1->setsize($size);
  156. $this->p1->setcenter($center);
  157. // $this->p1->setlegends($gdatelocale->getshortmonth());
  158. $this->piegraph->add($this->p1);
  159. }
  160. $this->piegraph->stroke();
  161. }
  162. function get_auth_code($length=4)
  163. {
  164. $spam = new antispam();
  165. $chars = $spam->rand($length);
  166. if( $spam->stroke() === false ) {
  167. return false;
  168. }
  169. return $chars;
  170. }
  171. /*完成图形创建并显示*/
  172. function display($obj)
  173. {
  174. $this->graph->add($obj);
  175. $this->graph->stroke();
  176. }
  177. }
  178. ?>