php生成图表

tech2022-09-02  111

php生成图表

Data is everywhere around us, but it is boring to deal with raw data alone. That’s where visualization comes into the picture.

数据无处不在,但仅处理原始数据很无聊。 这就是可视化进入图片的地方。

If you take a look at the Google Trends plot below, you will realize that interest in Data Visualization has seen a steady growth over the years.

如果您查看下面的Google趋势图 ,您将意识到多年来对数据可视化的兴趣一直在稳定增长。

So, if you are dealing with data and are not already using some kind of charting component, there is a good chance that you are going to need one soon. That’s the reason I decided to make a list of libraries that will make the task of visualizing data easier for you.

因此,如果您正在处理数据并且尚未使用某种图表组件,则很有可能很快就会需要一个图表组件。 这就是我决定列出一个库列表的原因,该列表将使您更轻松地可视化数据。

But before we get started, you need to understand that there are two types of charting components: server side and client side. Both have their pros and cons, and what’s best for you completely depends on the objective of your project and the resources you have. I’ve covered client side charts in this post, but there is some overlap as you’ll see below.

但是在开始之前,您需要了解图表组件有两种类型:服务器端和客户端。 两者都有其优点和缺点,什么对您来说最好完全取决于您的项目目标和所拥有的资源。 我已经在这篇文章中介绍了客户端图表,但是您会在下面看到一些重叠。

服务器端与客户端图表生成 (Server Side vs Client Side Chart Generation)

Server side components usually generate image charts. They employ algorithms to convert data into chart images and increase server overhead. Apart from using the server’s processing power, image based charts increase the bandwidth consumption. Sometimes large image sizes lead to slow downloads, which might be annoying to end users.

服务器端组件通常会生成图像图表。 他们采用算法将数据转换为图表图像并增加服务器开销。 除了使用服务器的处理能力之外,基于图像的图表还会增加带宽消耗。 有时,大尺寸图片会导致下载缓慢,这可能会给最终用户带来烦恼。

The above limitations of server side charts can be overcome by using client side components (JavaScript based). A browser will fetch the JSON or XML data from the server and render the charts locally. Client side components also have a greater scope of customization. They are often interactive, support animation and can easily blend into the design of your application.

服务器端图表的上述限制可以通过使用客户端组件(基于JavaScript)来克服。 浏览器将从服务器获取JSON或XML数据,并在本地呈现图表。 客户端组件也具有更大的定制范围。 它们通常是交互式的,支持动画,并且可以轻松地融入您的应用程序设计中。

Some charting libraries (like FusionCharts) provide wrappers which allow you to write your code in PHP only. Using the PHP wrapper you can generate the required JavaScript and HTML code as a string on the server. You can then output these strings in the page to generate charts.

一些图表库(例如FusionCharts)提供了包装程序,使您仅可以用PHP编写代码。 使用PHP包装程序,您可以在服务器上生成所需JavaScript和HTML代码作为字符串。 然后,您可以在页面中输出这些字符串以生成图表。

With this in mind, let’s go through some options for charting that have PHP components.

考虑到这一点,让我们看一下具有PHP组件的图表选项。

Google图表(客户端) (Google Charts (Client Side))

Although it is JavaScript based, Google Charts has a couple of options to make your charts entirely in PHP. You can either use server-side code (in our case PHP) to get the data, or get one of the open-source wrappers to do it for you.

尽管它是基于JavaScript的,但是Google Charts有两个选项可以使您的图表完全使用PHP。 您可以使用服务器端代码 (在我们的示例中为PHP)来获取数据,也可以使用一种开放源代码包装程序为您完成数据。

The good thing about Google Charts, apart from it being completely free, is the fact that they have lots of code examples to help you get started quickly.

除了完全免费之外,Google Charts的好处还在于,它们具有许多代码示例来帮助您快速入门。

One important point to keep in mind about Google Charts is that you are not allowed to host Google’s JS files on your server.

关于Google图表要记住的重要一点是, 不允许在服务器上托管Google的JS文件。

使用Google图表创建图表: (Creating Charts Using Google Charts:)

