Skip to content

Fix pack chart labels using xfun::js() for JavaScript callbacks#12

Merged
yihui-bot merged 2 commits intomainfrom
copilot/investigate-pack-labels-issue
Mar 31, 2026
Merged

Fix pack chart labels using xfun::js() for JavaScript callbacks#12
yihui-bot merged 2 commits intomainfrom
copilot/investigate-pack-labels-issue

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 31, 2026

G2's pack mark transforms tree data into d3-hierarchy nodes, nesting original fields under d.data.name — inaccessible via plain JSON field strings. The fix uses xfun::js(), which xfun::tojson() serializes as a raw JS expression (unquoted), enabling callback functions in the G2 spec.

  • examples/marks.Rmd: Replace broken labels = list(list(text = 'value')) with style.labelText JS callback per G2 official docs; add color = 'depth' encoding for visual depth cues
g2() |>
  mark_pack(
    data = list(type = 'inline', value = tree_data),
    encode = list(value = 'value', color = 'depth'),
    style = list(
      labelText = xfun::js(
        '(d) => d.r >= 10 && d.height === 0 ? d.data.name : ""'
      ),
      labelFontSize = 8
    ),
    legend = FALSE
  )

xfun::js() is the general pattern for any G2 property requiring a JS function (dynamic styles, tooltip titles, conditional visibility, etc.).

@yihui-bot yihui-bot marked this pull request as ready for review March 31, 2026 18:06
@yihui-bot yihui-bot merged commit 77a7604 into main Mar 31, 2026
5 checks passed
@yihui-bot yihui-bot deleted the copilot/investigate-pack-labels-issue branch March 31, 2026 18:06
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.

3 participants