在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
一、 么是JpGraph
以前用PHP作图时必须要掌握复杂抽象的画图函数,或者借助一些网上下载的花柱形图、饼形图的类来实现。没有一个统一的chart类来实现图表的快速开发。 现在我们有了一个新的选择:JpGraph。专门提供图表的类库。它使得作图变成了一件非常简单的事情,你只需从数据库中取出相关数据,定义标题,图表类型,然后的事情就交给JpGraph,只需掌握为数不多的JpGraph内置函数(可以参照JpGraph附带例子学习),就可以画出非常炫目的图表! 一、 JpGraph安装方法: 1、先到http://www.aditus.nu/jpgraph/下载最新的版本。 2、确保你的PHP版本最低为4.04(最好是4.1.1),并且支持GD库。必须确保GD库可以正常运行,可以通过运行phpinfo()来查看GD库的信息是否存在的方法来判断。同时要有要求GD库的版本应为2.0,而不是1.0。 3、将下载的JpGraph压缩包解压到任意文件夹。 4、设置jpgraph.php(jpgraph的主配置文件)。设置jpgraph的cache(缓存)文件夹,和TTF(字体)文件夹。 分别在35行和38行 35 // DEFINE("CACHE_DIR","/tmp/jpgraph_cache/"); 38 // DEFINE("TTF_DIR","/usr/X11R6/lib/X11/fonts/truetype/"); Linux系统改为: DEFINE("CACHE_DIR","/tmp/jpgraph_cache/");' DEFINE("TTF_DIR","/usr/X11R6/lib/X11/fonts/truetype/"); Windows系统改为: DEFINE("CACHE_DIR","c:/apache/htdocs/ jpgraph_cache/");' DEFINE("TTF_DIR","c:/windows/fonts"); 注意事项: (1)cache(缓存)文件夹路径可以自己定义,而TTF(字体)文件夹必须是%system%/Fonts。 (2)确保PHP对cache(缓存)文件夹有写的权限。 5、完成上述设置后就可以使用JpGraph了,可以先将JpGraph的例子copy到htdocs文件夹中,运行一下看看。呵呵,200多个例子,包含各类图表,够学一阵子的。 在实际使用中,笔者还遇到了一些问题,比如字体错误等等,还在研究中…… 从数据库中读取数据到jpgraph图表中 1、 将./src/Examples目录中的文件example16.2.php以及./src目录中的文件jpgraph_bar.php、 jpgraph_gradient.php、jpgraph_line.php、jpgraph_plotmark.inc、jpgraph.php拷贝 到同一目录下。 2、建立数据库jpg,数据库表test 建立2个字段: id(主键):int number:int 并添加一些数据 3、修改example16.2.php 修改后的代码 <?php include ("jpgraph.php"); include ("jpgraph_line.php"); include ("jpgraph_bar.php"); $connect=mysql_connect("localhost","root",""); mysql_select_db("jpg",$connect); $query=mysql_query("select * from test",$connect); $i=0; while ($array=mysql_fetch_array($query)) { $l2datay[$i]=$array["number"]; $i++; } mysql_close($connect); // Create the graph. $graph = new Graph(400,200,"auto"); $graph->SetScale("textlin"); $graph->img->SetMargin(40,130,20,40); $graph->SetShadow(); // Create the bar plot $bplot = new BarPlot($l2datay); $bplot->SetFillColor("orange"); $bplot->SetLegend("Result"); // Add the plots to t'he graph $graph->Add($bplot); $graph->title->Set("Adding a line plot to a bar graph v1"); $graph->xaxis->title->Set("X-title"); $graph->yaxis->title->Set("Y-title"); $graph->title->SetFont(FF_FONT1,FS_BOLD); $graph->yaxis->title->SetFont(FF_FONT1,FS_BOLD); $graph->xaxis->title->SetFont(FF_FONT1,FS_BOLD); //$graph->xaxis->SetTickLabels($datax); //$graph->xaxis->SetTextTickInterval(2); // Display the graph $graph->Stroke(); ?> 4、刷新页面即可看到结果
【附加信息】 官方网站:http://www.aditus.nu/jpgraph/ 看看jpGraph能够达到的效果:
Line
plots can be easily adapted to almost any needs, it is possible to
adjust colors, transparency levels, line weight, line type (solid,
dotted, dashed etc), backgrounds, scale, axis positions and formatting.
Filled Line Plots
Line
pots can also be filled both with a solid color, a semi transparent
color or a gradient fill (both horizontal and vertical). The fill can
also be one or several vertical band between two X-values.
Step Line Plots
A
variant of plain line plot is the stepped line plot where each data
point is connected by two 90 degrees link as is illustrated below.
Line Plots With Markers
Data
values for line plots, scatter plots, radar plots etc. can be
emphasized with either one of the multiple builtin markers (both plain
and 3D) or if that is not enough it is also possible to use an
arbitrary image as marker. When using an image as a marker it is also
easy to set a specific scaling factor for the image to get a proper
size without first having to manipulate the image in an external
drawing program. The library will also cache any scaled image to reduce
plot time and the need to do re-scaling for every data position.
Line Plots With Inverted Y-axis
Line Plots With Values
Standard Bar plots
Horizontal Bar plots
Adding backgrounds and patterns to bar plots
Combined Line and Bar plots
As
a final example of Bar plots we show some examples on how it is
possible to combine bar and line plots. When adding a line plot to a
bar graph it is possible to choose which point on the bar would
correspond to the data point for the line (left, right or center
position). In the examples below we have chosen to align the line with
the center of the bars. In the last graph we have also added a vertical
pattern to show how it is possible to emphasize a particular interval.
Pie Plots
3D Pie plots
A
variant of Pie plot is to display the Pie in a 3D perspective. This has
all the formatting of the 2D pie but in addition it is possible to
define the inclination of the Pie to adjust the perspective.
Exploding Pie plots
As
a final example we show how to emphasize certain values by exploding
one or several Pie slices. This formatting is available for both 2- and
3D Pie plots.
Ring Plots
A
variant of the Pie plot is the ring plot. As can be expected this has
all the formatting of the ordinary Pie but in addition it is possible
to format a ring in the middle. The center ring can also display an
arbitrary text and have a user specified color.
Scatter plots
Linked scatter plots
A
variant of the scatter plot is the linked scatter plot. By enable the
links a line will be drawn that connects the data points in order. As
usual color, weight and style of the lines can be adjusted. In the
right graph we have also illustrated one of the pre-defined style of
axis positioning/formatting.
Combined Scatter and Line plots
Impulse Plots
Field Plots
Spline Curves
Geographic Map Plots
Stock Plots
Another
type of plots is the so-called Stock plots. The most common use of
these graphs is to display values from the stock market. Each data
point can display 4 values which are normally take as the
open-close-high-low values of various stocks.
Polar Plots (180 degrees)
Polar Plots (360 degrees)
Error plots
Error
plot is primarily used to display an error interval for each data
point. Usually a line plots is combined with an error plot to show the
errors for each data point.
Balloon plots
Radar Plots
Radar
plots are often used to visualize how a number of measurements compares
with goals. The human capabilities of detecting unregular vs regular
shapes makes this type of plot a very powerful way to illustrate
progress for a number of parameters.
Gantt Charts
The
Gantt chart is standard way to illustrate project plans or any
activities that is layed out in time. The Gantt charts in JpGraph are
extremely flexible and can easily be used to create very complex
illustration of timed activities.
Canvas plots
Spam challenge images
Additional plots available in the Pro-Version
More information about these types of plots are available in the information for the Pro-version
of JpGraph. The additional graph types available are Windrose plots,
Odometer plots and barcodes (both linear and 2-dimensional.)
|
2022-11-06
2022-08-15
2022-07-18
2022-08-17
2022-08-17
请发表评论