-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Word-wrap overflown X-axis labels #961
Comments
I haven't used recharts in a while, and have extracted out the <XAxis dataKey="name" interval={0} width={30} label={props => <Text width={30}>{props.text}</Text>} /> but I'm just guessing. I took a quick peak at the source of XAxis but didn't see how it uses the label prop. |
Doesn't work unfortunately... |
Any news ? |
Hi James2516, I just only use Please click the bellow link to know more! |
I was finally able to figure it out based on their Line Chart Example fiddle, which uses a custom component passed into the You can do something similar to what they've done and return the |
If you need word wrap on long string or word which has no spaces I have a fiddle which shows how to chunk long text strings and splits it up into multiple |
I created a export default function EllipsisAxisTick({ maxLines = 3, payload, ...rest }) {
const [text, setText] = useState(payload.value);
const [suffix, setSuffix] = useState('');
const measuredRef = useCallback(
node => {
if (node === null) {
return;
}
let { wordsByLines } = node.state;
let biggestLine = wordsByLines.reduce((a, b) => (a.width > b.width ? a : b));
let tempText = text;
const tempSuffix = wordsByLines.length > maxLines || biggestLine.width > rest.width ? '…' : '';
while (wordsByLines.length > maxLines || biggestLine.width > rest.width) {
tempText = tempText.slice(0, -1);
wordsByLines = node.getWordsByLines(
{
...rest,
children: tempText + tempSuffix
},
true
);
biggestLine = wordsByLines.reduce((a, b) => (a.width > b.width ? a : b));
}
if (tempText !== text) {
setText(tempText);
setSuffix(tempSuffix);
}
},
[maxLines, rest, text]
);
return (
<g>
<Text {...rest} ref={measuredRef}>
{text + suffix}
</Text>
<title>{payload.value}</title>
</g>
);
}
// ...
<YAxis
dataKey="agenda.title"
type="category"
width={133}
tick={<EllipsisAxisTick maxLines={3} />}
/> Unlike the other solutions seen here, this is based on a maximum number of lines while respecting the width. If it goes beyond that adds an ellipsis and a title to the hover. I noticed that |
This does no longer works in 2.0.0 because getWordsByLine is no longer accessible through the ref I think it was introduced in this commit fc1669a Would be cool to expose it again or maybe include this multiline ellipsis approach in the component itself, as it seems to work very well |
I've accidentally created a PR to include |
Any solution to this? I have the "2.0.9", version. |
#198
This is supposedly fixed by #243 but it doesn't seem to work.
How do I use it?
<XAxis dataKey="name" interval={0} width={30} label={<Text width={30} />} />
?@techniq
The text was updated successfully, but these errors were encountered: