HighChart - 将图像保存到myChart.png文件中

时间:2017-11-28 17:06:18

标签: node.js highcharts png

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

  

curl -H“Content-Type:application / json”-X POST -d   '{“infile”:{“title”:{“text”:“Steep Chart”},“xAxis”:{“categories”:   [“Jan”,“Feb”,“Mar”]},“系列”:[{“data”:[29.9,71.5,106.4]}]}}'   127.0.0.1:7801 -o mychart.png

使用上面的卷曲,我可以获得图像。我试图使用节点。

在此示例中,要将高图图像保存到文件,要将.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);
});

1 个答案:

答案 0 :(得分:0)

此代码适用于我:

   //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在保存文件结束(或失败)后执行的回调函数中执行。