Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Real-time data with D3

I was wondering if I could use the D3 library with real-time data my server would be sending via websockets. I can't see any documentation or examples that demonstrate this. My initial expectation was to do so by the following sample from code:

ws = new WebSocket(ws://localhost:8888/ma");   
some more code....

  ws.onmessage = function(evt) {
        d3.json("evt.data", function(json) {
......    
.......More code.....
......
 }
}

But this doesn't seem to work, but I know the client receives the data by checking the console log.

Furthermore there is a recursive function which flattens out a JSON document:

// Returns a flattened hierarchy containing all leaf nodes under the root.
function classes(root) {
var classes = [];

function recurse(name, node) {
  if (node.children) node.children.forEach(function(child) { recurse(node.name, child);    });
  else classes.push({packageName: name, className: node.name, value: node.size});
  }

  recurse(null, root);
  return {children: classes};   
}

     console.log(evt.data);
  };

  ws.onclose = function (evt) {
       alert("Connection terminated")};

  });
 });

If my JSON doc is flat already then I presume it won't be required ie:

{ID: 1, Name: 'my name', age: 65, car: 'Ford'}

D3 is completely new to me so help would be appreciated.

Thanks

like image 449
user94628 Avatar asked Dec 02 '12 18:12

user94628


People also ask

How does D3 data work?

data() D3 is data driven. The data() function is used to join the specified array of data to the selected DOM elements and return the updated selection. D3 works with different types of data like Array, CSV, TSV, JSON, XML etc.

Is d3js still relevant?

The JavaScript ecosystem has completely changed during this time, in terms of libraries, best practices and even language features. Nevertheless, D3 is still here. And it's more popular than ever.

What is D3 in data science?

D3 itself is a JavaScript (JS) library and on top of that, you'll need a basic understanding of HTML (Hypertext Markup Language) and CSS (Cascading Style Sheets) to get the most out of it. If you're a data scientist, chances are JavaScript, at best, ranks as your fourth language, after R, Python, and SQL.

Is d3js slow?

D3. js really slow for data as much as 50,000 rows and even slow for the force directed graphs for data of 2000 rows.


1 Answers

I haven't used websockets with D3 (yet) but it looks like you're expecting d3.json to be a JSON parser. It's not - it's an AJAX loader that delegates to JSON.parse for parsing. So you probably want something like:

var ws = new WebSocket("ws://localhost:8888/ma");

var data = [];

ws.onmessage = function(evt) {
    // append new data from the socket
    data.push(JSON.parse(evt.data));
    update();
};

// now use the standard join pattern to deal with updates
function update() {
    var chunk = d3.selectAll('p')
        .data(data);

    // entry might be all you need, if you're always appending
    chunk.enter().append('p')
        .text(function(d) { return d; });

}
like image 168
nrabinowitz Avatar answered Oct 27 '22 15:10

nrabinowitz