在文章Cayley圖數據庫的簡介及使用中,咱們已經瞭解了Cayley圖數據庫的安裝、數據導入以及進行查詢等。
Cayley圖數據庫是Google開發的開源圖數據庫,雖然功能尚未Neo4J來得那麼強大,但也有不少新的功能等待着咱們去探索。本文將繼續上篇文章的旅程,給讀者介紹如何在Cayley圖數據庫中實現查詢結果的可視化。
下面,讓咱們一塊兒來探究Cayley的奧祕吧~html
Cayley圖數據庫的查詢語句的參考網址爲:https://github.com/cayleygraph/cayley/blob/master/docs/GizmoAPI.md 。 若想實現查詢結果的可視化,則須要使用Tag()函數,返回的結果樣式應當以下:node
[ { "source": "node1", "target": "node2" }, { "source": "node1", "target": "node3" }, ]
即返回的結果中對節點會打上Tag,source爲來源,顏色爲藍色,target爲目的地,顏色爲橙色。
咱們使用的數據仍來自文章Cayley圖數據庫的簡介及使用 。 首先導入數據:git
./cayley load -c cayley_example.yml -i data/China_Movie.nq
接着啓動查詢語句的web界面:github
./cayley http -i ./data/China_Movie.nq -d memstore --host=:64210
在瀏覽器中輸入網址:http://localhost:64210 ,選擇Visualize,web
輸入命令:算法
g.V('<沈騰>').Tag("source").Out('<ACT_IN>').Tag("target").All();
就能能到關係圖的可視化結果了,以下:數據庫
接着咱們來查看某個實體的全部屬性及屬性值,輸入的命令以下:json
var eq = "<流浪地球>"; var attrs = g.V(eq).OutPredicates().ToArray(); values = new Array(); for (i in attrs) { var value = g.V(eq).Out(attrs[i]).ToValue(); values[i] = value; } var s = new Array(); for (i in attrs) { var key_val_json = new Object(); key_val_json["id"] = values[i]; key_val_json["source"] = eq; key_val_json["target"]= attrs[i]+":"+values[i]; s[i] = key_val_json; } for (i =0; i< s.length; i++) { g.Emit(s[i]); }
出來的圖以下:數組
這樣咱們就實現了Cayley圖數據庫的可視化,可是效果通常,並且不支持對邊賦值,所以沒法在邊上顯示關係。瀏覽器
利用D3.js,咱們能夠把查詢到的結果,本身來畫關係圖。筆者主要參考的項目的Github地址爲: https://github.com/ownthink/KG-View/blob/master/index.html 。咱們只須要將查詢到的結果複製粘貼到該HTML文件中便可。仍是以《流浪地球》的全部屬性及屬性值爲例,查詢的命令以下:
var eq = "<流浪地球>"; var attrs = g.V(eq).OutPredicates().ToArray(); values = new Array(); for (i in attrs) { var value = g.V(eq).Out(attrs[i]).ToValue(); values[i] = value; } var s = new Array(); for (i in attrs) { var key_val_json = new Object(); key_val_json["source"] = eq; key_val_json["rela"] = attrs[i]; key_val_json["target"] = values[i]; key_val_json["type"] = "resolved"; s[i] = key_val_json; } for (i =0; i< s.length; i++) { g.Emit(s[i]); }
返回的結果以下:
{ "result": [ { "rela": "<ISA>", "source": "<流浪地球>", "target": "<Movie>", "type": "resolved" }, { "rela": "<rank>", "source": "<流浪地球>", "target": "2", "type": "resolved" }, { "rela": "<src>", "source": "<流浪地球>", "target": "/item/%E6%B5%81%E6%B5%AA%E5%9C%B0%E7%90%83", "type": "resolved" }, { "rela": "<box_office>", "source": "<流浪地球>", "target": "40.83億", "type": "resolved" }, { "rela": "<avg_price>", "source": "<流浪地球>", "target": "46", "type": "resolved" }, { "rela": "<avg_people>", "source": "<流浪地球>", "target": "50", "type": "resolved" }, { "rela": "<begin_date>", "source": "<流浪地球>", "target": "2019.02.05", "type": "resolved" } ] }
將result的結果數組複製粘貼至index.html文件,內容以下:
<!DOCTYPE html> <meta charset="utf-8"> <style>.link { fill: none; stroke: #666; stroke-width: 1.5px;}#licensing { fill: green;}.link.licensing { stroke: green;}.link.resolved { stroke-dasharray: 0,2 1;}circle { fill: #ccc; stroke: #333; stroke-width: 1.5px;}text { font: 12px Microsoft YaHei; pointer-events: none; text-shadow: 0 1px 0 #fff, 1px 0 0 #fff, 0 -1px 0 #fff, -1px 0 0 #fff;}.linetext { font-size: 12px Microsoft YaHei;}</style> <body> <script src="https://d3js.org/d3.v3.min.js"></script> <script> var links = [ { "rela": "<ISA>", "source": "<流浪地球>", "target": "<Movie>", "type": "resolved" }, { "rela": "<rank>", "source": "<流浪地球>", "target": "2", "type": "resolved" }, { "rela": "<src>", "source": "<流浪地球>", "target": "/item/%E6%B5%81%E6%B5%AA%E5%9C%B0%E7%90%83", "type": "resolved" }, { "rela": "<box_office>", "source": "<流浪地球>", "target": "40.83億", "type": "resolved" }, { "rela": "<avg_price>", "source": "<流浪地球>", "target": "46", "type": "resolved" }, { "rela": "<avg_people>", "source": "<流浪地球>", "target": "50", "type": "resolved" }, { "rela": "<begin_date>", "source": "<流浪地球>", "target": "2019.02.05", "type": "resolved" } ]; var nodes = {}; links.forEach(function(link) { link.source = nodes[link.source] || (nodes[link.source] = {name: link.source}); link.target = nodes[link.target] || (nodes[link.target] = {name: link.target}); }); var width = 1920, height = 1080; var force = d3.layout.force() .nodes(d3.values(nodes)) .links(links) .size([width, height]) .linkDistance(180) .charge(-1500) .on("tick", tick) .start(); var svg = d3.select("body").append("svg") .attr("width", width) .attr("height", height); var marker= svg.append("marker") .attr("id", "resolved") .attr("markerUnits","userSpaceOnUse") .attr("viewBox", "0 -5 10 10") .attr("refX",32) .attr("refY", -1) .attr("markerWidth", 12) .attr("markerHeight", 12) .attr("orient", "auto") .attr("stroke-width",2) .append("path") .attr("d", "M0,-5L10,0L0,5") .attr('fill','#000000'); var edges_line = svg.selectAll(".edgepath") .data(force.links()) .enter() .append("path") .attr({ 'd': function(d) {return 'M '+d.source.x+' '+d.source.y+' L '+ d.target.x +' '+d.target.y}, 'class':'edgepath', 'id':function(d,i) {return 'edgepath'+i;}}) .style("stroke",function(d){ var lineColor; lineColor="#B43232"; return lineColor; }) .style("pointer-events", "none") .style("stroke-width",0.5) .attr("marker-end", "url(#resolved)" ); var edges_text = svg.append("g").selectAll(".edgelabel") .data(force.links()) .enter() .append("text") .style("pointer-events", "none") .attr({ 'class':'edgelabel', 'id':function(d,i){return 'edgepath'+i;}, 'dx':80, 'dy':0 }); edges_text.append('textPath') .attr('xlink:href',function(d,i) {return '#edgepath'+i}) .style("pointer-events", "none") .text(function(d){return d.rela;}); var circle = svg.append("g").selectAll("circle") .data(force.nodes()) .enter().append("circle") .style("fill",function(node){ var color; var link=links[node.index]; color="#F9EBF9"; return color; }) .style('stroke',function(node){ var color; var link=links[node.index]; color="#A254A2"; return color; }) .attr("r", 28) .on("click",function(node) { edges_line.style("stroke-width",function(line){ console.log(line); if(line.source.name==node.name || line.target.name==node.name){ return 4; }else{ return 0.5; } }); }) .call(force.drag); var text = svg.append("g").selectAll("text") .data(force.nodes()) .enter() .append("text") .attr("dy", ".35em") .attr("text-anchor", "middle") .style('fill',function(node){ var color; var link=links[node.index]; color="#A254A2"; return color; }).attr('x',function(d){ var re_en = /[a-zA-Z]+/g; if(d.name.match(re_en)){ d3.select(this).append('tspan') .attr('x',0) .attr('y',2) .text(function(){return d.name;}); } else if(d.name.length<=4){ d3.select(this).append('tspan') .attr('x',0) .attr('y',2) .text(function(){return d.name;}); }else{ var top=d.name.substring(0,4); var bot=d.name.substring(4,d.name.length); d3.select(this).text(function(){return '';}); d3.select(this).append('tspan') .attr('x',0) .attr('y',-7) .text(function(){return top;}); d3.select(this).append('tspan') .attr('x',0) .attr('y',10) .text(function(){return bot;}); } }); function tick() { circle.attr("transform", transform1); text.attr("transform", transform2); edges_line.attr('d', function(d) { var path='M '+d.source.x+' '+d.source.y+' L '+ d.target.x +' '+d.target.y; return path; }); edges_text.attr('transform',function(d,i){ if (d.target.x<d.source.x){ bbox = this.getBBox(); rx = bbox.x+bbox.width/2; ry = bbox.y+bbox.height/2; return 'rotate(180 '+rx+' '+ry+')'; } else { return 'rotate(0)'; } }); } function linkArc(d) { return 'M '+d.source.x+' '+d.source.y+' L '+ d.target.x +' '+d.target.y } function transform1(d) { return "translate(" + d.x + "," + d.y + ")"; } function transform2(d) { return "translate(" + (d.x) + "," + d.y + ")"; } </script>
在瀏覽器中打開,效果以下:
這個繪圖的效果會比Cayley自帶的效果好一些,但功能仍是有限。
網上關於Cayley的相關資料比較少,基本只有官方文檔和社區做爲參考。本文所講述的內容若有不足之處,還請讀者多多指教~另外,關於Cayley的可視化,如讀者有更好地辦法實現,也歡迎告知筆者~
注意:不妨瞭解下筆者的微信公衆號: Python爬蟲與算法(微信號爲:easy_web_scrape), 歡迎你們關注~