我想要使用d3.js或alchemy.js獲得可視化 - 但鍊金術,特別是需要數據源在GraphJSON中。從Neo4j到GraphJSON與Ruby
我一直在玩Max De Marzi(使用neography),Michael Hunger(cy2neo,js),Neo4j和Neo4j.rb的教程和示例 - 但我似乎無法一路獲得。主要是因爲我不知道自己在做什麼 - 但這就是我想學習的方式。
我試圖實現將是沿着線: https://bl.ocks.org/mbostock/3750558 或默認的可視化瀏覽:http://graphalchemist.github.io/Alchemy/#/docs
你可以看到GraphJSON格式應該是什麼樣子的發現它這個頁面還於: http://graphalchemist.github.io/Alchemy/#/docs
如果我運行下面...
get '/followers' do
Neo4j::Session.open(:server_db, "http://localhost:7474")
query = Neo4j::Session.query('MATCH (a--(b)--(c) RETURN a,b,c LIMIT 30')
puts "--------------"
puts query_to_graph_json(query)
query_to_graph_json(query)
end
# This is supposed to grab nodes and edges, but it never gets edges.
# It's originally from a conversation at the neo4j.rb site
def query_to_graph_json(query)
nodes = {}
edges = {}
add_datum = Proc.new do |datum|
case datum
when Neo4j::ActiveNode, Neo4j::Server::CypherNode
nodes[datum.neo_id] = {
id: datum.neo_id,
properties: datum.props #was attributes, but kept saying that wasn't a method
}
when Neo4j::ActiveRel, Neo4j::Server::CypherRelationship
edges[[datum.start_node.neo_id, datum.end_node.neo_id]] = {
source: datum.start_node.neo_id,
target: datum.end_node.neo_id,
type: datum.rel_type,
properties: datum.props
}
else
raise "Invalid value found: #{datum.inspect}"
end
end
query.each do |row|
row.to_a.each do |datum|
if datum.is_a?(Array)
datum.each {|d| add_datum.call(d) }
else
add_datum.call(datum)
end
end
end
{
nodes: nodes.values,
edges: edges.values
}.to_json
end
我去拿......
{
"nodes": [
{
"id": 597,
"properties": {
"name": "John",
"type": "Person"
}
},
{
"id": 127,
"properties": {
"name": "Chris",
"type": "Person"
}
},
{
"id": 129,
"properties": {
"name": "Suzie",
"type": "Person"
}
},
],
"edges": [
]
}
問題是我需要邊緣。
如果我跑......
get '/followers' do
content_type :json
neo = Neography::Rest.new("http://localhost:7474")
cypher = "MATCH (a)--(b)--(c) RETURN ID(a),a.name,ID(b),b.name,ID(c),c.name LIMIT 30"
puts neo.execute_query(cypher).to_json
end
我會得到的路徑表。但它沒有按照我需要的格式進行格式化 - 而且我也不知道如何從這種格式轉換爲GraphJSON格式。
{
"columns": [
"ID(a)",
"a.name",
"ID(b)",
"b.name",
"ID(c)",
"c.name"
],
"data": [
[
597,
"John",
127,
"Chris",
129,
"Suzie"
],
[
597,
"John",
6,
"Adam",
595,
"Pee-Wee"
]
]
}