# An editable saved route · Topology

Preview

![An editable saved route](topology-interchange-wide-light.png)

![An editable saved route](topology-interchange-wide-dark.png)

900 × 400 pixels

## Create this example

Create a console project, add the ChartForgeX package, and replace Program.cs with this source. Run it to write SVG, PNG and HTML with the default system fonts.

For the same typography as the gallery, [download the gallery-font recipe](topology-interchange-wide-light.gallery.csharp.txt) and copy the gallery’s fonts directory beside your program.

```
#region Setup
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using ChartForgeX;
using ChartForgeX.Core;
using ChartForgeX.Primitives;
using ChartForgeX.Rendering;
using ChartForgeX.Themes;
using ChartForgeX.Topology;
using ChartForgeX.Typography;
using ChartForgeX.VisualArtifacts;
using ChartForgeX.VisualBlocks;

var mode = ThemeMode.Light;
var theme = VisualTheme.Graphite();
var model = TopologyInterchangeRecipe.Create("wide", mode);
var context = new VisualRenderContext(
    new VisualLayoutOptions(new VisualSize(900, 400)), theme, mode,
    new VisualFrame("An editable saved route", "Native JSON is loaded into detached owners before editing", showLegend: null, showSurface: false));
var prepared = model.Prepare(context);
File.WriteAllText("visual.svg", prepared.ToSvg());
File.WriteAllBytes("visual.png", prepared.ToPng());
File.WriteAllText("visual.html", prepared.ToArtifact("example", VisualArtifactKind.Topology).ToHtmlPage());
#endregion

/// <summary>Saves native topology semantics, loads editable owners and changes a detail without parsing source text.</summary>
public static class TopologyInterchangeRecipe {
    /// <summary>Creates an edited graph after the same public JSON save/load boundary used by a file workflow.</summary>
    public static TopologyChart Create(string variant = "wide", ThemeMode mode = ThemeMode.Light) {
        var source = Author(variant, mode);
        var envelope = VisualArtifactInterchangeEnvelope.FromJson(source.ToVisualArtifact().ToInterchangeJson());
        return Edit(envelope.ToTopologyChart());
    }

    /// <summary>Writes original JSON, loads it, edits native owners, then writes script-free SVG and PNG.</summary>
    /// <param name="directory">A caller-owned output directory.</param>
    /// <param name="variant">Use wide or compact.</param>
    /// <param name="mode">The authored resolved color mode.</param>
    /// <returns>The detached edited model; the original JSON remains unchanged.</returns>
    public static TopologyChart SaveLoadAndEdit(string directory, string variant = "wide", ThemeMode mode = ThemeMode.Light) {
        if (string.IsNullOrWhiteSpace(directory)) throw new ArgumentException("An output directory is required.", nameof(directory));
        Directory.CreateDirectory(directory);
        var path = Path.Combine(directory, "request-route.json");
        File.WriteAllText(path, Author(variant, mode).ToVisualArtifact().ToInterchangeJson());
        var envelope = VisualArtifactInterchangeEnvelope.FromJson(File.ReadAllText(path));
        // Envelope IDs are retained. Common source-language/extension metadata remains on envelope.
        // The imported model uses Manual bounds. Set a fresh LayoutMode explicitly when rearranging it.
        var edited = Edit(envelope.ToTopologyChart());
        edited.SaveSvg(Path.Combine(directory, "request-route-edited.svg"));
        edited.SavePng(Path.Combine(directory, "request-route-edited.png"));
        return edited;
    }

    private static TopologyChart Edit(TopologyChart chart) {
        var source = chart.Nodes.Single(node => node.Id == "request");
        source.Details[0].Value = "Delivered";
        source.Status = TopologyHealthStatus.Healthy;
        chart.Edges.Single(edge => edge.Id == "dispatch").Label = "Accepted";
        return chart;
    }

    private static TopologyChart Author(string variant, ThemeMode mode) {
        if (variant is not ("wide" or "compact")) throw new ArgumentOutOfRangeException(nameof(variant));
        var compact = variant == "compact";
        var chart = TopologyChart.Create().Id("saved-request-route").Title("An editable saved route")
            .Subtitle("The loaded graph owns independent native data")
            .Theme(VisualTheme.Graphite()).ThemeMode(mode).Viewport(new TopologyViewport(compact ? 390 : 900, compact ? 520 : 400));
        chart.Nodes.Add(new TopologyNode { Id = "request", Label = "Request", X = 40, Y = 75, Width = 210, Height = 110,
            Kind = TopologyNodeKind.Server, Status = TopologyHealthStatus.Warning, ShowStatusBadge = true });
        chart.Nodes[0].Details.Add(new TopologyNodeDetail { Label = "State", Value = "Queued" });
        chart.Nodes[0].Ports.Add(new TopologyNodePort { Id = "send", Side = compact ? TopologyEdgePort.Bottom : TopologyEdgePort.Right, Offset = .5 });
        chart.Nodes[0].Metadata["host.record"] = "request-42";
        chart.Nodes.Add(new TopologyNode { Id = "worker", Label = "Worker", X = compact ? 40 : 470, Y = compact ? 285 : 75,
            Width = 210, Height = 110, Status = TopologyHealthStatus.Healthy });
        chart.Edges.Add(new TopologyEdge { Id = "dispatch", SourceNodeId = "request", TargetNodeId = "worker", SourcePortId = "send",
            Label = "Dispatch", Direction = VisualLinkDirection.Forward, Routing = TopologyEdgeRouting.Orthogonal });
        chart.Accessibility.Name = "Saved request route";
        return chart;
    }
}
```

