查看: 970|回复: 0

[PHP学习] php图形jpgraph操作实例分析

发表于 2017-12-13 08:00:02

本文实例讲述了php图形jpgraph操作。分享给大家供大家参考,具体如下:

  1. <?php
  2. include ("src/jpgraph.php");
  3. include("src/jpgraph_bar.php");
  4. include ("src/jpgraph_line.php");
  5. //设置显示的数据数组;
  6. //调用类库
  7. //设置图像的大小
  8. $graph = new Graph(400,200,"auto");
  9. $graph->SetScale("textlin");
  10. //设置图形的边距
  11. $graph->img->SetMargin(40,180,40,40);
  12. //设置图形的背景图片,填充方式有:BGIMG_FILLPLOT, BGIMG_FILLFRAME, BGIMG_COPY
  13. $graph->SetBackgroundImage("abc.jpg",BGIMG_FILLPLOT);
  14. $graph->img->SetAngle(45); //设置图形在图像中的角度
  15. //设置背景图片的对比度,must be between -1 <= x <= 1, (0,0)=original image
  16. $graph->AdjBackgroundImage(0,0);
  17. //设置投影;
  18. //$graph->SetShadow();
  19. //设置标题
  20. $graph->title->Set("test image");
  21. //设置标题字体样式
  22. $graph->title->SetFont(FF_FONT1,FS_BOLD);
  23. //设置标题的边距
  24. $graph->title->SetMargin(3);
  25. //设置图列的位置
  26. $graph->legend->Pos(0.05,0.5,"right","center");
  27. //设置图列的投影,颜色
  28. $graph->legend->SetShadow('darkgray@0.1');
  29. $graph->legend->SetFillColor('lightblue@0.3');
  30. //设置x轴的标记
  31. $graph->xaxis->SetTickLabels($label_x);
  32. //设置X轴的显示值的角度;
  33. $graph->xaxis->SetLabelAngle(30);
  34. //设置x轴标题和字体颜色
  35. $graph->xaxis->title->Set('Year 2006');
  36. $graph->xaxis->title->SetFont(FF_FONT1,FS_BOLD);
  37. $graph->xaxis->title->SetColor('white');
  38. //设置x轴的字体和颜色
  39. $graph->xaxis->SetFont(FF_FONT1,FS_BOLD);
  40. $graph->xaxis->SetColor('yellow');
  41. //设置y轴的字体和颜色
  42. $graph->yaxis->SetFont(FF_FONT1,FS_BOLD);
  43. $graph->yaxis->SetColor('yellow');
  44. //设置是否显示格子。默认为显示;
  45. //$graph->ygrid->Show(false);
  46. //设置格子的颜色和粗细。值越小,格子越粗。
  47. $graph->ygrid->SetColor('yellow@0.5');
  48. //设置y轴更优美一些
  49. $graph->yaxis->scale->SetGrace(20);
  50. //设置图列的数据
  51. $bplot1 = new BarPlot($datay1);
  52. $bplot2 = new BarPlot($datay2);
  53. //设置图列的填充颜色
  54. $bplot1->SetFillColor('orange@0.4');
  55. $bplot2->SetFillColor('brown@0.4');
  56. //设置值的格式
  57. $bplot1->value->SetFormat('%d');
  58. //设置图列标签
  59. $bplot1->SetLegend('Label 1');
  60. $bplot2->SetLegend('Label 2');
  61. //设置图列在图中的阴影
  62. $bplot1->SetShadow('black@0.4');
  63. $bplot2->SetShadow('black@0.4');
  64. //生成图列
  65. $gbarplot = new GroupBarPlot(array($bplot1,$bplot2));
  66. $gbarplot->SetWidth(0.9);
  67. $graph->Add($gbarplot);
  68. //生成图形
  69. $graph->Stroke();
  70. //上面所说的时在生成柱形图,当生成线性图时用下面的方法
  71. $p1 = new LinePlot($datay);
  72. $p1->mark->SetType(MARK_FILLEDCIRCLE);
  73. $p1->mark->SetFillColor("red");
  74. $p1->mark->SetWidth(4);
  75. $p1->SetColor("blue");
  76. $p1->SetCenter();
  77. $p1->SetLegend("Triumph Tiger -98");
  78. $graph->Add($p1);
  79. $p2 = new LinePlot($data2y);
  80. $p2->mark->SetType(MARK_STAR);
  81. $p2->mark->SetFillColor("red");
  82. $p2->mark->SetWidth(4);
  83. $p2->SetColor("red");
  84. $p2->SetCenter();
  85. $p2->SetLegend("New tiger -99");
  86. $graph->Add($p2);
  87. // Style can also be specified as SetStyle([1|2|3|4]) or
  88. // SetStyle("solid"|"dotted"|"dashed"|"lobgdashed")
  89. $lineplot->SetStyle("dashed");//设置线的样式
  90. $graph->yaxis->scale->SetGrace(20); //设置y轴更优美一些
  91. ?>
复制代码

2.柱形图和饼状图举例

  1. if($tag == 1)
  2. {
  3. $graph = new Graph(600,400,"auto");
  4. $graph->SetScale("textlin");
  5. $graph->setMarginColor('lightblue');
  6. $graph->SetShadow();
  7. $graph->setMargin(30,100,30,60);
  8. //设置标题;
  9. $graph->title->set("文章分类汇总");
  10. $graph->title->SetMargin(3);
  11. $graph->title->setfont(FF_SIMSUN,FS_BOLD);
  12. $graph->title->setcolor('black@0.5');
  13. $graph->yaxis->title->SetFont(FF_SIMSUN,FS_BOLD);
  14. $graph->xaxis->title->SetFont(FF_SIMSUN,FS_BOLD);
  15. $graph->xaxis->SetFont(FF_SIMSUN,FS_NORMAL);
  16. $graph->xaxis->SetColor('darkblue','black');
  17. $graph->xaxis->SetTickLabels($name);
  18. $graph->xaxis->SetLabelAngle(30);
  19. $bplot = new BarPlot($article_num);
  20. $bplot->SetFillColor("orange");
  21. $bplot->value->SetFormat('%d');
  22. $bplot->SetShadow('darkgray');
  23. $bplot->value->show();
  24. $graph->legend->SetFont(FF_SIMSUN,FS_BOLD);
  25. $bplot->SetLegend("文章数");
  26. $graph->Add($bplot);
  27. $graph->Stroke();
  28. }
  29. else
  30. {
  31. $graph1 = new PieGraph(600,400,"auto");
  32. $graph1->SetScale("textlin");
  33. $graph1->SetShadow();
  34. $graph1->title->setFont(FF_SIMSUN,FS_BOLD);
  35. $graph1->title->set("用户文章饼形图");
  36. $graph1->setMargin(30,100,30,60);
  37. $p1 = new pieplot3d($article_num);
  38. $p1->setAngle(80);
  39. $p1->setsize(0.5);
  40. $p1->setShadow();
  41. $p1->ExplodeSlice(2);
  42. $p1->SetCenter(0.4);
  43. $graph1->legend->SetFont(FF_SIMSUN,FS_NORMAL);
  44. $graph1->legend->setshadow();
  45. $p1->SetLegends($name);
  46. $graph1->Add($p1);
  47. $graph1->Stroke();
  48. }
  49. //生成本地图片
  50. $graph->Stroke("路径/文件名.png");
复制代码

更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP图形与图片操作技巧汇总》、《PHP基本语法入门教程》、《php面向对象程序设计入门教程》、《PHP网络编程技巧总结》、《PHP数组(Array)操作技巧大全》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

希望本文所述对大家PHP程序设计有所帮助。



回复

使用道具 举报