Fixed height, responsive width of VictoryChart
Hey there!
I'm trying to create a chart which should have a fixed height of say 250px and fill 100% of the width of it's container. Kind of like a normal div would. Is that possible? I've looked through most of the documentation and couldn't find any pointers. (I imagine that ideally it should work via enabling vectorEffect: "non-scaling-stroke" as well.)
Cheers and thank you for this great library! Daniel
Or setting preserveAspectRatio and/or viewbox on <svg> to maintain ratio.
Don't understand why you'd want a chart to lose it's aspect ratio?
What I'm aiming for is a time series that spans the whole width of the screen. Thus rewarding users with larger screens with very high fidelity and yet have everything being legible on smaller screens.
Basically like this example: http://dygraphs.com/gallery/#g/resize
@danielberndt you can set standalone={false} on VictoryChart and it will render a <g> tag that you can render inside a custom <svg>
Thanks for the tip, @boygirl. But I'm still not entirely sure how I can accomplish what I was aiming for. The graph still seems to follow a certain aspect ratio (it seems like some default height and width are assumed if I don't state any height and width props for the <VictoryChart>).
And once I start resizing the chart, the font changes in size as well and the tick count doesn't adapt to the new width. In case you haven't already, have a look at the example link I posted above and resize the window to see what I'm aiming for :)
Coming here with the same issue. Once the SVG has been made it's scaled literally, so if you have a wide screen, a graph doesn't get wider but larger in both height and width, so a simple graph will be full screen. There seems to be some 4:3 aspect built in somewhere even if you override the width + height. I had some success by setting the width/height props to the pixel dimensions of the examined container after it had been rendered - then listening to resize, although the font sizes were all off. So if you set a 10px fontSize, it might end up being 3px or 23px depending on how much the image ends up being scaled.
@samkelleher @danielberndt Victory renders responsive SVGs using the viewBox attribute which is aspect ratio based. You can read more about how responsive container interact with other props and some options you have for altering this default behavior here https://formidable.com/open-source/victory/guides/layout/
tl:dr the ~4:3 aspect ratio is coming from the width and height props defined in Victory's default theme https://github.com/FormidableLabs/victory-core/blob/master/src/victory-theme/grayscale.js
Hi all, so is there no way of doing this at all? Trying to achieve the same thing as @danielberndt but no luck so far...
@jcbmb you can change the aspect ratio by altering the width and height props on VictoryChart. Unfortunately responsive svgs don't scale things like text.
The way to achieve what your after would be to add a resize handler to your container and update width and height on VictoryChart as you resize. You could also update your font sizes in your style prop at the same time.
If anyone is looking for an easy way to do this, I'd recommend using react-dimesions
All you have to do use the HOC they provide to get the height and width of the container, and then provide them to the chart, and everything works exactly how you'd expect.
Thanks for posting that @cbranch101. Very handy.
Curious how it's done in this showcase: https://projects.fivethirtyeight.com/sumo/
That's exactly the behavior I want (except how laggy the resize is--is this a Victory limitation?), especially the font sizes remaining constant. Victory by default gives you huge fonts for full-width charts.
This is one way:
constructor(props) {
super(props);
this.state = {
chartWidth: 0
}
}
componentDidMount() {
this.setState({
chartWidth: window.innerWidth
});
window.addEventListener('resize', this.updateDimensions.bind(this));
// remove this on unmount
}
updateDimensions(event) {
this.setState({
chartWidth: event.target.innerWidth
})
}
In your render function:
<svg viewBox={"0 0" + " "+ this.state.chartWidth +" " + "350"} preserveAspectRatio="none" width="100%">
<VictoryChart
domainPadding={{x: 40}}
standalone={false}
width={this.state.chartWidth}
height={350}
>
// Chart stuff
</VictoryChart>
</svg>
2020 update:
If you are using this solution in 2020 (not sure if victory have a built in solution now) and you are using react functions/hooks vs classes (which you should be); something like this would be a more modern approach...
const [width, setWidth] = useState(window.innerWidth)
const updateWidth = (ev) => {
setWidth(ev.target.innerWidth)
}
// useEffect replaces `componentDidMount` and others
useEffect(() => {
window.addEventListener('resize', updateWidth)
// Removes listener on unmount
return () => {
window.removeEventListener('resize', updateWidth)
}
},[])
Replace {this.state.chartWidth} with {width} in you render return
@jasonhealy thanks but one thing problem with the code, the font size are too small. how can i adjust the font-size base on the example you have?
I see several comments that say 'create a resize handler' to prevent font sizes from becoming too small when using Victory with flex but an example sure would be helpful if anyone has one.
@rweller59 I'm using Jason's method directly (i.e. it's not nested inside SVG tags) with VictoryChart and it works great. My VictoryCandlestick grows and shrinks horizontally, font sizes remain the same, and the x-axis is populated with more labels when space allows.
I know this is an old thread, but in case anyone needs responsive charts that respect the container's (aka parent element)'s width:
ContainerWidthSizer.tsx
import * as React from 'react'
interface State {
width: number
}
interface Props {
children: (state: State) => React.ReactNode
}
class ContainerWidthSizer extends React.Component<Props, State> {
state = {
width: 0
}
container: HTMLDivElement
componentDidMount() {
this.setState({
width: this.container.getBoundingClientRect().width
})
}
render() {
return (
<div ref={c => this.container = c}>
{this.props.children(this.state)}
</div>
)
}
}
export default ContainerWidthSizer
Usage
<ContainerWidthSizer>
{({ width }) => (
<VictoryChart style={styles.chart} domain={{ y: [0, 31] }} height={box.height} width={width}>
)}
</ContainerWidthSizer>
I found this library: https://github.com/ctrlplusb/react-sizeme, seems maintained and well-supported.
@jasonhealy -Thank you, this worked beautifully!
@jasonhealy Thank you so much!
Alternative hooks solution:
const [size, onLayout] = useComponentSize();
<View onLayout={onLayout}>
<VictoryChart width={size?.width} height={300}>
...
UseComponentSize.tsx
import * as React from 'react';
const useComponentSize = () => {
const [size, setSize] = React.useState(null);
const onLayout = React.useCallback(event => {
const { width, height } = event.nativeEvent.layout;
setSize({ width, height });
}, []);
return [size, onLayout];
};
export default useComponentSize;