上篇文章中介绍了phantomjs的使用场景,方法。

本篇文章详细介绍使用php,highcharts 结合phantomjs纯后台生成图片。包含一步步详细的php代码

一.highcharts 结合phantomjs纯后台生成图片系列的准备:

下载phantomjs解析插件,从highcharts官方下载所需插件.

新建一个工程文件夹phantomjs,所必备的js文件有:

highcharts 结合phantomjs纯后台生成图片系列二之php

其中jquery.js为 v1.7.1;

highcharts-convert.js的下载地址可去github上下载.

二.highcharts 结合phantomjs纯后台生成图片系列highcharts-convert.js的使用

highcharts官方文档有关于highcharts-convert.js的使用介绍:

PhantomJS is started from the command line with our highcharts-convert.js script as first parameter. With the other command line parameters we pass over the Highcharts configuration, the name of the output file and parameters for the graphical layout. Example usage on the command line:

1
phantomjs highcharts-convert.js -infile options.js -outfile chart.png -scale 2.5 -width 300

参数说明如下:

highcharts 结合phantomjs纯后台生成图片系列二之php

详细说明请点这里.

我们知道highcharts在页面上展示时,是先通过php从表中取出数据后,组装好数组后,以json串传给highcharts即可。

那么看见上面的命令行,我们大概知道把 json串放在option.js文件里即可,那么,是不是这样呢?

1.先看一个例子:

1>infile.json:

1
2
3
4
5
6
7
8
{
 xAxis: {
 categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
 },
 series: [{
 data: [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4]
 }]
};

使用过highcharts图表的童鞋,一看到这个写法就知道,其实就是对x轴和y轴赋值。详细highcharts的用法,可参考中文文档

2>callback.js:

1
2
3
4
5
6
7
function(chart) {
 chart.renderer.arc(200, 150, 100, 50, -Math.PI, 0).attr({
 fill : '#FCFFC5',
 stroke : 'black',
 'stroke-width' : 1
 }).add();
}

关于callback.js的作用,英文是这样解释的:

1
The callback is a function which will be called in the constructor of Highcharts to be executed on chart load. All code of the callback must be enclosed by a function.

本人英文水平一般,在这里就不再翻译,大概的意思就是负责渲染highchart图表,包括坐标位置,什么颜色填充,尺寸大小等参数。

3>执行:

phantomjs highcharts-convert.js -infile infile.json  -callback callback.js -outfile a.png -width 2400 -constr Chart -scale 1

4>回车后,输出如下:

highcharts 结合phantomjs纯后台生成图片系列二之php

5>看看phantomjs目录下,生成了一个a.png:

highcharts 结合phantomjs纯后台生成图片

很明显,这就是一个由highcharts生成的图片。也就告诉我们之前猜想的是对的:

  1. 将提供数据的json串放入infile.json里;
  2. 通过在回调函数callback.js来渲染,就生了一张highchaarts图片

三.highcharts 结合phantomjs纯后台生成图片系列生成json串

看到这里,就知道了highcharts 结合phantomjs纯后台生成图片的重点就在于option.js里的json串怎么是生成的。

下面贴出我项目中写好的一个生成这个json串的方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/**
 * 生成json数据串
 */
 public function generatecharts()
 {
 $aperiod = 'month';
 $date = date("Y-m");
 $date = Yii::app()->request->getParam('date',date("Y-m"));
 
// 拿到要查询的日期
 if(date("m",strtotime($date)) == date("m",time())) // 查询日期是本月时,查询前一天的数据
 $date = date("Y-m-d",strtotime("-1 day"));
 else
 $date = date("Y-m-t",strtotime($date)); // 获取上月最后一天的月数据
 
$data_type = 3; // 月数据
 $org_type = 0; // 整站
 
// 获取数据,趋势图表展示用
 $charts_data = Report::getSplineCharts($date,$data_type,$org_type);
// ***************************************************************
 $series = $charts_data['yAxisChart1'];
 $predictChart = $charts_data['predictChart1'];
 $month = $charts_data['month'];
 $xAxis = $charts_data['xAxis'];
 $text = "$month"."月销售签约目标:".number_format($predictChart);
 
$result = '';
 $start_fake_json = "{";
 $result .= $start_fake_json;
 
$end_fake_json = "
 credits: {enabled: false},
 chart: {
 renderTo: 'chart3',
 zoomType: 'xy'
 },
 legend: {
 verticalAlign: 'top',
 y: 10,
 borderWidth: 1
 },
 
title: {
 text: ''
 },
 xAxis:{
 categories: $xAxis,
 },
 
plotOptions: {
 series: {
 dataLabels: {
 enabled: true
 },
 }
 },
 
yAxis: {
 min: 0,
 title: {
 text: ''
 },
 gridLineColor:'#EEE',
 plotLines: [{
 width: 1,
 color: '#aa4643',
 value: $predictChart,
 label: {
 text: '$text'
 },
 zIndex: 1
 }],
 },
 series: $series
 }";
 
$result .= $end_fake_json;
 // echo $result;exit;
 // 新建文件
 $json_file_name = dirname(Yii::app()->BasePath).'/email_png'.'/201507'.'/json/'.date("Y-m-d")."_total_num.json";
 // echo $json_file_name;exit;
 $fp = fopen($json_file_name, "w+"); // 打开文件指针,创建文件
 if(!is_writable($json_file_name))
 {
 die("文件:".$json_file_name."不可写,请检查!");
 }
 fwrite($fp,$result);
 fclose($fp);
 
return $json_file_name;
 
}

上面的方法,简要总结一下就是从表中读取想要拿到的数据,然后,拼装成highcharts格式的字符串,这样,前面一直说的json串就生成了,下一步就是愉快的采用命令行去调用了。

1
2
3
4
5
6
7
8
9
10
11
// 拿到 json
      $infile = $this->generatecharts();
$highcharts_convert = "....../highcharts-convert.js" ; //此处是highcharts_convert.js文件的绝对路径
$outfile = "....../img" // 此处是生成的图片要存放的路径,可根据你的需要自行设置
 
本项目我是使用的yii的console command来执行php脚本的.
// 执行命令
$command = "phantomjs $highcharts_convert -infile $infile -outfile $outfile -scale 2.5 -width 800 -constr Chart";
// 放在命令行执行
exec($command,$output,$return_var);
// ......

附上我最终的结果图:

highcharts 结合phantomjs纯后台生成图片系列二之php

四.总结

  1. 纯后台生成highcharts图片首先选对神器phantomjs;
  2. 弄清楚highcharts-convert.js的用法
  3. 有思路写清楚提供数据的这个json串怎么生成
  4. callback.js这个回调函数来渲染图片
  5. 最后,图片就生成了,大功告成。

五.参考
1.http://javascript.ruanyifeng.com/tool/phantomjs.html
2.http://www.highcharts.com/docs/export-module/render-charts-serverside
3.

