|
| 1 | +import { useEffect, useRef, useState } from 'react'; |
| 2 | +import * as d3 from 'd3'; |
| 3 | + |
| 4 | +interface DistributionBarChartProps { |
| 5 | + column: string; |
| 6 | + realData: number[]; |
| 7 | + syntheticData: number[]; |
| 8 | +} |
| 9 | + |
| 10 | +// Define margins for the chart |
| 11 | +const margin = { top: 10, 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 DistributionBarChart = ({ |
| 20 | + column, |
| 21 | + realData, |
| 22 | + syntheticData, |
| 23 | +}: DistributionBarChartProps) => { |
| 24 | + const svgRef = useRef<SVGSVGElement>(null); // Reference to the SVG element |
| 25 | + const containerRef = useRef<HTMLDivElement>(null); // Reference to the container div |
| 26 | + const [containerWidth, setContainerWidth] = useState(800); // Default container width |
| 27 | + |
| 28 | + // Create x-axis scale using d3.scaleBand, with padding for spacing between bars |
| 29 | + // const x0 = useMemo( |
| 30 | + // () => |
| 31 | + // d3 |
| 32 | + // .scaleBand() |
| 33 | + // .domain(data.map(d => d.name)) |
| 34 | + // .range([ |
| 35 | + // 0, |
| 36 | + // Math.max( |
| 37 | + // containerWidth - margin.right, |
| 38 | + // data.length * (barWidth + barGap) |
| 39 | + // ), |
| 40 | + // ]) |
| 41 | + // .padding(0.2), |
| 42 | + // [data, containerWidth] |
| 43 | + // ); |
| 44 | + |
| 45 | + // // Create y-axis scale using d3.scaleLinear, with a range from the height to 0 |
| 46 | + // const y = useMemo( |
| 47 | + // () => |
| 48 | + // d3 |
| 49 | + // .scaleLinear() |
| 50 | + // .domain([ |
| 51 | + // d3.min(data, d => d.values) ?? 0, // Minimum value in the dataset (or 0 if undefined) |
| 52 | + // d3.max(data, d => d.values) ?? 0, // Maximum value in the dataset (or 0 if undefined) |
| 53 | + // ]) |
| 54 | + // .nice() // Rounds the domain to nice round values |
| 55 | + // .range([height, 0]), |
| 56 | + // [data] |
| 57 | + // ); |
| 58 | + |
| 59 | + useEffect(() => { |
| 60 | + const plotWidth = containerWidth - margin.left - margin.right; |
| 61 | + const plotHeight = height - margin.top - margin.bottom; |
| 62 | + |
| 63 | + const combinedData = [...realData, ...syntheticData]; |
| 64 | + const xScale = d3 |
| 65 | + .scaleLinear() |
| 66 | + .domain([d3.min(combinedData) || 0, d3.max(combinedData) || 1]) |
| 67 | + .range([0, plotWidth]); |
| 68 | + |
| 69 | + const binsReal = d3 |
| 70 | + .bin() |
| 71 | + .domain(xScale.domain() as [number, number]) |
| 72 | + .thresholds(30)(realData); |
| 73 | + |
| 74 | + const binsSynthetic = d3 |
| 75 | + .bin() |
| 76 | + .domain(xScale.domain() as [number, number]) |
| 77 | + .thresholds(30)(syntheticData); |
| 78 | + |
| 79 | + // Clear any previous SVG content to avoid overlapping elements |
| 80 | + d3.select(svgRef.current).selectAll('*').remove(); |
| 81 | + |
| 82 | + // Create the SVG container and set its dimensions |
| 83 | + const svg = d3 |
| 84 | + .select(svgRef.current) |
| 85 | + .attr('class', `min-h-[${height}px]`) |
| 86 | + .attr('width', containerWidth) |
| 87 | + .attr('height', height + margin.top + margin.bottom) |
| 88 | + .append('g') |
| 89 | + .attr('transform', `translate(${margin.left},${margin.top})`); |
| 90 | + // Add axes |
| 91 | + svg.append('g') |
| 92 | + .attr('transform', `translate(0, ${plotHeight})`) |
| 93 | + .call(d3.axisBottom(xScale)); |
| 94 | + |
| 95 | + svg.append('defs') |
| 96 | + .append('style') |
| 97 | + .attr('type', 'text/css') |
| 98 | + .text( |
| 99 | + "@import url('https://fonts.googleapis.com/css2?family=Avenir:wght@600');" |
| 100 | + ); |
| 101 | + |
| 102 | + const realDensityFactor = 1 / realData.length; |
| 103 | + const syntheticDensityFactor = 1 / syntheticData.length; |
| 104 | + |
| 105 | + const yScale = d3 |
| 106 | + .scaleLinear() |
| 107 | + .domain([ |
| 108 | + 0, |
| 109 | + d3.max([ |
| 110 | + ...binsReal.map(bin => bin.length * realDensityFactor), |
| 111 | + ...binsSynthetic.map( |
| 112 | + bin => bin.length * syntheticDensityFactor |
| 113 | + ), |
| 114 | + ]) || 1, |
| 115 | + ]) |
| 116 | + .range([plotHeight, 0]); |
| 117 | + |
| 118 | + // Add axes |
| 119 | + svg.append('g') |
| 120 | + .attr('transform', `translate(0, ${plotHeight})`) |
| 121 | + .call(d3.axisBottom(xScale)); |
| 122 | + svg.append('g').call(d3.axisLeft(yScale)); |
| 123 | + |
| 124 | + // Draw real data histogram |
| 125 | + svg.selectAll('.bar-real') |
| 126 | + .data(binsReal) |
| 127 | + .enter() |
| 128 | + .append('rect') |
| 129 | + .attr('class', 'bar-real') |
| 130 | + .attr('x', d => xScale(d.x0 || 0)) |
| 131 | + .attr('y', d => yScale(d.length * realDensityFactor)) |
| 132 | + .attr('width', d => xScale(d.x1 || 0) - xScale(d.x0 || 0) - 1) |
| 133 | + .attr( |
| 134 | + 'height', |
| 135 | + d => plotHeight - yScale(d.length * realDensityFactor) |
| 136 | + ) |
| 137 | + .style('fill', 'steelblue') |
| 138 | + .style('opacity', 0.5); |
| 139 | + |
| 140 | + // Draw synthetic data histogram |
| 141 | + svg.selectAll('.bar-synthetic') |
| 142 | + .data(binsSynthetic) |
| 143 | + .enter() |
| 144 | + .append('rect') |
| 145 | + .attr('class', 'bar-synthetic') |
| 146 | + .attr('x', d => xScale(d.x0 || 0)) |
| 147 | + .attr('y', d => yScale(d.length * syntheticDensityFactor)) |
| 148 | + .attr('width', d => xScale(d.x1 || 0) - xScale(d.x0 || 0) - 1) |
| 149 | + .attr( |
| 150 | + 'height', |
| 151 | + d => plotHeight - yScale(d.length * syntheticDensityFactor) |
| 152 | + ) |
| 153 | + .style('fill', 'orange') |
| 154 | + .style('opacity', 0.5); |
| 155 | + |
| 156 | + // Add title |
| 157 | + svg.append('text') |
| 158 | + .attr('x', plotWidth / 2) |
| 159 | + .attr('y', 10) |
| 160 | + .attr('text-anchor', 'middle') |
| 161 | + .style('font-size', '12px') |
| 162 | + .style('font-weight', 'bold') |
| 163 | + .text(`Distribution for ${column}`); |
| 164 | + |
| 165 | + // Add a legend label for the mean line |
| 166 | + }, [containerWidth, column, realData, syntheticData]); |
| 167 | + |
| 168 | + useEffect(() => { |
| 169 | + // Set up the ResizeObserver to track changes in the container's size |
| 170 | + const resizeObserver = new ResizeObserver(entries => { |
| 171 | + if (!entries || entries.length === 0) return; |
| 172 | + const { width } = entries[0].contentRect; |
| 173 | + setContainerWidth(width); // Update the state with the new container width |
| 174 | + }); |
| 175 | + |
| 176 | + if (containerRef.current) { |
| 177 | + resizeObserver.observe(containerRef.current); // Start observing the container |
| 178 | + } |
| 179 | + |
| 180 | + return () => { |
| 181 | + if (containerRef.current) { |
| 182 | + resizeObserver.unobserve(containerRef.current); // Cleanup on component unmount |
| 183 | + } |
| 184 | + }; |
| 185 | + }, []); |
| 186 | + |
| 187 | + // Render the chart container and SVG element with horizontal scroll if needed |
| 188 | + return ( |
| 189 | + <div |
| 190 | + ref={containerRef} |
| 191 | + style={{ width: '100%', display: 'flex', overflowX: 'auto' }} |
| 192 | + className={`min-h-[${height}px] flex-col`} |
| 193 | + > |
| 194 | + <svg ref={svgRef}></svg> |
| 195 | + </div> |
| 196 | + ); |
| 197 | +}; |
| 198 | + |
| 199 | +export default DistributionBarChart; |
0 commit comments