Skip to content

Conversation

@ANAMASGARD
Copy link
Contributor

@ANAMASGARD ANAMASGARD commented Oct 21, 2025

Add Multi-line Text Support Throughout animint2

Closes #221

This PR enables newline characters (\n) to work in all text elements of animint2 visualizations. Users can now create multi-line text in plot titles, axis labels, legend titles, and geom_text labels.

What Now Works:
✅ Plot titles - ggtitle("Line 1\nLine 2")
✅ X-axis titles - xlab("X Axis\nSubtitle")
✅ Y-axis titles - ylab("Y Axis\nSubtitle")
✅ Legend titles - scale_color_discrete(name="Legend\nTitle")
✅ Text labels - geom_text(aes(label="Text\nLabel"))
✅ Tooltips - Already worked, still works (backward compatible)

Below is the screenshots of the scripts i ran locally to verify the test locally they fail as aspected
Screenshot From 2025-10-21 19-08-38
Screenshot From 2025-10-21 19-08-44

Tests check that \n converts to <br/> in:
- plot titles (ggtitle)
- axis titles (xlab/ylab)
- legend titles
- geom_text labels

Currently fail since feature not implemented.
Addresses #221
Changes:
- R/z_multiline.R: new helper to convert \n to <br/>
- R/z_animint.R: apply conversion to plot title, x/y axis titles
- R/z_animintHelpers.R: apply conversion to legend titles and geom_text labels
- inst/htmljs/animint.js: render <br/> as SVG tspan elements, fix overlap with proper height calculation
- DESCRIPTION: add z_multiline.R to Collate
- NEWS.md: document multiline text support

Fixes #221
@ANAMASGARD
Copy link
Contributor Author

Add Multi-line Text Support Throughout animint2

Fixes #221

Summary

This PR enables newline characters (\n) to work in all text elements of animint2 visualizations, not just tooltips. Users can now create multi-line text in plot titles, axis labels, legend titles, and geom_text labels.

What Now Works

Plot titles - ggtitle("Line 1\nLine 2")
X-axis titles - xlab("X Axis\nSubtitle")
Y-axis titles - ylab("Y Axis\nSubtitle")
Legend titles - scale_color_discrete(name="Legend\nTitle")
Text labels - geom_text(aes(label="Text\nLabel"))
Tooltips - Already worked, still works (backward compatible)

Visual Verification

The screenshot below demonstrates multi-line text working correctly in all contexts:

Screenshot From 2025-10-21 19-30-47

Example Usage

library(animint2)

data <- data.frame(
  x = 1:5,
  y = 1:5,
  group = c("A", "B", "C", "A", "B"),
  label = c("Single", "Two\nLines", "Three\nLines\nHere", "Text", "Label")
)

viz <- animint(
  plot1 = ggplot(data, aes(x, y, color = group, label = label)) +
    geom_point(size = 5) +
    geom_text(vjust = -1) +
    ggtitle("Multi-line Test\nIssue #221 Demonstration") +
    xlab("X Axis\n(Horizontal Position)") +
    ylab("Y Axis\n(Vertical Position)") +
    scale_color_discrete(name = "Data Group\n(Click to Select)")
)

animint2dir(viz, "output-dir")

@tdhock
Copy link
Collaborator

tdhock commented Oct 21, 2025

No obvious timing issues in HEAD=multiline-text-support-221
Comparison Plot

Generated via commit 4ac0712

Download link for the artifact containing the test results: ↓ atime-results.zip

Task Duration
R setup and installing dependencies 2 minutes and 36 seconds
Installing different package versions 15 seconds
Running and plotting the test cases 3 minutes and 27 seconds

@ANAMASGARD
Copy link
Contributor Author

Ran tests locally - all 19 multiline text tests pass successfully. The test failures shown are due to missing optional packages (mapproj, sp, XML) and GitHub API permissions, not related to this PR. Sir can you please help me figure out on how to pass the JS_Coverage test suite .

@tdhock
Copy link
Collaborator

tdhock commented Oct 21, 2025

To get this PR to close the corresponding issue, please add

Closes #221

on its own line, in the first comment of the PR.

@tdhock
Copy link
Collaborator

tdhock commented Oct 21, 2025

looks like a step in the right direction, thanks!
however here #261 (comment) the second line of the overall title "Issue #221 Demonstration" is going into the plotting area.
Can you please add a test which makes sure the bottom of this title element is above the plotting area? (it should fail for the current code)

@tdhock
Copy link
Collaborator

tdhock commented Oct 21, 2025

Also in #261 (comment) it seems like there is an inconsistency between the X and Y axes.

  • there is almost no space between the Y axis title (Vertical Position) and the Y axis ticks (2 3 4)
  • there is a big space between the X axis title (X Axis) and the X axis tick (3).

can this be adjusted to be more consistent please?
also it should be consistent with the case of single line X/y axis title, as below from https://5-merge--animint-manual-en.netlify.app/ch06/#aes-tooltip which was created with current master
image

Copy link
Collaborator

@tdhock tdhock left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add tests then fix

ANAMASGARD and others added 3 commits October 22, 2025 14:30
Tests check:
- plot title doesn't overlap with plot area
- x-axis and y-axis spacing is consistent
- multiline spacing matches single-line baseline

Tests currently fail, demonstrating the bugs in current code.
Addresses feedback on PR #261.
Two fixes:
1. Add 5px margin below title to prevent overlap with plot area
2. Use same base spacing (30px) for both X and Y axis labels

Both axes now have consistent spacing, and multiline titles
stay above the plot area.

Addresses feedback on PR #261.
@ANAMASGARD ANAMASGARD requested a review from tdhock October 22, 2025 13:58
Comment on lines 138 to 149
var lines = textStr.split('<br/>');

if (lines.length > 1) {
// Multi-line text: create tspan elements to measure properly
var lineHeight = 1.2; // em units
lines.forEach(function(line, i) {
textElement.append('tspan')
.attr('x', -1000)
.attr('dy', i === 0 ? 0 : lineHeight + 'em')
.text(line);
});

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this seems like an ovely complex way of measuring.
can't you just get the bounding box after rendering or something simpler like that?

also where does the -1000 come from? please avoid such "magic numbers"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done , please review and also give your feedback if anything else need to be changed .

Copy link
Collaborator

@tdhock tdhock left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please simplify height/width calculation

ANAMASGARD and others added 2 commits October 22, 2025 21:32
- removed hardcoded -1000 positioning
- reuse setMultilineText() instead of duplicating logic
- rely on getBBox() for accurate measurements

fixes spacing issues
@ANAMASGARD ANAMASGARD requested a review from tdhock October 22, 2025 16:22
setMultilineText(textElement, pText);
} else {
// Single line: simple text rendering
textElement.text(pText);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it seems unusual that there needs to be a special case for multiple lines.

also this if length > 1 else pattern is repeated in the setMultilineText function.

please simplify

@ANAMASGARD ANAMASGARD requested a review from tdhock October 23, 2025 12:38
xlab("X Label\nLine 2") +
ylab("Y Label\nLine 2")
)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please undo addition of empty space in functions and test_that blocks

@tdhock
Copy link
Collaborator

tdhock commented Oct 23, 2025

can you please post an updated screen shot?

@ANAMASGARD
Copy link
Contributor Author

Updated screenshot after spacing fixes:
Screenshot From 2025-10-23 20-06-25

@ANAMASGARD ANAMASGARD requested a review from tdhock October 23, 2025 15:18
@tdhock
Copy link
Collaborator

tdhock commented Oct 27, 2025

can you please test increasing and decreasing font size?

#64 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

line breaks in various text elements

3 participants