<jupyter_start><jupyter_text>Nodes and Links should be supplied to the Graph mark. Node attributes| Attribute|      Type     |  Description | Default ||:----------:|:-------------:|:------:|| label |  str | node label | mandatory attribute || label_display |  {center, outside, none} | label display options | center || shape |  {circle, ellipse, rect} | node shape | circle || shape_attrs |  dict | [node SVG attributes](https://www.w3.org/TR/SVG/shapes.html) | {'r': 15} | Link Attributes| Attribute|      Type     |  Description | Default ||:----------:|:-------------:|:------:|| source |  int | source node index | mandatory attribute || target |  int | target node index | mandatory attribute || value |  float | value of the link. Use np.nan if you do not want a link| - | Link data can also be supplied using a link_matrix which is a numpy array of shape (n, n) where n is the number of nodes in the graph Link Data can be passed in through 3 data attributes - 1. link_data - list of dicts (e.g. [{'source': 0, 'target': 1, 'value': 10}, {'source': 2, 'target': 1, 'value': 20},...]2. link_matrix - 2-d numpy array of shape (n, n) where n is the number of nodes3. link_color - 2-d numpy array of shape (n, n) where n is the number of nodes. This attribute can be used to encode the link_color by passing in a link_color scale<jupyter_code>fig_layout = Layout(width='960px', height='500px')<jupyter_output><empty_output><jupyter_text>Directed GraphIf we dont specify the x and y location for nodes then the force layout will set the location of nodes<jupyter_code>node_data = [
    dict(label='A', shape='rect'),
    dict(label='B', shape='ellipse'),
    dict(label='C', shape='ellipse'),
    dict(label='D', shape='rect'),
    dict(label='E', shape='ellipse'),
    dict(label='F', shape='circle'),
    dict(label='G', shape='ellipse'),
]
link_data = [{'source': s, 'target': t} for s, t in np.random.randint(0, 7, (10, 2)) if s != t]
graph = Graph(node_data=node_data, link_data=link_data, charge=-600, colors=['orange'] * 7)
Figure(marks=[graph], layout=fig_layout)
graph.link_type = 'line'<jupyter_output><empty_output><jupyter_text>Explicitly setting x and y location for nodes using x and y scales. Force layout animation is disabled in this case<jupyter_code>node_data = list('ABCDEFG')

#using link matrix to set links
link_matrix = np.zeros((7, 7))
xs = LinearScale()
ys = LinearScale()
x = [100, 200, 200, 300, 300, 300, 300]
y = [2, .5, 4, 8, 6, 4, 1]
graph = Graph(node_data=node_data, link_matrix=link_matrix, link_type='line', 
              colors=['orange'] * 7,
              scales={'x': xs, 'y': ys, }, x=x, y=y, 
              directed=False)
Figure(marks=[graph], layout=fig_layout)<jupyter_output><empty_output><jupyter_text>Color code nodes using 'color' data attribute<jupyter_code>node_data = list('ABCDEFG')

#using link matrix to set links
link_matrix = np.zeros((7, 7))
xs = LinearScale(min=0, max=500)
ys = LinearScale(min=0, max=10)
cs = ColorScale(scheme='Reds')
x = [100, 200, 200, 300, 300, 300, 300]
y = [2, .5, 4, 8, 6, 4, 1]
graph = Graph(node_data=node_data, link_matrix=link_matrix, link_type='line', 
              color=np.random.rand(7),
              scales={'x': xs, 'y': ys, 'color': cs}, x=x, y=y, 
              directed=False)
Figure(marks=[graph], layout=fig_layout)<jupyter_output><empty_output><jupyter_text>Color code links using link_color scale<jupyter_code>node_data = list('ABCDEFG')

link_data = [{'source': s, 'target': t, 'value': np.random.rand()} for s, t in np.random.randint(0, 7, (20, 2))]

xs = LinearScale()
ys = LinearScale()
lcs = ColorScale(scheme='Greens')
x = [100, 200, 200, 300, 300, 300, 300]
y = [2, .5, 4, 8, 6, 4, 1]
graph = Graph(node_data=node_data, link_data=link_data, link_type='line',
              colors=['orange'], directed=False, 
              scales={'x': xs, 'y': ys, 'link_color': lcs}, 
              x=x, y=y, color=np.random.rand(7))
Figure(marks=[graph], layout=fig_layout)<jupyter_output><empty_output><jupyter_text>Custom node attributes<jupyter_code>node_data = [
    {'label': 'A', 'shape': 'circle', 'shape_attrs': {'r': 20}, 'foo': 1},
    {'label': 'Node B', 'shape': 'rect', 'shape_attrs': {'rx': 10, 'ry': 10, 'width': 40}, 'foo': 2},
    {'label': 'C', 'shape': 'ellipse', 'foo': 4},
    {'label': 'D', 'shape': 'rect', 'shape_attrs': {'width': 30, 'height': 30}, 'foo': 100},
]

link_data = [{'source': s, 'target': t, 'value': np.random.rand()} for s, t in np.random.randint(0, 4, (8, 2))]

graph = Graph(node_data=node_data, link_data=link_data, link_distance=150)
Figure(marks=[graph], layout=fig_layout)<jupyter_output><empty_output><jupyter_text>Add tooltips on nodes<jupyter_code>tooltip = Tooltip(fields=['label', 'foo'], formats=['', '', ''])
graph.tooltip = tooltip<jupyter_output><empty_output><jupyter_text>Add a line chart as a tooltip<jupyter_code>import bqplot.pyplot as plt
plt.clear()
line = plt.plot(np.cumsum(np.random.randn(20)))
# hover on nodes to see the plot
graph.tooltip = plt.current_figure()<jupyter_output><empty_output><jupyter_text>Custom styling of hovered and selected nodes<jupyter_code>graph.hovered_style = {'stroke': 'red'}
graph.unhovered_style = {'opacity': '0.4'}

graph.selected_style = {'opacity': '1', 'stroke': 'white', 'stroke-width': '2.5'}
# click on any node to select it (selected attribute gets populated with the index of the selected node)
graph.selected<jupyter_output><empty_output><jupyter_text>Add element click and background click handlers<jupyter_code>def print_event(self, target):
    print(target)

graph.on_background_click(print_event)
graph.on_element_click(print_event)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1726872246696035, 'fraction_numerical': 0.029427312775330398, 'mean_word_length': 4.721774193548387, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 41, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '142310', 'license': 'Apache-2.0', 'n_tokens_mistral': 2102, 'n_tokens_neox': 1944, 'n_words': 564, 'path': 'examples/Marks/Object Model/Graph.ipynb', 'repo_name': 'meeseeksmachine/bqplot', 'size': '5675'}