如何使用 Laravel 生成图表?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18222044/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to generate a chart using Laravel?
提问by syamimi
I'm working with Laravel 3. I want to make a reporting page on the website. I have some view table as per below:
我正在使用 Laravel 3。我想在网站上制作一个报告页面。我有一些视图表,如下所示:
+---------+-----------------+-------+
| user_id | username | total |
+---------+-----------------+-------+
| 1 | user | 12 |
| 2 | admin | 3 |
| 3 | user2 | 1 |
| 4 | user3 | 1 |
+---------+-----------------+-------+
I want to show the data in chart view. What is the best way to make it?
我想在图表视图中显示数据。制作它的最佳方法是什么?
回答by devXen
I second phpChart. Used it in the past for an online report task. Very easy to create charts quickly.
我第二个phpChart。过去将其用于在线报告任务。非常容易快速创建图表。
Here's the solution to your scenario using phpChartbased on their online example-- Axis Labels Rotated Text 2:
这是使用phpChart基于他们的在线示例- Axis Labels Rotated Text 2的方案的解决方案:
<?php
$line = array(array('user', 12), array('admin', 3), array('user2', 1), array('user3', 1));
$pc = new C_PhpChartX(array($line),'user_chart');
$pc->add_plugins(array('canvasTextRenderer'));
//set series
$pc->add_series(array('renderer'=>'plugin::BarRenderer'));
//set axes
$pc->set_axes(array(
'xaxis' => array(
'renderer'=>'plugin::CategoryAxisRenderer',
'tickRenderer'=>'plugin::CanvasAxisTickRenderer'),
'yaxis' => array(
'autoscale'=>true,
'tickRenderer'=>'plugin::CanvasAxisTickRenderer')
));
$pc->draw(800,500);
?>
Result:
结果:
Change the 6th line to PieRenderer you will get a pie chart.
将第 6 行更改为 PieRenderer,您将获得一个饼图。
<?php
$line = array(array('user', 12), array('admin', 3), array('user2', 1), array('user3', 1));
$pc = new C_PhpChartX(array($line),'chart_1');
$pc->add_plugins(array('canvasTextRenderer'));
//set series
$pc->add_series(array('renderer'=>'plugin::PieRenderer'));
//set axes
$pc->set_series_default(array(
'renderer'=>'plugin::PieRenderer',
'rendererOptions'=>array('showDataLabels'=>true)));
$pc->set_legend(array('show'=>true,
'rendererOptions'=> array('numberRows'=> 1),
'location'=> 's'));
$pc->draw(800,500);
?>
Here's a great intro on Codeproject I found: http://www.codeproject.com/Articles/604542/Creating-Interactive-HTML5-Graphs-in-PHP
这是我找到的关于 Codeproject 的精彩介绍:http: //www.codeproject.com/Articles/604542/Creating-Interactive-HTML5-Graphs-in-PHP
回答by Rubens Mariuzzo
Laravel doesn't provides any charting library out of the box. You need to find a third party library written in PHP to generate charts from your Laravel app.
Laravel 没有提供任何开箱即用的图表库。你需要找到一个用 PHP 编写的第三方库来从你的 Laravel 应用程序生成图表。
Some free library for charting are:
一些免费的图表库是:
pChart - A PHP class to build charts. Sample chart shown below:
phpCHART - Make HTML5 Charts in PHP. Sample chart shown below:
pChart - 用于构建图表的 PHP 类。示例图表如下所示:
phpCHART - 用 PHP 制作 HTML5 图表。示例图表如下所示:
I strongly encourage you to search for php charting libraryhere in StackOverflow to see the opinion from other experienced users.
我强烈建议您在 StackOverflow 中搜索 php 图表库,以查看其他有经验的用户的意见。
When choosing a particular library add it to you composer.json
file as any other dependency.
选择特定库时,将其composer.json
作为任何其他依赖项添加到您的文件中。