I'm having a problem clearing and redrawing a diagram. When I first setup the diagram, it is using auto-layout to render an Org Chart from Left-to-Right
protected DiagramLayout LayoutValue = new DiagramLayout()
{
Type = LayoutType.OrganizationalChart,
VerticalSpacing = 50,
HorizontalSpacing = 25,
EnableAnimation = true,
Orientation = LayoutOrientation.LeftToRight,
LayoutInfo = new TreeInfo()
{
CanEnableSubTree = true
//Orientation = SubTreeOrientation.Vertical
}
};
Here is the markup:
<SfDiagram @ref="Diagram"
ID="diagram"
Height="100%"
Nodes="@NodeCollection"
Connectors="@ConnectorCollection"
NodeDefaults="@NodeDefaults"
ConnectorDefaults="@ConnectorDefaults"
Constraints="@DiagramConstraints"
SelectedItems="@SelectedItems"
Tool="@Tool"
Layout="@LayoutValue">
<DiagramSnapSettings Constraints="@SnapConstraints.None"></DiagramSnapSettings>
<DiagramEvents SelectionChanged="@SelectionChanged"></DiagramEvents>
</SfDiagram>
When the diagram first loads. it looks like this (which is what I want)
However, the user can select a different element to render, which is supposed to clear the diagram and redraw it. This is done by
- calling await Diagram.Clear();
- removing all the nodes from ConnectorCollection and NodeCollection
- adding new nodes to ConnectorCollection and NodeCollection
The problem is that this results in a diagram that renders the leaf nodes from top to bottom, instead of left to right, example here:
In order to "force" the rendering to be correct, I explicitly force the layout to be LayoutOrientation.LeftToRight again, wait for it to render, and then reapply the node orientations, as follows:
internal async Task Reload(LearningElement deepElement)
{
DeepElement = deepElement;
await LoadData();
// --------------------------------
// Inexplicably necessary code
// (but not 100% reliable)
// --------------------------------
// do not delete this, or the flowchart will not render correctly.
Diagram.Layout.Orientation = LayoutOrientation.LeftToRight;
await Diagram.DoLayout();
await InvokeAsync(() => StateHasChanged());
// --------------------------------
// Inexplicably necessary code
// --------------------------------
await SetLayoutColor();
await InvokeAsync(() => StateHasChanged());
}
However, this is not always reliable - sometimes the end result is what I want, but sometimes it does not render the leaf nodes from left to right.
You can see that in this video: https://drive.google.com/file/d/1EJIShvCBXatuTxCF1xmImchxRUlVOnXB/view?usp=sharing