|
| 1 | +import { useEffect, useRef, useState } from 'react'; |
| 2 | +import * as d3 from 'd3'; |
| 3 | +import { useTranslation } from 'react-i18next'; |
| 4 | + |
| 5 | +interface CountBarChartProps { |
| 6 | + column: string; |
| 7 | + realData: number[]; |
| 8 | +} |
| 9 | + |
| 10 | +// Define margins for the chart |
| 11 | +const margin = { top: 30, right: 50, bottom: 40, left: 50 }; |
| 12 | +// Define height for the chart, adjusting for margins |
| 13 | +const height = 300 - margin.top - margin.bottom; |
| 14 | + |
| 15 | +// Define width of bars and adjust for screenwidth |
| 16 | +// const barWidth = 0.05 * window.innerWidth < 40 ? 40 : 0.05 * window.innerWidth; |
| 17 | +// const barGap = 5; |
| 18 | + |
| 19 | +const CountBarChart = ({ column, realData }: CountBarChartProps) => { |
| 20 | + const svgRef = useRef<SVGSVGElement>(null); // Reference to the SVG element |
| 21 | + const containerRef = useRef<HTMLDivElement>(null); // Reference to the container div |
| 22 | + const [containerWidth, setContainerWidth] = useState(800); // Default container width |
| 23 | + const { t } = useTranslation(); |
| 24 | + useEffect(() => { |
| 25 | + const plotWidth = containerWidth - margin.left - margin.right; |
| 26 | + const plotHeight = height - margin.top - margin.bottom; |
| 27 | + |
| 28 | + const xScale = d3 |
| 29 | + .scaleLinear() |
| 30 | + .domain([d3.min(realData) || 0, d3.max(realData) || 1]) |
| 31 | + .range([0, plotWidth]); |
| 32 | + |
| 33 | + const binsReal = d3 |
| 34 | + .bin() |
| 35 | + .domain(xScale.domain() as [number, number]) |
| 36 | + .thresholds(30)(realData); |
| 37 | + |
| 38 | + // Clear any previous SVG content to avoid overlapping elements |
| 39 | + d3.select(svgRef.current).selectAll('*').remove(); |
| 40 | + |
| 41 | + // Create the SVG container and set its dimensions |
| 42 | + const svg = d3 |
| 43 | + .select(svgRef.current) |
| 44 | + .attr('class', `min-h-[${height}px]`) |
| 45 | + .attr('width', containerWidth) |
| 46 | + .attr('height', height + margin.top + margin.bottom) |
| 47 | + .append('g') |
| 48 | + .attr('transform', `translate(${margin.left},${margin.top})`); |
| 49 | + // Add axes |
| 50 | + svg.append('g') |
| 51 | + .attr('transform', `translate(0, ${plotHeight})`) |
| 52 | + .call(d3.axisBottom(xScale)); |
| 53 | + |
| 54 | + svg.append('defs') |
| 55 | + .append('style') |
| 56 | + .attr('type', 'text/css') |
| 57 | + .text( |
| 58 | + "@import url('https://fonts.googleapis.com/css2?family=Avenir:wght@600');" |
| 59 | + ); |
| 60 | + |
| 61 | + //const realDensityFactor = 1 / realData.length; |
| 62 | + |
| 63 | + const yScale = d3 |
| 64 | + .scaleLinear() |
| 65 | + .domain([0, d3.max([...binsReal.map(bin => bin.length)]) || 1]) |
| 66 | + .range([plotHeight, 0]); |
| 67 | + |
| 68 | + // Add axes |
| 69 | + svg.append('g') |
| 70 | + .attr('transform', `translate(0, ${plotHeight})`) |
| 71 | + .call(d3.axisBottom(xScale)); |
| 72 | + svg.append('g').call(d3.axisLeft(yScale)); |
| 73 | + |
| 74 | + // Draw real data histogram |
| 75 | + svg.selectAll('.bar-real') |
| 76 | + .data(binsReal) |
| 77 | + .enter() |
| 78 | + .append('rect') |
| 79 | + .attr('class', 'bar-real') |
| 80 | + .attr('x', d => xScale(d.x0 || 0)) |
| 81 | + .attr('y', d => yScale(d.length)) |
| 82 | + .attr('width', d => xScale(d.x1 || 0) - xScale(d.x0 || 0) - 1) |
| 83 | + .attr('height', d => plotHeight - yScale(d.length)) |
| 84 | + .style('fill', 'steelblue') |
| 85 | + .style('opacity', 0.5); |
| 86 | + |
| 87 | + // Add title |
| 88 | + svg.append('text') |
| 89 | + .attr('x', plotWidth / 2) |
| 90 | + .attr('y', 10) |
| 91 | + .attr('text-anchor', 'middle') |
| 92 | + .style('font-size', '12px') |
| 93 | + .style('font-weight', 'bold') |
| 94 | + .text(`${t('distribution.distributionFor')} ${column}`); |
| 95 | + }, [containerWidth, column, realData]); |
| 96 | + |
| 97 | + useEffect(() => { |
| 98 | + // Set up the ResizeObserver to track changes in the container's size |
| 99 | + const resizeObserver = new ResizeObserver(entries => { |
| 100 | + if (!entries || entries.length === 0) return; |
| 101 | + const { width } = entries[0].contentRect; |
| 102 | + setContainerWidth(width); // Update the state with the new container width |
| 103 | + }); |
| 104 | + |
| 105 | + if (containerRef.current) { |
| 106 | + resizeObserver.observe(containerRef.current); // Start observing the container |
| 107 | + } |
| 108 | + |
| 109 | + return () => { |
| 110 | + if (containerRef.current) { |
| 111 | + resizeObserver.unobserve(containerRef.current); // Cleanup on component unmount |
| 112 | + } |
| 113 | + }; |
| 114 | + }, []); |
| 115 | + |
| 116 | + // Render the chart container and SVG element with horizontal scroll if needed |
| 117 | + return ( |
| 118 | + <div |
| 119 | + ref={containerRef} |
| 120 | + style={{ width: '100%', display: 'flex', overflowX: 'auto' }} |
| 121 | + className={`min-h-[${height}px] flex-col`} |
| 122 | + > |
| 123 | + <svg ref={svgRef}></svg> |
| 124 | + </div> |
| 125 | + ); |
| 126 | +}; |
| 127 | + |
| 128 | +export default CountBarChart; |
0 commit comments