java后端提供生成Echars关系图的xml(Dom4j)
·
前言
今天接到一个生成Echars关系图的需求,本来认为下班前两个小时就搞定了,但是使用Dom4j生成的过程中仍然很繁琐,也没有在网上找到源代码(可能是我太笨了),所以就自己记录一下。
我们要实现的图是: 这个.

全程使用Dom4j 最后使用流的方式写入response
public void creatXml(HttpServletResponse response) throws IOException {
Document document = DocumentHelper.createDocument();
Element root = XmlUtils.getElement(document);
//生成node节点
creatNodes(root);
//生成edges节点
creatEdges(root);
//写入到response输出流
response.setContentType("text/xml;charset=utf-8");
response.setHeader("cache-control", "no-cache");
XMLWriter w = new XMLWriter(response.getWriter());
w.write(document);
w.close();
}
/**
* 生成node节点信息
*/
public void creatNodes(Element root){
Element root_nodes = root.addElement("nodes");
List<String> node_num = rulerSerivice.selectOneAndTwo();
//遍历插入
for (String s : node_num) {
Element root_node = root_nodes.addElement("node").addAttribute("id",s )
.addAttribute("label", s);
Element root_attvalues = root_node.addElement("attvalues");
root_attvalues.addElement("attvalue").addAttribute("for",""+s).addAttribute("value","0");
root_node.addElement("viz:size").addAttribute("value","9.485714");
root_node.addElement("viz:position").addAttribute("x", XYZUtils.xyzNum())
.addAttribute("y", XYZUtils.xyzNum())
.addAttribute("z", XYZUtils.xyzNum());
root_node.addElement("viz:color").addAttribute("r",""+ RGButils.rgbNum())
.addAttribute("g",RGButils.rgbNum())
.addAttribute("b",RGButils.rgbNum());
}
}
/**
* 创建edges节点
*/
public void creatEdges(Element root){
List<Rule> rules = rulerSerivice.selectselectRulesAndTotal();
Element root_edges = root.addElement("edges");
for (Rule rule : rules) {
Element root_edge = root_edges.addElement("edge");
root_edge.addAttribute("id",rule.getRule_total());
root_edge.addAttribute("source",rule.getRule_one().replace(" ",""));
root_edge.addAttribute("target",rule.getRule_two().replace(" ",""));
root_edge.addElement("attvalues").addElement("attvalue").addAttribute("置信度",String.valueOf(rule.getRule_confidence()));
}
ublic class XmlUtils {
public static Element getElement(Document document){
//生成xml跟文件
Element root = document.addElement("gexf")
.addAttribute("xmlns","http://www.gexf.net/1.2draft")
.addAttribute("version","1.2")
.addAttribute("xmlns:viz","http://www.gexf.net/1.2draft/viz")
.addAttribute("xmlns:xsi","http://www.w3.org/2001/XMLSchema-instance")
.addAttribute("xsi:schemaLocation","http://www.gexf.net/1.2draft http://www.gexf.net/1.2draft/gexf.xsd")
.addNamespace("viz","http://www.gexf.net/1.2draft/viz");
Element root1 = root.addElement("meta").addAttribute("lastmodifieddate","2014-01-30");
Element root2 = root.addElement("graph").addAttribute("defaultedgetype","undirected")
.addAttribute("mode","static");
root1.addElement("creator").addText("Gephi 0.8.1");
root1.addElement("description");
Element root21 = root2.addElement("attributes")
.addAttribute("class","node")
.addAttribute("mode","static");
root21.addElement("attribute").addAttribute("id","modularity_class")
.addAttribute("title","Modularity Class").addAttribute("type","integer");
return root2;
}
}
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐


所有评论(0)