Skip to content

Commit

Permalink
add custom label option for xaxis
Browse files Browse the repository at this point in the history
  • Loading branch information
riyavsinha committed May 8, 2024
1 parent 5637d68 commit dfb40eb
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
18 changes: 13 additions & 5 deletions src/components/logo/XAxis.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import { xrange } from "../../common/utils";
export type XAxisProps = {
/** The total number of positions in the logo. */
n: number;
/** SVG transform to apply to the axis. */
/** Optional. SVG transform to apply to the axis. */
transform?: string;
/** The width of each glyph in the containing logo. */
/** Optional. The width of each glyph in the containing logo. */
glyphWidth: number;
/** The number of the first position in the logo. Defaults to 1. */
/** Optional. The number of the first position in the logo. Defaults to 1. */
startPos?: number;
/** The rotation of the axis. */
/** Optional. Labels to use for each position instead of the number. Length must match number of positions `n` parameter. */
labels?: string[];
/** Optional. The rotation of the axis. Default is -90 (horizontal, left-to-right). */
rotation?: number;
};

Expand All @@ -22,9 +24,15 @@ export const XAxis = ({
n,
transform,
glyphWidth,
labels,
startPos = 1,
rotation = -90,
}: XAxisProps) => {
if (labels && labels.length !== n) {
throw new Error(
`Length of labels (${labels.length}) must match the number of positions (${n}).`
);
}
const numbers = xrange(n);
const rotationTransform = `rotate(${rotation})`;
return (
Expand All @@ -39,7 +47,7 @@ export const XAxis = ({
transform={rotationTransform}
style={{ transformBox: "fill-box", transformOrigin: "right" }}
>
{n + startPos}
{labels ? labels[n] : n + startPos}
</text>
))}
</g>
Expand Down
9 changes: 9 additions & 0 deletions src/stories/components/XAxis.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,12 @@ export const LargePositions: Story = {
startPos: 1847933,
},
};

export const CustomLabels: Story = {
render: VerticalLabels.render,
args: {
...VerticalLabels.args,
labels: ["E77", "R90", "M101", "K299", "Y401"],
rotation: -45,
},
};

0 comments on commit dfb40eb

Please sign in to comment.