I have a problem with the dynamic change of cx and cy attributes depending on the size of window.innerHeight/window.innerWidth. I am sending to component window.innerHeight/window.innerWidth as hight/width and it look like this:
const SomeChart = ({width, height}) => {
const BubbleChartRef = useRef(null)
const InitialData = [{'r':2,'x':2,'y':1},
{'r':4,'x':3,'y':2},
{'r':5,'x':7,'y':10},
{'r':7,'x':5,'y':3},
{'r':3,'x':8,'y':9}]
useEffect(() => {
const svg = d3.select(BubbleChartRef.current)
let yScale = d3.scaleLinear().domain([0, 20]).range([0,height])
let xScale = d3.scaleLinear().domain([0, 20]).range([0,width])
svg.selectAll("circle")
.data(InitialData)
.enter()
.append("circle")
.attr('r', (d)=>d.r)
.attr('cx', (d, i)=>xScale(d.x))
.attr('cy', (d, i)=>yScale(d.y))
.attr('stroke', 'black')
.attr('fill', 'red')
.style('stroke-width', '1px')
}, [width, height])
return <svg ref={BubbleChartRef} className='bubble-chart-svg'/>
}
this is bubble-chart-svg css class:
.bubble-chart-svg{
width: 50vw;
height: 50vh;
}
When I add console.log(xScale(4))
in I get information about the new position cx
(after resize) but circle element in svg does not change.
Is it possible to change the position of these elements on my svg after changing the window size?
EDIT
Here is my controling window size component:
const BubbleChart = () => {
const [height, setHeight] = useState(window.innerWidth);
const [width, setWidth] = useState(window.innerHeight);
const updateDimensions = useCallback(() => {
setHeight(window.innerHeight);
setWidth(window.innerWidth);
},[])
useEffect(() => {
window.addEventListener('resize', updateDimensions);
}, []);
useEffect(() => {
updateDimensions();
return () => window.removeEventListener('resize', updateDimensions);
}, [])
return <SomeChart width={width/2} height={height/2} ></SomeChart>
}