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: 244 color: "#2563eb"5 fontWeight: "bold"6 align: "center"7 width: 3008 height: 509}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: 30018 height: 8019}20
21page22show title23show descriptionRendered Diagram
Properties
Text elements support the following properties:
| Property | Type | Description |
|---|---|---|
value | string | string[] | Text content (single line or array of lines) |
fontSize | number | number[] | Font size(s) for each line |
color | color | (color | null)[] | Text color(s) for each line |
fontWeight | fontWeight | (fontWeight | null)[] | Font weight (normal, bold, etc.) |
fontFamily | fontFamily | (fontFamily | null)[] | Font family for each line |
align | align | (align | null)[] | Text alignment (left, center, right) |
lineSpacing | number | Spacing between lines |
width | number | Text box width |
height | number | Text box height |
Methods
Text elements support these methods for manipulation:
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 linessetValue(line, value)- Set text content for specific linesetFontSize(value)- Set font size for all textsetFontSize(line, size)- Set font size for specific linesetColor(line, color)- Set color for specific linesetFontWeight(weight)- Set font weight for all textsetFontWeight(line, weight)- Set font weight for specific linesetFontFamily(family)- Set font family for all textsetFontFamily(line, family)- Set font family for specific linesetAlign(alignment)- Set alignment for all textsetAlign(line, alignment)- Set alignment for specific linesetLineSpacing(spacing)- Set line spacingsetWidth(width)- Set text box widthsetHeight(height)- Set text box height
Multiple Element Methods
setValues([...])- Set text content for multiple linessetFontSizes([...])- Set font sizes for multiple linessetColors([...])- Set colors for multiple linessetFontWeights([...])- Set font weights for multiple linessetFontFamilies([...])- Set font families for multiple linessetAligns([...])- 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: 3008 height: 1009}10
11page12show styledRendered Diagram
Dynamic Text Updates
Source Code
1text status = {2 value: "Status: Waiting"3 fontSize: 144 color: "gray"5 fontFamily: "monospace"6}7
8page9show status10
11page12status.setColor(0, "orange")13status.setValue(0, "Status: Processing")14
15page16status.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: 144 color: ["gray", "gray", "gray"]5}6
7page8show progress9
10page11progress.setValues(["Step 1: Initialize ✓", "Step 2: Process...", "Step 3: Complete"])12progress.setColors(["green", "orange", "gray"])13
14page15progress.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: 1810 color: "#1f2937"11 fontWeight: "bold"12 align: "center"13 width: 35014 height: 3015}16
17text stats = {18 value: ["Max: 30", "Min: 10", "Avg: 20"]19 fontSize: 1220 color: "#4b5563"21 align: "left"22 width: 15023 height: 6024}25
26page27show myArr28show statsRendered 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: 209 width: 35010 height: 12011}12
13page14show richRendered 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: 1410 color: "#1f2937"11 fontFamily: "Arial"12 align: "center"13 width: 30014 height: 4015}16
17page18show sorting19
20page21sorting.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
7page8show myGraphRendered 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: belowNumbers5 above: "Prime Number Sequence"6}7
8text belowNumbers = {9 value: "These are the first three prime numbers."10 fontSize: 1411 color: "gray"12 fontWeight: "normal"13 fontFamily: "Georgia"14 align: "center"15 lineSpacing: 1016 width: 10017 height: 4018}19
20page21show numbersRendered 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: 2008 height: 509}10
11array dataset1 = {12 value: [10, 20, 30]13 color: ["red", "green", "blue"]14 above: sharedLabel15}16
17array dataset2 = {18 value: [15, 25, 35]19 color: ["orange", "purple", "yellow"]20 below: sharedLabel21}22
23page 2x124show 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
Related
- 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