2017-11-28 33 views
0

私は画像(.PNG)ファイルにhighchartグラフを保存しようとしています使用してのNode.jsHighChart - myChart.pngファイルに画像を保存

カール-H "のContent-Type:アプリケーション/ JSON" -X POST -d '{"infile":{"title":{"text": "スティープチャート"} "xAxis":{"categories": ["Jan"、 "Feb"、 "Mar" ]}、 "シリーズ":[{ "データ":[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', 
 
\t 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). 
 

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

答えて

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

    }); 

私はbase64エンコードからそれを翻訳し、ファイルとして保存するファイル・システム・オブジェクト(fs)を使用しています。 exproter.killPoolprocess.exitは、ファイルの保存後に実行されるコールバック関数で実行されます(または失敗します)。