admin管理员组文章数量:1387312
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 pletely new to me so help would be appreciated.
Thanks
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 pletely new to me so help would be appreciated.
Thanks
Share Improve this question asked Dec 2, 2012 at 18:44 user94628user94628 3,73118 gold badges54 silver badges90 bronze badges 4- Could you please reformat the code? The second code block makes no sense to me … – filmor Commented Dec 2, 2012 at 19:17
- Related: stackoverflow./questions/13591891/… – rosshamish Commented Dec 2, 2012 at 19:26
- @filmor That function is from the D3 code example at: github./mbostock/d3/blob/master/examples/bubble/bubble.js. I think it's used to 'flatten' out the JSON structure if the original JSON document is nested. – user94628 Commented Dec 2, 2012 at 19:36
- @RossHamish, that's a question I also put up. I'm hoping that this would be a simpler way of asking it, as getting data in realtime seems to be my main problem. – user94628 Commented Dec 2, 2012 at 19:37
1 Answer
Reset to default 6I 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; });
}
本文标签: javascriptRealtime data with D3Stack Overflow
版权声明:本文标题:javascript - Real-time data with D3 - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744541142a2611620.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论