search
Homephp教程php手册jpgraph+php+mysql生成柱状图(含附件)

  很详细的注释,希望对像我一样刚入门的朋友有帮助,附代码:

  CREATE DATABASE `jpgraph`; //创建数据库

  USE `jpgraph`;

  //创建测试表

  DROP TABLE IF EXISTS `jpg_temp`;

  CREATE TABLE `jpg_temp` (

  `year` int(11) NOT NULL,

  `money` float NOT NULL,

  `number` int(11) NOT NULL

  ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

  //测试数据

  insert into `jpg_temp`(`year`,`money`,`number`) values (2007,150.78,120),(2008,100,90),(2009,120,100),(2010,100,78),(2011,70.56,60),(2012,90,100),(2013,140,180),(2014,150,200),(2015,130,110),(2016,110.85,150),(2020,180,230),(2050,200,210);

  //数据库链接文件

  

  /*

  * ProjectName:jpgraph

  * CreateDate:2011-5-12

  * Author:Wilr

  * Description:数据库链接

  */

  $dbConn=mysql_connect("127.0.0.1","root","root");

  if(!$dbConn)

  echo '数据库通信失败';

  mysql_select_db("jpgraph");

  mysql_query("set names 'utf8'",$dbConn);

  $sql= " SELECT * FROM jpg_temp";

  $result=mysql_query($sql,$dbConn);

  $rowCount=mysql_num_rows($result);

  $datay=array();

  $datax=array();

  $number=array();

  while ($row=mysql_fetch_array($result)){

  $datay[]=$row["money"];

  $datax[]=$row["year"];

  $number[]=$row["number"];

  }

  //echo each($datay);

  //print_r($datay);

  mysql_close($dbConn);

  ?>

  //生成图表类

  

  require_once ('jpgraph/jpgraph.php'); //载入基本类

  require_once ('jpgraph/jpgraph_bar.php'); //载入柱状图

  include_once('db_config.php'); //载入数据处理文件

  $graph=new Graph(900,500); //创建一个图表 指定大小

  $graph->SetScale("textlin"); //设置坐标刻度类型

  $graph->img->SetMargin(40,180,30,40);//设置统计图边距 左、右、上、下

  //$graph->SetMarginColor("lightblue");//设置画布背景色 淡蓝色

  //$graph->SetBackgroundImage('stship.jpg',BGIMG_COPY); //设置背景图片

  //$graph->img->SetAngle(45); //设置图形在图像中的角度

  //设置标题信息

  $graph->title->Set('Wilr测试报表'); //设置统计图标题

  $graph->title->SetFont(FF_SIMSUN,FS_BOLD,20); //设置标题字体

  $graph->title->SetMargin(3);//设置标题的边距

  //设置X轴信息

  $graph->xaxis->title->Set('(单位:年)'); //标题

  $graph->xaxis->title->SetFont(FF_SIMSUN,FS_BOLD,10); //标题字体 大小

  $graph->xaxis->title->SetColor('black');//颜色

  $graph->xaxis->SetFont(FF_SIMSUN,FS_BOLD,10);//X轴刻度字体 大小

  $graph->xaxis->SetColor('black');//X轴刻度颜色

  $graph->xaxis->SetTickLabels($datax); //设置X轴标记

  $graph->xaxis->SetLabelAngle(0);//设置X轴的显示值的角度;

  //设置Y轴的信息

  $graph->yaxis->SetFont(FF_SIMSUN,FS_BOLD,10);//标题

  $graph->yaxis->SetColor('black');//颜色

  $graph->ygrid->SetColor('black@0.9');//X,y交叉表格颜色和透明度 @为程度值

  $graph->yaxis->scale->SetGrace(0);//设置Y轴显示值柔韧度(解释有点问题 呵呵 原谅)

  //设置数据

  $bplot1 = new BarPlot($datay);

  $bplot2 = new BarPlot($number);

  //设置柱状图柱颜色和透明度

  $bplot1->SetFillColor('orange@0.4');

  $bplot2->SetFillColor('brown@0.4');

  //设置值显示

  $bplot1->value->Show(); //显示值

  $bplot1->value->SetFont(FF_SIMSUN,FS_BOLD,10);//显示字体大小

  $bplot1->value->SetAngle(90); //显示角度

  $bplot1->value->SetFormat('%0.2f'); //显示格式 0.2f:精确到小属数点后2位

  $bplot2->value->Show();

  $bplot2->value->SetFont(FF_SIMSUN,FS_BOLD,10);

  $bplot2->value->SetAngle(90);

  $bplot2->value->SetFormat('%0.0f');

  //设置图列标签

  $graph->legend->SetFillColor('lightblue@0.9');//设置图列标签背景颜色和透明度

  $graph->legend->Pos(0.01,0.12,"right","center");//位置

  $graph->legend->SetFont(FF_SIMSUN,FS_NORMAL,10);//显示字体 大小

  $bplot1->SetLegend('消费金额(单位:万元)');

  $bplot2->SetLegend('人数(单位:万人次)');

  //设置每个柱状图的颜色和阴影透明度

  $bplot1->SetShadow('black@0.4');

  $bplot2->SetShadow('black@0.4');

  //生成图列

  $gbarplot = new GroupBarPlot(array($bplot1,$bplot2));

  $gbarplot->SetWidth(0.5); //柱状的宽度

  $graph->Add($gbarplot);

  $graph->Stroke(); //输出图像

  ?>

 

附件:  效果图.zip (25 K) 



Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software