Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Jun 4, 2024. It is now read-only.

Commit 28243e7

Browse files
Merge pull request #19 from plotly/image_streams
getImage() now returns imageStream
2 parents 44e24ef + 51c9f4e commit 28243e7

File tree

4 files changed

+27
-20
lines changed

4 files changed

+27
-20
lines changed

README.md

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ plotly.getFigure('fileOwner', 'fileId', function (err, figure) {
183183
`callback(err, imageData)`
184184

185185
`err` is an Error Object
186-
`imageData` is a `base64`-encoded string
186+
`imageStream` is a Stream of base-64 encoded imageData
187187

188188
```javascript
189189
var plotly = require('plotly')('username','apiKey');
@@ -203,8 +203,11 @@ var imgOpts = {
203203
height: 500
204204
};
205205

206-
plotly.getImage(figure, imgOpts, function (error, imageData) {
207-
fs.writeFile('1.png', imageData, 'base64');
206+
plotly.getImage(figure, imgOpts, function (error, imageStream) {
207+
if (error) return console.log (error);
208+
209+
var fileStream = fs.createWriteStream('1.png');
210+
imageStream.pipe(fileStream);
208211
});
209212
```
210213

@@ -222,8 +225,11 @@ plotly.getFigure('fileOwner', 'fileId', function (err, figure) {
222225
height: 500
223226
};
224227

225-
plotly.getImage(figure, imgOpts, function (error, imageData) {
226-
fs.writeFile('1.png', imageData, 'base64');
227-
});
228+
plotly.getImage(figure, imgOpts, function (error, imageStream) {
229+
if (error) return console.log (error);
230+
231+
var fileStream = fs.createWriteStream('2.png');
232+
imageStream.pipe(fileStream);
233+
});
228234
});
229235
```

examples/saving_image.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

33
var fs = require('fs');
4-
var plotly = require('../.')('your_username','api_key');
4+
var plotly = require('../.')('your_username','your_apikey');
55

66
var trace1 = {
77
x: [1, 2, 3, 4],
@@ -25,6 +25,9 @@ var imgOpts = {
2525
height: 500
2626
};
2727

28-
plotly.getImage(figure, imgOpts, function (error, imageData) {
29-
fs.writeFile('1.png', imageData, 'base64');
28+
plotly.getImage(figure, imgOpts, function (error, imageStream) {
29+
if (error) return console.log (error);
30+
31+
var fileStream = fs.createWriteStream('1.png');
32+
imageStream.pipe(fileStream);
3033
});

index.js

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ function Plotly(username,apiKey) {
2525
this.port = 443;
2626
}
2727
this.streamHost = '';
28-
this.version='1.0.1';
28+
this.version='1.0.2';
2929
this.platform='nodejs';
3030
this.origin='plot';
3131
}
@@ -209,15 +209,13 @@ Plotly.prototype.getImage = function (figure, opts, callback) {
209209
};
210210

211211
function handleResponse(res) {
212-
parseRes(res, function (err, body) {
213-
if (res.statusCode !== 200) {
214-
var error = new Error('Bad response status code ' + res.statusCode);
215-
error.msg = body;
216-
return callback(error, null);
217-
}
218-
var imageData = JSON.parse(body).payload;
219-
callback(null, imageData);
220-
});
212+
if (res.statusCode !== 200) {
213+
var error = new Error('Bad response status code ' + res.statusCode);
214+
error.msg = res.body;
215+
return callback(error, null);
216+
}
217+
218+
callback(null, res);
221219
}
222220

223221
var req = https.request(options, handleResponse);

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "plotly",
3-
"version": "1.0.1",
3+
"version": "1.0.2",
44
"description": "Simple node.js wrapper for the plot.ly API",
55
"main": "index.js",
66
"devDependencies": {

0 commit comments

Comments
 (0)