Giter Site home page Giter Site logo

plotly-nodejs's Introduction

#Plotly Node API

Analyze and Visualize Data, Together

If you have a question about streaming let us know or open an issue!

[email protected] && [email protected]

Streaming Plot Examples

##Installation

npm install plotly

##Usage

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

var data = [{x:[], y:[], stream:{token:'yourStreamtoken', maxpoints:200}}];
var graphOptions = {fileopt : "extend", filename : "nodenodenode"};

plotly.plot(data,graphOptions,function() {
  var stream = 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

var plotly = 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.js

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

var data = [{x:[0,1,2], y:[3,2,1], type: 'bar'}];
var graphOptions = {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.js
var plotly = require('plotly')('username','apiKey');

var initData = [{x:[], y:[], stream:{token:'token', maxpoints:200}}];
var initGraphOptions = {fileopt : "extend", filename : "nodenodenode"};

plotly.plot(initData, initGraphOptions, function (err, msg) {
  if (err) return console.log(err)
  console.log(msg);

  var stream1 = plotly.stream('token', function (err, res) {
    console.log(err, res);
    clearInterval(loop); // once stream is closed, stop writing
  });

  var i = 0;
  var loop = setInterval(function () {
      var streamObject = 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!
 */
var config = 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 information
var data = {
    '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 options
var graphOptions = {
    "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) return console.log("ERROR", err)

    console.log(resp)

    var plotlystream = Plotly.stream(token, function () {})
    var signalstream = Signal({tdelta: 100}) // 


    plotlystream.on("error", function (err) {
        signalstream.destroy()
    })

    // Okay - stream to our plot!
    signalstream.pipe(plotlystream)
})

##var plotly.getFigure(fileOwner, fileId[, callback]) file_ownder accepts a string of the file owners name
fileId is an integer, representing the graph ID. callback(figure) where figure is a the JSON object of the graph figure.

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

plotly.getFigure('fileOwner', 'fileId', function (err, figure) {
    if (err) console.log(err);
    console.log(figure);
});

##var plotly.saveImage(figure, path[, callback]) figure accepts a string of the file owners name
path is a string of the filepath and file name you wish to save the image as. callback(err) is a function, where err is an Error Object.

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

var trace1 = {
  x: [1, 2, 3, 4], 
  y: [10, 15, 13, 17], 
  type: "scatter"
};

var data = [trace1];

plotly.saveImage({'data': data}, 'path/to/image_name', function (err) {
  if (err) console.log(err);
});

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

var plotly = require('../.')('username','apiKey');

// grab the figure from an existing plot
plotly.getFigure('fileOwner', 'fileId', function (err, figure) {
  if (err) console.log(err);
  // now save that figure as a static image!
  plotly.saveImage(figure, 'path/to/image_name', function (err) {
    if (err) console.log(err);
  });
});

plotly-nodejs's People

Contributors

alexander-daniel avatar bpostlethwaite avatar chriddyp avatar oppenlander avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.