I am working with D3 charts, with react, I created bar chart and made it responsive.
When I resize the browser the bars are taking width accordingly.
Issue
One thing I want to a achieve is dynamic label, like when I resize the page, the length of label is bit big so it is getting one over other
So I want to overcome this, I googled a lot and found One example, there when it is rendered on full screen it looks like 
And when I resize the browser to a small screen !it looks like this]3.
For above this is the reference I found
So same I want to achieve but not getting any idea.
What I did
import React, { useRef, useEffect, useState } from 'react';
import { select, axisBottom, axisRight, scaleLinear, scaleBand } from 'd3';
import ResizeObserver from 'resize-observer-polyfill';
const useResizeObserver = (ref) => { // doing this for resizing
const [dimensions, setDimensions] = useState(null);
useEffect(() => {
const observeTarget = ref.current;
const resizeObserver = new ResizeObserver((entries) => {
entries.forEach((entry) => {
setDimensions(entry.contentRect);
});
});
resizeObserver.observe(observeTarget);
return () => {
resizeObserver.unobserve(observeTarget);
};
}, [ref]);
return dimensions;
};
function BarChart({ data }) {
const svgRef = useRef();
const wrapperRef = useRef();
const dimensions = useResizeObserver(wrapperRef);
// will be called initially and on every data change
useEffect(() => {
const svg = select(svgRef.current);
if (!dimensions) return;
// scales
const xScale = scaleBand()
.domain(data.map((value, index) => index + '2021+01+06'))
.range([0, dimensions.width]) // change
.padding(0.5);
const yScale = scaleLinear()
.domain([0, 150]) // todo
.range([dimensions.height, 0]); // change
// create x-axis
const xAxis = axisBottom(xScale).ticks(data.length);
svg
.select('.x-axis')
.style('transform', `translateY(${dimensions.height}px)`)
.call(xAxis);
// create y-axis
const yAxis = axisRight(yScale);
svg
.select('.y-axis')
.style('transform', `translateX(${dimensions.width}px)`)
.call(yAxis);
// draw the bars
svg
.selectAll('.bar')
.data(data)
.join('rect')
.attr('class', 'bar')
.style('transform', 'scale(1, -1)')
.attr('x', (value, index) => xScale(index + '2021+01+06'))
.attr('y', -dimensions.height)
.attr('width', xScale.bandwidth())
.transition()
.attr('fill', '#015e89')
.attr('height', (value) => dimensions.height - yScale(value));
}, [data, dimensions]);
return (
<div ref={wrapperRef} style={{ marginBottom: '2rem' }}>
<svg ref={svgRef}>
<g className="x-axis" />
<g className="y-axis" />
</svg>
</div>
);
}
export default BarChart;
here is my codesandbox
