从json文件生成图表的PNG图像

时间:2018-03-20 21:07:49

标签: highcharts

我在Highcharts图表中显示大型数据库的内容,我想自动捕获图表的图像,比如每天晚上9点,并将图像保存在文件中。我曾尝试安装服务器端导出服务器解决方案,但我无法使其在raspberry pi上运行。

作为替代方案,我已经捕获了用于在文件中生成图形的json。我可以将文件发送到Highcharts并获得PNG图像作为回报吗?我究竟该如何做到这一点? 感谢。

json文件包含以下内容。这与传递给浏览器的json完全相同,并生成一个正确的图形:

[{
    "name": "Consumed",
    "data": [
        [1521612002000, 668],
        [1521612182000, 668],
        [1521634338000, 487]
    ]
}, {
    "name": "Paid For",
    "data": [
        [1521612002000, 668],
        [1521612182000, 668],
        [1521634338000, 75]
    ]
}, {
    "name": "Produced",
    "data": [
        [1521612002000, 0],
        [1521612182000, 0],
        [1521634338000, 412]
    ]
}, {
    "name": "Paid For kWh",
    "data": [
        [1521612002000, null],
        [1521615425000, 0.6118],
        [1521634338000, null]
    ]
}]

我尝试过以下curl命令:

curl -H "Content-Type: application/json" -X POST -d '{"infile": combined_readings.json}' http://export.highcharts.com > file.png

导致以下错误:

SyntaxError: Unexpected token c in JSON at position 11<br> &nbsp; &nbsp;at Object.parse (native)<br> &nbsp; &nbsp;at parse (/var/app/current/node_modules/highcharts-export-server/node_modules/body-parser/lib/types/json.js:88:17)<br> &nbsp; &nbsp;at /var/app/current/node_modules/highcharts-export-server/node_modules/body-parser/lib/read.js:116:18<br> &nbsp; &nbsp;at invokeCallback (/var/app/current/node_modules/highcharts-export-server/node_modules/raw-body/index.js:262:16)<br> &nbsp; &nbsp;at done (/var/app/current/node_modules/highcharts-export-server/node_modules/raw-body/index.js:251:7)<br> &nbsp; &nbsp;at IncomingMessage.onEnd (/var/app/current/node_modules/highcharts-export-server/node_modules/raw-body/index.js:307:7)<br> &nbsp; &nbsp;at emitNone (events.js:86:13)<br> &nbsp; &nbsp;at IncomingMessage.emit (events.js:185:7)<br> &nbsp; &nbsp;at endReadableNT (_stream_readable.js:974:12)<br> &nbsp; &nbsp;at _combinedTickCallback (internal/process/next_tick.js:74:11)

在高层支持的帮助下,我能够解决几个问题:     - curl命令需要一个json字符串(如注释中所述)     - 需要根据浏览器所需的格式重新格式化json字符串

这是重新格式化的json字符串:

{
  "series": [{
    "name": "Consumed",
    "data": [
      [1521612002000, 668],
      [1521612182000, 668],
      [1521634338000, 487]
    ]
},  {
    "name": "Paid For",
    "data": [
      [1521612002000, 668],
      [1521612182000, 668],
      [1521634338000, 75]
    ]
},  {
    "name": "Produced",
    "data": [
      [1521612002000, 0],
      [1521612182000, 0],
      [1521634338000, 412]
    ]
},  {
    "name": "Paid For kWh",
    "data": [
      [1521612002000, null],
      [1521615425000, 0.6118],
      [1521634338000, null]
    ]
  }]
}

所需的curl命令如下所示:

curl -H "Content-Type: application/json" -X POST -d "{\"infile\": \"{series: [{name: 'Consumed',data: [[1521612002000, 668],[1521612182000, 668],[1521634338000, 487]]}, {name: 'Paid For',data: [[1521612002000, 668],[1521612182000, 668],[1521634338000, 75]]}, {name: 'Produced',data: [[1521612002000, 0],[1521612182000, 0],[1521634338000, 412]]}, {name: 'Paid For kWh',data: [[1521612002000, null],[1521615425000, 0.6118],[1521634338000, null]]}]}\"}" export.highcharts.com > chart.png

0 个答案:

没有答案