Skip to content

Commit 273b27e

Browse files
authored
Update diagram-change-shape-color-onshapeclick.md (#3395)
1 parent c4c67c4 commit 273b27e

File tree

1 file changed

+93
-39
lines changed

1 file changed

+93
-39
lines changed

knowledge-base/diagram-change-shape-color-onshapeclick.md

Lines changed: 93 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,17 @@ res_type: kb
2222

2323
## Description
2424

25-
I am using the Diagram `OnShapeClick` event to update the `DiagramShapeFill` `Color` of the selected Diagram shape. However, this resets the positions of the shapes that the user has dragged. How to update the shape background colors while maintaining the current shape positions?
25+
I am using the Diagram `OnShapeClick` event to update the `DiagramShapeFill` `Color` of the selected Diagram shape. However, this resets the positions of the shapes that the user has dragged. How to update the shape background colors while maintaining the current shape positions? In addition, I want to persist the custom shape colors when the Diagram `Layout` changes.
2626

2727
## Cause
2828

29-
The Diagram `OnShapeClick` event handler is an `EventCallback` and triggers component re-render. If the [Diagram shapes are defined](slug:diagram-shapes#basics) without their `X` and `Y` properties and [shape dragging](slug:diagram-shapes#editability) is enabled, the component definition does not include the current shape positions. As a result, a re-render resets the shapes to their original places.
29+
The Diagram `OnShapeClick` event handler is an `EventCallback` and triggers component re-render. If the [Diagram shapes are defined](slug:diagram-shapes#basics) without their `X` and `Y` properties and [shape dragging](slug:diagram-shapes#editability) is enabled, the Razor component definition does not include the current shape positions. As a result, a re-render resets the shapes to their original places.
30+
31+
Similarly, if the Diagram Shape background colors are not part of the Razor component declaration, they will be reset to the default value if the Diagram `Layout` changes.
3032

3133
## Solution
3234

33-
Use the Diagram `SaveAsJsonAsync` and `LoadFromJsonAsync` methods to [persist the Diagram state through JSON](slug:diagram-overview#define-shapes-and-connections-in-json) on each re-render:
35+
Use the Diagram `SaveAsJsonAsync` and `LoadFromJsonAsync` methods to [define and persist the Diagram state through JSON](slug:diagram-overview#define-shapes-and-connections-in-json) on each render:
3436

3537
1. Implement classes with property names that correspond to the ones in the [Diagram JSON state](slug:diagram-overview#define-shapes-and-connections-in-json).
3638
1. Subscribe to the [Diagram `OnShapeClick` event](slug:diagram-events#onshapeclick).
@@ -45,49 +47,33 @@ Use the Diagram `SaveAsJsonAsync` and `LoadFromJsonAsync` methods to [persist th
4547
@using System.Text.Json
4648
@using System.Text.Json.Serialization
4749
48-
<TelerikDiagram @ref="@DiagramRef"
49-
Height="100vh"
50-
OnShapeClick="@OnDiagramShapeClick"
51-
Zoom="0.8">
52-
<DiagramLayout Type="@DiagramLayoutType.Tree" />
50+
Drag a shape and then click on a shape to change its background color. The shape positions will persist.
51+
<br />
52+
Click on a shape and
53+
<TelerikButton OnClick="@OnButtonClick"
54+
ThemeColor="@ThemeConstants.Button.ThemeColor.Success">
55+
Toggle Diagram Layout Type
56+
</TelerikButton>
57+
The shape background colors will persist.
5358
54-
<DiagramShapes>
55-
@foreach (DiagramModel shape in DiagramData)
56-
{
57-
<DiagramShape Id="@shape.Id" Type="@shape.Type">
58-
<DiagramShapeContent Text="@shape.Text" />
59-
<DiagramShapeFill Color="@shape.BackgroundColor"/>
60-
</DiagramShape>
61-
}
62-
</DiagramShapes>
63-
64-
<DiagramConnections>
65-
@foreach (DiagramModel shape in DiagramData)
66-
{
67-
if (shape.ParentId is not null)
68-
{
69-
<DiagramConnection @key="@shape"
70-
FromId="@( $"{shape.ParentId}" )"
71-
ToId="@( $"{shape.Id}" )" />
72-
}
73-
}
74-
</DiagramConnections>
59+
<TelerikDiagram @ref="@DiagramRef"
60+
OnShapeClick="@OnDiagramShapeClick">
61+
<DiagramConnectionDefaults Type="@DiagramConnectionType.Cascading" />
62+
<DiagramLayout Type="@DiagramLayoutType" />
63+
<DiagramShapeDefaults Type="@DiagramShapeType.Rectangle" />
7564
</TelerikDiagram>
7665
7766
@code {
7867
#nullable enable
7968
8069
private TelerikDiagram? DiagramRef { get; set; }
81-
private string DiagramJson { get; set; } = string.Empty;
8270
83-
private List<DiagramModel> DiagramData { get; set; } = Enumerable.Range(1, 6).Select(x => new DiagramModel()
71+
private DiagramLayoutType DiagramLayoutType { get; set; } = DiagramLayoutType.Tree;
72+
73+
private void OnButtonClick()
8474
{
85-
Id = x.ToString(),
86-
ParentId = x == 1 ? null : (x <= 4 ? 1 : 3),
87-
Text = $"Shape {x}",
88-
Type = x % 2 == 0 ? DiagramShapeType.Rectangle : DiagramShapeType.Circle,
89-
BackgroundColor = "#666"
90-
}).ToList();
75+
DiagramLayoutType = DiagramLayoutType == DiagramLayoutType.Tree ? DiagramLayoutType.Force : DiagramLayoutType.Tree;
76+
}
9177
9278
private async Task OnDiagramShapeClick(DiagramShapeClickEventArgs args)
9379
{
@@ -102,7 +88,7 @@ Use the Diagram `SaveAsJsonAsync` and `LoadFromJsonAsync` methods to [persist th
10288
if (diagramStateObject is not null && diagramStateObject.Shapes is not null)
10389
{
10490
var rnd = Random.Shared;
105-
string newFillColor = $"rgb({rnd.Next(0, 127)},{rnd.Next(0, 127)},{rnd.Next(0, 127)})";
91+
string newFillColor = $"rgb({rnd.Next(48, 128)},{rnd.Next(48, 128)},{rnd.Next(48, 128)})";
10692
10793
diagramStateObject.Shapes.First(x => x.Id == args.Id).Fill.Color = newFillColor;
10894
@@ -111,6 +97,74 @@ Use the Diagram `SaveAsJsonAsync` and `LoadFromJsonAsync` methods to [persist th
11197
}
11298
}
11399
100+
protected override async Task OnAfterRenderAsync(bool firstRender)
101+
{
102+
if (firstRender && DiagramRef is not null)
103+
{
104+
// Wait for HTML and client-side Diagram instance
105+
await Task.Delay(1);
106+
107+
await DiagramRef.LoadFromJsonAsync(DiagramJson);
108+
StateHasChanged();
109+
}
110+
111+
await base.OnAfterRenderAsync(firstRender);
112+
}
113+
114+
private string DiagramJson { get; set; } = @"
115+
{
116+
""shapes"": [
117+
{
118+
""id"": ""shape1"",
119+
""type"": ""Circle"",
120+
""content"": {
121+
""text"": ""Shape 1""
122+
},
123+
""x"": 200,
124+
""y"": 50
125+
},
126+
{
127+
""id"": ""shape2"",
128+
""content"": {
129+
""text"": ""Shape 2""
130+
},
131+
""height"": 100,
132+
""width"": 160,
133+
""x"": 50,
134+
""y"": 200
135+
},
136+
{
137+
""id"": ""shape3"",
138+
""type"": ""Display"",
139+
""content"": {
140+
""text"": ""Shape 3""
141+
},
142+
""x"": 300,
143+
""y"": 200
144+
}
145+
],
146+
""connections"": [
147+
{
148+
""from"": {
149+
""shapeId"": ""shape1""
150+
},
151+
""to"": {
152+
""shapeId"": ""shape2""
153+
}
154+
},
155+
{
156+
""from"": {
157+
""shapeId"": ""shape1"",
158+
""connector"":""Right""
159+
},
160+
""to"": {
161+
""shapeId"": ""shape3"",
162+
""connector"":""Top""
163+
}
164+
}
165+
]
166+
}";
167+
114168
#region Diagram State Classes
115169
116170
public class DiagramState
@@ -192,7 +246,7 @@ Use the Diagram `SaveAsJsonAsync` and `LoadFromJsonAsync` methods to [persist th
192246
Utf8JsonWriter writer,
193247
DiagramShapeType shapeType,
194248
JsonSerializerOptions options) =>
195-
writer.WriteStringValue(shapeType.ToString().ToLower());
249+
writer.WriteStringValue(shapeType.ToString());
196250
}
197251
}
198252
```

0 commit comments

Comments
 (0)