11.4 Example of Applying Styles Programmatically with GraphVisualization

The example in this section describes how to programmatically set up feature flags, rule-based styles, base styles, and other settings options using GraphVisualization.

It uses the sample_data.json graph configuration that is described in Basic Example with GraphVisualization. In this example, the graph widget is created first, and the data is loaded later.

from oraclegraph import GraphVisualization as Graph
import json

# Load data from JSON file
with open('sample_data.json', 'r') as f:
    data = json.load(f)
data['isLastResultSet'] = True

# Configure feature flags
feature_flags = {
    "exploration": {
        "expand": True,
        "focus": True
    },
    "modes": {
        "interaction": True
    },
    "displaySizeControl": True,
}

# Define base styles
base_styles = {
    "edge": {
        "color": "lightgray"
    },
    "edge:hover": {
        "color": "red",
        "opacity": 0.4
    },
    "vertex": {
        "color": "#195F74",
        "label": "${properties.ID}"
    }
}

# Define rule-based styles
rule_based_styles = [
    {
        "legendTitle": "Label - ACCOUNTS",
        "stylingEnabled": True,
        "component": "vertex",
        "target": "vertex",
        "conditions": {
            "conditions": [
                {
                    "property": "ID",
                    "operator": ">",
                    "value": 0
                }
            ]
        },
        "style": {
            "label": {
                "text": "${properties.ID}"
            }
        }
    }
]

# Configure additional settings
defaults = {
    "interactionActive": True,
    "stickyActive": True
}

# Create a graph widget
graph = Graph()

# Assign data
graph.data = data

# Set Feature Flags Programmatically
graph.set_feature_flag("featureFlags", feature_flags)

# Configure Settings Programmatically
graph.set_setting("layout", "force")
graph.set_setting("ruleBasedStyles", rule_based_styles)
graph.set_setting("baseStyles", base_styles)
graph.set_setting("defaults", defaults)

graph.height = 400
graph

The preceding code produces a similar output as shown in Figure 11-2.