【问题标题】:HighChart - Save Image into myChart.png fileHighChart - 将图像保存到 myChart.png 文件中
【发布时间】:2018-05-12 05:47:11
【问题描述】:

我正在尝试使用 node.js 将 highchart 图形保存到图像 (.png) 文件中

curl -H "Content-Type: application/json" -X POST -d '{"infile":{"title": {"text": "Steep Chart"}, "xAxis": {"categories": [“一月”、“二月”、“三月”]}、“系列”:[{“数据”:[29.9、71.5、106.4]}]}}' 127.0.0.1:7801 -o mychart.png

使用上面的 curl,我可以得到图像。我正在尝试使用节点。

在此示例中,要将 highchart 图像保存到文件中,需要进行哪些更改才能将 .png 文件作为我的输出?

//Include the exporter module
const exporter = require('highcharts-export-server');

//Export settings 
var exportSettings = {
    fileName  : 'myChart',     // The name of the chart
    file      : 'myChart.png', // The name of the chart plus its extension
    type      : 'png',
	options: {
        title: {
            text: 'My Chart'
        },
        xAxis: {
            categories: ["Jan", "Feb", "Mar", "Apr", "Mar", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
        },
        series: [
            {
                type: 'line',
                data: [1, 3, 2, 4]
            },
            {
                type: 'line',
                data: [5, 3, 4, 2]
            }
        ]
    }
};

//Set up a pool of PhantomJS workers
exporter.initPool();

//Perform an export
/*
    Export settings corresponds to the available CLI arguments described
    above.
*/
exporter.export(exportSettings, function (err, res) {
    //The export result is now in res.
    //If the output is not PDF or SVG, it will be base64 encoded (res.data).
    //If the output is a PDF or SVG, it will contain a filename (res.filename).

	console.log('res : ' + res.data + ' : ' + res.filename);
	console.log('err : ' + err);
    //Kill the pool when we're done with it, and exit the application
    exporter.killPool();
    process.exit(1);
});

【问题讨论】:

    标签: node.js highcharts png


    【解决方案1】:

    这段代码对我来说很好用:

       //Include the exporter module
        const exporter = require('./node_modules/highcharts-export-server');
        var settings = require('./settings'); // exporting options
    
    
        //Set up a pool of PhantomJS workers
        exporter.initPool();
    
        //Perform an export
        /*
            Export settings corresponds to the available CLI arguments described
            above.
        */
        exporter.export(settings, function(err, res) {
    
    
          //If the output is not PDF or SVG, it will be base64 encoded (res.data).
          //If the output is a PDF or SVG, it will contain a filename (res.filename).
          //Kill the pool when we're done with it, and exit the application
    
          require("fs").writeFile("out.png", res.data, 'base64', function(err) {
            exporter.killPool();
            process.exit(1);
            console.log(err);
          });
    
          console.log(err);
    
        });
    

    我正在使用文件系统对象 (fs) 将其从 base64 编码转换并保存为文件。 exproter.killPoolprocess.exit 在保存文件结束(或失败)后执行的回调函数中执行。

    【讨论】:

      猜你喜欢
      • 2013-05-31
      • 1970-01-01
      • 1970-01-01
      • 2011-05-08
      • 1970-01-01
      • 2021-07-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多