Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/419.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Neo4j结果(JSON格式)创建D3树形图_Javascript_Json_D3.js_Neo4j_Graph Databases - Fatal编程技术网

Javascript Neo4j结果(JSON格式)创建D3树形图

Javascript Neo4j结果(JSON格式)创建D3树形图,javascript,json,d3.js,neo4j,graph-databases,Javascript,Json,D3.js,Neo4j,Graph Databases,我试图用D3树来表示层次结构,其中包含一些额外的字段细节。现在我不确定/不知道如何获得D3树图所需的JSON;直接来自JavaNeo4JAPI。出于某种原因,我们在Neo4j之上使用嵌入式Neo4j和Web服务 寻求建议和示例Java代码,为关系生成以下JSON格式: 下面给出了一个示例Json和D3实现 域模型: (Switch)-[:CONNECTED_TO]->(Switch) 使用JSON格式(见下文),我想生成一个D3树,如下所示 var json= { “名称”:“开始”,

我试图用D3树来表示层次结构,其中包含一些额外的字段细节。现在我不确定/不知道如何获得D3树图所需的JSON;直接来自JavaNeo4JAPI。出于某种原因,我们在Neo4j之上使用嵌入式Neo4j和Web服务

寻求建议和示例Java代码,为关系生成以下JSON格式:

下面给出了一个示例Json和D3实现

域模型:

(Switch)-[:CONNECTED_TO]->(Switch)
使用JSON格式(见下文),我想生成一个D3树,如下所示

var json=
{
“名称”:“开始”,
“显示”:“输入”,
“父项”:“null”,
“儿童”:[
{
“名称”:“Swith-1”,
“显示”:“美利坚合众国入境开关”,
“父项”:“开始”,
“儿童”:[
{
“名称”:“Swith-1.1”,
“显示”:“IL输入开关”,
“父项”:“Swith-1”,
“儿童”:[
{
“名称”:“Swith-1.1.1”,
“显示”:“芝加哥入口开关”,
“父项”:“Swith-1.1”
},
{
“名称”:“Swith-1.1.2”,
“显示”:“斯普林菲尔德入口开关”,
“父项”:“Swith-1.1”
}
] 
},
{
“名称”:“Swith-1.2”,
“显示”:“CA输入开关”,
“父项”:“Swith-1”
}
]
},
{
“名称”:“Swith-2”,
“显示”:“外部网关”,
“父项”:“开始”
}
]
};
var宽度=700;
var高度=650;
var maxLabel=150;
var持续时间=500;
var半径=5;
var i=0;
变种根;
var tree=d3.layout.tree()
.尺寸([高度、宽度]);
var diagonal=d3.svg.diagonal()
.投影(函数(d){返回[d.y,d.x];});
var svg=d3.选择(“正文”).追加(“svg”)
.attr(“宽度”,宽度)
.attr(“高度”,高度)
.附加(“g”)
.attr(“transform”,“translate”(+maxLabel+”,0)”);
root=json;
root.x0=高度/2;
root.y0=0;
根。子。forEach(塌陷);
函数更新(源)
{
//计算新的树布局。
var nodes=tree.nodes(root.reverse();
var links=tree.links(节点);
//为固定深度进行规格化。
forEach(函数(d){d.y=d.depth*maxLabel;});
//更新节点…
var node=svg.selectAll(“g.node”)
.数据(节点,函数(d){
返回d.id | |(d.id=++i);
});
//在父节点的上一个位置输入任何新节点。
var nodeEnter=node.enter()
.附加(“g”)
.attr(“类”、“节点”)
.attr(“transform”,函数(d){return“translate”(“+source.y0+”,“+source.x0+”));})
。开启(“点击”,点击);
nodeEnter.append(“圆”)
.attr(“r”,0)
.style(“填充”,函数(d){
返回d.(儿童?)浅蓝色“:“白色”;
});
nodeEnter.append(“文本”)
.attr(“x”,函数(d){
var间距=计算半径(d)+5;
返回d.children | | d.| children?-间距:间距;
})
.attr(“dy”,“3”)
.attr(“文本锚定”,函数(d){return d.children | | d.| u children?“end”:“start”})
.text(函数(d){返回d.name;})
.style(“填充不透明度”,0);
//将节点转换到其新位置。
var nodeUpdate=node.transition()
.持续时间(持续时间)
.attr(“转换”,函数(d){return“translate”(+d.y+),“+d.x+”);});
节点更新。选择(“圆圈”)
.attr(“r”,函数(d){return computeRadius(d);})
.style(“fill”,函数(d){return d._children?“lightsteelblue”:“fff”});
nodeUpdate.select(“文本”).style(“填充不透明度”,1);
//将退出节点转换到父节点的新位置。
var nodeExit=node.exit().transition()
.持续时间(持续时间)
.attr(“transform”,函数(d){return“translate”(“+source.y+”,“+source.x+”)”);})
.remove();
nodeExit.select(“圆圈”).attr(“r”,0);
nodeExit.select(“文本”).style(“填充不透明度”,0);
//更新链接…
var link=svg.selectAll(“path.link”)
.data(链接,函数(d){返回d.target.id;});
//在父对象的上一个位置输入任何新链接。
link.enter()插入(“路径”,“g”)
.attr(“类”、“链接”)
.attr(“d”,函数(d){
var o={x:source.x0,y:source.y0};
返回对角线({source:o,target:o});
});
//过渡链接到他们的新位置。
link.transition()
.持续时间(持续时间)
.attr(“d”,对角线);
//将退出节点转换到父节点的新位置。
link.exit().transition()
.持续时间(持续时间)
.attr(“d”,函数(d){
var o={x:source.x,y:source.y};
返回对角线({source:o,target:o});
})
.remove();
//将旧位置隐藏起来,以便过渡。
nodes.forEach(函数(d){
d、 x0=d.x;
d、 y0=d.y;
});
}
功能计算机(d)
{
if(d.children | | d.| u children)返回半径+(半径*nbEndNodes(d)/10);
否则返回半径;
}
函数nbEndNodes(n)
{
nb=0;
如果(儿童){
n、 children.forEach(函数(c){
nb+=nbEndNodes(c);
});
}
else if(n._儿童){
n、 _children.forEach(函数(c){
nb+=nbEndNodes(c);
});
}
else-nb++;
返回nb;
}
功能点击(d)
{
如果(d.儿童){
d、 _children=d.children;
d、 children=null;
} 
否则{
d、 儿童=d.\U儿童;
d、 _children=null;
}
更新(d);
}
功能崩溃(d){
如果(d.儿童){
d、 _children=d.children;
d、 _儿童。forEach(崩溃);
d、 children=null;
}
}
更新(根)
html{
字体:10px无衬线;
}
svg{
边框:1px纯银;
}
.节点{
光标:指针;
}
.node circ
//Construct neo4j raw query
var query = [
    'MATCH (term:Term), (other:Term)',
    'OPTIONAL MATCH (term) -[relall]-> (other)',
    'WHERE ID(term) = {termId}',
    'RETURN other, COUNT(relall), type(relall)',
    // COUNT(rel) is a hack for 1 or 0, 1 indicates there is an outgoing relationship.
].join('\n')
@GET
@javax.ws.rs.Path("/getTree/{name}/")
@Produces({ "application/json" })
public Response getTree(@PathParam("name") final String name, @Context final GraphDatabaseService db)
        throws IOException {

    System.out.println(" Attribute Name To SearchByName: " + name);

    HashMap<Long, TreeNode> treeNodeMap = new HashMap<Long, TreeNode>();

    TreeNode rootNode = null;

    String attrubute = "name";

    try (Transaction tx = db.beginTx()) {
        final Node swtch = db.findNode(Labels.Switch, attrubute, name);
        if (swtch != null) {
            TraversalDescription td = db.traversalDescription().depthFirst()
                    .expand(PathExpanders.forTypeAndDirection(RelationshipTypes.CONNECTED_TO, Direction.OUTGOING));

            for (Path directoryPath : td.traverse(swtch)) 
            {
                Iterable<Relationship> connectedTos = directoryPath.endNode().getRelationships(Direction.OUTGOING,RelationshipTypes.CONNECTED_TO);

                if (connectedTos != null) 
                {
                    for(Relationship connectedTo : connectedTos)
                    {
                        //For the Current Relationship
                        //get the start node as parent
                        Node parentNode = connectedTo.getStartNode();
                        Long parentNodeId = parentNode.getId();
                        TreeNode parentTreeNode = treeNodeMap.get(parentNodeId);

                        if(parentTreeNode == null)
                        {
                            ////Populate the Parent Details
                            parentTreeNode = new TreeNode(parentNode.getProperty("name", "NoName").toString());
                            if(rootNode == null)
                                rootNode = parentTreeNode;
                            //Add to the linear HashMap for subsequent searches
                            treeNodeMap.put(parentNodeId, parentTreeNode);
                        }

                        //For the Current Relationship get the end node Children
                        Node childNode = connectedTo.getEndNode();
                        Long childNodeId = childNode.getId();
                        TreeNode childTreeNode = treeNodeMap.get(childNodeId);

                        if(childTreeNode == null)
                        {
                            childTreeNode = new TreeNode(childNode.getProperty("name", "NoName").toString());
                            treeNodeMap.put(childNodeId, childTreeNode);
                            parentTreeNode.setChildren(childTreeNode);
                        }
                    }
            }
        }
        tx.success();
    }
    }
    /*
     System.out.println("JSON: " + objectMapper.writeValueAsString(rootNode));
     System.out.println("LinearHashMap: " + objectMapper.writeValueAsString(treeNodeMap ));
     */

    return Response.ok().entity(objectMapper.writeValueAsString(rootNode)).build();

}