@@ -80,11 +80,34 @@ function ChartsAxisHighlight(props: ChartsAxisHighlightProps) {
8080
8181 const getXPosition = getValueToPositionMapper ( xScale ) ;
8282 const getYPosition = getValueToPositionMapper ( yScale ) ;
83+
84+ const axisX = axis . x ;
85+ const axisY = axis . y ;
86+
87+ const isBandScaleX = xAxisHighlight === 'band' && axisX !== null && isBandScale ( xScale ) ;
88+ const isBandScaleY = yAxisHighlight === 'band' && axisY !== null && isBandScale ( yScale ) ;
89+
90+ if ( process . env . NODE_ENV !== 'production' ) {
91+ const isXError = isBandScaleX && xScale ( axisX . value ) === undefined ;
92+ const isYError = isBandScaleY && yScale ( axisY . value ) === undefined ;
93+
94+ if ( isXError || isYError ) {
95+ console . error (
96+ [
97+ `MUI X Charts: The position value provided for the axis is not valid for the current scale.` ,
98+ `This probably means something is wrong with the data passed to the chart.` ,
99+ `The ChartsAxisHighlight component will not be displayed.` ,
100+ ] . join ( '\n' ) ,
101+ ) ;
102+ }
103+ }
104+
83105 return (
84106 < React . Fragment >
85- { xAxisHighlight === 'band' && axis . x !== null && isBandScale ( xScale ) && (
107+ { isBandScaleX && xScale ( axisX . value ) !== undefined && (
86108 < ChartsAxisHighlightPath
87- d = { `M ${ xScale ( axis . x . value ) ! - ( xScale . step ( ) - xScale . bandwidth ( ) ) / 2 } ${
109+ // @ts -expect-error, xScale value is checked in the statement above
110+ d = { `M ${ xScale ( axisX . value ) - ( xScale . step ( ) - xScale . bandwidth ( ) ) / 2 } ${
88111 yScale . range ( ) [ 0 ]
89112 } l ${ xScale . step ( ) } 0 l 0 ${
90113 yScale . range ( ) [ 1 ] - yScale . range ( ) [ 0 ]
@@ -94,10 +117,11 @@ function ChartsAxisHighlight(props: ChartsAxisHighlightProps) {
94117 />
95118 ) }
96119
97- { yAxisHighlight === 'band' && axis . y !== null && isBandScale ( yScale ) && (
120+ { isBandScaleY && yScale ( axisY . value ) === undefined && (
98121 < ChartsAxisHighlightPath
99122 d = { `M ${ xScale . range ( ) [ 0 ] } ${
100- yScale ( axis . y . value ) ! - ( yScale . step ( ) - yScale . bandwidth ( ) ) / 2
123+ // @ts -expect-error, yScale value is checked in the statement above
124+ yScale ( axisY . value ) - ( yScale . step ( ) - yScale . bandwidth ( ) ) / 2
101125 } l 0 ${ yScale . step ( ) } l ${
102126 xScale . range ( ) [ 1 ] - xScale . range ( ) [ 0 ]
103127 } 0 l 0 ${ - yScale . step ( ) } Z`}
0 commit comments