Skip to main content

Text

Text elements allow you to add formatted labels, descriptions, and annotations to your visualizations. They support rich styling options for typography and layout.

Source Code

1text title = {
2 value: "Algorithm Visualization"
3 fontSize: 24
4 color: "#2563eb"
5 fontWeight: "bold"
6 align: "center"
7 width: 300
8 height: 50
9}
10
11text description = {
12 value: ["This demonstrates text styling", "with multiple lines and colors"]
13 fontSize: [14, 12]
14 color: ["#374151", "#6b7280"]
15 fontFamily: ["Arial", "Georgia"]
16 align: ["left", "right"]
17 width: 300
18 height: 80
19}
20
21page
22show title
23show description

Rendered Diagram

Properties

Text elements support the following properties:

PropertyTypeDescription
valuestring | string[]Text content (single line or array of lines)
fontSizenumber | number[]Font size(s) for each line
colorcolor | (color | null)[]Text color(s) for each line
fontWeightfontWeight | (fontWeight | null)[]Font weight (normal, bold, etc.)
fontFamilyfontFamily | (fontFamily | null)[]Font family for each line
alignalign | (align | null)[]Text alignment (left, center, right)
lineSpacingnumberSpacing between lines
widthnumberText box width
heightnumberText box height

Methods

Text elements support these methods for manipulation:

info

Text objects do not support the setText() method since they are text themselves. Use setValue(), setFontSize(), and other text-specific methods instead.

Single Property Methods

  • setValue(value) - Set text content for all lines
  • setValue(line, value) - Set text content for specific line
  • setFontSize(value) - Set font size for all text
  • setFontSize(line, size) - Set font size for specific line
  • setColor(line, color) - Set color for specific line
  • setFontWeight(weight) - Set font weight for all text
  • setFontWeight(line, weight) - Set font weight for specific line
  • setFontFamily(family) - Set font family for all text
  • setFontFamily(line, family) - Set font family for specific line
  • setAlign(alignment) - Set alignment for all text
  • setAlign(line, alignment) - Set alignment for specific line
  • setLineSpacing(spacing) - Set line spacing
  • setWidth(width) - Set text box width
  • setHeight(height) - Set text box height

Multiple Element Methods

  • setValues([...]) - Set text content for multiple lines
  • setFontSizes([...]) - Set font sizes for multiple lines
  • setColors([...]) - Set colors for multiple lines
  • setFontWeights([...]) - Set font weights for multiple lines
  • setFontFamilies([...]) - Set font families for multiple lines
  • setAligns([...]) - Set alignments for multiple lines

Examples

Basic Text

Multi-line Text with Styles

Source Code

1text styled = {
2 value: ["Title Text", "Subtitle", "Body content here"]
3 fontSize: [20, 16, 12]
4 color: ["#1f2937", "#4b5563", "#6b7280"]
5 fontWeight: ["bold", "normal", "normal"]
6 align: ["center", "center", "left"]
7 width: 300
8 height: 100
9}
10
11page
12show styled

Rendered Diagram

Dynamic Text Updates

Source Code

1text status = {
2 value: "Status: Waiting"
3 fontSize: 14
4 color: "gray"
5 fontFamily: "monospace"
6}
7
8page
9show status
10
11page
12status.setColor(0, "orange")
13status.setValue(0, "Status: Processing")
14
15page
16status.setColor(0, "green")
17status.setValue(0, "Status: Complete")
18status.setFontWeight("bold")

Rendered Diagram

Multiple Lines with setValues

Source Code

1text progress = {
2 value: ["Step 1: Initialize", "Step 2: Process", "Step 3: Complete"]
3 fontSize: 14
4 color: ["gray", "gray", "gray"]
5}
6
7page
8show progress
9
10page
11progress.setValues(["Step 1: Initialize ✓", "Step 2: Process...", "Step 3: Complete"])
12progress.setColors(["green", "orange", "gray"])
13
14page
15progress.setValues(["Step 1: Initialize ✓", "Step 2: Process ✓", "Step 3: Complete ✓"])
16progress.setColors(["green", "green", "green"])

Rendered Diagram

Text as Data Labels

Source Code

