@@ -214,6 +214,25 @@ export default function ChartContainer({
214214 return result ;
215215 } ;
216216
217+ const calculateYRange = useCallback ( ( dataArray ) => {
218+ let min = Infinity ;
219+ let max = - Infinity ;
220+ dataArray . forEach ( item => {
221+ item . data . forEach ( point => {
222+ if ( point . y < min ) min = point . y ;
223+ if ( point . y > max ) max = point . y ;
224+ } ) ;
225+ } ) ;
226+ if ( min === Infinity || max === - Infinity ) {
227+ return { min : 0 , max : 1 } ;
228+ }
229+ if ( min === max ) {
230+ return { min : min - 1 , max : max + 1 } ;
231+ }
232+ const pad = ( max - min ) * 0.05 ;
233+ return { min : min - pad , max : max + pad } ;
234+ } , [ ] ) ;
235+
217236 const chartOptions = useMemo ( ( ) => ( {
218237 responsive : true ,
219238 maintainAspectRatio : false ,
@@ -326,7 +345,6 @@ export default function ChartContainer({
326345 display : true ,
327346 title : { display : true , text : 'Value' } ,
328347 bounds : 'data' ,
329- grace : '5%' ,
330348 ticks : {
331349 callback : function ( value ) {
332350 return Number ( value . toPrecision ( 2 ) ) ;
@@ -429,6 +447,15 @@ export default function ChartContainer({
429447 const dataArray = metricDataArrays [ key ] || [ ] ;
430448 const showComparison = dataArray . length === 2 ;
431449
450+ const yRange = calculateYRange ( dataArray ) ;
451+ const options = {
452+ ...chartOptions ,
453+ scales : {
454+ ...chartOptions . scales ,
455+ y : { ...chartOptions . scales . y , min : yRange . min , max : yRange . max }
456+ }
457+ } ;
458+
432459 let stats = null ;
433460 if ( showComparison ) {
434461 const normalDiff = getComparisonData ( dataArray [ 0 ] . data , dataArray [ 1 ] . data , 'normal' ) ;
@@ -442,6 +469,30 @@ export default function ChartContainer({
442469 } ;
443470 }
444471
472+ let comparisonChart = null ;
473+ if ( showComparison ) {
474+ const compData = createComparisonChartData ( dataArray [ 0 ] , dataArray [ 1 ] , key ) ;
475+ const compRange = calculateYRange ( compData . datasets ) ;
476+ const compOptions = {
477+ ...chartOptions ,
478+ scales : {
479+ ...chartOptions . scales ,
480+ y : { ...chartOptions . scales . y , min : compRange . min , max : compRange . max }
481+ }
482+ } ;
483+ comparisonChart = (
484+ < ResizablePanel title = { `⚖️ ${ key } 对比分析 (${ compareMode } )` } initialHeight = { 440 } >
485+ < ChartWrapper
486+ chartId = { `metric-comp-${ idx } ` }
487+ onRegisterChart = { registerChart }
488+ onSyncHover = { syncHoverToAllCharts }
489+ data = { compData }
490+ options = { compOptions }
491+ />
492+ </ ResizablePanel >
493+ ) ;
494+ }
495+
445496 return (
446497 < div key = { key } className = "flex flex-col gap-3" >
447498 < ResizablePanel title = { key } initialHeight = { 440 } >
@@ -450,20 +501,10 @@ export default function ChartContainer({
450501 onRegisterChart = { registerChart }
451502 onSyncHover = { syncHoverToAllCharts }
452503 data = { createChartData ( dataArray ) }
453- options = { chartOptions }
504+ options = { options }
454505 />
455506 </ ResizablePanel >
456- { showComparison && (
457- < ResizablePanel title = { `⚖️ ${ key } 对比分析 (${ compareMode } )` } initialHeight = { 440 } >
458- < ChartWrapper
459- chartId = { `metric-comp-${ idx } ` }
460- onRegisterChart = { registerChart }
461- onSyncHover = { syncHoverToAllCharts }
462- data = { createComparisonChartData ( dataArray [ 0 ] , dataArray [ 1 ] , key ) }
463- options = { chartOptions }
464- />
465- </ ResizablePanel >
466- ) }
507+ { comparisonChart }
467508 { stats && (
468509 < div className = "bg-white rounded-lg shadow-md p-3" >
469510 < h4 className = "text-sm font-medium text-gray-700 mb-1" > { key } 差值统计</ h4 >
0 commit comments