Sanady

Untitled

Nov 12th, 2018
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var listen = new EventSource('http://vmzakova.fei.stuba.sk/sse/sse.php');
  2. var cline = document.getElementById("cervena");
  3. var mline = document.getElementById("modra");
  4.  
  5.  
  6. listen.onopen = function(){
  7.     console.log('DEBUG: Connectioned');
  8. }
  9.  
  10. var config = {
  11.     type: 'line',
  12.     data: {
  13.         labels: [],
  14.         datasets: [{
  15.             label: 'Data 1',
  16.             fill: false,
  17.             borderColor: window.chartColors.red,
  18.             backgroundColor: window.chartColors.red,
  19.             data: [],
  20.             hidden: false
  21.         }, {
  22.             label: 'Data 2',
  23.             fill: false,
  24.             backgroundColor: window.chartColors.blue,
  25.             borderColor: window.chartColors.blue,
  26.             data: [],
  27.             hidden: false
  28.         }]
  29.     },
  30.     options: {
  31.         responsive: true,
  32.         title: {
  33.             display: true,
  34.             text: 'Graf:'
  35.         },
  36.         tooltips: {
  37.             mode: 'index',
  38.             intersect: false,
  39.         },
  40.         hover: {
  41.             mode: 'nearest',
  42.             intersect: true
  43.         },
  44.         scales: {
  45.             xAxes: [{
  46.                 display: true,
  47.                 ticks: {
  48.                     callback: function(dataLabel, index) {
  49.                         return index % 2 === 0 ? dataLabel : '';
  50.                     }
  51.                 },
  52.                 scaleLabel: {
  53.                     display: true,
  54.                     labelString: 'Horizontalna hodnota'
  55.                 }
  56.             }],
  57.             yAxes: [{
  58.                 display: true,
  59.                 beginAtZero: false,
  60.                 scaleLabel: {
  61.                     display: true,
  62.                     labelString: 'Vertikalna hodnota'
  63.                 }
  64.             }]
  65.         }
  66.     }
  67. };
  68.  
  69. window.onload = function() {
  70.     var ctx = document.getElementById("mainGraf").getContext('2d');
  71.     window.myLine = new Chart(ctx, config);
  72. };
  73.  
  74. listen.onmessage = function(e){
  75.     //console.log(e.data);
  76.     var info = JSON.parse(e.data);
  77.  
  78.     console.log(info.x);
  79.     console.log(info.y1);
  80.     console.log(info.y2);
  81.  
  82.     if (config.data.datasets.length > 0) {
  83.  
  84.         config.data.labels.push(info.x);
  85.         config.data.datasets[0].data.push(info.y1);
  86.         config.data.datasets[1].data.push(info.y2);
  87.  
  88.         window.myLine.update();
  89.     }
  90. }
  91.  
  92. window.onload = function(){
  93.     document.getElementById('stopGraf').addEventListener('click', function() {
  94.         listen.close();
  95.         console.log('DEBUG: Connection has been stopped!');
  96.     });
  97. }
  98.  
  99. /*document.getElementById('stopGraf').addEventListener('click', function() {
  100.     listen.close();
  101.     console.log('DEBUG: Connection has been stopped!');
  102. });*/
  103.  
  104. /*function cLineCheck(){
  105.     if(cline.checked == true){
  106.         config.data.datasets[0].hidden = 'false';
  107.         window.myLine.update();
  108.     }
  109.     else{
  110.         config.data.datasets[0].hidden = 'true';
  111.         window.myLine.update();
  112.     }
  113. }
  114.  
  115. function mLineCheck(){
  116.     if(mline.checked == true){
  117.         config.data.datasets[1].hidden = 'false';
  118.         window.myLine.update();
  119.     }
  120.     else{
  121.         config.data.datasets[1].hidden = 'true';
  122.         window.myLine.update();
  123.     }
  124. }*/
  125.  
  126. /*cline.addEventListener('click', function(){
  127.     if(cline.checked == true){
  128.         config.data.datasets[0].hidden = 'false';
  129.         window.myLine.update();
  130.     }
  131.     else{
  132.         config.data.datasets[0].hidden = 'true';
  133.         window.myLine.update();
  134.     }
  135.  
  136. });
  137.  
  138. mline.addEventListener('click', function(){
  139.     if(mline.checked == true){
  140.         config.data.datasets[1].hidden = 'false';
  141.         window.myLine.update();
  142.     }
  143.     else{
  144.         config.data.datasets[1].hidden = 'true';
  145.         window.myLine.update();
  146.     }
  147.  
  148. });*/
Advertisement
Add Comment
Please, Sign In to add comment