Black Lives Matter. Please consider donating to Black Girls Code today.

Plotly Node Export plot as image?

Hi there,

I’m a new user of Plotly on NodeJS. I’ve managed to create a plot that is accessible via the webpage. However I now need to export it as an image and I’m having awful difficulty doing this! When I run the code it doesn’t work, and I get the following error in the logs:

2018-03-29T15:08:55.192291+00:00 app[web.1]: /app/app_modules/charts.js:68
2018-03-29T15:08:55.192293+00:00 app[web.1]:             imageStream.pipe(fileStream);
2018-03-29T15:08:55.192294+00:00 app[web.1]:                         ^
2018-03-29T15:08:55.192295+00:00 app[web.1]:
2018-03-29T15:08:55.192296+00:00 app[web.1]: TypeError: Cannot read property 'pipe' of null

My code looks like this:

plot: function(senderID, title, xaxis, yaxis, xValues, yValues, type, callback)
{
    let fileName;   // Name of the chart
    let data = [    // Chart Data
    {
        x:xValues,  // X axis values
        y:yValues,  // Y axis values
        type: type  // Chart type
    }];
    let layout = {  // Chart Layout
        title: title,   // Chart Title
        xaxis: {
            title: xaxis    // X axis title
        },
        yaxis: {
          title: yaxis  // Y axis title
        }
    };
    let options = { // Chart options
        layout: layout, // Layout as specified above
        fileopt : "overwrite",  // Overwrite existing chart
        filename : fileName // Chart filename as specified above
    };
    let imgOptions = {
        format: 'png',
        width: 1000,
        height: 500
    };

    plotly.getImage(data, imgOptions, function(error, imageStream)
    {
        if (error)
        {
            console.log(error);
        }

        let fileStream = fs.createWriteStream('./public/chart.png');
        imageStream.pipe(fileStream);
    });
},

Where am I going wrong?