Skip to content

Instantly share code, notes, and snippets.

@loganhasson
Created February 18, 2014 02:11
Show Gist options
  • Save loganhasson/9063336 to your computer and use it in GitHub Desktop.
Save loganhasson/9063336 to your computer and use it in GitHub Desktop.
def self.make_json
# just gotta do this cuz of lame scope stuff
path = nil
# set up some arrays for my node hashes and my link hashes
nodes = []
links = []
# just for figuring out when to change "groups" (not important aside from coloring in D3.js
group = 1
current_parent = nil
# This is just getting the path array from a YAML file
TLC_STORE.transaction do
path = TLC_STORE["traveled_path"]
end
# just looking at the 120th to 135th elements of the multidimensional array becuase it's 14000+ elements long
path[120..135].each do |pair|
# just debugging
puts "#{pair[0].id} - #{pair[1].id}"
# this if block is not important here, only checking to see that the first element of a pair isn't nil
# it only ever is on the very first pair
if !pair[0]
nodes << {"name" => "Start", "group" => 1}
nodes << {"name" => pair[1].id, "group" => group}
links << {"source" => 1, "target" => 0, "value" => 1}
current_parent = pair[1].id
else
# unimportant stuff for changing the group (for D3 coloring)
if pair[0].id != current_parent
group += 1
current_parent = pair[0].id
end
parent = {"name" => pair[0].id, "group" => group}
child = {"name" => pair[1].id, "group" => group}
nodes << parent if !nodes.include?(parent)
nodes << child if !nodes.include?(child)
links << {"source" => nodes.index{|c| c["name"] == child["name"]}, "target" => nodes.index{|p| p["name"] == parent["name"]}, "value" => 1}
end
end
nodes.each_with_index do |node, i|
puts "#{i}. #{node}"
end
links.each_with_index do |link, i|
puts "#{i}. #{link}"
end
json = {"nodes" => nodes, "links" => links}.to_json
File.open('first_200.json', 'w') do |f|
f.write(json)
f.close
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment