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
Call a method in the model to handle the graph network.
God.find_by(name: 'Rheā').add_destination God.find_by(name: 'Hēra', figure: 1)
# => #<Edge:0x000000010b0d4560
# id: 1,
# comment: "",
# figure: 0,
# directed: true,
# destination_type: "God",
# destination_id: 2,
# departure_type: "God",
# departure_id: 1,
# created_at: Sun, 12 Jun 2022 11:11:06.995007000 UTC +00:00,
# updated_at: Sun, 12 Jun 2022 11:11:06.995007000 UTC +00:00>
God.find_by(name: 'Rheā').connecting_count
# => 1
God.find_by(name: 'Rheā').aheads
# => [#<Edge:0x000000010b5642b0
# id: 1,
# comment: "",
# figure: 0,
# directed: true,
# destination_type: "God",
# destination_id: 2,
# departure_type: "God",
# departure_id: 1,
# created_at: Sun, 12 Jun 2022 11:11:06.995007000 UTC +00:00,
# updated_at: Sun, 12 Jun 2022 11:11:06.995007000 UTC +00:00>]
God.find_by(name: 'Rheā').aheads.first.destination
# => #<God:0x000000010b5efb58 id: 2, name: "Hēra", created_at: Sun, 12 Jun 2022 11:11:06.984341000 UTC +00:00, updated_at: Sun, 12 Jun 2022 11:11:06.984341000 UTC +00:00>
Providing features:
- 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:
// v7.4.4
d3.json("http://127.0.0.1:3000/data_network").then(function (graph) {});
Calculates the total using the breadth first search.
Worst-case performance
Worst-case space complexity
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)
# => 16
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.
- acts_as_tree_diagram: ActsAsTreeDiagram extends ActsAsTree to add simple function for draw tree diagram with html.
- earned_value_calculator: Calculates the earned value of a project to indicate a diagnosis result.
- timeline_rails_helper: The TimelineRailsHelper provides a timeline_molecules_tag helper to draw a vertical time line usable with vanilla CSS.
- 株式会社旗指物