1array myArr = {
2 value: [10, 25, 30, 15]
3 color: ["red", "green", "blue", "orange"]
4 above: "chartTitle"
5}
6
7text chartTitle = {
8 value: "Sample Data Visualization"
9 fontSize: 18
10 color: "#1f2937"
11 fontWeight: "bold"
12 align: "center"
13 width: 350
14 height: 30
15}
16
17text stats = {
18 value: ["Max: 30", "Min: 10", "Avg: 20"]
19 fontSize: 12
20 color: "#4b5563"
21 align: "left"
22 width: 150
23 height: 60
24}
25
26page
27show myArr
28show stats

Rendered Diagram

Rich Text Formatting

Source Code

1text rich = {
2 value: ["HEADER", "Important Notice", "Regular text here", "Footer info"]
3 fontSize: [24, 16, 14, 10]
4 color: ["#dc2626", "#f59e0b", "#374151", "#9ca3af"]
5 fontWeight: ["bold", "bold", "normal", "normal"]
6 fontFamily: ["Arial", "Georgia", "Times", "Courier"]
7 align: ["center", "center", "left", "right"]
8 lineSpacing: 20
9 width: 350
10 height: 120
11}
12
13page
14show rich

Rendered Diagram

Algorithm Step Description

Source Code

1array sorting = {
2 value: [64, 34, 25, 12]
3 color: ["red", "red", null, null]
4 below: "stepDesc"
5}
6
7text stepDesc = {
8 value: "Step 1: Compare first two elements"
9 fontSize: 14
10 color: "#1f2937"
11 fontFamily: "Arial"
12 align: "center"
13 width: 300
14 height: 40
15}
16
17page
18show sorting
19
20page
21sorting.setValues([34, 64, 25, 12])
22sorting.setColors(["green", "green", null, null])
23stepDesc.setValue(0, "Step 2: Swap if needed, continue")
24stepDesc.setColor(0, "#059669")

Rendered Diagram

Special Features

Implicit Text Fields

When you assign a string directly to positioning properties like above, below, left, or right, Merlin automatically creates an implicit text field:

Source Code

1graph myGraph = {
2 nodes: [n1, n2, n3]
3 edges: [n1-n2, n2-n3, n3-n1]
4 below: "Triangle Graph Representation"
5}
6
7page
8show myGraph

Rendered Diagram

The string "Triangle Graph Representation" automatically becomes a text element with default styling.

Linked Text Boxes

For more advanced styling and reusability, you can link explicit text elements to data structures by referencing them without quotes:

Source Code

1array numbers = {
2 value: [1, 2, 3]
3 color: ["blue", "green", "red"]
4 below: belowNumbers
5 above: "Prime Number Sequence"
6}
7
8text belowNumbers = {
9 value: "These are the first three prime numbers."
10 fontSize: 14
11 color: "gray"
12 fontWeight: "normal"
13 fontFamily: "Georgia"
14 align: "center"
15 lineSpacing: 10
16 width: 100
17 height: 40
18}
19
20page
21show numbers

Rendered Diagram

Benefits of Linked Text

Linked text boxes provide several advantages:

  • Custom styling - Full control over typography and layout
  • Reusability - Same text element can be referenced by multiple structures
  • Dynamic updates - Text can be modified independently
  • Complex formatting - Multi-line text with per-line styling

Source Code

1text sharedLabel = {
2 value: ["Data Analysis", "Updated: 2024"]
3 fontSize: [16, 12]
4 color: ["#1f2937", "#6b7280"]
5 fontWeight: ["bold", "normal"]
6 align: ["center", "center"]
7 width: 200
8 height: 50
9}
10
11array dataset1 = {
12 value: [10, 20, 30]
13 color: ["red", "green", "blue"]
14 above: sharedLabel
15}
16
17array dataset2 = {
18 value: [15, 25, 35]
19 color: ["orange", "purple", "yellow"]
20 below: sharedLabel
21}
22
23page 2x1
24show dataset1 (0, 0)
25show dataset2 (1, 0)

Rendered Diagram

Use Cases

Text elements are ideal for:

  • Titles and headers - Chart titles, section headers
  • Step descriptions - Algorithm step explanations
  • Labels and annotations - Data labels, code comments
  • Status indicators - Current state, progress messages
  • Legends - Color coding explanations
  • Instructions - User guidance, help text
  • Mathematical notation - Formulas, equations (basic)
  • Multi-language content - Different fonts per language
  • Methods Reference - Complete list of text methods
  • Array - Text can label array elements
  • Matrix - Text can describe matrix operations
  • Graph - Text can annotate graph structures