Class: Relyze::Graph::Edge
- Inherits:
-
Object
- Object
- Relyze::Graph::Edge
- Defined in:
- C:/Program Files/Relyze/lib/relyze/core/graph.rb
Overview
A base class representing an edge in a graph.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
A data [Object] to associate with this edge.
-
#display ⇒ Object
readonly
- Hash
-
A hash of display properties for this edge.
-
#graph ⇒ Object
readonly
- Relyze::Graph::Graph
-
The graph this edge belongs to.
-
#label ⇒ Object
- String, nil
-
Gets this edge's label (if any).
-
#lhs ⇒ Object
readonly
- Relyze::Graph::Node
-
This edges left hand side node.
-
#name ⇒ Object
- String
-
Gets this edge's name.
-
#rhs ⇒ Object
readonly
- Relyze::Graph::Node
-
This edges right hand side node.
Instance Method Summary collapse
-
#initialize(graph, lhs, rhs, data) ⇒ Edge
constructor
Initialize a new edge object.
-
#remove ⇒ nil
Remove this edge from the graph.
-
#to_dot ⇒ String
Export this edge object as a DOT edge.
-
#to_s ⇒ String
Get this edges text content.
-
#to_svg ⇒ String
Export this edge object as a SVG element, suitable for embedding in HTML.
Constructor Details
#initialize(graph, lhs, rhs, data) ⇒ Edge
Initialize a new edge object.
670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 |
# File 'C:/Program Files/Relyze/lib/relyze/core/graph.rb', line 670 def initialize( graph, lhs, rhs, data ) @graph = graph @lhs = lhs @rhs = rhs @name = "%s - %s" % [ @lhs.name, @rhs.name ] @label = nil @data = data @display = { :points_x => [], :points_y => [], :edge_width => @graph.display.fetch( :edge_width, 2.0 ), :edge_color => @graph.display.fetch( :edge_color, '#000000' ) } @lhs.edges << self if( @lhs != @rhs ) @rhs.edges << self end end |
Instance Attribute Details
#data ⇒ Object (readonly)
A data [Object] to associate with this edge.
653 654 655 |
# File 'C:/Program Files/Relyze/lib/relyze/core/graph.rb', line 653 def data @data end |
#display ⇒ Object (readonly)
- Hash
-
A hash of display properties for this edge.
656 657 658 |
# File 'C:/Program Files/Relyze/lib/relyze/core/graph.rb', line 656 def display @display end |
#graph ⇒ Object (readonly)
- Relyze::Graph::Graph
-
The graph this edge belongs to.
644 645 646 |
# File 'C:/Program Files/Relyze/lib/relyze/core/graph.rb', line 644 def graph @graph end |
#label ⇒ Object
- String, nil
-
Gets this edge's label (if any).
662 663 664 |
# File 'C:/Program Files/Relyze/lib/relyze/core/graph.rb', line 662 def label @label end |
#lhs ⇒ Object (readonly)
- Relyze::Graph::Node
-
This edges left hand side node.
647 648 649 |
# File 'C:/Program Files/Relyze/lib/relyze/core/graph.rb', line 647 def lhs @lhs end |
#name ⇒ Object
- String
-
Gets this edge's name.
659 660 661 |
# File 'C:/Program Files/Relyze/lib/relyze/core/graph.rb', line 659 def name @name end |
#rhs ⇒ Object (readonly)
- Relyze::Graph::Node
-
This edges right hand side node.
650 651 652 |
# File 'C:/Program Files/Relyze/lib/relyze/core/graph.rb', line 650 def rhs @rhs end |
Instance Method Details
#remove ⇒ nil
Remove this edge from the graph.
694 695 696 697 698 699 700 701 702 703 704 705 |
# File 'C:/Program Files/Relyze/lib/relyze/core/graph.rb', line 694 def remove @lhs.edges.delete( self ) if( @lhs != @rhs ) @rhs.edges.delete( self ) end @lhs = nil @rhs = nil return nil end |
#to_dot ⇒ String
Export this edge object as a DOT edge.
762 763 764 765 766 767 768 769 |
# File 'C:/Program Files/Relyze/lib/relyze/core/graph.rb', line 762 def to_dot dot = "\t%s %s %s [color=\"%s\"" % [ @lhs.name, ( @graph.directed? ? '->' : '--' ), @rhs.name, @display[:edge_color] ] if( not @label.nil? ) dot << ", label=\"%s\"" % @label.to_s end dot << "]\r\n" return dot end |
#to_s ⇒ String
Get this edges text content. If a data object is available we use this to generate the content, otherwise this edges name is used.
775 776 777 778 779 780 781 |
# File 'C:/Program Files/Relyze/lib/relyze/core/graph.rb', line 775 def to_s if( @data.nil? ) return @name end return @data.respond_to?( :render ) ? @data.render : @data.to_s end |
#to_svg ⇒ String
Export this edge object as a SVG element, suitable for embedding in HTML.
710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 |
# File 'C:/Program Files/Relyze/lib/relyze/core/graph.rb', line 710 def to_svg svg = "<g class=\"relyze_graph_edge\" style=\"stroke:%s; fill:%s\">" % [ @display[:edge_color], @display[:edge_color] ] svg << "<title>%s</title>" % @name if( @graph.directed? ) svg << "<defs>" svg << "\t<marker id=\"marker_arrow\" markerWidth=\"10\" markerHeight=\"10\" refX=\"10\" refY=\"5\" orient=\"auto\">" svg << "\t\t<path d=\"M2,2 L2,10 L10,6 L2,2\"/>" svg << "\t</marker>" svg << "</defs>" end svg << "\t<polyline points=\"" @display[:points_x].each_index do | i | svg << "%f,%f " % [ @display[:points_x][i], @display[:points_y][i] ] end svg << "\" " svg << "style=\"fill:none; stroke-width:%f; " % [ @display[:edge_width] ] if( @graph.directed? ) svg << "marker-end:url(#marker_arrow); " end svg << "\"/>" if( not @label.nil? ) x1 = @display[:points_x][@display[:points_x].length-1] x2 = @display[:points_x][@display[:points_x].length-2] y1 = @display[:points_y][@display[:points_y].length-1] y2 = @display[:points_y][@display[:points_y].length-2] x = ( ( [x1,x2].max - [x1,x2].min ) / 2.0 ) + [x1,x2].min y = ( ( [y1,y2].max - [y1,y2].min ) / 2.0 ) + [y1,y2].min svg << "<text text-anchor=\"middle\" x=\"%f\" y=\"%f\" " % [ x, y - (@graph.display[:font_size] * 2.0) ] svg << "style=\"font-family:%s;font-size:%dpt\"" % [ @graph.display[:font_name], @graph.display[:font_size] ] svg << ">" svg << "%s" % @graph.svg_escape( @label.to_s ) svg << "</text>" end svg << "</g>" return svg end |