Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion components/diagram/shapes.md
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ When using a visual function, the Diagram ignores all Shape parameters in the Ra

> This section links to the documentation of Kendo UI for jQuery. The Telerik Diagram for Blazor is not a wrapper of the Kendo UI Diagram. However, both components use the same client-side rendering engine. When the Kendo UI documentation mentions the `kendo.dataviz.diagram` JavaScript namespace, you must use `TelerikBlazor.DiagramCommon` instead.

In addition to the following example, also check this [Blazor Diagram demo](https://demos.telerik.com/blazor-ui/diagram/overview), which uses a visual function.
In addition to the following example, also check the [Blazor Diagram Overview demo](https://demos.telerik.com/blazor-ui/diagram/overview) and the [Show Diagram Shape Text Below Image](slug:diagram-kb-show-shape-text-below-image) knowledge base article. Both use a visual function.

>caption Using Diagram Shape visual function

Expand Down
134 changes: 134 additions & 0 deletions knowledge-base/diagram-show-shape-text-below-image.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
---
title: Show Diagram Shape Text Below Image
description: Learn how to display shape text below the shape image in a Telerik Blazor Diagram component.
type: how-to
page_title: How to Show Shape Text Below the Image in Telerik Diagram for Blazor
slug: diagram-kb-show-shape-text-below-image
tags: telerik, blazor, diagram
ticketid: 1698784
res_type: kb
---

## Environment

<table>
<tbody>
<tr>
<td>Product</td>
<td>Diagram for Blazor</td>
</tr>
</tbody>
</table>

## Description

This KB answers the following questions:

* 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.
* Can the Shape text be positioned under the image without using visual functions with JavaScript?
* How to display and position the text in a Diagram Shape below the image?

## Solution

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.

>caption Arrange Diagram Shape image and text one below the other

````RAZOR
<TelerikDiagram>
<DiagramConnectionDefaults Type="@DiagramConnectionType.Cascading" />
<DiagramLayout Type="@DiagramLayoutType.Tree" />
<DiagramShapeDefaults Visual="imageShapeVisual">
<DiagramShapeDefaultsContent Color="black" FontSize="16" />
</DiagramShapeDefaults>

<DiagramShapes>
@foreach (DiagramShapeModel diagramShape in DiagramData)
{
<DiagramShape @key="@diagramShape" Id="@diagramShape.Id" DataItem="@diagramShape" />
}
</DiagramShapes>

<DiagramConnections>
@foreach (DiagramShapeModel diagramShape in DiagramData)
{
if (!string.IsNullOrEmpty(diagramShape.ParentId))
{
<DiagramConnection @key="@diagramShape" FromId="@diagramShape.ParentId" ToId="@diagramShape.Id" />
}
}
</DiagramConnections>
</TelerikDiagram>

@* Move JavaScript code to a separate JS file *@
<script suppress-error="BL9992">
function imageShapeVisual(context) {
let diagramNS = TelerikBlazor.DiagramCommon;

let shapeGroup = new diagramNS.Group();

let shapeRectangle = new diagramNS.Rectangle({
width: context.width,
height: context.height,
stroke: {
color: "transparent"
}
});
shapeGroup.append(shapeRectangle);

let layoutSpacing = 6;
// Make sure the layout content can fit, otherwise you will get unexpected results.
let imageDimension = context.height - context.content.fontSize * 1.5;
let imageTextRect = new diagramNS.Rect(0, 0, context.width, context.height);

let imageTextLayout = new diagramNS.Layout(imageTextRect, {
alignContent: "center",
alignItems: "center",
justifyContent: "center",
orientation: "vertical",
spacing: layoutSpacing
});
shapeGroup.append(imageTextLayout);

let image = new diagramNS.Image({
source: context.dataItem.Source,
width: imageDimension,
height: imageDimension
});
let title = new diagramNS.TextBlock({
text: context.dataItem.Title,
color: context.color
});

imageTextLayout.append(image);
imageTextLayout.append(title);
imageTextLayout.reflow();

return shapeGroup;
}
</script>

@code {
private List<DiagramShapeModel> DiagramData { get; set; } = new()
{
new DiagramShapeModel() { Id = "shape-1", Title = "Shape 1 Title" },
new DiagramShapeModel() { Id = "shape-2", ParentId = "shape-1", Title = "Shape 2 Title" },
new DiagramShapeModel() { Id = "shape-3", ParentId = "shape-1", Title = "Shape 3 Title" },
};

public class DiagramShapeModel
{
public string Id { get; set; } = string.Empty;
public string ParentId { get; set; } = string.Empty;
public string Title { get; set; } = "Default Shape Title";
public string Source { get; set; } = Base64SvgImage;
}

private const string Base64SvgImage = "data:image/svg;base64,iVBORw0KGgoAAAANSUhEUgAAAKQAAACkCAMAAAAua3VzAAACylBMVEVMaXFc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5QBc5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BxGDiOHkWpxp5F5pGirywrWkaN8ZdwItpGZ+f/mrZhGKt747xNVlpEZNx8vvdhFLvDd8KQFq0iHGLVyBEaRt/oVjjOJDCjpX3vCIDJn5sAdW+aQjs3P7uIwhpywdbCvGJhCigJuDNqCJWR7voa8GTvIBH+Nl2AFKT7WoHnThhHkZJW28lsmkEEXtRcMMICUrCOlyYyP3PobcTfJ2Mj2+zwKbo2AnKbW7EEnnxZGQN5Ta9acxiZSpX56mChnhyt7SNfYtQMWWxkNrCHLBqnHM/MvYglZ5D/4pwph0U6sIJ2iNR9Oe/iMlAWkfOZYrTPyqgZzbKSutWrmDVWk1ZfpkWlKXGTWZF2QymlH+HwfP89+GSoySZf6ST97vqdahaYiImXJOiBTC/hn0ST1B7CQSntT3sgD9bodbTSn+RoKMjtQzLem91rzHJ0T4uNWhOuPXLPFlGfhscvFPCFpe9GWc3oig28p+JZwbx4ldHdB7F6tB/L0umK+xfDVeh0rbubhJBBpbq/xEKWBSCcPfQ+1WfDFaCHIG29rvmR/ZNpb3vqfAyaq8JTpiIwM/lLbFdWRMs8KnKPKFF3BUl2QTSna/+7/SGlwlwKwwuenDbyRWcdzCVd7iozYPwEww0pVywuZ9s584rUeI2tVeYAdU9IDycjCUh5jSR8yfBuVc7mD2khIpxhe3Td2DBfZ0w0Giv7Iupl3+DXbGSn/03D/ukId6eV8lm+z3Ed/ARgDufegHbAZT5Eui3IB2EZKlxYAMI50Xu8AjCMjutYCsI20zs8DxuPv3l0G+Tv/AJiXQD+0DbN3AAAAAElFTkSuQmCC";
}
````

## See Also

* [Diagram Shapes](slug:diagram-shapes)
* [Diagram Demo with Shape Visual](https://demos.telerik.com/blazor-ui/diagram/overview)