`
天梯梦
  • 浏览: 13638263 次
  • 性别: Icon_minigender_2
  • 来自: 洛杉矶
社区版块
存档分类
最新评论

codeigniter 操作 图标

阅读更多

简单型

 

Charting

Panaci: A Charting Library

Panaci is an application library for CodeIgniter.
If your application needs to output graphic charts at run-time, such as bar, line
area, step or impulse charts, then Panaci makes this rather easy.It utilises the Panachart charting class released under the GPL by Eugen Fernea.

Features

Compact (23k) and efficient code.
Bandwidth friendly, the average image size for a 450*250 image is around 2K
Can output to a file or directly to an HTTP stream
Supports several plot types eg. bars, lines, dots, areas, step, impulse
Supports multiple series plots on the same image
Supports automatic scaling of plot area by axis labels size

An example plot

75cbed1cdb9c912906a8f13b83d0b9b4

Basic usage

From your controller

 

$params = array('width' => 450, 'height' =>  250, 'margin' => 15, 'backgroundColor' =>  '#eeeeee');
$this->load->library('chart', $params); 
 

From your Controller function

 

function graph()
{
  $data_2001 = array(43,163,56,21,0,22,0,5,73,152,123,294);
  $data_2002 = array(134,101,26,46,22,64,0,28,8,0,50,50);
  $Labels = array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');

  $this->chart->setTitle("Annual Rainfall","#000000",2);
  $this->chart->setLegend(SOLID, "#444444", "#ffffff", 2);
  $this->chart->setPlotArea(SOLID,"#444444", '#dddddd');
  $this->chart->setFormat(0,',','.');

  $this->chart->addSeries($data_2001,'dot','2001 ', SOLID,'#00ff00', '#00ff00');
  $this->chart->addSeries($data_2002,'area','2002 ', SOLID,'#ff0000', '#00ffff');

  $this->chart->setXAxis('#000000', SOLID, 1, "2001");
  $this->chart->setYAxis('#000000', SOLID, 2, "Rainfall in MM");
  $this->chart->setLabels($Labels, '#000000', 1, HORIZONTAL);
  $this->chart->setGrid("#bbbbbb", DASHED, "#bbbbbb", DOTTED);
  $this->chart->plot('./images/file.png');

  $this->load->view('graph');
} 
 

Demo and Documentation

There is an online demo available at Panaci

Download

A download is available from here Panaci

 

 

 

========================================

 

pChart是一个免费的PHP图表生成库,可以生成多种图表如饼图或者柱状图等等, 需要GD库的支持 。下面我来简单讲讲如何在CI中方便地使用它。

首先我们要下载 pChart。访问http://pchart.sourceforge.net/download.php 就可以下载到最新版的pChart,目前最新的版本是1.27。解压下载到的文件 ,我们要用到的只是其中的pChart文件夹,里面有pChart.class、pCache.class和pData.class这三个文件。我们把pChart文件夹复制到
application/libraries / 下面。

然后要准备字体,因为我们做报表很可能要输出中文 ,所以必须使用一种中文字体,至于选什么字体就看你的喜好了(如果是商业用途的话请注意字体的版权以免引起版权纠纷),把中文字体的ttf文件复制到
application/libraries/pChart 下面即可。

通过库的形式 来使用pChart,因此在
application/libraries/ 下面创建一个文件,命名为 Chart.php,代码如下:

PHP
<?php
class Chart {
function Chart() {
include(APPPATH."libraries/pChart/pData.class");
include(APPPATH."libraries/pChart/pChart.class");
}
function draw_line_graph($params) {
$DataSet = new pData;
$DataSet->AddPoint($params['data'],"Serie1");  //需要显示的数据
$DataSet->AddPoint($params['date'],"Serie2"); //横坐标的数据
$DataSet->AddSerie("Serie1");
$DataSet->SetAbsciseLabelSerie("Serie2");
$DataSet->SetSerieName("订单总金额","Serie1");
$DataSet->SetYAxisName("RMB"); //纵坐标上显示的文字
$DataSet->SetXAxisName('横坐标:日期'); //横坐标上显示的文字
$DataSet->SetXAxisFormat("date"); //横坐标的数据类型

$Test = new pChart($params['height'],$params['width']); //图表文件的高度和宽度
$Test->setDateFormat($params['date_format']); //横坐标显示的日期格式
$Test->setColorPalette(0,255,0,0);

$Test->setFontProperties(APPPATH."libraries/pChart/FZLTXIHK.ttf",12); //设置使用的字体及字号
$Test->setGraphArea(60,60,$params['x_area'],$params['y_area']); //图形区域的高度和宽度
$Test->drawGraphArea(252,252,252); //线的颜色
$Test->drawScale($DataSet->GetData(),$DataSet->GetDataDescription(),SCALE_NORMAL,150,150,150,TRUE,0,2);
$Test->drawGrid(4,TRUE,230,230,230,255);

$Test->drawLineGraph($DataSet->GetData(),$DataSet->GetDataDescription());
$Test->drawPlotGraph($DataSet->GetData(),$DataSet->GetDataDescription(),3,2,255,255,255);
$Test->setFontProperties(APPPATH."libraries/pChart/FZLTXIHK.ttf",8); //设置数据值所用字体及字号
$Test->writeValues($DataSet->GetData(),$DataSet->GetDataDescription(),"Serie1"); //输出每个点的数据值

$Test->setFontProperties(APPPATH."libraries/pChart/FZLTXIHK.ttf",11); //设置使用的字体及字号
$Test->drawLegend(75,65,$DataSet->GetDataDescription(),255,255,255);

$Test->setFontProperties(APPPATH."libraries/pChart/FZLTXIHK.ttf",12); //设置使用的字体及字号
$Test->drawTitle(60,22,$params['title'],50,50,50,585);

$imagefile='public/temp/'.$params['filename'].'.png'; //设置生成文件的保存路径
$Test->Render($imagefile);   //生成文件

return $imagefile;  //返回文件名
}
}
 

控制器

PHP


function test(){
$this->load->library('chart'); //载入pChart库

$params['data']=array(100,200,150,600,230,150,510); //要显示的数据
for($i=0;$i<7;$i++){
$params['date'][$i]=1250217066+$i*86400; //要显示的日期,注意这里是Unix时间戳,pChart会自动传换成你要的格式
}
$params['title']='销售报表'; //图片标题
$params['date_format']='m月j日';//设置日期格式
$params['filename']='test_image';  //文件名    
$params['height']=600; //高度
$params['width']=300; //宽度
$params['x_area']=560; //图形区域高度
$params['y_area']=280; //图形区域宽度
$data['chart_image']=$this->chart->draw_line_graph($params);//生成图片
$data['baseurl']=site_url();
$this->load->view('test_view.html',$data);
}
 

视图:test_view.html




<html>
<head><title></title></head>
<body>
<img src="<?php echo $baseurl.$chart_image;?>" />
</body>
</html>
 

最终生成的图表是这样的:
test_image.png
关于pChart的更多用法,请参考它的在线文档:
http://pchart.sourceforge.net/documentation.php

 

 

 

 

=======================================================

 

 

<!-- // end:header.php //--> <!-- CONTENT -->

Codeigniter: Intergrating OpenFlashCharts

Yes I know, it has been some time since I last posted anything on the site. Things have just been crazy at work, trying to get up to speed on using Pentaho for a major project. In fact, I might start posting some Pentaho related topics in the future. There’s definitely a need for more help and guides on using Pentaho for beginners.

 

Anyway, one of the things on the project was to pull data from Pentaho and display it inside OpenFlashCharts on a CI platform. If you don’t know what OpenFlashCharts is, go visit the website. It’s a pretty awesome kit.

 

There’s been quite a bit of chatter on the net about integrating CI with OpenFlashCharts, but ever since version 2 came out there have been more questions about how to do it.

In the new version of OpenFlashCharts, it uses the JSON format to describe what type of chart to render in the flash object. OFC comes with a bunch of libraries (in various programming languages) which will generate the JSON format for the flash object. For PHP, the kit comes with the generic PHP version and a PHP5 version.

 

Somebody by the name of Thomas did managed to stitch OpenFlashCharts 2 with CI and he posted it up on the CI wiki. So kudos to Thomas (whoever you are) for making my life easier. If you like you can download his library and give it a spin.

 

For Thomas, he used the PHP5 libraries of OpenFlashCharts, which unfortunately is a bit incomplete. I wasn’t able to generate more advance graphs such as Hollow Areas, Dotted lines etc. The flash object gave me an ‘infinity’ error. After some investigation, I found that the OFC PHP5 libraries are not generating the same JSON as the generic PHP version. Taking a page out of his book, I modified his library to work with the generic PHP version instead.

 

One advantage of doing this is that suddenly, you can apply all the tutorial codes on the OFC website because they were written for the generic PHP library and not the PHP5 version.

 

Download: download icon

 

So, download the zip file and extract the files to the relevant folders of your CI install. The assets folder should go to wherever you put your stuff like images and javascript files. Just make sure you change the view to reflect the correct path. I put my assets in the root of my webserver folder, and I access my CI through http://dev.ci/ .

 

When I run the http://dev.ci/charts , I would get this graph with 3 data lines.

 

OFC Test Chart

 

So how does it all work?
The OFC library, php-ofc-library and the OpenFlashChartLib.php can now be accessed by calling the load library call.

 

$this->load->library('OpenFlashChartLib', NULL, 'OFCL');
 

If you look inside the view folder for chart_view.php , you can see where the OFC flash object is getting its JSON feed from, http://dev.ci/charts/get_data .

 

Inside get_data() function of the charts controller, it’s basically the same code as the tutorial at http://teethgrinder.co.uk/open-flash-chart-2/data-lines-2.php

 

The difference here is that instead of calling the

 

$chart = new open_flash_chart();

 

I’m now doing

 

$chart = $this->OFCL->create('open_flash_chart');
 

to perform the same instantiation but through the OpenFlashChartLib (OFCL) library.

So, just change all the object instantiation call accordingly, and the graph should show up nice and neat.

Here’s Dilbert to close out…

 

 

Dilbert Pie Chart

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics