最近由于需要统计云平台资源使用情况,利用python简单的写了一个脚本,获取数据,然后生成html文件,利用highcharts生成图表,但是发现生成的html带有js的,通过邮件发送不过去,实现不了自动获取,并且生成邮件报告的目的。
经过一番搜索,找到了一个方法,可以轻松在服务端生成highcharts的图片
https://github.com/pesla/highcharts-phantomjs
也就是这个github里面的啦。是利用phantomjs 来生成的。比较方便快捷
# 这是它的基本用法,
phantomjs highcharts-convert.js -infile options1.json -outfile chart1.png -scale 2.5 -width 300 -constr Chart -callback callback.js
- 下载highcharts-phantomjs 里面所有js,放到指定目录下
- 下载phantomjs,这个直接可以在官网下载,下载完直接使用,不用安装。http://phantomjs.org/
- 生成json文件,及之前写的highcharts的图表js部分
#json文件
{chart: {
plotBackgroundColor: null,
plotBorderWidth: null,
plotShadow: false
},
credits: {
text:"云计算部",
href:'http://www.che001.com'
},
title: {
text: 'TITLENAME'
},
tooltip: {
pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
},
plotOptions: {
pie: {
allowPointSelect: true,
cursor: 'pointer',
dataLabels: {
enabled: true,
color: '#000000',
connectorColor: '#000000',
format: '<b>{point.name}</b>: {point.percentage:.1f} %'
}
}
},
series: [{
type: 'pie',
name: 'USING',
data: [
{
name: '空闲资源',
y: NOTUSAGE,
sliced: true,
selected: true
},
# 这个JSON_DATA是其他数据,
JSON_DATA
]
}]
}
- 生成图片
phantomjs highcharts-convert.js -infile options1.json -outfile chart1.png -scale 2.5 -width 300 -constr Chart -callback callback.js
# phantomjs 就是之前下载的phantomjs了,是个可执行文件,可以把它链接到环境变量里,也可以直接带全路径执行。
# highcharts-convert.js 就是在步骤1 下载的highcharts-phantomjs 里的js所有的文件,注意要全部下载,不能只下载highcharts-convert.js,因为js之间有调用。
# infile 就是刚才步骤3写的json文件了,里面定义各种图表信息。
# outfile 就是要生成的文件
# scale 是缩放比例 constr 是chart类型,callback是回调函数,可以不写。