Skip to content
This repository was archived by the owner on Aug 29, 2025. It is now read-only.

Repository files navigation

Plotly Node API

Circle CI

Analyze and Visualize Data, Together

Streaming Plot Examples

Installation

npminstallplotly

Usage

varplotly=require('plotly')('username','apiKey');vardata=[{x:[],y:[],stream:{token:'yourStreamtoken',maxpoints:200}}];vargraphOptions={fileopt : "extend",filename : "nodenodenode"};plotly.plot(data,graphOptions,function(){varstream=plotly.stream('yourStreamtoken',function(res){console.log(res);});someReadableStream.pipe(stream);});

Full REST API Documentation can be found here: https://plot.ly/api/rest/

Sign up for plotly here: https://plot.ly/ and obtain your API key and Stream Tokens in your plotly settings: https://plot.ly/settings.

Methods

var plotly = require('plotly')(username, apiKey)

username is a string containing your username apiKey is a string containing your API key

varplotly=require('plotly')('username','apiKey');

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
graphOptions is an Object containing styling options like axis information and titles for your graph. Docs: https://plot.ly/api/rest
callback(err,msg) where err is an error Object, and msg is the return response Object

The msg object has the following attributes : msg.url,msg.filename,msg.message,msg.warning,msg.error

// examples/rest-example.jsvarplotly=require('plotly')('username','apiKey');vardata=[{x:[0,1,2],y:[3,2,1],type: 'bar'}];vargraphOptions={fileopt : "extend",filename : "nodenodenode"};plotly.plot(data,graphOptions,function(err,msg){console.log(msg);});

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

// examples/streaming-example.jsvarplotly=require('plotly')('username','apiKey');varinitData=[{x:[],y:[],stream:{token:'token',maxpoints:200}}];varinitGraphOptions={fileopt : "extend",filename : "nodenodenode"};plotly.plot(initData,initGraphOptions,function(err,msg){if(err)returnconsole.log(err)console.log(msg);varstream1=plotly.stream('token',function(err,res){console.log(err,res);clearInterval(loop);// once stream is closed, stop writing});vari=0;varloop=setInterval(function(){varstreamObject=JSON.stringify({x : i,y : i});stream1.write(streamObject+'\n');i++;},1000);});

Live Streaming Example

// examples/signal-stream.js/* If you have not signed up for Plotly you can do so using https://plot.ly * or see the example signup.js. Once you do, populate the config.json in this * example folder! */varconfig=require('./config.json'),username=config['user'],apiKey=config['apiKey'],token=config['token'],Plotly=require('../.')(username,apiKey),Signal=require('random-signal')// build a data object - see https://plot.ly/api/rest/docs for informationvardata={'x':[]// empty arrays since we will be streaming our data to into these arrays,'y':[],'type':'scatter','mode':'lines+markers',marker: {color: "rgba(31, 119, 180, 0.96)"},line: {color:"rgba(31, 119, 180, 0.31)"},stream: {"token": token,"maxpoints": 100}}// build your layout and file optionsvargraphOptions={"filename": "streamSimpleSensor","fileopt": "overwrite","layout": {"title": "streaming mock sensor data"},"world_readable": true}/* * Call plotly.plot to set the file up. * If you have included a streaming token * you should get a "All Streams Go!" message */Plotly.plot(data,graphOptions,function(err,resp){if(err)returnconsole.log("ERROR",err)console.log(resp)varplotlystream=Plotly.stream(token,function(){})varsignalstream=Signal({tdelta: 100})//plotlystream.on("error",function(err){signalstream.destroy()})// Okay - stream to our plot!signalstream.pipe(plotlystream)})

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

varplotly=require('plotly')('username','apiKey');plotly.getFigure('fileOwner','fileId',function(err,figure){if(err)console.log(err);console.log(figure);});

plotly.getImage(figure[, options, callback])

figure is a JSON object of the graph figure options.format | jpeg, png, pdf, eps, webpoptions.width | width in px (default : 700) options.height | height in px (default : 500)

callback(err, imageData)

err is an Error Object imageStream is a Stream of base-64 encoded imageData

varplotly=require('plotly')('username','apiKey');varfs=require('fs');vartrace1={x: [1,2,3,4],y: [10,15,13,17],type: "scatter"};varfigure={'data': [trace1]};varimgOpts={format: 'png',width: 1000,height: 500};plotly.getImage(figure,imgOpts,function(error,imageStream){if(error)returnconsole.log(error);varfileStream=fs.createWriteStream('1.png');imageStream.pipe(fileStream);});

You can also use getFigure() and getImage() together!

varplotly=require('../.')('username','apiKey');// grab the figure from an existing plotplotly.getFigure('fileOwner','fileId',function(err,figure){if(err)returnconsole.log(err);varimgOpts={format: 'png',width: 1000,height: 500};plotly.getImage(figure,imgOpts,function(error,imageStream){if(error)returnconsole.log(error);varfileStream=fs.createWriteStream('2.png');imageStream.pipe(fileStream);});});

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.

varplotly=require('../.')('username','apiKey');plotly.deletePlot('88',function(err,plot){if(err)console.log(err)elseconsole.log(plot);});

About

node.js wrapper for Plotly's Chart Studio Streaming and REST APIs

Resources

Stars

166 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages