I have this following requirements:
- Query Wikipedia using SPARQL
- Use that data to build a tree using jstree.
The data I get from the query could be piece together to show a path from 1 node to another. Basically a graph. Then at the second step, I have to convert it to a tree (with some nuances ommited)
After some processing, I reach the stage with the following data
//Object contain all nodes
{Id:[Array of parents]}
For example, if I have
{A:[B,C],
B:[],
C:[]}
means that there are 4 nodes in my tree. A
has 2 parents B
and C
while B
and C
belong to root. (Fact is,this data is not suited for a tree, but I need to do it anyway)
The problem being that jstree data model does not allow same ID to be presented so I need to create an additional fake A. The above data would turn to same thing like this for consumption by jstree
[
{id:0_A,parent:B},
{id:1_A,parent:C},
{id:B,parent:#},
{id:C,parent:#},
]
I am stuck at the idea how to do this. I have a faint feeling that I need to recursively go through each element in the parent array till there is only 1 element in their parent array and create additional fake node accordingly. But I cannot make up a comprehensive way to do it.
Example:
{A:[B,C,D],
B:[],
C:[],
D:[B,C]
}
//Recursively following B,C,D in A will give us the result that 4 fake A are needed
//The same in D. Following B and C will lead to 2 node that only have 1 or less result in their parent array--> 2 fake D
would turn in
[
{id:0_A,parent:B},
{id:1_A,parent:C},
{id:2_A,parent:0_D},
{id:3_A,parent:1_D},
{id:B,parent:#},
{id:C,parent:#},
{id:0_D,parent:B},
{id:1_D,parent:C},
]
jsfiddle of the tree above http://jsfiddle.net/2wrcdvjq/1/
from Turning graph data with multiple parents into data for jstree Javascript
0 komentar:
Posting Komentar