I have plotted data from the SitePoint survey of most popular PHP frameworks in 2015. To see the live chart, go here and click ‘Run’.

我已经从2015年最流行PHP框架的SitePoint调查中绘制了数据。要查看实时图表,请转到此处并单击“运行”。

Here’s the HTML code for the chart: (you can view the full code here.)

这是图表HTML代码:(您可以在此处查看完整的代码 。)

<html> <head> <!--Load the AJAX API--> <script type="text/javascript" src="https://www.google.com/jsapi"></script> <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> <script type="text/javascript"> // Load the Visualization API. google.load('visualization', '1', {'packages':['corechart']}); // Set a callback to run when the Google Visualization API is loaded. google.setOnLoadCallback(drawChart); function drawChart() { var jsonData = $.ajax({ url: "getData.php", dataType:"json", async: false }).responseText; // Create our data table out of JSON data loaded from server. var data = new google.visualization.DataTable(jsonData); // Instantiate and draw our chart, passing in some options. var chart = new google.visualization.ColumnChart(document.getElementById('chart_div')); chart.draw(data, {width: 800, height: 400}); } </script> </head> <body> <!--Div that will hold the column chart--> <div id="chart_div"></div> </body> </html>

Here’s the getData.php file included in code above:

这是上面的代码中包含的getData.php文件:

<?php // It reads a json formatted text file and outputs it. $string = file_get_contents("data.json"); echo $string; // Instead you can query your database and parse into JSON. ?>

FusionCharts(客户端) (FusionCharts (Client Side))

FusionCharts is primarily famous for its JavaScript or jQuery charts, but not many developers are aware of its official PHP wrapper. You can leverage FusionCharts’ exhaustive library of more than 90+ interactive charts and 965 maps through its PHP wrapper. Their charts are highly customizable and they have a pretty active support system in place.

FusionCharts主要以其JavaScript或jQuery图表而闻名,但并不是很多开发人员都知道它的官方PHP包装器 。 您可以通过FusionChartsPHP包装器使用90多个交互式图表和965个地图的详尽库。 他们的图表是高度可定制的,并且已经有相当活跃的支持系统。

They have a nice collection of dashboards (many of which are available for free download) and a huge chart fiddles gallery for inspiration.

他们有一个很好的仪表板集合 (其中许多可以免费下载)和一个巨大的图表小提琴画廊,以寻求灵感。

An important point to keep in mind about FusionCharts is that it’s a paid option. Although the wrapper is free, you will need a license to use the library in commercial applications. That should not be as big a deal as it sounds as you can always download the free trial which has no feature restrictions.

关于FusionCharts的重要注意事项是它是付费选项。 尽管包装程序是免费的,但您需要获得许可才能在商业应用程序中使用该库。 听起来好像没什么大不了的,因为您可以随时下载没有功能限制的免费试用版。

使用FusionCharts创建图表: (Creating Charts Using FusionCharts:)

To see the chart live, go here and click ‘Run’.

要实时查看图表,请转到此处并单击“运行”。

Here is the PHP code to generate a column chart: (you can view the full code here)

这是生成柱形图PHP代码:(您可以在此处查看完整的代码 )

<html> <head> // Including FusionCharts library files <script src="fusioncharts.js"></script> </head> <body> <?php // Example of how to draw a chart using FusionCharts and PHP. // fusioncharts.php functions to help us easily embed the charts. include("fusioncharts.php"); ?> <?php // Create the chart - Column 2D Chart with data given in constructor parameter // Syntax for the constructor - new FusionCharts("type of chart", "unique chart id", "width of chart", "height of chart", "div id to render the chart", "type of data", "actual data") $columnChart = new FusionCharts("Column2D", "myFirstChart" , 780, 400, "chart-1", "jsonurl", "data.json"); // Render the chart $columnChart->render(); ?> <div id="chart-1"><!-- FusionCharts will render here--></div> </body> </html>

pChart(服务器端) (pChart (Server Side))