```
#region Setup
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using ChartForgeX;
using ChartForgeX.Core;
using ChartForgeX.Primitives;
using ChartForgeX.Rendering;
using ChartForgeX.Themes;
using ChartForgeX.Topology;
using ChartForgeX.Typography;
using ChartForgeX.VisualArtifacts;
using ChartForgeX.VisualBlocks;

var mode = ThemeMode.Dark;
var theme = VisualTheme.Graphite();
var model = TopologyInterchangeRecipe.Create("wide", mode);
var context = new VisualRenderContext(
    new VisualLayoutOptions(new VisualSize(900, 400)), theme, mode,
    new VisualFrame("An editable saved route", "Native JSON is loaded into detached owners before editing", showLegend: null, showSurface: false));
var prepared = model.Prepare(context);
File.WriteAllText("visual.svg", prepared.ToSvg());
File.WriteAllBytes("visual.png", prepared.ToPng());
File.WriteAllText("visual.html", prepared.ToArtifact("example", VisualArtifactKind.Topology).ToHtmlPage());
#endregion

/// <summary>Saves native topology semantics, loads editable owners and changes a detail without parsing source text.</summary>
public static class TopologyInterchangeRecipe {
    /// <summary>Creates an edited graph after the same public JSON save/load boundary used by a file workflow.</summary>
    public static TopologyChart Create(string variant = "wide", ThemeMode mode = ThemeMode.Light) {
        var source = Author(variant, mode);
        var envelope = VisualArtifactInterchangeEnvelope.FromJson(source.ToVisualArtifact().ToInterchangeJson());
        return Edit(envelope.ToTopologyChart());
    }

    /// <summary>Writes original JSON, loads it, edits native owners, then writes script-free SVG and PNG.</summary>
    /// <param name="directory">A caller-owned output directory.</param>
    /// <param name="variant">Use wide or compact.</param>
    /// <param name="mode">The authored resolved color mode.</param>
    /// <returns>The detached edited model; the original JSON remains unchanged.</returns>
    public static TopologyChart SaveLoadAndEdit(string directory, string variant = "wide", ThemeMode mode = ThemeMode.Light) {
        if (string.IsNullOrWhiteSpace(directory)) throw new ArgumentException("An output directory is required.", nameof(directory));
        Directory.CreateDirectory(directory);
        var path = Path.Combine(directory, "request-route.json");
        File.WriteAllText(path, Author(variant, mode).ToVisualArtifact().ToInterchangeJson());
        var envelope = VisualArtifactInterchangeEnvelope.FromJson(File.ReadAllText(path));
        // Envelope IDs are retained. Common source-language/extension metadata remains on envelope.
        // The imported model uses Manual bounds. Set a fresh LayoutMode explicitly when rearranging it.
        var edited = Edit(envelope.ToTopologyChart());
        edited.SaveSvg(Path.Combine(directory, "request-route-edited.svg"));
        edited.SavePng(Path.Combine(directory, "request-route-edited.png"));
        return edited;
    }

    private static TopologyChart Edit(TopologyChart chart) {
        var source = chart.Nodes.Single(node => node.Id == "request");
        source.Details[0].Value = "Delivered";
        source.Status = TopologyHealthStatus.Healthy;
        chart.Edges.Single(edge => edge.Id == "dispatch").Label = "Accepted";
        return chart;
    }

    private static TopologyChart Author(string variant, ThemeMode mode) {
        if (variant is not ("wide" or "compact")) throw new ArgumentOutOfRangeException(nameof(variant));
        var compact = variant == "compact";
        var chart = TopologyChart.Create().Id("saved-request-route").Title("An editable saved route")
            .Subtitle("The loaded graph owns independent native data")
            .Theme(VisualTheme.Graphite()).ThemeMode(mode).Viewport(new TopologyViewport(compact ? 390 : 900, compact ? 520 : 400));
        chart.Nodes.Add(new TopologyNode { Id = "request", Label = "Request", X = 40, Y = 75, Width = 210, Height = 110,
            Kind = TopologyNodeKind.Server, Status = TopologyHealthStatus.Warning, ShowStatusBadge = true });
        chart.Nodes[0].Details.Add(new TopologyNodeDetail { Label = "State", Value = "Queued" });
        chart.Nodes[0].Ports.Add(new TopologyNodePort { Id = "send", Side = compact ? TopologyEdgePort.Bottom : TopologyEdgePort.Right, Offset = .5 });
        chart.Nodes[0].Metadata["host.record"] = "request-42";
        chart.Nodes.Add(new TopologyNode { Id = "worker", Label = "Worker", X = compact ? 40 : 470, Y = compact ? 285 : 75,
            Width = 210, Height = 110, Status = TopologyHealthStatus.Healthy });
        chart.Edges.Add(new TopologyEdge { Id = "dispatch", SourceNodeId = "request", TargetNodeId = "worker", SourcePortId = "send",
            Label = "Dispatch", Direction = VisualLinkDirection.Forward, Routing = TopologyEdgeRouting.Orthogonal });
        chart.Accessibility.Name = "Saved request route";
        return chart;
    }
}
```

<!-- PowerForge markdown artifact sha256:817EA7740FD741C2385FF06C6502803A8EE373B89E25392931420DDA68C4230D -->
