Acts As Graph Diagram
Acts As Graph Diagram extends Active Record to add simple function for draw the Force Directed Graph with html.
See It Work
Usage
Append the line to your model file like below:
class God < ApplicationRecord
acts_as_graph_diagram
end
God.find_by(name: 'Rheā').add_destination God.find_by(name: 'Hēra', figure: 1)
God.find_by(name: 'Rheā').connecting_count
God.find_by(name: 'Rheā').aheads
God.find_by(name: 'Rheā').aheads.first.destination
Methods
- aheads
- behinds
- add_destination(node, comment: '', figure: 0)
- add_departure(node, comment: '', figure: 0)
- get_destination(node)
- get_departure(node)
- remove_destination(node)
- remove_departure(node)
- connecting?(node)
- connecting_count()
- add_connection(node, directed: false, comment: '', figure: 0)
- sum_tree(column)
- assemble_tree_nodes()
Draws the graph diagram with D3.js
Distributes to represent a graph JSON via API.
- Implement a renderer in your controller file as below:
class GodsController < ApplicationController
def data_network
render json: { 'nodes' => God.all.pluck(:id, :name)
.map { |x| Hash[id: x[0], name: x[1]] },
'links' => Edge.all.pluck(:destination_id, :departure_id)
.map { |x| Hash[target: x[0], source: x[1]] } }
end
end
- And append the line to your routes.rb file like below:
Rails.application.routes.draw do
get 'data_network' => 'gods#data_network'
end
- Then execute a D3 query in your javascript file as below:
d3.json("http://127.0.0.1:3000/data_network").then(function (graph) {});
Calculates the total using the breadth first search.
PERT Chart. Drawn in Adobe Illustrator - inspired by a chart at netmba.com. Created by Jeremy Kemp. 2005/01/11 From Wikipedia, the free encyclopedia
Milestone.create(name: 10)
Milestone.create(name: 20)
Milestone.create(name: 30)
Milestone.create(name: 40)
Milestone.create(name: 50)
Milestone.find_by(name: 10).add_destination(Milestone.find_by(name: 20), figure: 3)
Milestone.find_by(name: 10).add_destination(Milestone.find_by(name: 30), figure: 4)
Milestone.find_by(name: 30).add_destination(Milestone.find_by(name: 40), figure: 1)
Milestone.find_by(name: 40).add_destination(Milestone.find_by(name: 50), figure: 3)
Milestone.find_by(name: 30).add_destination(Milestone.find_by(name: 50), figure: 2)
Milestone.find_by(name: 20).add_destination(Milestone.find_by(name: 50), figure: 3)
Milestone.find_by(name: 10).sum_tree(:figure)
Installation
Add this line to your application's Gemfile:
gem "acts_as_graph_diagram"
$ bundle
Or install it yourself as:
$ gem install acts_as_graph_diagram
Then executes:
$ bin/rails generate acts_as_graph_diagram
$ bin/rails db:migrate
Development
Rails console
test/dummy/bin/rails console
Test
bin/test
Contributing
Bug reports and pull requests are welcome on Github at https://github.com/routeflags/acts_as_graph_diagram. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.
License
The gem is available as open source under the terms of the MIT License.
Changelog
Available here.
Code of Conduct
Everyone interacting in the project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.
You may enjoy owning other libraries and my company.
Аcknowledgments