@@ -76,14 +76,6 @@ const DistributionBarChart = ({
7676 . domain ( xScale . domain ( ) as [ number , number ] )
7777 . thresholds ( 30 ) ( syntheticData ) ;
7878
79- const yScale = d3
80- . scaleLinear ( )
81- . domain ( [
82- 0 ,
83- d3 . max ( [ ...binsReal , ...binsSynthetic ] , d => d . length ) || 1 ,
84- ] )
85- . range ( [ plotHeight , 0 ] ) ;
86-
8779 // Clear any previous SVG content to avoid overlapping elements
8880 d3 . select ( svgRef . current ) . selectAll ( '*' ) . remove ( ) ;
8981
@@ -99,7 +91,6 @@ const DistributionBarChart = ({
9991 svg . append ( 'g' )
10092 . attr ( 'transform' , `translate(0, ${ plotHeight } )` )
10193 . call ( d3 . axisBottom ( xScale ) ) ;
102- svg . append ( 'g' ) . call ( d3 . axisLeft ( yScale ) ) ;
10394
10495 svg . append ( 'defs' )
10596 . append ( 'style' )
@@ -108,16 +99,41 @@ const DistributionBarChart = ({
10899 "@import url('https://fonts.googleapis.com/css2?family=Avenir:wght@600');"
109100 ) ;
110101
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+
111124 // Draw real data histogram
112125 svg . selectAll ( '.bar-real' )
113126 . data ( binsReal )
114127 . enter ( )
115128 . append ( 'rect' )
116129 . attr ( 'class' , 'bar-real' )
117130 . attr ( 'x' , d => xScale ( d . x0 || 0 ) )
118- . attr ( 'y' , d => yScale ( d . length ) )
131+ . attr ( 'y' , d => yScale ( d . length * realDensityFactor ) )
119132 . attr ( 'width' , d => xScale ( d . x1 || 0 ) - xScale ( d . x0 || 0 ) - 1 )
120- . attr ( 'height' , d => plotHeight - yScale ( d . length ) )
133+ . attr (
134+ 'height' ,
135+ d => plotHeight - yScale ( d . length * realDensityFactor )
136+ )
121137 . style ( 'fill' , 'steelblue' )
122138 . style ( 'opacity' , 0.5 ) ;
123139
@@ -128,9 +144,12 @@ const DistributionBarChart = ({
128144 . append ( 'rect' )
129145 . attr ( 'class' , 'bar-synthetic' )
130146 . attr ( 'x' , d => xScale ( d . x0 || 0 ) )
131- . attr ( 'y' , d => yScale ( d . length ) )
147+ . attr ( 'y' , d => yScale ( d . length * syntheticDensityFactor ) )
132148 . attr ( 'width' , d => xScale ( d . x1 || 0 ) - xScale ( d . x0 || 0 ) - 1 )
133- . attr ( 'height' , d => plotHeight - yScale ( d . length ) )
149+ . attr (
150+ 'height' ,
151+ d => plotHeight - yScale ( d . length * syntheticDensityFactor )
152+ )
134153 . style ( 'fill' , 'orange' )
135154 . style ( 'opacity' , 0.5 ) ;
136155
0 commit comments