|
| 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: (string | number)[]; |
| 8 | +} |
| 9 | + |
| 10 | +const margin = { top: 30, right: 50, bottom: 60, left: 50 }; // Increased bottom margin for rotated labels |
| 11 | +const height = 300 - margin.top - margin.bottom; |
| 12 | + |
| 13 | +const CountBarChart = ({ column, realData }: CountBarChartProps) => { |
| 14 | + const svgRef = useRef<SVGSVGElement>(null); |
| 15 | + const containerRef = useRef<HTMLDivElement>(null); |
| 16 | + const [containerWidth, setContainerWidth] = useState(800); |
| 17 | + const { t } = useTranslation(); |
| 18 | + |
| 19 | + useEffect(() => { |
| 20 | + const plotWidth = containerWidth - margin.left - margin.right; |
| 21 | + const plotHeight = height - margin.top - margin.bottom; |
| 22 | + |
| 23 | + // Determine if data is categorical or numerical |
| 24 | + const isNumerical = realData.every(d => typeof d === 'number'); |
| 25 | + |
| 26 | + // Process data based on type |
| 27 | + let processedData; |
| 28 | + if (isNumerical) { |
| 29 | + // For numerical data, create bins |
| 30 | + const numericData = realData as number[]; |
| 31 | + const bins = d3 |
| 32 | + .bin() |
| 33 | + .domain(d3.extent(numericData) as [number, number]) |
| 34 | + .thresholds(10)(numericData); |
| 35 | + |
| 36 | + processedData = bins.map(bin => ({ |
| 37 | + key: `${bin.x0?.toFixed(2)} - ${bin.x1?.toFixed(2)}`, |
| 38 | + value: bin.length, |
| 39 | + })); |
| 40 | + } else { |
| 41 | + // For categorical data, count occurrences |
| 42 | + const counts = d3.rollup( |
| 43 | + realData, |
| 44 | + v => v.length, |
| 45 | + d => d |
| 46 | + ); |
| 47 | + processedData = Array.from(counts, ([key, value]) => ({ |
| 48 | + key, |
| 49 | + value, |
| 50 | + })); |
| 51 | + } |
| 52 | + |
| 53 | + // Clear previous content |
| 54 | + d3.select(svgRef.current).selectAll('*').remove(); |
| 55 | + |
| 56 | + // Create SVG |
| 57 | + const svg = d3 |
| 58 | + .select(svgRef.current) |
| 59 | + .attr('width', containerWidth) |
| 60 | + .attr('height', height + margin.top + margin.bottom) |
| 61 | + .append('g') |
| 62 | + .attr('transform', `translate(${margin.left},${margin.top})`); |
| 63 | + |
| 64 | + // Create scales |
| 65 | + const xScale = d3 |
| 66 | + .scaleBand() |
| 67 | + .domain(processedData.map(d => String(d.key))) |
| 68 | + .range([0, plotWidth]) |
| 69 | + .padding(0.1); |
| 70 | + |
| 71 | + const yScale = d3 |
| 72 | + .scaleLinear() |
| 73 | + .domain([0, d3.max(processedData, d => d.value) || 0]) |
| 74 | + .range([plotHeight, 0]); |
| 75 | + |
| 76 | + // Add bars |
| 77 | + svg.selectAll('.bar') |
| 78 | + .data(processedData) |
| 79 | + .enter() |
| 80 | + .append('rect') |
| 81 | + .attr('class', 'bar') |
| 82 | + .attr('x', d => xScale(String(d.key)) || 0) |
| 83 | + .attr('y', d => yScale(d.value)) |
| 84 | + .attr('width', xScale.bandwidth()) |
| 85 | + .attr('height', d => plotHeight - yScale(d.value)) |
| 86 | + .style('fill', 'steelblue') |
| 87 | + .style('opacity', 0.5); |
| 88 | + |
| 89 | + // Add axes |
| 90 | + svg.append('g') |
| 91 | + .attr('transform', `translate(0,${plotHeight})`) |
| 92 | + .call(d3.axisBottom(xScale)) |
| 93 | + .selectAll('text') |
| 94 | + .attr('transform', 'rotate(-45)') |
| 95 | + .style('text-anchor', 'end') |
| 96 | + .attr('dx', '-.8em') |
| 97 | + .attr('dy', '.15em'); |
| 98 | + |
| 99 | + svg.append('g').call(d3.axisLeft(yScale)); |
| 100 | + |
| 101 | + // Add title |
| 102 | + svg.append('text') |
| 103 | + .attr('x', plotWidth / 2) |
| 104 | + .attr('y', -10) |
| 105 | + .attr('text-anchor', 'middle') |
| 106 | + .style('font-size', '12px') |
| 107 | + .style('font-weight', 'bold') |
| 108 | + .text(`${t('distribution.countFor')} ${column}`); |
| 109 | + }, [containerWidth, column, realData]); |
| 110 | + |
| 111 | + useEffect(() => { |
| 112 | + const resizeObserver = new ResizeObserver(entries => { |
| 113 | + if (!entries || entries.length === 0) return; |
| 114 | + const { width } = entries[0].contentRect; |
| 115 | + setContainerWidth(width); |
| 116 | + }); |
| 117 | + |
| 118 | + if (containerRef.current) { |
| 119 | + resizeObserver.observe(containerRef.current); |
| 120 | + } |
| 121 | + |
| 122 | + return () => { |
| 123 | + if (containerRef.current) { |
| 124 | + resizeObserver.unobserve(containerRef.current); |
| 125 | + } |
| 126 | + }; |
| 127 | + }, []); |
| 128 | + |
| 129 | + return ( |
| 130 | + <div |
| 131 | + ref={containerRef} |
| 132 | + style={{ width: '100%', display: 'flex', overflowX: 'auto' }} |
| 133 | + className={`min-h-[${height}px] flex-col`} |
| 134 | + > |
| 135 | + <svg ref={svgRef}></svg> |
| 136 | + </div> |
| 137 | + ); |
| 138 | +}; |
| 139 | + |
| 140 | +export default CountBarChart; |
0 commit comments