使用Highcharts Node.js模块生成图像时遇到的问题

问题描述 投票:0回答:1

我已经在Visual Studio 2019中创建了项目Node Js Console Application。添加了Node V12.16.3npm v6.14.5

我添加了具有以下许可条款的节点js导出服务器软件包。

enter image description here

服务器软件包版本详细信息

enter image description here

[Package.Json代码

{
    "name": "prj.services.export.high-chart-export-engine",
    "version": "0.0.0",
    "description": "prj.Services.Export.HighChartExportEngine",
    "main": "app.js",  
    "author": {
        "name": ""
    },
    "dependencies": {
         "highcharts-export-server": "^2.0.28"
    }
}

下面是我的app.js代码

var exportSettings = {
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).

//Kill the pool when we're done with it, and exit the application
exporter.killPool();
process.exit(1);
});

返回base64,如果我将其转换为base64图像,则比创建具有2KB大小的空白图像要大。

node.js highcharts
1个回答
0
投票

我使用了与您相同的配置,并使用生成的base64字符串显示了正确的图像。看下面的例子。确保正确使用字符串,不要忘记在实际字符串之前添加所需的信息(例如data:image/png;base64)。顺便说一句。如果您使用的是Highcharts v7.0.0及更高版本,则不需要获取样式模式的脚本(更多信息在此处:https://www.highcharts.com/docs/chart-design-and-style/style-by-css)。

示例:https://jsfiddle.net/BlackLabel/u4sro916/

[UPDATE]

writeFileSync的异步版本就是writeFile。我已经使用该功能将文件另存为PNG,并且可以按预期工作。这是导出的回调的更新代码:

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).

    // Kill the pool when we're done with it, and exit the application
    exporter.killPool();

    fs.writeFile('image.png', res.data, 'base64', function(err) {
        if (!err) {
            console.log('File successfully created!');
        }
        process.exit(1);
    });
});
© www.soinside.com 2019 - 2024. All rights reserved.