|
| 1 | +--- |
| 2 | +title: Show Diagram Shape Text Below Image |
| 3 | +description: Learn how to display shape text below the shape image in a Telerik Blazor Diagram component. |
| 4 | +type: how-to |
| 5 | +page_title: How to Show Shape Text Below the Image in Telerik Diagram for Blazor |
| 6 | +slug: diagram-kb-show-shape-text-below-image |
| 7 | +tags: telerik, blazor, diagram |
| 8 | +ticketid: 1698784 |
| 9 | +res_type: kb |
| 10 | +--- |
| 11 | + |
| 12 | +## Environment |
| 13 | + |
| 14 | +<table> |
| 15 | + <tbody> |
| 16 | + <tr> |
| 17 | + <td>Product</td> |
| 18 | + <td>Diagram for Blazor</td> |
| 19 | + </tr> |
| 20 | + </tbody> |
| 21 | +</table> |
| 22 | + |
| 23 | +## Description |
| 24 | + |
| 25 | +This KB answers the following questions: |
| 26 | + |
| 27 | +* How to show the text label of a Diagram Shape below the image? By default, the text overlays the image, making it hard to read. |
| 28 | +* Can the Shape text be positioned under the image without using visual functions with JavaScript? |
| 29 | +* How to display and position the text in a Diagram Shape below the image? |
| 30 | + |
| 31 | +## Solution |
| 32 | + |
| 33 | +To rearrange the text labels and images in the Blazor Diagram Shapes, use the [`Visual` parameter of the `<DiagramShapeDefaults>` or `<DiagramShape>` tag](slug:diagram-shapes#visual-function). This will either affect all Shapes or a specific Shape. The associated JavaScript function may position the image and text with [`x` and `y` properties](https://www.telerik.com/kendo-jquery-ui/documentation/api/javascript/dataviz/diagram/text_block), but usually, the better option is to use a [`Layout` primitive](https://www.telerik.com/kendo-jquery-ui/documentation/api/javascript/dataviz/diagram/layout) as in the example below. |
| 34 | + |
| 35 | +>caption Arrange Diagram Shape image and text one below the other |
| 36 | +
|
| 37 | +````RAZOR |
| 38 | +<TelerikDiagram> |
| 39 | + <DiagramConnectionDefaults Type="@DiagramConnectionType.Cascading" /> |
| 40 | + <DiagramLayout Type="@DiagramLayoutType.Tree" /> |
| 41 | + <DiagramShapeDefaults Visual="imageShapeVisual"> |
| 42 | + <DiagramShapeDefaultsContent Color="black" FontSize="16" /> |
| 43 | + </DiagramShapeDefaults> |
| 44 | +
|
| 45 | + <DiagramShapes> |
| 46 | + @foreach (DiagramShapeModel diagramShape in DiagramData) |
| 47 | + { |
| 48 | + <DiagramShape @key="@diagramShape" Id="@diagramShape.Id" DataItem="@diagramShape" /> |
| 49 | + } |
| 50 | + </DiagramShapes> |
| 51 | +
|
| 52 | + <DiagramConnections> |
| 53 | + @foreach (DiagramShapeModel diagramShape in DiagramData) |
| 54 | + { |
| 55 | + if (!string.IsNullOrEmpty(diagramShape.ParentId)) |
| 56 | + { |
| 57 | + <DiagramConnection @key="@diagramShape" FromId="@diagramShape.ParentId" ToId="@diagramShape.Id" /> |
| 58 | + } |
| 59 | + } |
| 60 | + </DiagramConnections> |
| 61 | +</TelerikDiagram> |
| 62 | +
|
| 63 | +@* Move JavaScript code to a separate JS file *@ |
| 64 | +<script suppress-error="BL9992"> |
| 65 | + function imageShapeVisual(context) { |
| 66 | + let diagramNS = TelerikBlazor.DiagramCommon; |
| 67 | +
|
| 68 | + let shapeGroup = new diagramNS.Group(); |
| 69 | +
|
| 70 | + let shapeRectangle = new diagramNS.Rectangle({ |
| 71 | + width: context.width, |
| 72 | + height: context.height, |
| 73 | + stroke: { |
| 74 | + color: "transparent" |
| 75 | + } |
| 76 | + }); |
| 77 | + shapeGroup.append(shapeRectangle); |
| 78 | +
|
| 79 | + let layoutSpacing = 6; |
| 80 | + // Make sure the layout content can fit, otherwise you will get unexpected results. |
| 81 | + let imageDimension = context.height - context.content.fontSize * 1.5; |
| 82 | + let imageTextRect = new diagramNS.Rect(0, 0, context.width, context.height); |
| 83 | +
|
| 84 | + let imageTextLayout = new diagramNS.Layout(imageTextRect, { |
| 85 | + alignContent: "center", |
| 86 | + alignItems: "center", |
| 87 | + justifyContent: "center", |
| 88 | + orientation: "vertical", |
| 89 | + spacing: layoutSpacing |
| 90 | + }); |
| 91 | + shapeGroup.append(imageTextLayout); |
| 92 | +
|
| 93 | + let image = new diagramNS.Image({ |
| 94 | + source: context.dataItem.Source, |
| 95 | + width: imageDimension, |
| 96 | + height: imageDimension |
| 97 | + }); |
| 98 | + let title = new diagramNS.TextBlock({ |
| 99 | + text: context.dataItem.Title, |
| 100 | + color: context.color |
| 101 | + }); |
| 102 | +
|
| 103 | + imageTextLayout.append(image); |
| 104 | + imageTextLayout.append(title); |
| 105 | + imageTextLayout.reflow(); |
| 106 | +
|
| 107 | + return shapeGroup; |
| 108 | + } |
| 109 | +</script> |
| 110 | +
|
| 111 | +@code { |
| 112 | + private List<DiagramShapeModel> DiagramData { get; set; } = new() |
| 113 | + { |
| 114 | + new DiagramShapeModel() { Id = "shape-1", Title = "Shape 1 Title" }, |
| 115 | + new DiagramShapeModel() { Id = "shape-2", ParentId = "shape-1", Title = "Shape 2 Title" }, |
| 116 | + new DiagramShapeModel() { Id = "shape-3", ParentId = "shape-1", Title = "Shape 3 Title" }, |
| 117 | + }; |
| 118 | +
|
| 119 | + public class DiagramShapeModel |
| 120 | + { |
| 121 | + public string Id { get; set; } = string.Empty; |
| 122 | + public string ParentId { get; set; } = string.Empty; |
| 123 | + public string Title { get; set; } = "Default Shape Title"; |
| 124 | + public string Source { get; set; } = Base64SvgImage; |
| 125 | + } |
| 126 | +
|
| 127 | + private const string Base64SvgImage = "data:image/svg;base64,iVBORw0KGgoAAAANSUhEUgAAAKQAAACkCAMAAAAua3VzAAACylBMVEVMaXFc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5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fZ0w0Giv7Iupl3+DXbGSn/03D/ukId6eV8lm+z3Ed/ARgDufegHbAZT5Eui3IB2EZKlxYAMI50Xu8AjCMjutYCsI20zs8DxuPv3l0G+Tv/AJiXQD+0DbN3AAAAAElFTkSuQmCC"; |
| 128 | +} |
| 129 | +```` |
| 130 | + |
| 131 | +## See Also |
| 132 | + |
| 133 | +* [Diagram Shapes](slug:diagram-shapes) |
| 134 | +* [Diagram Demo with Shape Visual](https://demos.telerik.com/blazor-ui/diagram/overview) |
0 commit comments