|
| 1 | +var config = require('./config.json'), |
| 2 | + username = config.user, |
| 3 | + apikey = config.apikey, |
| 4 | + tokens = config.tokens, |
| 5 | + Plotly = require('../.')(username, apikey); |
| 6 | + |
| 7 | +function initTrace(token) { |
| 8 | + return { |
| 9 | + x: [], // init. data arrays |
| 10 | + y: [], |
| 11 | + type: 'scatter', |
| 12 | + mode: 'markers', |
| 13 | + marker: { color: '#91C149' }, |
| 14 | + stream: { |
| 15 | + 'token': token, |
| 16 | + 'maxpoints': 100 |
| 17 | + } |
| 18 | + }; |
| 19 | +} |
| 20 | + |
| 21 | +var data = tokens.map(initTrace); |
| 22 | + |
| 23 | +var layout = { |
| 24 | + filename: 'stream-style-attributes', |
| 25 | + fileopt: 'overwrite', |
| 26 | + layout: { |
| 27 | + title: 'streaming data <i>and</i> style attributes' |
| 28 | + }, |
| 29 | + world_readable: true |
| 30 | +}; |
| 31 | + |
| 32 | +Plotly.plot(data, layout, function(err, resp) { |
| 33 | + if(err) return console.log('ERROR', err, data); |
| 34 | + console.log(resp); |
| 35 | + |
| 36 | + tokens.forEach(function(token) { |
| 37 | + var plotlystream = Plotly.stream(token, function() { |
| 38 | + clearInterval(loop); |
| 39 | + }), |
| 40 | + i = 0; |
| 41 | + |
| 42 | + var loop = setInterval(function() { |
| 43 | + var streamData = { |
| 44 | + x: Math.random(), |
| 45 | + y: 2 * Math.random(), |
| 46 | + marker: { color: '#91C149' } |
| 47 | + }; |
| 48 | + |
| 49 | + // pts below threshold get a different color |
| 50 | + if(streamData.y < 1) streamData.marker.color = '#000000'; |
| 51 | + |
| 52 | + plotlystream.write(JSON.stringify(streamData) + '\n'); |
| 53 | + i++; |
| 54 | + |
| 55 | + }, 1000); |
| 56 | + }); |
| 57 | +}); |
0 commit comments