|
1 | | -// This is a manifest file that'll be compiled into application.js, which will include all the files |
2 | | -// listed below. |
3 | | -// |
4 | | -// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts, |
5 | | -// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path. |
6 | | -// |
7 | | -// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the |
8 | | -// the compiled file. |
9 | | -// |
10 | | -// WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD |
11 | | -// GO AFTER THE REQUIRES BELOW. |
12 | | -// |
13 | 1 | //= require jquery |
14 | 2 | //= require jquery_ujs |
| 3 | +//= require jquery.ui.all |
15 | 4 | //= require_tree . |
16 | 5 | // |
17 | 6 | //= require highcharts |
18 | 7 | //= require highcharts/highcharts-more |
19 | 8 |
|
20 | 9 | $(function() { |
21 | 10 | // Create the chart |
22 | | - $('#container').highcharts({ |
| 11 | + chart = new Highcharts.Chart({ |
23 | 12 | chart: { |
| 13 | + renderTo: 'container', |
24 | 14 | type: 'area', |
25 | | - plotBorderWidth: 0, |
26 | | - events: { |
27 | | - load: function() { |
28 | | - // set up the updating of the chart each second |
29 | | - var series = this.series[0]; |
30 | | - setInterval(function() { |
31 | | - var x = (new Date()).getTime(), // current time |
32 | | - |
33 | | - // Previous value +/- 10 max |
34 | | - y = Math.round(series.data.slice(-1)[0]['y'] + (Math.random() * 10 * (Math.round(Math.random()) * 2 - 1))); |
35 | | - if (y < 0) { |
36 | | - y = -y |
37 | | - } |
38 | | - series.addPoint([x, y], true, true); |
39 | | - }, 1000); |
40 | | - } |
41 | | - } |
| 15 | + plotBorderWidth: 0 |
42 | 16 | }, |
43 | 17 | title: { |
44 | | - text: 'Live random data' |
| 18 | + text: 'Live random data received through SSE' |
45 | 19 | }, |
46 | 20 | xAxis: { |
47 | 21 | type: 'datetime' |
@@ -71,14 +45,45 @@ $(function() { |
71 | 45 | name: 'Random data', |
72 | 46 | lineWidth: 2, |
73 | 47 | data: (function() { |
74 | | - // generate an array of random data |
75 | | - var data = [], time = (new Date()).getTime(), i; |
76 | | - var previous; |
77 | | - for( i = -60; i <= 0; i+=1) { |
78 | | - data.push([time + i * 1000, Math.round(Math.random() * 100)]); |
| 48 | + // Initializing points 20 points to 0 |
| 49 | + var datapoints = [], time = (new Date()).getTime(), i=-20; |
| 50 | + while(i <= 0) { |
| 51 | + // time is milliseconds, we display the last 20 sec points |
| 52 | + datapoints.push([(time + (i*1000)), 0]); |
| 53 | + i += 1; |
79 | 54 | } |
80 | | - return data; |
| 55 | + return datapoints; |
81 | 56 | })() |
82 | 57 | }] |
83 | 58 | }); |
| 59 | + |
| 60 | + if (!!window.EventSource) { |
| 61 | + var source = new EventSource('/realtime-analytics'); |
| 62 | + var date = new Date(); |
| 63 | + |
| 64 | + // counter matches what you send in lib/realtime_analytics.rb |
| 65 | + source.addEventListener('FOOBAR', function(e) { |
| 66 | + x = (new Date().getTime()); |
| 67 | + y = parseFloat(e.data) |
| 68 | + chart.series[0].addPoint([x, y], true, true); |
| 69 | + $("#last_received").html(y); |
| 70 | + $("#last_received").effect( "highlight", {}, 500 ); |
| 71 | + }, false); |
| 72 | + |
| 73 | + source.addEventListener('message', function(e) { |
| 74 | + console.log(e.data); |
| 75 | + }, false); |
| 76 | + |
| 77 | + source.addEventListener('open', function(e) { |
| 78 | + console.log("SSE connection opened"); |
| 79 | + }, false); |
| 80 | + |
| 81 | + source.addEventListener('error', function(e) { |
| 82 | + if (e.readyState == EventSource.CLOSED) { |
| 83 | + console.log("SSE connection closed"); |
| 84 | + } |
| 85 | + }, false); |
| 86 | + } else { |
| 87 | + console.log("SSE not supported by your browser"); |
| 88 | + } |
84 | 89 | }); |
0 commit comments