highcharts 结合phantomjs纯后台生成图片系列二之php2的更多相关文章

  1. highcharts 结合phantomjs纯后台生成图片系列二之php

    上篇文章中介绍了phantomjs的使用场景,方法.本篇文章详细介绍使用php,highcharts 结合phantomjs纯后台生成图片. 一.准备: 下载phantomjs解析插件,从 highc ...

  2. highcharts 结合phantomjs纯后台生成图片

    highcharts 结合phantomjs纯后台生成图片 highcharts 这个图表展示插件我想大家应该都知道,纯javascript编写,相比那些flash图表插件有很大的优势,至少浏览器不用 ...

  3. [知识库分享系列] 二、.NET(ASP.NET)

    最近时间又有了新的想法,当我用新的眼光在整理一些很老的知识库时,发现很多东西都已经过时,或者是很基础很零碎的知识点.如果分享出去大家不看倒好,更担心的是会误人子弟,但为了保证此系列的完整,还是选择分享 ...

  4. Web 开发人员和设计师必读文章推荐【系列二十九】

    <Web 前端开发精华文章推荐>2014年第8期(总第29期)和大家见面了.梦想天空博客关注 前端开发 技术,分享各类能够提升网站用户体验的优秀 jQuery 插件,展示前沿的 HTML5 ...

  5. Web 开发精华文章集锦(jQuery、HTML5、CSS3)【系列二十七】

    <Web 前端开发精华文章推荐>2014年第6期(总第27期)和大家见面了.梦想天空博客关注 前端开发 技术,分享各类能够提升网站用户体验的优秀 jQuery 插件,展示前沿的 HTML5 ...

  6. Web 前端开发精华文章推荐(HTML5、CSS3、jQuery)【系列二十三】

    <Web 前端开发精华文章推荐>2014年第2期(总第23期)和大家见面了.梦想天空博客关注 前端开发 技术,分享各类能够提升网站用户体验的优秀 jQuery 插件,展示前沿的 HTML5 ...

  7. Web 前端开发精华文章推荐(HTML5、CSS3、jQuery)【系列二十二】

    <Web 前端开发精华文章推荐>2014年第一期(总第二十二期)和大家见面了.梦想天空博客关注 前端开发 技术,分享各类能够提升网站用户体验的优秀 jQuery 插件,展示前沿的 HTML ...

  8. 【圣诞特献】Web 前端开发精华文章推荐【系列二十一】

    <Web 前端开发精华文章推荐>2013年第九期(总第二十一期)和大家见面了.梦想天空博客关注 前端开发 技术,分享各种增强网站用户体验的 jQuery 插件,展示前沿的 HTML5 和  ...

  9. WPF入门教程系列(二) 深入剖析WPF Binding的使用方法

    WPF入门教程系列(二) 深入剖析WPF Binding的使用方法 同一个对象(特指System.Windows.DependencyObject的子类)的同一种属性(特指DependencyProp ...

随机推荐

  1. Sqlite: unable to open database file

    A database connect, there updated both queries (different statement, and regardless of order), after ...

  2. ARPA

    ARPA是英文Advanced Research Projects Agency的缩写,代表美国国防部高级研究计划署.是美国国防部高级研究计划管理局因军事目的而建立的,开始时只连接了4台主机,这便是只 ...

  3. thinkphp通行证服务,验证登录,注销登录

    <?php /** * 通行证服务 */ class PassportService extends Service { /** * 验证用户或者管理员是否已登录 * @return boole ...

  4. 使用OPTIMIZE TABLE命令来整理表碎片实践

    操作环境:ubuntu 14.10   mysql 5.6.25 对含有BLOB或TEXT字段的表,若经常做修改或删除类的操作,需要定期执行OPTIMIZE TABLE命令来整理碎片. 1.creat ...

  5. Careercup - Facebook面试题 - 6321181669982208

    2014-05-02 09:40 题目链接 原题: Given a number N, write a program that returns all possible combinations o ...

  6. ffmpeg 音频转码

    大多数厂家摄像机输出的音频流格式都是PCM,有一些场合(比如讲音视频流保存成Ts流)需要将PCM格式转成AAC格式.基本的思路是先解码得到音频帧,再将音频帧编码成AAC格式.编码和解码之间需要添加一个 ...

  7. 一个有趣的 SQL 查询(查询7天连续登陆)

    一个有趣的 SQL 查询 一个朋友有这样一个SQL查询需求: 有一个登录表(tmp_test),包含用户ID(uid)和登录时间(login_time).表结构如下: . row ********** ...

  8. [转]谈谈C++中的swap函数

    1,最通用的模板交换函数模式:创建临时对象,调用对象的赋值操作符. template <class T> void swap ( T& a, T& b ) { T c(a) ...

  9. 【BZOJ】【3759】Hungergame饥饿游戏

    博弈论/高斯消元 如果没有打开箱子这个操作,那么就是一个很裸的Nim游戏…… 但是有了打开箱子这个操作,就变得蛋疼了T_T 首先我们可以想到一种直接的做法:打开所有箱子,当然如果此时所有a[i]的xo ...

  10. 01-08-01【Nhibernate (版本3.3.1.4000) 出入江湖】NHibernate中的三种状态

    以下属于不明来源资料: 引入 在程序运行过程中使用对象的方式对数据库进行操作,这必然会产生一系列的持久化类的实例对象.这些对象可能是刚刚创建并准备存储的,也可能是从数据库中查询的,为了区分这些对象,根 ...