YOUSEFUL / DELPHI / COMPONENTS
TAtlasDiagram
Interactive view of a TAtlasGraph: select, drag, connect, rename, add and delete nodes.
Description
Assign a TAtlasGraph to Graph and the diagram draws its nodes as rounded rectangles with word-wrapped captions and its edges as 2-pixel arrows between the node borders, repainting after every change to the graph. Several diagrams may show the same graph. Node coordinates are client pixels: there is no zoom or scrolling.
Mouse and keyboard: a left click selects the node under the cursor (or clears the selection) and dragging moves it, calling MoveNode on the graph for every mouse move. A double click on empty space adds a node captioned New node with its top left corner at the cursor, and on the selected node opens an input box (Edit node, fixed English text) to change its caption. Delete removes the selected node and its edges. With ConnectMode on, clicking a second node while one is selected adds an edge from the selected node to it and switches ConnectMode off. Edges cannot be selected or deleted with the mouse; use TAtlasGraph.DeleteEdge.
The selected node is drawn in clHighlight. Edges use Font.Color; nodes use NodeColor and NodeTextColor, which TAtlasThemer sets to the theme's alternate and text colours. Not implemented: zoom, scrolling, multiple selection, rubber-band selection, undo, edge labels and automatic layout after edits (call LayoutHierarchy).
Members
| Member | Kind | What it does |
|---|---|---|
Create | constructor | Creates a 900 x 600 white diagram without a graph, with TabStop = True and double buffering. |
Destroy | destructor | Detaches from the graph (the graph itself is not freed). |
Render | method | Draws the diagram on any canvas, over the rectangle of the control's client area, so size a bitmap to ClientWidth x ClientHeight. |
NodeAt | method | ID of the topmost node (the last in Nodes order) containing client point X, Y, or 0 when there is none or no graph. |
SelectedID | property | ID of the selected node, 0 when none. |
Graph | property | The model to show and edit. |
ConnectMode | property | When True, the next click on a node other than the selected one adds an edge from the selected node to it and resets ConnectMode to False. |
NodeColor | property | Fill colour of unselected nodes. |
NodeTextColor | property | Border and caption colour of unselected nodes. |
Align | property | Standard VCL alignment in the parent. |
Anchors | property | Standard VCL anchors. |
Color | property | Background; clWhite by default. |
Font | property | Caption font; Font.Color is also the colour of the edges and arrowheads. |
ParentFont | property | Standard VCL property: use the parent's font. |
TabStop | property | True by default. |
Constructors
Create constructor
constructor Create(AOwner: TComponent); override;Creates a 900 x 600 white diagram without a graph, with TabStop = True and double buffering.
Destructors
Destroy destructor
destructor Destroy; override;Detaches from the graph (the graph itself is not freed).
Properties
SelectedID property
property SelectedID: Integer read FSelected;ID of the selected node, 0 when none. Only user input changes it; it is cleared when that node is deleted or the graph is replaced.
if Diagram.SelectedID <> 0 then Graph.DeleteNode(Diagram.SelectedID);Graph property
property Graph: TAtlasGraph read FGraph write SetGraph;The model to show and edit. Assigning it registers a listener and clears the selection; nil detaches. Freeing the graph detaches it automatically.
Diagram.Graph := Graph;ConnectMode property
property ConnectMode: Boolean read FConnectMode write FConnectMode default False;When True, the next click on a node other than the selected one adds an edge from the selected node to it and resets ConnectMode to False. With no node selected, the first click selects the source. Connecting an already connected pair raises EAtlasGraphError (Duplicate edge).
Diagram.ConnectMode := True;NodeColor property
property NodeColor: TColor read FNodeColor write SetNodeColor default $00F3E8DC;Fill colour of unselected nodes.
NodeTextColor property
property NodeTextColor: TColor read FNodeTextColor write SetNodeTextColor default clBlack;Border and caption colour of unselected nodes.
Align property
property Align;Standard VCL alignment in the parent.
Anchors property
property Anchors;Standard VCL anchors.
Color property
property Color;Background; clWhite by default.
Font property
property Font;Caption font; Font.Color is also the colour of the edges and arrowheads.
ParentFont property
property ParentFont;Standard VCL property: use the parent's font.
TabStop property
property TabStop;True by default. The diagram takes the focus when clicked, so Delete works.
Methods
Render method
procedure Render(Target: TCanvas);Draws the diagram on any canvas, over the rectangle of the control's client area, so size a bitmap to ClientWidth x ClientHeight. Paint calls it.
Bitmap.SetSize(Diagram.ClientWidth, Diagram.ClientHeight);
Diagram.Render(Bitmap.Canvas);NodeAt method
function NodeAt(X, Y: Integer): Integer;ID of the topmost node (the last in Nodes order) containing client point X, Y, or 0 when there is none or no graph.
ID := Diagram.NodeAt(120, 60);Examples
Show a graph
Connects a diagram to an existing graph; it repaints on every change to the graph.
uses System.Classes, Vcl.Controls, Atlas.Graph, Atlas.Diagram;
function AddDiagram(Owner: TComponent; Parent: TWinControl; Graph: TAtlasGraph): TAtlasDiagram;
begin
Result := TAtlasDiagram.Create(Owner);
Result.Parent := Parent;
Result.Align := alClient;
Result.Graph := Graph;
end;
Connect command
For a Connect button: the user then clicks the source node and the target node.
uses Atlas.Diagram;
procedure StartConnecting(Diagram: TAtlasDiagram);
begin
Diagram.ConnectMode := True;
if Diagram.CanFocus then Diagram.SetFocus;
end;
Save the diagram as PNG
Renders the current view into a bitmap of the control's size.
uses Vcl.Graphics, Vcl.Imaging.pngimage, Atlas.Diagram;
procedure SaveDiagram(Diagram: TAtlasDiagram; const FileName: string);
var
Bitmap: TBitmap;
Png: TPngImage;
begin
Bitmap := TBitmap.Create;
Png := TPngImage.Create;
try
Bitmap.SetSize(Diagram.ClientWidth, Diagram.ClientHeight);
Diagram.Render(Bitmap.Canvas);
Png.Assign(Bitmap);
Png.SaveToFile(FileName);
finally
Png.Free;
Bitmap.Free;
end;
end;
Related
TAtlasGraph · TAtlasGraphNode · EAtlasGraphError · TAtlasThemer
Also in Atlas VCL
TAtlasTable · TAtlasGrid · TAtlasValueEdit · TAtlasTree · TAtlasWorkbook · TAtlasSheetGrid · TAtlasPivot · TAtlasInspector · TAtlasRichEdit · TAtlasSpellChecker · TAtlasChart · TAtlasGauge · TAtlasThemer · TAtlasWizard · TAtlasWorkspace · TAtlasCommandBar · TAtlasActionTile · TAtlasTileBoard · TAtlasNavigation · TAtlasMap · TAtlasGraph · TAtlasPrintDocument · TAtlasSchedule · TAtlasScheduler · TAtlasTaskPlan · TAtlasGantt · TAtlasPdfDocument · TAtlasPdfViewer