@@ -46,6 +46,10 @@ class AbstractChart<
4646> extends Component < AbstractChartProps & IProps , AbstractChartState & IState > {
4747 private chartId = nextChartId ++ ;
4848
49+ protected getValidData = ( data : number [ ] = [ ] ) => {
50+ return data . filter ( value => typeof value === "number" && isFinite ( value ) ) ;
51+ } ;
52+
4953 protected getGradientId = ( id : string ) => {
5054 return `chart-kit-${ this . chartId } -${ id } ` ;
5155 } ;
@@ -55,48 +59,70 @@ class AbstractChart<
5559 } ;
5660
5761 calcScaler = ( data : number [ ] ) => {
62+ const values = this . getValidData ( data ) ;
63+
64+ if ( values . length === 0 ) {
65+ return 1 ;
66+ }
67+
5868 if ( this . props . fromZero && this . props . fromNumber ) {
5969 return (
60- Math . max ( ...data , this . props . fromNumber ) - Math . min ( ...data , 0 ) || 1
70+ Math . max ( ...values , this . props . fromNumber ) - Math . min ( ...values , 0 ) || 1
6171 ) ;
6272 } else if ( this . props . fromZero ) {
63- return Math . max ( ...data , 0 ) - Math . min ( ...data , 0 ) || 1 ;
73+ return Math . max ( ...values , 0 ) - Math . min ( ...values , 0 ) || 1 ;
6474 } else if ( this . props . fromNumber ) {
6575 return (
66- Math . max ( ...data , this . props . fromNumber ) -
67- Math . min ( ...data , this . props . fromNumber ) || 1
76+ Math . max ( ...values , this . props . fromNumber ) -
77+ Math . min ( ...values , this . props . fromNumber ) || 1
6878 ) ;
6979 } else {
70- return Math . max ( ...data ) - Math . min ( ...data ) || 1 ;
80+ return Math . max ( ...values ) - Math . min ( ...values ) || 1 ;
7181 }
7282 } ;
7383
7484 calcBaseHeight = ( data : number [ ] , height : number ) => {
75- const min = Math . min ( ...data ) ;
76- const max = Math . max ( ...data ) ;
85+ const values = this . getValidData ( data ) ;
86+
87+ if ( values . length === 0 ) {
88+ return height ;
89+ }
90+
91+ const min = Math . min ( ...values ) ;
92+ const max = Math . max ( ...values ) ;
7793 if ( min >= 0 && max >= 0 ) {
7894 return height ;
7995 } else if ( min < 0 && max <= 0 ) {
8096 return 0 ;
8197 } else if ( min < 0 && max > 0 ) {
82- return ( height * max ) / this . calcScaler ( data ) ;
98+ return ( height * max ) / this . calcScaler ( values ) ;
8399 }
84100 } ;
85101
86102 calcHeight = ( val : number , data : number [ ] , height : number ) => {
87- const max = Math . max ( ...data ) ;
88- const min = Math . min ( ...data ) ;
103+ if ( typeof val !== "number" || ! isFinite ( val ) ) {
104+ return 0 ;
105+ }
106+
107+ const values = this . getValidData ( data ) ;
108+
109+ if ( values . length === 0 ) {
110+ return 0 ;
111+ }
112+
113+ const max = Math . max ( ...values ) ;
114+ const min = Math . min ( ...values ) ;
89115
90116 if ( min < 0 && max > 0 ) {
91- return height * ( val / this . calcScaler ( data ) ) ;
117+ return height * ( val / this . calcScaler ( values ) ) ;
92118 } else if ( min >= 0 && max >= 0 ) {
93119 return this . props . fromZero
94- ? height * ( val / this . calcScaler ( data ) )
95- : height * ( ( val - min ) / this . calcScaler ( data ) ) ;
120+ ? height * ( val / this . calcScaler ( values ) )
121+ : height * ( ( val - min ) / this . calcScaler ( values ) ) ;
96122 } else if ( min < 0 && max <= 0 ) {
97123 return this . props . fromZero
98- ? height * ( val / this . calcScaler ( data ) )
99- : height * ( ( val - max ) / this . calcScaler ( data ) ) ;
124+ ? height * ( val / this . calcScaler ( values ) )
125+ : height * ( ( val - max ) / this . calcScaler ( values ) ) ;
100126 }
101127 } ;
102128
@@ -207,6 +233,8 @@ class AbstractChart<
207233 formatYLabel = ( yLabel : string ) => yLabel ,
208234 verticalLabelsHeightPercentage = DEFAULT_X_LABELS_HEIGHT_PERCENTAGE
209235 } = config ;
236+ const values = this . getValidData ( data ) ;
237+ const labelData = values . length === 0 ? [ 0 ] : values ;
210238
211239 const {
212240 yAxisLabel = "" ,
@@ -218,12 +246,12 @@ class AbstractChart<
218246
219247 if ( count === 1 ) {
220248 yLabel = `${ yAxisLabel } ${ formatYLabel (
221- data [ 0 ] . toFixed ( decimalPlaces )
249+ labelData [ 0 ] . toFixed ( decimalPlaces )
222250 ) } ${ yAxisSuffix } `;
223251 } else {
224252 const label = this . props . fromZero
225- ? ( this . calcScaler ( data ) / count ) * i + Math . min ( ...data , 0 )
226- : ( this . calcScaler ( data ) / count ) * i + Math . min ( ...data ) ;
253+ ? ( this . calcScaler ( labelData ) / count ) * i + Math . min ( ...labelData , 0 )
254+ : ( this . calcScaler ( labelData ) / count ) * i + Math . min ( ...labelData ) ;
227255 yLabel = `${ yAxisLabel } ${ formatYLabel (
228256 label . toFixed ( decimalPlaces )
229257 ) } ${ yAxisSuffix } `;
0 commit comments