知识图谱:
知识图谱样式:relation-graph - A Relationship Graph Component
图谱前端样式:
'rootId': 'a',
'nodes': [
{ 'id': 'a', 'text': 'a'},
{ 'id': 'b', 'text': 'b'},
{ 'id': 'b1', 'text': 'b1'},
{ 'id': 'b1-1', 'text': 'b1-1'},
{ 'id': 'b1-2', 'text': 'b1-2'},
{ 'id': 'b1-3', 'text': 'b1-3'},
{ 'id': 'b1-4', 'text': 'b1-4'},
{ 'id': 'b1-5', 'text': 'b1-5'},
{ 'id': 'b1-6', 'text': 'b1-6'},
{ 'id': 'b2', 'text': 'b2'},
{ 'id': 'b2-1', 'text': 'b2-1'},
{ 'id': 'b2-2', 'text': 'b2-2'},
{ 'id': 'c', 'text': 'c'},
{ 'id': 'c1', 'text': 'c1'},
{ 'id': 'c2', 'text': 'c2'},
{ 'id': 'c3', 'text': 'c3'}],
'lines': [
{ 'from': 'a', 'to': 'b'},
{ 'from': 'b', 'to': 'b1'},
{ 'from': 'b1', 'to': 'b1-1'},
{ 'from': 'b1', 'to': 'b1-2'},
{ 'from': 'b1', 'to': 'b1-3'},
{ 'from': 'b1', 'to': 'b1-4'},
{ 'from': 'b1', 'to': 'b1-5'},
{ 'from': 'b1', 'to': 'b1-6'},
{ 'from': 'b', 'to': 'b2'},
{ 'from': 'b2', 'to': 'b2-1'},
{ 'from': 'b2', 'to': 'b2-2'},
{ 'from': 'a', 'to': 'c'},
{ 'from': 'c', 'to': 'c1'},
{ 'from': 'c', 'to': 'c2'},
{ 'from': 'c', 'to': 'c3'}]
rootId是中心数据,nodes里面放数据,lines里面放关系指向。
比如:
'rootId': '1',
'nodes': [
{ 'id': '1', 'text': '中心'},
{ 'id': '2', 'text': '分节点1'},
{ 'id': '3', 'text': '分节点2'},
{ 'id': '4', 'text': '分节点1-1'},],
'lines': [
{ 'from': '1', 'to': '2'},
{ 'from': '1', 'to': '3'},
{ 'from': '2', 'to': '4'}]
后端代码处理:
@Override
public Map<String, Object> getAtlas(String id) {
Map<String,Object> resultMap = new HashMap<>();
List<Map<String,Object>> nodesList = new ArrayList<>();
List<Map<String,Object>> linesList = new ArrayList<>();
Map<String,Object> nodesMap = new HashMap<>();
// 中心点数据
Zdxm zdxm = impProMapper.selectZdxmByXh(id);
nodesMap.put("id",zdxm.getXh());
nodesMap.put("text",zdxm.getXmmc());
nodesList.add(nodesMap);
// 节点数据
List<ImpProjectItem> impProjectItemList = impProMapper.selectProjectInfo(id);
// 判断是否有节点数据,若有则递归拼接节点
if (ObjectUtil.isNotEmpty(impProjectItemList)){
List<ImpProjectItem> parentList = impProjectItemList.stream().filter(e -> ObjectUtil.isEmpty(e.getParentId())).collect(Collectors.toList());
parentList.forEach(e ->{
Map<String,Object> nodeMap = new HashMap<>();
Map<String,Object> linesMap = new HashMap<>();
// 数据
nodeMap.put("id",e.getId());
nodeMap.put("text",e.getTitle());
nodesList.add(nodeMap);
// 节点指向关系
linesMap.put("from",id);
linesMap.put("to",e.getId());
linesList.add(linesMap);
getAtlas(e.getId(),impProjectItemList,nodesList,linesList);
});
}
resultMap.put("rootId",id);
resultMap.put("nodes",nodesList);
resultMap.put("lines",linesList);
return resultMap;
}
public void getAtlas(String parentId,List<ImpProjectItem> impProjectItemList,List<Map<String,Object>> nodesList ,List<Map<String,Object>> linesList){
List<ImpProjectItem> childList = impProjectItemList.stream().filter(impProjectItem -> ObjectUtil.isNotEmpty(impProjectItem.getParentId()) && impProjectItem.getParentId().equals(parentId)).collect(Collectors.toList());
if (ObjectUtil.isNotEmpty(childList)){
childList.forEach(e ->{
Map<String,Object> nodeMap = new HashMap<>();
Map<String,Object> linesMap = new HashMap<>();
// 数据
nodeMap.put("id",e.getId());
nodeMap.put("text",e.getTitle());
nodesList.add(nodeMap);
// 节点指向关系
linesMap.put("from",parentId);
linesMap.put("to",e.getId());
linesList.add(linesMap);
// 判断是否还有节点
if (ObjectUtil.isNotEmpty(impProjectItemList.stream().filter(impProjectItem -> ObjectUtil.isNotEmpty(impProjectItem.getParentId()) && impProjectItem.getParentId().equals(e.getId())).collect(Collectors.toList()))){
getAtlas(e.getId(),impProjectItemList,nodesList,linesList);
}
});
}
}