$(document).ready(function() { var button = document.getElementById("startbutton"); var button2 = document.getElementById("stopbutton"); new Boolean(stop); button.onclick = function() { console.log(this.id + " START!"); stop = false; receivedata(); }; button2.onclick = function() { console.log(this.id + " STOP!"); stop = true; }; function receivedata() { var socket = 0; //-------------------------------------------------------------------------------------------**SOCKETS**----------------------- setInterval(function() { **socket = io.connect('http://localhost:8080'); socket.on('messages', function(EEG) {** document.getElementById('messages').innerHTML = (socket.id); if (stop) { updateChart(0); } else { updateChart(EEG); } **})** }, 100); }; //-----------------------------------------------------------------------------------------DIMENSIONS-------------------- // set the dimensions and margins of the graph // Create the Scalable Vector Graphics container and set the origin. var svg = d3.select("#chart"), margin = { top: 30, right: 10, bottom: 50, left: 50 }, width = Math.floor(svg.attr("width") - margin.left - margin.right), height = Math.floor(svg.attr("height") - margin.top - margin.bottom); var g = svg.append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")"); //-------------------------------------------------------------------------------------------DATA---------------------- let data = [{ values: [] }]; //Dadas input que se muestra del graph //------------------------------------------------------------------------------------------ATRIBUTOS------------------- g.append("defs").append("clipPath") .attr("id", "clip2") .append("rect") .attr("x", 0) .attr("y", 0) .attr("width", width) .attr("height", height); //----------------------------------------------------------------------------------------DOMAINS----------------------- // set the ranges and Y domain var x = d3.scaleTime().range([0, width]), y = d3.scaleLinear().range([height, 0]), z = d3.scaleOrdinal(d3.schemeCategory10); y.domain([-1.5, 1.5]); //-----------------------------------------------------------------------------------------LINE------------------------- //Define the line var line = d3.line() .curve(d3.curveBasis) //definimos tipo de curva .x(function(d) { return x(d.time); }) //para el eje x definimos el tiempo .y(function(d) { return y(d.eeg); }); //para el eje y definimos los datos EEG //-----------------------------------------------------------------------------------------AXIS------------------------- //Definimos el eje x var x_axis = d3.axisBottom() .scale(x); var x_axis_svg = g.append("g") .attr("class", "axis axis--x") .attr("transform", "translate(0," + height + ")"); // Add the Y Axis g.append("g") .attr("class", "axis axis--y") .call(d3.axisLeft(y)) .append("text") .attr("transform", "rotate(-90)") .attr("y", 6) .attr("dy", "0.71em") .attr("fill", "#000") //-------------------------------------------------------------------------------------NI-ZORRA------------------------- let pathsG = g.append("g").attr("id", "paths").attr("class", "paths") .attr("clip-path", "url(#clip2)"); // parse the date / time //var parseTime = d3.timeParse("%H:%M:%S.%L"); //-----------------------------------------------------------------------------------------UPDATE-DATA---------------------- let duration = 200; //how quickly to move (will look jerky if less that data input rate) let limit = 100; // how many datapoints, total points = (duration * limit) function updateChart(EEG) { let now = new Date(); //Calcula fecha actual console.log(now); data.forEach((e) => { let last = e.values.length ? e.values[e.values.length - 1].speed : 10; let newv = Math.round(Math.min(100, Math.max(2, last + (Math.random() * 6) - 3))); // EEG = 0; e.values.push({ time: now, eeg: EEG, }); console.log(data[0].values.length, "=", EEG, '\n', '-------------------------------------------'); }); //-----------------------------------------------------------------------------------------SLIDE-AXIS----------------- // Cambio de dominio para el eje x x.domain([now - ((limit - 2) * duration), now - duration]) // Transición del eje x por la izquierda x_axis_svg.transition().duration(duration).ease(d3.easeLinear, 2).call(x_axis); //-----------------------------------------------------------------------------------------UPDATE-AXIS--------------------- //Join var minerG = pathsG.selectAll(".minerLine").data(data); var minerGEnter = minerG.enter() //Enter .append("g") .attr("class", "minerLine") .merge(minerG); //Join var minerSVG = minerGEnter.selectAll("path").data(function(d) { return [d]; }); var minerSVGenter = minerSVG.enter() //Enter .append("path").attr("class", "line") .style("stroke", function(d) { return z(d.id); }) .merge(minerSVG) //Update .transition() .duration(duration) .ease(d3.easeLinear, 10) .attr("d", function(d) { return line(d.values) }) .attr("transform", null) } });