pChart is one of the few native PHP charting libraries that is still under active development. But, as discussed above, there are both pros and cons to being a purely server side charting component.

pChart是少数仍在积极开发中的本地PHP图表库之一。 但是,如上所述,纯服务器端图表组件既有优点也有缺点。

The main advantage is that you don’t have to worry about cross-device and cross-browser compatibility as pChart will generate images for you. But you lose the interactivity that client side libraries like FusionCharts and Google Charts offer.

主要优点是您不必担心跨设备和跨浏览器的兼容性,因为pChart会为您生成图像。 但是,您会失去FusionCharts和Google Charts等客户端库提供的交互性。

使用pChart创建图表: (Creating Charts Using pChart:)

If you are trying to get started with pChart, SitePoint has an excellent, if somewhat old tutorial for you: Charting with pChart.

如果您尝试开始使用pChart,SitePoint会为您提供一个很好的教程,甚至有些古老: pChart进行图表绘制 。

Here is the PHP code for the plot (download source code from here):

这是该绘图PHP代码(从此处下载源代码):

<?php /* CAT:Bar Chart */ /* pChart library inclusions */ include("../class/pData.class.php"); include("../class/pDraw.class.php"); include("../class/pImage.class.php"); /* Create and populate the pData object */ $MyData = new pData(); /* This is the data to be plotted */ $MyData->addPoints(array(2112,1005,703,620,482,420,346,306,293,231),""); $MyData->setAxisName(0,"Votes"); /* These are the data labels */ $MyData->addPoints(array("Laravel","Symfony2","Nette","Yii 2","CodeIgniter","PHPixie","Zend 2","No Framework","Yii 1", "Phalcon"),"Frameworks"); $MyData->setSeriesDescription("Frameworks","Framework"); $MyData->setAbscissa("Frameworks"); /* Create the pChart object */ $myPicture = new pImage(700,230,$MyData); /* Turn off Antialiasing */ $myPicture->Antialias = FALSE; /* Add a border to the picture */ $myPicture->drawRectangle(0,0,699,229,array("R"=>0,"G"=>0,"B"=>0)); /* Set the default font */ $myPicture->setFontProperties(array("FontName"=>"../fonts/pf_arma_five.ttf","FontSize"=>6)); /* Define the chart area */ $myPicture->setGraphArea(60,40,650,200); /* Draw the scale */ $scaleSettings = array("GridR"=>200,"GridG"=>200,"GridB"=>200,"DrawSubTicks"=>TRUE,"CycleBackground"=>TRUE); $myPicture->drawScale($scaleSettings); /* Write the chart legend */ $myPicture->drawLegend(580,12,array("Style"=>LEGEND_NOBORDER,"Mode"=>LEGEND_HORIZONTAL)); /* Turn on shadow computing */ $myPicture->setShadow(TRUE,array("X"=>1,"Y"=>1,"R"=>0,"G"=>0,"B"=>0,"Alpha"=>10)); /* Draw the chart */ $myPicture->setShadow(TRUE,array("X"=>1,"Y"=>1,"R"=>0,"G"=>0,"B"=>0,"Alpha"=>10)); $settings = array("Gradient"=>TRUE,"GradientMode"=>GRADIENT_EFFECT_CAN,"DisplayPos"=>LABEL_POS_INSIDE,"DisplayValues"=>TRUE,"DisplayR"=>255,"DisplayG"=>255,"DisplayB"=>255,"DisplayShadow"=>TRUE,"Surrounding"=>10); $myPicture->drawBarChart(); /* Render the picture (choose the best way) */ $myPicture->autoOutput("pictures/example.drawBarChart.simple.png"); ?>

ChartLogix PHP图表(服务器端) (ChartLogix PHP Graphs (Server Side))

If you require only basic charts in your application, then ChartLogix PHP Graphs can be a good option. It has a limited collection that contains most commonly used charts like bar, pie, line etc., and is priced at just $44.99 for unlimited commercial use.

如果您的应用程序仅需要基本图表,则ChartLogix PHP Graphs是一个不错的选择。 它具有有限的集合,其中包含最常用的图表,如条形图,饼图,折线图等,其无限商业用途的价格仅为$ 44.99。

