1

I am using pubnub to send the touch and move events from a mobile screen. I'm able to subscribe to it with JavaScript and pubnub library and display the data on the page with the below code. The data looks like array data, which is how I'm sending it but I can not access the array elements. If I JSON.stringify the data it comes with the [] but if I don't I just get the three values seperated by commas.

I want to graph the x position against time. I pass data from pubnub to the cloud and the javascript reads it and prints it. The data is [phone#, time in 10 millisecond increments, x-position on screen]. It prints what appears to be an array, one line at a time (see output below). Here is the code:

<html>
Output from App (phone#, 100's of milliseconds, x-position on screen:
    <div id=box></div>
     <script src=http://cdn.pubnub.com/pubnub.min.js></script>
         <script type="text/javascript" src="../smoothie.js"></script>
    <script>   (function(){
  var pn = PUBNUB.init({ subscribe_key : 'sub-c-51b36680-27cf-11e4-97e6-02ee2ddab7fe', publish_key : 'pub-c-165f3bbe-22a9-47f7-b3f1-33b86035939f' });
  var box = pn.$('box'), input = pn.$('input'), channel = 'z';
  pn.subscribe({
  channel : channel,
  callback : function(text) {
  var tst = JSON.stringify(text);
  box.innerHTML = tst + '<br>' + box.innerHTML; }
  });
  pn.bind( 'keyup', input, function(e) {
  (e.keyCode || e.charCode) === 13 &&
  pn.publish({
  channel : channel,
  message : { "msgtext": input.value },
  x : (input.value='')
  })
})
})()</script>
    </html> 

It looks like this onscreen when I move my finger on the mobile device with the app:

Output from App (phone#, 100's of milliseconds, x-position on screen:

[5551212,2276.639892,249]
[5551212,2276.537952,249]
[5551212,2276.442852,251]
[5551212,2275.867848,275]
[5551212,2275.832444,277]
[5551212,2275.210072,299]
[5551212,2274.950404,318]
[5551212,2274.793408,325]
[5551212,2271.274288,268]
[5551212,2274.621032,335]
[5551212,2274.609068,338]
[5551212,2271.561424,305]
[5551212,2275.341676,285]
[5551212,2274.077036,353]
[5551212,2271.694492,327]
[5551212,2274.4738,347]
[5551212,2274.48674,342]
[5551212,2274.206684,352]

Looks like array values. Well, I don't seem to be able to access the elements of the array with normal array syntax like tst[i]. I've tried creating as seperate array name, setting the values and doing a loop with document right like document.write("the x position is", tst[i]") but that doesn't work.

Ultimately, what I'm trying to do is get this program called smoothie charts for javascript to take the time and x-position values and graph them as the data comes in. I thought I could use an array or a time series. I'm looking for help to get smoothie to plot this incoming data.

Here is the sample smoothie chart example:

<!DOCTYPE html>
<html>
  <head>
    <title>Smoothie Chart Example</title>
    <script type="text/javascript" src="smoothie.js"></script>
  </head>
  <body>

    <h1>Smoothie Example</h1>

    <canvas id="mycanvas" width="900" height="300"></canvas>

    <script type="text/javascript">

      // Random data
      var line1 = new TimeSeries();
      var line2 = new TimeSeries();
      setInterval(function() {
        line1.append(new Date().getTime(), Math.random());
        line2.append(new Date().getTime(), Math.random());
      }, 1000);

      var smoothie = new SmoothieChart({ grid: { strokeStyle: 'rgb(125, 0, 0)', fillStyle: 'rgb(60, 0, 0)', lineWidth: 1, millisPerLine: 250, verticalSections: 6 } });
      smoothie.addTimeSeries(line1, { strokeStyle: 'rgb(0, 255, 0)', fillStyle: 'rgba(0, 255, 0, 0.4)', lineWidth: 3 });
      smoothie.addTimeSeries(line2, { strokeStyle: 'rgb(255, 0, 255)', fillStyle: 'rgba(255, 0, 255, 0.3)', lineWidth: 3 });

      smoothie.streamTo(document.getElementById("mycanvas"), 1000);
    </script>

    <p><a href="tutorial.html">Return to tutorial</a></p>

  </body>
</html>

The smoothie library is really small. There is an overview here: http://smoothiecharts.org/ and the libary can be downloaded here:

I'm not sure that tst in the original script is actually an array and if it is I'm not sure how to read it into smoothie much less just print each array element using document.write.

YakovL
  • 7,557
  • 12
  • 62
  • 102

1 Answers1

0

Creating a Line Chart with Smoothie.js graphs from a PubNub Data Stream using the Smoothy Stream Add-on

Skip to the source code on GitHub:

Get the PubNub Smoothy Stream Add-on from GitHub right now!

Data streamed to your screen using PubNub Data Stream Network and painted in smoothy.js canvas. Smoothie Charts: A JavaScript Charting Library for Streaming Data can receive live updates in realtime using the Smoothy Stream library add-on.

Stream Live Data to Smoothy.js Chart

Originally the need for this library was requested on StackOverflow for Creating a line chart with smoothie graphs from a PubNub Stream.

You should also check out the live PubNub Smoothy Stream Add-on Live Demo to see this in action. But don't skip the usage example below.

Usage Example - Smoothy.js Data Stream Demo

Start a smoothy stream using a PubNub Data Channel. You can receive mouse coordinates or stock prices or server cpu usage in realtime from a remote source anywhere on the internet. The following example will receive stock prices from a live data stream feed Streaming JavaScript Market Orders on PubNub DSN (Data Stream Network).

<canvas id="chart1" width="400" height="100"></canvas>

<script src="https://cdn.pubnub.com/pubnub.min.js"></script>
<script src="js/smoothy.min.js"></script>
<script src="js/smoothstream.js"></script>
<script>(function(){

    // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
    // Start Smooth Stream
    // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
    var stream = smoothstream({
        channel       : "pubnub-market-orders",
        subscribe_key : "sub-c-4377ab04-f100-11e3-bffd-02ee2ddab7fe",

        chart         : 'chart1',
        key           : function (message) { return message.bid_price },
        strokeStyle   : 'rgba(185,230,199,1)',
        fillStyle     : 'rgba(222,238,191,0.7)',
        lineWidth     : 5
    });

})();</script>

Stopping a Smoothy Stream

You can also stop a stream using the .stop(); method.

stream.stop();

Stream the X coordinate from Touch Events from a Mobile Device

You can stream the coordinates of touch and mouse movement events to the smoothy chart too if you really wanted to do that. You do want to do that because for awesome. Start by transmitting the coordinates and change the stream receiver source code to look like the following:

// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Receive Touch Event Coordinates
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
var stream = smoothstream({
    channel       : "pubnub-market-orders",
    subscribe_key : "sub-c-4377ab04-f100-11e3-bffd-02ee2ddab7fe",
    chart         : 'chart1',

    key           : function (coord) { return coord.x }, // <-- *HERE*

    strokeStyle   : 'rgba(185,230,199,1)',
    fillStyle     : 'rgba(222,238,191,0.7)',
    lineWidth     : 5
});

You need to change the key parameter to match the format of the transmitted touch/mouse screen coordinates. You may be transmitting an array which will look like this instead:

function (coords) { return coords[0] }, // <-- *HERE*

This will paint the X coordinate on the smoothy chart in realtime as place your finger on the mobile device's screen and drag up and down.

Corona SDK Transmission Example based on Stackoverflow Question

You'll want to reference the elements var x = message[1]; and var y = message[2];.

// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Corona SDK Transmission Example based on Stackoverflow Question
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
var stream = smoothstream({
    channel       : "CORONA_SDK_CHANNEL",
    subscribe_key : "sub-c-4377ab04-f100-11e3-bffd-02ee2ddab7fe",

    x             : function (coord) { return coord[1] }, // <-- (x) HERE
    y             : function (coord) { return coord[2] }, // <-- (y) HERE

    chart         : 'chart1',
    strokeStyle   : 'rgba(185,230,199,1)',
    fillStyle     : 'rgba(222,238,191,0.7)',
    lineWidth     : 5
});
Community
  • 1
  • 1
Stephen Blum
  • 6,498
  • 2
  • 34
  • 46
  • 1
    Thanks. I am sending the coordinates from an app in Corona SDK. The format is essentially [id, time in #250 millisecond units, x-position]. The data looks like this in the original post [123, 55.7389, 350]. – Guy Forrester Dec 19 '14 at 15:21
  • I'll add the code that shows based on this info. You'll want to reference the elements `var x = message[1];` and `var y = message[2];` – Stephen Blum Dec 19 '14 at 17:24