#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; // Native PNG and standalone SVG use bundled Archivo. // Add ChartFontAssets.StyleSheet once for other text on a host page. var mode = ThemeMode.Light; var theme = VisualTheme.Graphite(); var model = TopologyInterchangeRecipe.Create("compact", mode); var context = new VisualRenderContext( new VisualLayoutOptions(new VisualSize(390, 520)), theme, mode, new VisualFrame("An editable saved route", "Native JSON is loaded into detached owners before editing", showLegend: null, showSurface: false), FontSpec.FromFamily(ChartFontStacks.ProductSans)); 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 /// Saves native topology semantics, loads editable owners and changes a detail without parsing source text. public static class TopologyInterchangeRecipe { /// Creates an edited graph after the same public JSON save/load boundary used by a file workflow. 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()); } /// Writes original JSON, loads it, edits native owners, then writes script-free SVG and PNG. /// A caller-owned output directory. /// Use wide or compact. /// The authored resolved color mode. /// The detached edited model; the original JSON remains unchanged. 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; } }