搭建highcharts server,将图表发送至邮箱

发布于 2018-03-13 作者 shen100 1050次 浏览 版块 分享

安装highcharts-export-server

npm install highcharts-export-server -g

如果没有安装node.js,请先安装node.js

在安装的过程中,报错

Writing location.js file
Error checking path, continuing { Error: EACCES: permission denied, open '/usr/lib/node_modules/highcharts-export-server/node_modules/phantomjs-prebuilt/lib/location.js'
    at Object.fs.openSync (fs.js:652:18)
    at Object.fs.writeFileSync (fs.js:1299:33)
    at writeLocationFile (/usr/lib/node_modules/highcharts-export-server/node_modules/phantomjs-prebuilt/lib/util.js:84:6)
    at Promise._successFn (/usr/lib/node_modules/highcharts-export-server/node_modules/phantomjs-prebuilt/install.js:375:11)
    at nextTickCallback (/usr/lib/node_modules/highcharts-export-server/node_modules/kew/kew.js:47:28)
    at _combinedTickCallback (internal/process/next_tick.js:131:7)
    at process._tickCallback (internal/process/next_tick.js:180:9)
  errno: -13,
  code: 'EACCES',
  syscall: 'open',
  path: '/usr/lib/node_modules/highcharts-export-server/node_modules/phantomjs-prebuilt/lib/location.js' }

添加--unsafe-perm选项,然后再次运行

npm install highcharts-export-server -g --unsafe-perm

测试下highcharts server

找个工作目录,新建options.json, 其内容为

{"title":{"text":"不同城市的月平均气温","x":-20},"subtitle":{"text":"数据来源:WorldClimate.com","x":-20},"xAxis":{"categories":["一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"]},"yAxis":{"title":{"text":"温度(°C)"},"plotLines":[{"value":0,"width":1,"color":"#808080"}]},"tooltip":{"valueSuffix":"°C"},"legend":{"layout":"vertical","align":"right","verticalAlign":"middle","borderWidth":0},"series":[{"name":"东京","data":[7,6.9,9.5,14.5,18.2,21.5,25.2,26.5,23.3,18.3,13.9,9.6]},{"name":"纽约","data":[-0.2,0.8,5.7,11.3,17,22,24.8,24.1,20.1,14.1,8.6,2.5]},{"name":"柏林","data":[-0.9,0.6,3.5,8.4,13.5,17,18.6,17.9,14.3,9,3.9,1]},{"name":"伦敦","data":[3.9,4.2,5.7,8.5,11.9,15.2,17,16.6,14.2,10.3,6.6,4.8]}]}

然后运行highcharts-export-server --infile options.json --outfile chart.png, 如果在当前目录生成了chart.png,那么就安装成功了。

如果报以下的错,那么重新安装,再试下

uncaughtException: TypeError: "file" argument must be a non-empty string
    at normalizeSpawnArguments (child_process.js:384:11)
    at exports.spawn (child_process.js:497:38)
    at Object.exports.exec (/usr/local/lib/node_modules/highcharts-export-server/node_modules/phantomjs-prebuilt/lib/phantomjs.js:72:10)
    at spawnWorker (/usr/local/lib/node_modules/highcharts-export-server/lib/phantompool.js:127:32)
    at Object.init [as initPool] (/usr/local/lib/node_modules/highcharts-export-server/lib/phantompool.js:330:9)
    at Object.<anonymous> (/usr/local/lib/node_modules/highcharts-export-server/bin/cli.js:193:10)
    at Module._compile (module.js:653:30)
    at Object.Module._extensions..js (module.js:664:10)
    at Module.load (module.js:566:32)
    at tryModuleLoad (module.js:506:12)

如果还不行,那么看下错误信息中的 /usr/local/lib/node_modules/highcharts-export-server/node_modules/phantomjs-prebuilt/lib/phantomjs.js:72:10,
phantomjs.js中第72行报错了,

70	exports.exec = function () {
71	  var args = Array.prototype.slice.call(arguments)
72	  return spawn(exports.path, args)
73	}

如上,exports.path为null。我们再看下第18行到26行的代码

18	try {
19	  var location = require('./location')
20	  exports.path = path.resolve(__dirname, location.location)
21	  exports.platform = location.platform
22	  exports.arch = location.arch
23	} catch(e) {
24	  // Must be running inside install script.
25	  exports.path = null
26	}

看下第19行 var location = require(‘./location’), 而在/usr/local/lib/node_modules/highcharts-export-server/node_modules/phantomjs-prebuilt/lib/目录下却没有location.js,
我们新建个 location.js, 粘贴以下代码(注意module.exports.platform的值根据实际的系统)

module.exports.location = "/usr/local/bin/phantomjs"
module.exports.platform = "linux"
module.exports.arch = "x64"

然后再运行highcharts-export-server --infile options.json --outfile chart.png

启动highcharts server

通过下面的命令来启动端口为 3000 的 http 服务

highcharts-export-server --enableServer 1 --port 3000

安装nodemailer

npm install nodemailer

安装axios

npm install axios

示例

const nodemailer = require('nodemailer');
const axios = require('axios');

const params = {
    host: '',
    port: 465,
    sercure: true,
    auth: {
        user: '', 
        pass: ''
    }
}

const mailOptions = {
    from: '',
    to: [''],
    subject: '',
    html: ''
}

let chartData = {
    b64: true, // 返回base64字符串,而不是二进制
    infile: {
        title: {
            text: 'Steep Chart'
        }, 
        xAxis: {
            categories: [
                'Jan', 
                'Feb', 
                'Mar'
            ]
        }, 
        series: [
            {
                data: [
                    29.9, 
                    71.5, 
                    106.4
                ]
            }
        ]
    }
};

let url = 'http://127.0.0.1:3000';  // 如果highcharts-export-server  安装在另一台机器上,记得修改此处的ip

axios.post(url, chartData)
    .then(function (response) {
        console.log(response);
        if (response && response.status === 200) {
            let imgBase64 = 'data:image/png;base64,' + response.data;
            mailOptions.html = [
                `<div>`,
                `<img src=${imgBase64} />`,
                `</div>`,
            ].join('');

            const transporter = nodemailer.createTransport(params)
            transporter.sendMail(mailOptions, (error, info) => {
                if (error) {
                    return console.log(error);
                }
                console.log('Message %s sent: %s', info.messageId, info.response);
                process.exit(0);
            });
        }
    })
    .catch(function (error) {
        console.log(error);
    });
收藏
2回复
hujunyuan80581楼•2018-03-17

bucuo

liushen2楼•2018-03-17

up