@@ -14,8 +14,33 @@ type PlotProps = {
1414
1515type LabelPosition = "top" | "bottom" | "left" | "right"
1616
17+ // Constants for chart configuration
18+ const MAX_COST_LIMIT = 60
19+ const CHART_ROUNDING_INTERVAL = 5
20+
21+ // Constants for overlap detection thresholds
22+ const OVERLAP_THRESHOLDS = {
23+ horizontal : 4 , // Cost units
24+ vertical : 5 , // Score units
25+ labelToPoint : {
26+ horizontal : 3 ,
27+ vertical : 6 ,
28+ } ,
29+ } as const
30+
31+ // Helper function to calculate rounded domain bounds
32+ const calculateRoundedDomain = (
33+ min : number ,
34+ max : number ,
35+ interval : number = CHART_ROUNDING_INTERVAL ,
36+ ) : [ number , number ] => {
37+ const roundedMin = Math . max ( 0 , Math . round ( ( min - interval ) / interval ) * interval )
38+ const roundedMax = Math . round ( ( max + interval ) / interval ) * interval
39+ return [ roundedMin , roundedMax ]
40+ }
41+
1742export const Plot = ( { tableData } : PlotProps ) => {
18- const chartData = useMemo ( ( ) => tableData . filter ( ( { cost } ) => cost < 60 ) , [ tableData ] )
43+ const chartData = useMemo ( ( ) => tableData . filter ( ( { cost } ) => cost < MAX_COST_LIMIT ) , [ tableData ] )
1944
2045 const chartConfig = useMemo (
2146 ( ) => chartData . reduce ( ( acc , run ) => ( { ...acc , [ run . label ] : run } ) , { } as ChartConfig ) ,
@@ -39,21 +64,21 @@ export const Plot = ({ tableData }: PlotProps) => {
3964 p1 : { cost : number ; score : number ; position : LabelPosition } ,
4065 p2 : { cost : number ; score : number ; position : LabelPosition } ,
4166 ) : boolean => {
42- // Approximate thresholds for overlap detection.
43- const horizontalThreshold = 4 // Cost units.
44- const verticalThreshold = 5 // Score units.
45-
4667 const costDiff = Math . abs ( p1 . cost - p2 . cost )
4768 const scoreDiff = Math . abs ( p1 . score - p2 . score )
4869
4970 // If points are far apart, no overlap.
50- if ( costDiff > horizontalThreshold * 2 || scoreDiff > verticalThreshold * 2 ) {
71+ if ( costDiff > OVERLAP_THRESHOLDS . horizontal * 2 || scoreDiff > OVERLAP_THRESHOLDS . vertical * 2 ) {
5172 return false
5273 }
5374
5475 // Check specific position combinations for overlap.
5576 // Same position for nearby points definitely overlaps.
56- if ( p1 . position === p2 . position && costDiff < horizontalThreshold && scoreDiff < verticalThreshold ) {
77+ if (
78+ p1 . position === p2 . position &&
79+ costDiff < OVERLAP_THRESHOLDS . horizontal &&
80+ scoreDiff < OVERLAP_THRESHOLDS . vertical
81+ ) {
5782 return true
5883 }
5984
@@ -66,7 +91,7 @@ export const Plot = ({ tableData }: PlotProps) => {
6691 // If both labels are on the same vertical side and points are close
6792 // horizontally.
6893 if ( ( p1IsTop && p2IsTop ) || ( p1IsBottom && p2IsBottom ) ) {
69- if ( costDiff < horizontalThreshold && scoreDiff < verticalThreshold / 2 ) {
94+ if ( costDiff < OVERLAP_THRESHOLDS . horizontal && scoreDiff < OVERLAP_THRESHOLDS . vertical / 2 ) {
7095 return true
7196 }
7297 }
@@ -88,25 +113,41 @@ export const Plot = ({ tableData }: PlotProps) => {
88113 switch ( position ) {
89114 case "top" :
90115 // Label is above, check if there's a point above.
91- if ( costDiff < 3 && other . score > point . score && other . score - point . score < 6 ) {
116+ if (
117+ costDiff < OVERLAP_THRESHOLDS . labelToPoint . horizontal &&
118+ other . score > point . score &&
119+ other . score - point . score < OVERLAP_THRESHOLDS . labelToPoint . vertical
120+ ) {
92121 return true
93122 }
94123 break
95124 case "bottom" :
96125 // Label is below, check if there's a point below.
97- if ( costDiff < 3 && other . score < point . score && point . score - other . score < 6 ) {
126+ if (
127+ costDiff < OVERLAP_THRESHOLDS . labelToPoint . horizontal &&
128+ other . score < point . score &&
129+ point . score - other . score < OVERLAP_THRESHOLDS . labelToPoint . vertical
130+ ) {
98131 return true
99132 }
100133 break
101134 case "left" :
102135 // Label is to the left, check if there's a point to the left.
103- if ( scoreDiff < 3 && other . cost < point . cost && point . cost - other . cost < 4 ) {
136+ if (
137+ scoreDiff < OVERLAP_THRESHOLDS . labelToPoint . horizontal &&
138+ other . cost < point . cost &&
139+ point . cost - other . cost < OVERLAP_THRESHOLDS . horizontal
140+ ) {
104141 return true
105142 }
106143 break
107144 case "right" :
108145 // Label is to the right, check if there's a point to the right.
109- if ( scoreDiff < 3 && other . cost > point . cost && other . cost - point . cost < 4 ) {
146+ if (
147+ scoreDiff < OVERLAP_THRESHOLDS . labelToPoint . horizontal &&
148+ other . cost > point . cost &&
149+ other . cost - point . cost < OVERLAP_THRESHOLDS . horizontal
150+ ) {
110151 return true
111152 }
112153 break
@@ -181,8 +222,8 @@ export const Plot = ({ tableData }: PlotProps) => {
181222 dataKey = "cost"
182223 name = "Cost"
183224 domain = { [
184- ( dataMin : number ) => Math . max ( 0 , Math . round ( ( dataMin - 5 ) / 5 ) * 5 ) ,
185- ( dataMax : number ) => Math . round ( ( dataMax + 5 ) / 5 ) * 5 ,
225+ ( dataMin : number ) => calculateRoundedDomain ( dataMin , 0 ) [ 0 ] ,
226+ ( ) => MAX_COST_LIMIT , // Always use 60 as the max
186227 ] }
187228 tickFormatter = { ( value ) => formatCurrency ( value ) }
188229 />
@@ -191,8 +232,8 @@ export const Plot = ({ tableData }: PlotProps) => {
191232 dataKey = "score"
192233 name = "Score"
193234 domain = { [
194- ( dataMin : number ) => Math . max ( 0 , Math . round ( ( dataMin - 5 ) / 5 ) * 5 ) ,
195- ( dataMax : number ) => Math . min ( 100 , Math . round ( ( dataMax + 5 ) / 5 ) * 5 ) ,
235+ ( dataMin : number ) => calculateRoundedDomain ( dataMin , 0 ) [ 0 ] ,
236+ ( dataMax : number ) => Math . min ( 100 , calculateRoundedDomain ( 0 , dataMax ) [ 1 ] ) ,
196237 ] }
197238 tickFormatter = { ( value ) => `${ value } %` }
198239 />
@@ -235,7 +276,7 @@ export const Plot = ({ tableData }: PlotProps) => {
235276 </ ScatterChart >
236277 </ ChartContainer >
237278 < div className = "py-4 text-xs opacity-50" >
238- (Note: Models with a cost of $60 or more are excluded from the scatter plot.)
279+ (Note: Models with a cost of ${ MAX_COST_LIMIT } or more are excluded from the scatter plot.)
239280 </ div >
240281 </ >
241282 )
0 commit comments