创建烛台图并使用getImage绘图

时间:2019-12-18 19:47:26

标签: javascript node.js plotly

我使用了plotly.plot,它将图发送到了我的帐户。您可以在烛台图上使用getImage吗?我已经发出了getImage请求,但返回的响应状态代码为500。我是否遗漏了一些需要添加的必需变量?

这是我的代码:

var title = `${data.name} from ${x[x.length - 1]} to ${x[0]}`;

//picture requirements
var imgOpts = {
     format: 'png',
     width: 1000,
     height: 500
};

var open = [], high = [], low = [], close = [];
//code that fills variables

var layout = {
        margin: {l: 50, r: 20, t: 50, b: 0},
        showlegend: false,
        xaxis: {
            autorange: true,
            range: [x[x.length - 1], x[0]],
            rangeslider: {visible: false},
            title: 'dates'
        },
        yaxis: {
            autorange: true
        }
    };

    var trace = {
        x: x, open: open, high: high, low: low, close: close, 
        line: {color: 'rgba(31,119,180,1)'}, increasing: {line: {color: '#20a605'}}, decreasing: 
        {line: {color: '#ff0d00'}},
        xaxis: 'x', yaxis: 'y',
        type:"candlestick"
    };

    var figure = {'data': [trace], 'layout': [layout]};


    //start to make graph
    plotly.getImage(figure, imgOpts, function (err) {
        if (err) return console.log (err);

        var fileStream = fs.createWriteStream('test.png');
        imageStream.pipe(fileStream);
    });

控制台消息:

Error: Bad response status code 500
at ClientRequest.handleResponse (C:\Users\gaming 
pc\bot_code\researchbot\node_modules\plotly\index.js:237:25)
    at Object.onceWrapper (events.js:300:26)
    at ClientRequest.emit (events.js:210:5)
    at HTTPParser.parserOnIncomingClient [as onIncoming] (_http_client.js:583:27)
    at HTTPParser.parserOnHeadersComplete (_http_common.js:115:17)
    at TLSSocket.socketOnData (_http_client.js:456:22)
    at TLSSocket.emit (events.js:210:5)
    at addChunk (_stream_readable.js:309:12)
    at readableAddChunk (_stream_readable.js:290:11)
    at TLSSocket.Readable.push (_stream_readable.js:224:10) {
msg: undefined
}

1 个答案:

答案 0 :(得分:0)

我为数字做的变量错误。布局不是数组。因此必须删除“ []”

var figure = { 'data': [trace], layout: layout};

plotly.getImage(figure, imgOpts, function (error, imageStream) {
    if (error) return console.log (error);

    var fileStream = fs.createWriteStream('test.png');
    imageStream.pipe(fileStream);
});