From cc9aabb9d0dfecc612ca80d61a865f3956c2cd1f Mon Sep 17 00:00:00 2001 From: etpinard Date: Wed, 15 Apr 2015 11:05:23 -0400 Subject: [PATCH 01/22] add multiple trace stream example --- examples/streaming-multiple-traces.js | 46 +++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 examples/streaming-multiple-traces.js diff --git a/examples/streaming-multiple-traces.js b/examples/streaming-multiple-traces.js new file mode 100644 index 0000000..1921b5c --- /dev/null +++ b/examples/streaming-multiple-traces.js @@ -0,0 +1,46 @@ +var config = require('./config.json'), + username = config.user, + apikey = config.apikey, + tokens = config.tokens, + Plotly = require('../.')(username, apikey), + Signal = require('random-signal'); + +function initTrace(i) { + return { + x: [], // init. data arrays + y: [], + type: 'scatter', + mode: 'lines+markers', + stream: { + "token": tokens[i], + "maxpoints": 100 + } + }; +} + +var data = [0, 1].map(initTrace); + +var layout = { + filename: "stream-multiple-traces", + fileopt: "overwrite", + layout: { + title: "streaming mock sensor data" + }, + world_readable: true +}; + + +Plotly.plot(data, layout, function (err, resp) { + if (err) return console.log("ERROR", err, data); + console.log(resp); + + [0, 1].forEach(function(i) { + var plotlystream = Plotly.stream(tokens[i], function() {}), + signalstream = Signal({tdelta: 100}); + + plotlystream.on("error", function (err) { signalstream.destroy(); }); + + signalstream.pipe(plotlystream); + }); + +}); From 63eec6e525feac3e544d770ee0aae495674c7c18 Mon Sep 17 00:00:00 2001 From: etpinard Date: Tue, 21 Jul 2015 17:22:08 -0400 Subject: [PATCH 02/22] add streaming style attributes example --- examples/streaming-style-attributes.js | 57 ++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 examples/streaming-style-attributes.js diff --git a/examples/streaming-style-attributes.js b/examples/streaming-style-attributes.js new file mode 100644 index 0000000..dd15a9e --- /dev/null +++ b/examples/streaming-style-attributes.js @@ -0,0 +1,57 @@ +var config = require('./config.json'), + username = config.user, + apikey = config.apikey, + tokens = config.tokens, + Plotly = require('../.')(username, apikey); + +function initTrace(token) { + return { + x: [], // init. data arrays + y: [], + type: 'scatter', + mode: 'markers', + marker: { color: '#91C149' }, + stream: { + 'token': token, + 'maxpoints': 100 + } + }; +} + +var data = tokens.map(initTrace); + +var layout = { + filename: 'stream-style-attributes', + fileopt: 'overwrite', + layout: { + title: 'streaming data and style attributes' + }, + world_readable: true +}; + +Plotly.plot(data, layout, function(err, resp) { + if(err) return console.log('ERROR', err, data); + console.log(resp); + + tokens.forEach(function(token) { + var plotlystream = Plotly.stream(token, function() { + clearInterval(loop); + }), + i = 0; + + var loop = setInterval(function() { + var streamData = { + x: Math.random(), + y: 2 * Math.random(), + marker: { color: '#91C149' } + }; + + // pts below threshold get a different color + if(streamData.y < 1) streamData.marker.color = '#000000'; + + plotlystream.write(JSON.stringify(streamData) + '\n'); + i++; + + }, 1000); + }); +}); From 19d79db3aaf5dbe0cae89de8340bfb97630b2e6b Mon Sep 17 00:00:00 2001 From: alexander-daniel Date: Mon, 26 Oct 2015 10:25:49 -0400 Subject: [PATCH 03/22] create new utf8 buffer from url encoded string for special character handling --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index f2a9296..b899cea 100644 --- a/index.js +++ b/index.js @@ -53,7 +53,7 @@ Plotly.prototype.plot = function(data, graphOptions, callback) { } // trim off last ambersand - urlencoded = urlencoded.substring(0, urlencoded.length - 1); + urlencoded = new Buffer(urlencoded.substring(0, urlencoded.length - 1), 'utf8'); var options = { host: self.host, From 74ef30b4a3c610c4e0349236ceca4819f8b86498 Mon Sep 17 00:00:00 2001 From: alexander-daniel Date: Mon, 26 Oct 2015 10:34:19 -0400 Subject: [PATCH 04/22] handle request errors for `getFigure`, `getImage` and `stream` --- index.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/index.js b/index.js index f2a9296..3271f18 100644 --- a/index.js +++ b/index.js @@ -135,6 +135,10 @@ Plotly.prototype.stream = function(token, callback) { } }); + stream.on('error', function (err) { + callback(err); + }); + if (stream.setTimeout) stream.setTimeout(Math.pow(2, 32) * 1000); return stream; @@ -175,6 +179,10 @@ Plotly.prototype.getFigure = function (fileOwner, fileId, callback) { }); }); + req.on('error', function (err) { + callback(err); + }); + req.end(); }; @@ -219,6 +227,11 @@ Plotly.prototype.getImage = function (figure, opts, callback) { } var req = https.request(options, handleResponse); + + req.on('error', function (err) { + callback(err); + }); + req.write(payload); req.end(); }; From 03a4a4f35c18f2379956a0203e258da483e2ad2d Mon Sep 17 00:00:00 2001 From: alexander-daniel Date: Mon, 26 Oct 2015 10:41:13 -0400 Subject: [PATCH 05/22] update test with UTF8 filename example --- test/test.js | 329 +++++++++++++++++++++++++++------------------------ 1 file changed, 174 insertions(+), 155 deletions(-) diff --git a/test/test.js b/test/test.js index db0c3ce..073f323 100644 --- a/test/test.js +++ b/test/test.js @@ -3,207 +3,226 @@ var test = require('tape'); test('makes a rest call', function (t) { - t.plan(2); + t.plan(2); - var plotly = require('../index')('node-test-account', 'tpmz9ye8hg'); - var data = [{x:[0,1,2], y:[3,2,1], type: 'bar'}]; - var layout = {fileopt : 'overwrite', filename : 'nodenodenodetest'}; + var plotly = require('../index')('node-test-account', 'tpmz9ye8hg'); + var data = [{x:[0,1,2], y:[3,2,1], type: 'bar'}]; + var layout = {fileopt : 'overwrite', filename : 'nodenodenodetest'}; - plotly.plot(data, layout, function (err, msg) { - t.isEqual(msg.url, 'https://plot.ly/~node-test-account/0', 'url matches'); - t.notOk(err ,'no error'); - t.end(); - }); + plotly.plot(data, layout, function (err, msg) { + t.isEqual(msg.url, 'https://plot.ly/~node-test-account/0', 'url matches'); + t.notOk(err ,'no error'); + t.end(); + }); }); test('makes a rest call', function (t) { - t.plan(2); - - var options = { - username: 'node-test-account', - apiKey: 'tpmz9ye8hg', - host: 'plot.ly', - port: 443 - }; - - var plotly = require('../index')(options); - var data = [{x:[0,1,2], y:[3,2,1], type: 'bar'}]; - var layout = {fileopt : 'overwrite', filename : 'nodenodenodetest'}; - - plotly.plot(data, layout, function (err, msg) { - t.isEqual(msg.url, 'https://plot.ly/~node-test-account/0', 'url matches'); - t.notOk(err ,'no error'); - t.end(); - }); + t.plan(2); + + var options = { + username: 'node-test-account', + apiKey: 'tpmz9ye8hg', + host: 'plot.ly', + port: 443 + }; + + var plotly = require('../index')(options); + var data = [{x:[0,1,2], y:[3,2,1], type: 'bar'}]; + var layout = {fileopt : 'overwrite', filename : 'nodenodenodetest'}; + + plotly.plot(data, layout, function (err, msg) { + t.isEqual(msg.url, 'https://plot.ly/~node-test-account/0', 'url matches'); + t.notOk(err ,'no error'); + t.end(); + }); }); test('makes a rest call', function (t) { - t.plan(2); - var plotly = require('../index')('node-test-account', 'tpmz9ye8hg'); - var data = [{x:[0,1,2], y:[3,2,1], type: 'bar'}]; - var layout = {fileopt : 'overwrite', filename : 'nodenodenodetest'}; - - plotly.plot(data, layout, function (err, msg) { - t.isEqual(msg.url, 'https://plot.ly/~node-test-account/0', 'url matches'); - t.notOk(err, 'no error'); - t.end(); - }); + t.plan(2); + var plotly = require('../index')('node-test-account', 'tpmz9ye8hg'); + var data = [{x:[0,1,2], y:[3,2,1], type: 'bar'}]; + var layout = {fileopt : 'overwrite', filename : 'nodenodenodetest'}; + + plotly.plot(data, layout, function (err, msg) { + t.isEqual(msg.url, 'https://plot.ly/~node-test-account/0', 'url matches'); + t.notOk(err, 'no error'); + t.end(); + }); }); test('plot with incorrect userdata and return error', function (t) { - t.plan(1); + t.plan(1); - var plotly = require('../index')('node-test-accountasdfadsgaghaha', 'tpmz9ye8hg'); - var data = [{x:[0,1,2], y:[3,2,1], type: 'bar'}]; - var layout = {fileopt : 'overwrite', filename : 'nodenodenodetest'}; + var plotly = require('../index')('node-test-accountasdfadsgaghaha', 'tpmz9ye8hg'); + var data = [{x:[0,1,2], y:[3,2,1], type: 'bar'}]; + var layout = {fileopt : 'overwrite', filename : 'nodenodenodetest'}; - plotly.plot(data, layout, function (err, msg) { + plotly.plot(data, layout, function (err, msg) { - var errMessage = msg.message.split(',')[0]; + var errMessage = msg.message.split(',')[0]; - t.isEqual(errMessage, 'Aw', 'incorrect user info returns error... not properly though.'); - t.end(); - }); + t.isEqual(errMessage, 'Aw', 'incorrect user info returns error... not properly though.'); + t.end(); + }); }); test('makes a rest call with host foo', function (t) { - t.plan(1); - var plotly = require('../index')('node-test-account', 'tpmz9ye8hg'); - plotly.host = 'foo'; - var data = [{x:[0,1,2], y:[3,2,1], type: 'bar'}]; - var layout = {fileopt : 'overwrite', filename : 'nodenodenodetest'}; - - plotly.plot(data, layout, function (err) { - t.ok(err, 'error received as host was set to "foo"'); - t.end(); - }); + t.plan(1); + var plotly = require('../index')('node-test-account', 'tpmz9ye8hg'); + plotly.host = 'foo'; + var data = [{x:[0,1,2], y:[3,2,1], type: 'bar'}]; + var layout = {fileopt : 'overwrite', filename : 'nodenodenodetest'}; + + plotly.plot(data, layout, function (err) { + t.ok(err, 'error received as host was set to "foo"'); + t.end(); + }); }); test('makes a rest call with no callback', function (t) { - t.plan(1); - var plotly = require('../index')('node-test-account', 'tpmz9ye8hg'); - plotly.host = 'foo'; - var data = [{x:[0,1,2], y:[3,2,1], type: 'bar'}]; - var layout = {fileopt : 'overwrite', filename : 'nodenodenodetest'}; + t.plan(1); + var plotly = require('../index')('node-test-account', 'tpmz9ye8hg'); + plotly.host = 'foo'; + var data = [{x:[0,1,2], y:[3,2,1], type: 'bar'}]; + var layout = {fileopt : 'overwrite', filename : 'nodenodenodetest'}; - plotly.plot(data, layout); - t.ok(true); - t.end(); + plotly.plot(data, layout); + t.ok(true); + t.end(); }); test('makes a rest call with object for data', function (t) { - t.plan(1); - var plotly = require('../index')('node-test-account', 'tpmz9ye8hg'); - plotly.host = 'foo'; - var data = {x:[0,1,2], y:[3,2,1], type: 'bar'}; - var layout = {fileopt : 'overwrite', filename : 'nodenodenodetest'}; + t.plan(1); + var plotly = require('../index')('node-test-account', 'tpmz9ye8hg'); + plotly.host = 'foo'; + var data = {x:[0,1,2], y:[3,2,1], type: 'bar'}; + var layout = {fileopt : 'overwrite', filename : 'nodenodenodetest'}; - plotly.plot(data, layout); - t.ok(true); - t.end(); + plotly.plot(data, layout); + t.ok(true); + t.end(); }); test('getFigure', function (t) { - t.plan(1); - var plotly = require('../index')('node-test-account', 'tpmz9ye8hg'); - plotly.getFigure('node-test-account', '0', function (err, figure) { - t.ok(figure); - t.end(); - }); + t.plan(1); + var plotly = require('../index')('node-test-account', 'tpmz9ye8hg'); + plotly.getFigure('node-test-account', '0', function (err, figure) { + t.ok(figure); + t.end(); + }); }); test('getFigure error', function (t) { - t.plan(1); - var plotly = require('../index')('node-test-account', 'tpmz9ye8hg'); - plotly.getFigure('node-test-account', '99', function (err) { + t.plan(1); + var plotly = require('../index')('node-test-account', 'tpmz9ye8hg'); + plotly.getFigure('node-test-account', '99', function (err) { - t.ok(err); - t.end(); - }); + t.ok(err); + t.end(); + }); }); test('getImage, good and error', function (t) { - t.plan(2); - var plotly = require('../index')('node-test-account', 'tpmz9ye8hg'); - - var trace1 = { - x: [1, 2, 3, 4], - y: [10, 15, 13, 17], - type: 'scatter' - }; - - var trace2 = { - x: [1, 2, 3, 4], - y: [16, 5, 11, 9], - type: 'scatter' - }; - - var figure = { - 'data': [trace1, trace2] - }; - - plotly.getImage(figure, {}, function (err, imageData) { - t.notOk(err); - t.ok(imageData); - t.end(); - }); + t.plan(2); + var plotly = require('../index')('node-test-account', 'tpmz9ye8hg'); + + var trace1 = { + x: [1, 2, 3, 4], + y: [10, 15, 13, 17], + type: 'scatter' + }; + + var trace2 = { + x: [1, 2, 3, 4], + y: [16, 5, 11, 9], + type: 'scatter' + }; + + var figure = { + 'data': [trace1, trace2] + }; + + plotly.getImage(figure, {}, function (err, imageData) { + t.notOk(err); + t.ok(imageData); + t.end(); + }); }); test('getImage, imageserver error', function (t) { - t.plan(2); - var plotly = require('../index')('node-test-account', 'tpmz9ye8hg'); - - var trace1 = { - x: [1, 2, 3, 4], - y: [10, 15, 13, 17], - type: 'scatter' - }; - - var trace2 = { - x: [1, 2, 3, 4], - y: [16, 5, 11, 9], - type: 'scatter' - }; - - var data = [trace1, trace2]; - plotly.getImage(data, 'img', function (err, imageData) { - t.ok(err); - t.notOk(imageData); - t.end(); - }); + t.plan(2); + var plotly = require('../index')('node-test-account', 'tpmz9ye8hg'); + + var trace1 = { + x: [1, 2, 3, 4], + y: [10, 15, 13, 17], + type: 'scatter' + }; + + var trace2 = { + x: [1, 2, 3, 4], + y: [16, 5, 11, 9], + type: 'scatter' + }; + + var data = [trace1, trace2]; + plotly.getImage(data, 'img', function (err, imageData) { + t.ok(err); + t.notOk(imageData); + t.end(); + }); }); +test('creates a plot with UTF chars in filename', function (t) { + t.plan(1); + + var options = { + username: 'node-test-account', + apiKey: 'tpmz9ye8hg', + host: 'plot.ly', + port: 443 + }; + + var plotly = require('../index')(options); + var data = [{x:[0,1,2], y:[3,2,1], type: 'bar'}]; + var layout = {fileopt : 'overwrite', filename : 'üüüü'}; + plotly.plot(data, layout, function (err, msg) { + t.notOk(err ,'no error'); + t.end(); + }); + +}); test('streams some data', function (t) { - t.plan(1); - var plotly = require('../index')('node-test-account', 'tpmz9ye8hg'); - var initdata = [{x:[], y:[], stream:{token:'i9zxn8goas', maxpoints:200}}]; - var initlayout = {fileopt : 'extend', filename : 'nodenodenode-test-stream'}; - - plotly.plot(initdata, initlayout, function (err) { - if (err) return console.log(err); - - var streamObject = JSON.stringify({ x : 1, y : 1 }); - - var stream = plotly.stream({ - token: 'i9zxn8goas', - host: undefined, - port: null - }); - - setInterval(function () { - stream.write(streamObject+'\n'); - }, 500); - - setTimeout(function () { - t.ok(true, 'no errors'); - t.end(); - process.exit(0); - }, 5000); - }); + t.plan(1); + var plotly = require('../index')('node-test-account', 'tpmz9ye8hg'); + var initdata = [{x:[], y:[], stream:{token:'i9zxn8goas', maxpoints:200}}]; + var initlayout = {fileopt : 'extend', filename : 'nodenodenode-test-stream'}; + + plotly.plot(initdata, initlayout, function (err) { + if (err) return console.log(err); + + var streamObject = JSON.stringify({ x : 1, y : 1 }); + + var stream = plotly.stream({ + token: 'i9zxn8goas', + host: undefined, + port: null + }); + + setInterval(function () { + stream.write(streamObject+'\n'); + }, 500); + + setTimeout(function () { + t.ok(true, 'no errors'); + t.end(); + process.exit(0); + }, 5000); + }); }); From e228142af9190a2da39f6df43ab3fd7a3cc2cdcf Mon Sep 17 00:00:00 2001 From: alexander-daniel Date: Mon, 26 Oct 2015 10:59:10 -0400 Subject: [PATCH 06/22] 1.0.3 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 8036ec6..1317388 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "plotly", - "version": "1.0.2", + "version": "1.0.3", "description": "Simple node.js wrapper for the plot.ly API", "main": "index.js", "devDependencies": { From aef733dcc4400d88b21bcaf2420b1891ebf94de7 Mon Sep 17 00:00:00 2001 From: alexander-daniel Date: Mon, 26 Oct 2015 11:35:20 -0400 Subject: [PATCH 07/22] ADD deletePlot method and example --- examples/delete-plot.js | 8 ++++++++ index.js | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 examples/delete-plot.js diff --git a/examples/delete-plot.js b/examples/delete-plot.js new file mode 100644 index 0000000..a26b0e3 --- /dev/null +++ b/examples/delete-plot.js @@ -0,0 +1,8 @@ +'use strict'; + +var plotly = require('../.')('username','apiKey'); + +plotly.deletePlot('888', function (err, msg) { + if (err) console.log(err); + else console.log(msg); +}); diff --git a/index.js b/index.js index 951a2f2..adc3e95 100644 --- a/index.js +++ b/index.js @@ -236,6 +236,47 @@ Plotly.prototype.getImage = function (figure, opts, callback) { req.end(); }; +Plotly.prototype.deletePlot = function (fid, callback) { + if (!callback) callback = function () {}; + + var self = this; + + // Create the base64 authstring from buffer + var encodedAPIAuth = new Buffer(this.username + ':' + this.apiKey).toString('base64'); + + var options = { + host: 'api.plot.ly', + port: this.port, + path: '/v2/files/' + this.username + ':' + fid + '/trash', + method: 'POST', + agent: false, + withCredentials: true, + headers: { + 'Plotly-Client-Platform': 'nodejs ' + this.version, + 'authorization': 'Basic ' + encodedAPIAuth + } + }; + + var req = https.request(options, function (res) { + parseRes(res, function (err, body) { + + if (res.statusCode === 200) { + var msg = 'Successfully deleted plot: ' + self.username + ':' + fid; + callback(null, msg); + } else { + callback(body); // Pass out the error message from the backend + } + + }); + }); + + req.on('error', function (err) { + callback(err); + }); + + req.end(); +}; + // response parse helper fn function parseRes (res, cb) { var body = ''; From dc1f54116281dab97bf3aabe38fd7ebd51b95648 Mon Sep 17 00:00:00 2001 From: alexander-daniel Date: Mon, 26 Oct 2015 11:42:41 -0400 Subject: [PATCH 08/22] if theres an error, build up an error obj from the res and body --- index.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index adc3e95..772f82c 100644 --- a/index.js +++ b/index.js @@ -261,10 +261,19 @@ Plotly.prototype.deletePlot = function (fid, callback) { parseRes(res, function (err, body) { if (res.statusCode === 200) { + var msg = 'Successfully deleted plot: ' + self.username + ':' + fid; callback(null, msg); + } else { - callback(body); // Pass out the error message from the backend + + var errObj = { + statusCode: res.statusCode, + err: body, + statusMessage: res.statusMessage + }; + + callback(errObj); // Pass out the error message from the backend } }); From dd7560f8a68f5a6235af7251a0eef264fc9e6882 Mon Sep 17 00:00:00 2001 From: alexander-daniel Date: Mon, 26 Oct 2015 11:53:08 -0400 Subject: [PATCH 09/22] version 1.0.4 --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 951a2f2..94a510d 100644 --- a/index.js +++ b/index.js @@ -25,7 +25,7 @@ function Plotly(username,apiKey) { this.port = 443; } this.streamHost = ''; - this.version='1.0.2'; + this.version='1.0.4'; this.platform='nodejs'; this.origin='plot'; } From be1c32756f0d5ea241a2d17c4aad46ad5301a5c7 Mon Sep 17 00:00:00 2001 From: alexander-daniel Date: Mon, 26 Oct 2015 11:53:11 -0400 Subject: [PATCH 10/22] 1.0.4 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 1317388..efb913f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "plotly", - "version": "1.0.3", + "version": "1.0.4", "description": "Simple node.js wrapper for the plot.ly API", "main": "index.js", "devDependencies": { From 659d1337a45a516134b94aef89597c5b7a6a87c0 Mon Sep 17 00:00:00 2001 From: alexander-daniel Date: Mon, 26 Oct 2015 13:15:42 -0400 Subject: [PATCH 11/22] ADD docs for deletePlot method --- README.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/README.md b/README.md index 9276b01..cf0a0aa 100644 --- a/README.md +++ b/README.md @@ -233,3 +233,16 @@ plotly.getFigure('fileOwner', 'fileId', function (err, figure) { }); }); ``` + +##plotly.deletePlot(fid[, callback]) +`fid` is a String, the id of the plot you wish you delete +`callback` is a function with `err` and `msg` as parameters. + +```javascript +var plotly = require('../.')('username','apiKey'); + +plotly.deletePlot('88', function (err, msg) { + if (err) console.log(err) + else console.log(msg); +}); +``` From 448865842ff4e670d4cedeaf1a8ed8b7e1c46d5b Mon Sep 17 00:00:00 2001 From: alexander-daniel Date: Mon, 26 Oct 2015 13:20:39 -0400 Subject: [PATCH 12/22] pass back the figure on successful deletePlot --- README.md | 6 +++--- examples/delete-plot.js | 6 +++--- index.js | 3 +-- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index cf0a0aa..52b153c 100644 --- a/README.md +++ b/README.md @@ -236,13 +236,13 @@ plotly.getFigure('fileOwner', 'fileId', function (err, figure) { ##plotly.deletePlot(fid[, callback]) `fid` is a String, the id of the plot you wish you delete -`callback` is a function with `err` and `msg` as parameters. +`callback` is a function with `err` and `figure` as parameters. `err`, if present, is the error message returned from the request. `figure` is the figure that was deleted. ```javascript var plotly = require('../.')('username','apiKey'); -plotly.deletePlot('88', function (err, msg) { +plotly.deletePlot('88', function (err, figure) { if (err) console.log(err) - else console.log(msg); + else console.log(figure); // msg is the figure that was deleted }); ``` diff --git a/examples/delete-plot.js b/examples/delete-plot.js index a26b0e3..56aec5c 100644 --- a/examples/delete-plot.js +++ b/examples/delete-plot.js @@ -1,8 +1,8 @@ 'use strict'; -var plotly = require('../.')('username','apiKey'); +var plotly = require('../.')('alexander.daniel','u1jactdk3m'); -plotly.deletePlot('888', function (err, msg) { +plotly.deletePlot('2718', function (err, figure) { if (err) console.log(err); - else console.log(msg); + else console.log(figure); }); diff --git a/index.js b/index.js index 772f82c..2e77bdd 100644 --- a/index.js +++ b/index.js @@ -262,8 +262,7 @@ Plotly.prototype.deletePlot = function (fid, callback) { if (res.statusCode === 200) { - var msg = 'Successfully deleted plot: ' + self.username + ':' + fid; - callback(null, msg); + callback(null, body); } else { From 4e4ad07c6456b3fa94f15ad6912b4a1f5c3485ff Mon Sep 17 00:00:00 2001 From: alexander-daniel Date: Mon, 26 Oct 2015 13:24:15 -0400 Subject: [PATCH 13/22] figure -> plot --- README.md | 6 +++--- examples/delete-plot.js | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 52b153c..f1d6b26 100644 --- a/README.md +++ b/README.md @@ -236,13 +236,13 @@ plotly.getFigure('fileOwner', 'fileId', function (err, figure) { ##plotly.deletePlot(fid[, callback]) `fid` is a String, the id of the plot you wish you delete -`callback` is a function with `err` and `figure` as parameters. `err`, if present, is the error message returned from the request. `figure` is the figure that was deleted. +`callback` is a function with `err` and `plot` as parameters. `err`, if present, is the error message returned from the request. `plot` is the plot that was deleted. ```javascript var plotly = require('../.')('username','apiKey'); -plotly.deletePlot('88', function (err, figure) { +plotly.deletePlot('88', function (err, plot) { if (err) console.log(err) - else console.log(figure); // msg is the figure that was deleted + else console.log(plot); // msg is the figure that was deleted }); ``` diff --git a/examples/delete-plot.js b/examples/delete-plot.js index 56aec5c..a3d1afc 100644 --- a/examples/delete-plot.js +++ b/examples/delete-plot.js @@ -2,7 +2,7 @@ var plotly = require('../.')('alexander.daniel','u1jactdk3m'); -plotly.deletePlot('2718', function (err, figure) { +plotly.deletePlot('2718', function (err, plot) { if (err) console.log(err); - else console.log(figure); + else console.log(plot); }); From 92956ee6edc15b61682c6a941e9ce29a53fba309 Mon Sep 17 00:00:00 2001 From: alexander-daniel Date: Mon, 26 Oct 2015 13:25:27 -0400 Subject: [PATCH 14/22] remove unneeded comment --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index f1d6b26..1c74348 100644 --- a/README.md +++ b/README.md @@ -243,6 +243,6 @@ var plotly = require('../.')('username','apiKey'); plotly.deletePlot('88', function (err, plot) { if (err) console.log(err) - else console.log(plot); // msg is the figure that was deleted + else console.log(plot); }); ``` From 6b4bfc78eac3d9212946b2e3f5c4620f24fe4543 Mon Sep 17 00:00:00 2001 From: alexander-daniel Date: Mon, 26 Oct 2015 13:32:02 -0400 Subject: [PATCH 15/22] 1.0.4 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 1317388..efb913f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "plotly", - "version": "1.0.3", + "version": "1.0.4", "description": "Simple node.js wrapper for the plot.ly API", "main": "index.js", "devDependencies": { From d3d48d358f3cb7c027819c2ba9e2ab8f20b1c078 Mon Sep 17 00:00:00 2001 From: alexander-daniel Date: Mon, 26 Oct 2015 13:32:15 -0400 Subject: [PATCH 16/22] 1.0.5 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index efb913f..2bff6b3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "plotly", - "version": "1.0.4", + "version": "1.0.5", "description": "Simple node.js wrapper for the plot.ly API", "main": "index.js", "devDependencies": { From 583418366f38b690cb5be52e137443ec26c789b9 Mon Sep 17 00:00:00 2001 From: alexander-daniel Date: Wed, 30 Dec 2015 12:57:54 -0500 Subject: [PATCH 17/22] wrap JSON.parse calls in try-catch blocks --- index.js | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index fb7186a..01371e9 100644 --- a/index.js +++ b/index.js @@ -69,7 +69,13 @@ Plotly.prototype.plot = function(data, graphOptions, callback) { var req = https.request(options, function (res) { parseRes(res, function (err, body) { - body = JSON.parse(body); + + /* Try to parse the response */ + try { + body = JSON.parse(body); + } catch (e) { + callback(e); + } if ( body['stream-status'] != undefined) { self.streamHost = url.parse(body['stream-host']).hostname; @@ -169,13 +175,23 @@ Plotly.prototype.getFigure = function (fileOwner, fileId, callback) { var req = https.get(options, function (res) { parseRes(res, function (err, body) { - if (JSON.parse(body).error) { - err = JSON.parse(body).error; - callback(err); - } else { - var figure = JSON.parse(body).payload.figure; + + /* Try to parse the response */ + try { + body = JSON.parse(body); + } catch (e) { + callback(e); + } + + if (body.error) { + callback(body.error); + } + + else { + var figure = body.payload.figure; callback(null, figure); } + }); }); From 0017cb5d41292d4b181080bf12f7fc12da96f5b3 Mon Sep 17 00:00:00 2001 From: alexander-daniel Date: Wed, 30 Dec 2015 15:25:56 -0500 Subject: [PATCH 18/22] 1.0.6 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 2bff6b3..423ced4 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "plotly", - "version": "1.0.5", + "version": "1.0.6", "description": "Simple node.js wrapper for the plot.ly API", "main": "index.js", "devDependencies": { From 7c8d474b7f88ecde281cb0a517e5dfdade436052 Mon Sep 17 00:00:00 2001 From: Ben Postlethwaite Date: Mon, 28 Nov 2016 13:10:45 -0500 Subject: [PATCH 19/22] Update README.md --- README.md | 4 ---- 1 file changed, 4 deletions(-) diff --git a/README.md b/README.md index 1c74348..9e98bc5 100644 --- a/README.md +++ b/README.md @@ -3,10 +3,6 @@ > Analyze and Visualize Data, Together -If you have a question about streaming let us know or open an issue! - -`ben@plot.ly` && `alexandre@plot.ly` - ## Streaming Plot Examples - [mock sensor stream](http://plot.ly/~streaming-demos/6/) - [math bar fight](http://plot.ly/~streaming-demos/44/) From 4cdc9b79b3ea2879ba8a7cfaeb43bc7b89b27153 Mon Sep 17 00:00:00 2001 From: Jason Allan Date: Tue, 13 Jun 2017 12:03:21 +0100 Subject: [PATCH 20/22] Fixing Headings --- README.md | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 9e98bc5..5adea70 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -#Plotly Node API +# Plotly Node API [![Circle CI](https://circleci.com/gh/plotly/plotly-nodejs/tree/master.svg?style=svg)](https://circleci.com/gh/plotly/plotly-nodejs/tree/master) > Analyze and Visualize Data, Together @@ -7,12 +7,12 @@ - [mock sensor stream](http://plot.ly/~streaming-demos/6/) - [math bar fight](http://plot.ly/~streaming-demos/44/) -##Installation +## Installation ```javascript npm install plotly ``` -##Usage +## Usage ```javascript var plotly = require('plotly')('username','apiKey'); @@ -27,19 +27,19 @@ plotly.plot(data,graphOptions,function() { }); ``` -####Full REST API Documentation can be found here: [https://plot.ly/api/rest/](https://plot.ly/api/rest/) +#### Full REST API Documentation can be found here: [https://plot.ly/api/rest/](https://plot.ly/api/rest/) Sign up for plotly here: [https://plot.ly/](https://plot.ly/) and obtain your API key and Stream Tokens in your plotly settings: [https://plot.ly/settings](https://plot.ly/settings). -#Methods -##var plotly = require('plotly')(username, apiKey) +# Methods +## var plotly = require('plotly')(username, apiKey) `username` is a string containing your username `apiKey` is a string containing your API key ```javascript var plotly = require('plotly')('username', 'apiKey'); ``` -##plotly.plot(data,graphOptions[, callback]) +## plotly.plot(data,graphOptions[, callback]) Plotly graphs are described declaratively with a data JSON Object and a graphOptions JSON Object. `data` is an array of Objects and with each object containing data and styling information of separate graph traces. Docs: [https://plot.ly/api/rest](https://plot.ly/api/rest) `graphOptions` is an Object containing styling options like axis information and titles for your graph. Docs: [https://plot.ly/api/rest](https://plot.ly/api/rest) @@ -58,7 +58,7 @@ plotly.plot(data, graphOptions, function (err, msg) { console.log(msg); }); ``` -##var stream = plotly.stream(token[, callback]) +## var stream = plotly.stream(token[, callback]) `token` accepts a token string `callback(res)` where `res` is a the response object with the following attributes : `res.msg`, `res.statusCode` @@ -156,7 +156,7 @@ Plotly.plot(data, graphOptions, function (err, resp) { ``` -##plotly.getFigure(fileOwner, fileId[, callback]) +## plotly.getFigure(fileOwner, fileId[, callback]) `file_owner` accepts a string of the file owner's name `fileId` is an integer, representing the graph ID `callback(figure)` where `figure` is a the JSON object of the graph figure @@ -170,7 +170,7 @@ plotly.getFigure('fileOwner', 'fileId', function (err, figure) { }); ``` -##plotly.getImage(figure[, options, callback]) +## plotly.getImage(figure[, options, callback]) `figure` is a JSON object of the graph figure `options.format` | `jpg`, `png`, `pdf`, `eps`, `webp` `options.width` | width in `px` (default : 700) @@ -230,7 +230,7 @@ plotly.getFigure('fileOwner', 'fileId', function (err, figure) { }); ``` -##plotly.deletePlot(fid[, callback]) +## plotly.deletePlot(fid[, callback]) `fid` is a String, the id of the plot you wish you delete `callback` is a function with `err` and `plot` as parameters. `err`, if present, is the error message returned from the request. `plot` is the plot that was deleted. From 54c46f6eeb15f9c1eabad1e889bc8b7540534138 Mon Sep 17 00:00:00 2001 From: "pablo.mint" Date: Sun, 27 Aug 2017 11:47:10 -0300 Subject: [PATCH 21/22] Allow both jpg and jpeg as possible file formats I initially intended to simply correct the README, pointing out that the correct option is 'jpeg' and not 'jpg', but allowing both seems friendlier --- README.md | 2 +- index.js | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 5adea70..2474a53 100644 --- a/README.md +++ b/README.md @@ -172,7 +172,7 @@ plotly.getFigure('fileOwner', 'fileId', function (err, figure) { ## plotly.getImage(figure[, options, callback]) `figure` is a JSON object of the graph figure -`options.format` | `jpg`, `png`, `pdf`, `eps`, `webp` +`options.format` | `jpg`, `jpeg`, `png`, `pdf`, `eps`, `webp` `options.width` | width in `px` (default : 700) `options.height` | height in `px` (default : 500) diff --git a/index.js b/index.js index 01371e9..72719a6 100644 --- a/index.js +++ b/index.js @@ -207,6 +207,10 @@ Plotly.prototype.getImage = function (figure, opts, callback) { if (!figure) return new Error('no figure provided!'); var self = this; + + // allow both jpg and jpeg as file formats + if (opts && opts.format === 'jpg') opts.format = 'jpeg'; + var payload = JSON.stringify({ figure: figure, format: opts.format || 'png', From 9af6bf1af91cdddb4bf333b9ff9060f4493ab9e0 Mon Sep 17 00:00:00 2001 From: "pablo.mint" Date: Mon, 28 Aug 2017 22:09:07 -0300 Subject: [PATCH 22/22] Let getImage error when opts.format eq 'jpg' --- README.md | 2 +- index.js | 4 ---- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/README.md b/README.md index 2474a53..855c846 100644 --- a/README.md +++ b/README.md @@ -172,7 +172,7 @@ plotly.getFigure('fileOwner', 'fileId', function (err, figure) { ## plotly.getImage(figure[, options, callback]) `figure` is a JSON object of the graph figure -`options.format` | `jpg`, `jpeg`, `png`, `pdf`, `eps`, `webp` +`options.format` | `jpeg`, `png`, `pdf`, `eps`, `webp` `options.width` | width in `px` (default : 700) `options.height` | height in `px` (default : 500) diff --git a/index.js b/index.js index 72719a6..01371e9 100644 --- a/index.js +++ b/index.js @@ -207,10 +207,6 @@ Plotly.prototype.getImage = function (figure, opts, callback) { if (!figure) return new Error('no figure provided!'); var self = this; - - // allow both jpg and jpeg as file formats - if (opts && opts.format === 'jpg') opts.format = 'jpeg'; - var payload = JSON.stringify({ figure: figure, format: opts.format || 'png',