Skip to content

Customize the Axis labels with SVG/HTML #2190

Closed Answered by Fil
Marttyn asked this question in Q&A
Discussion options

You must be logged in to vote

Using emojis should work:

const codes = new Map([
  ["Argentina", "AR"],
  ["Bolivia", "BO"],
  ["Chile", "CL"],
  ["Brazil", "BR"]
]);

function getFlagEmoji(c) {
  const countryCode = codes.get(c) ?? c;
  let codePoints = countryCode
    .toUpperCase()
    .split("")
    .map((char) => 127397 + char.charCodeAt(0));
  return String.fromCodePoint(...codePoints);
}

Plot.barX(
  [
    [1, "Argentina"],
    [2, "Brazil"],
    [3, "Chile"],
    [3, "Bolivia"]
  ],
  { x: "0", y: "1" }
).plot({ marginLeft: 100, y: { tickFormat: (d) => `${getFlagEmoji(d)} ${d}` } })

But I agree it could be nice to accept a SVG node as the output of the tickFormat function.

Replies: 1 comment 8 replies

Comment options

Fil
Oct 8, 2024
Collaborator

You must be logged in to vote
8 replies
@Marttyn
Comment options

@Fil
Comment options

Fil Oct 8, 2024
Collaborator

@Marttyn
Comment options

@Fil
Comment options

Fil Oct 8, 2024
Collaborator

@Marttyn
Comment options

Answer selected by Marttyn
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants