So I´m trying to create a hierarchical tree diagram using D3.js. It's supposed to work on top of a QtWebView, and so far, so good: it works. Now I need to tackle the data handling bit of the app. My data is stored in CSV format (not my choice, nor do I have any opinion on that matter) and is described below.
My question is: how to output JSON from this CSV?
I have been looking around for examples for a few days, but I'm stuck. I just wondered if someone could point me in the right direction, since I'm only getting examples on how to produce the tree with data in JSON format and people talking about how to use plugins to output JSON with Qt.
I have no trouble in reading from/writing to the CSV/JSON files. I just really need to know how to make an algorithm that would allow me from this data to output JSON.
This is my example CSV file (node_type and node_size are just for show):
node_id,name,node_size,node_type,parent_node 1,node1,122,4,17 2,node2,127,4,20 3,node3,64,2,5 4,node4,147,5,14 5,node5,146,5,12 6,node6,57,4,10 7,node7,149,1,20 8,node8,141,6,10 9,node9,65,5,10 10,node10,108,2,10 11,node11,97,6,2 12,node12,102,2,5 13,node13,79,6,3 14,node14,90,2,2 15,node15,129,1,8 16,node16,76,1,8 17,node17,68,6,13 18,node18,53,5,7 19,node19,112,6,20 20,node20,147,5,6
EDIT: I tried Pablo Navarro's tip and produced the following script for D3.js to handle. It's not producing any errors, but nothing is displayed. Can anyone hint on why?
EDIT2: Fixed typo in code.
var width = 960, height = 500;
var color = d3.scale.category20();
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
var nodesArray = [], edgesArray = [];
d3.csv('nodes.csv', function(data) {
for (var k = 0, n = data.length; k < n; k += 1) {
nodesArray.push({name: data[k].name, size: data[k].node_size, id: data[k].node_id}),
edgesArray.push({source: data[k].node_id, target: data[k].parent_node});
}
});
var force = d3.layout.force()
.nodes(nodesArray)
.links(edgesArray)
.size([width, height])
.start();
var link = svg.selectAll(".link")
.data(edgesArray)
.enter().append("line")
.attr("class", "link")
var node = svg.selectAll(".node")
.data(nodesArray)
.enter().append("circle")
.attr("class", "node")
.attr("r", nodesArray.size)
EDIT3: One small detail that I thinks might be making all the difference: I'm calling this locally, and by locally I don't mean in a "localhost" webserver of sorts, but in a file. From what I read in Jerome Crukier's tutorial:
This will no longer work in a local file system (ie opening a file in the browser). The resulting file can only run on a webserver, which can be local (ie the page has a url).
I'm not sure if this is exactly the case with my code, but once more, if someone has any insight, it will be appreciated.
EDIT 4:
Ok, after some searching online, I sadly have to admit to not knowing how to use Google & Stackoverflow: I found out everything I needed to know here, ironically, explained by mbostock (of D3.js) himself. I now understand that I had no idea on what I was actually looking for, but somehow I found it.
Not really sure what to do now regarding this (still open) question.