Although not in active development, it still provides personal support to all the buyers. It renders the output as an image and offers customisation of layouts, fonts, colour schemes & some visual effects.

尽管没有积极发展,它仍然为所有买家提供个人支持 。 它将输出呈现为图像,并提供自定义布局,字体,配色方案和一些视觉效果。

使用ChartLogix PHP图表创建图表: (Creating Charts Using ChartLogix PHP Graphs:)

ChartLogix does not come with a free trial, but that’s not a big problem because you can always use their designer feature to get familiar with the code and design. In fact, this is going to save you a lot of time as you can evaluate it within minutes.

ChartLogix没有免费试用版,但这不是一个大问题,因为您始终可以使用其设计器功能来熟悉代码和设计。 实际上,这将为您节省大量时间,因为您可以在数分钟内对其进行评估。

Below is the code to generate a bar chart and you can visit this page to play with it.

下面是生成条形图的代码,您可以访问此页面进行操作。

<?php include( 'chartlogix.inc.php' ); $bar = new BarChart(); //---- THE STYLE $bar->setBackground( 'EEEEEE', 'EEEEEE' ); $bar->setPadding( 20 ); $bar->setDefaultFont( 'arial.ttf' ); $bar->setTitleStyle( 'arial.ttf', 15, '000000' ); $bar->setTitlePosition( 0, 0 ); $bar->setLegendWidth( 40.00 ); $bar->setLegendTextStyle( 'arial.ttf', 10, '000000', 10 ); $bar->setLegendKeyStyle( 10, 10 ); $bar->setLegendBoxStyle( 'FFFFFF', '000000', 10 ); $bar->setLegendPosition( 1, 0 ); $bar->setXAxisTextStyle( 'arial.ttf', 10, '000000', 0 ); $bar->setYAxisTextStyle( 'arial.ttf', 10, '000000' ); $bar->setColumnSpacing( 25 ); $bar->setStackedBarSpacing( 0 ); //---- THE DATA $bar->setTitle( 'ChartLogix Bar & Line Graph' ); $bar->addColumns( array( '2002', '2003', '2004', '2005', '2006', '2007' ) ); $bar->doBarSeries( 'Bananas', 'FFFF00', 'FFCC00' ); $bar->setValueStyle(); $bar->addData( '2002', 1498 ); $bar->addData( '2003', 358 ); $bar->addData( '2004', 675 ); $bar->addData( '2005', 1449 ); $bar->addData( '2006', 1335 ); $bar->addData( '2007', 137 ); //---- DRAW PNG $bar->drawPNG( 500, 400 ); ?>

结论 (Conclusion)

I hope you find the above libraries useful, but if you want more options do check out PHPGraphLib and JpGraph. PHPGraph is a lightweight server side library and completely free to use for all purposes. JpGraph on the other hand offers a trimmed down free version. Both of these libraries are not under active development now, but their current stable version still works well.

我希望上述库对您有用,但是如果您需要更多选项,请查看PHPGraphLib和JpGraph 。 PHPGraph是一个轻量级的服务器端库,可以完全免费用于所有目的。 另一方面,JpGraph提供了精简的免费版本。 这两个库目前都没有处于活跃的开发中,但是它们当前的稳定版本仍然运行良好。

Charting is a crowded space and this is in no way a complete list of tools available for PHP charting. Instead, I’ve tried my best to list the tools that have a proven track record and would make the task of creating beautiful charts easier for you.

制图是一个拥挤的空间,这绝不是PHP制图可用的工具的完整列表。 相反,我尽力列出了具有良好记录的工具,这些工具将使您轻松创建漂亮的图表。

Do you use another PHP charting tool that I didn’t include here? Would love to learn about it in the comments section below.

您是否使用了我未在此处提供的另一个PHP图表工具? 希望在下面的评论部分中了解它。

翻译自: https://www.sitepoint.com/4-best-chart-generation-options-php-components/

php生成图表

相关资源:php生成图表库图片
最新回复(0)