react-native-chart-kit icon indicating copy to clipboard operation
react-native-chart-kit copied to clipboard

PieChart InvalidNumber Error

Open Thembelani opened this issue 5 years ago • 11 comments

Hi guys,

"react-native-chart-kit": "4.1.0", "react-native-svg": "^9.13.5", "prop-types": "^15.7.2",

The Piechart does not render. I get the following error

InvalidNumber: M NaN NaN A 88 88 0 0 1 NaN NaN L NaN NaN A 0 0 0 0 0 NaN NaN Z Simulator Screen Shot - iPhone 11 - 2019-12-10 at 11 55 03

Please help

Thembelani avatar Dec 10 '19 10:12 Thembelani

Can confirm that this happens on a fresh build, too.

stonebinox avatar Jun 24 '20 05:06 stonebinox

Same issue here. Has anyone found a solution? @stonebinox @Thembelani

geroale avatar Jul 04 '20 14:07 geroale

Same issue here. Has anyone found a solution? @stonebinox @Thembelani

It's a shame, really. This looked good but I couldn't get some of the other chart types going either.

Ended up dumping this lib and going with https://www.npmjs.com/package/react-native-svg-charts#piechart

stonebinox avatar Jul 05 '20 04:07 stonebinox

same problem happen in lineChart InvalidNumber: M-Infinity 140.2 64 140.2z

JesseWeb avatar Nov 18 '20 05:11 JesseWeb

same problem happen in lineChart

InvalidNumber: M0,0 L-Infinity,181 L64,181 Z

hengkx avatar Dec 06 '20 06:12 hengkx

I was able to solve this issue by making sure that I had an initial value if my data was referencing a state value that had not yet been set.

For example, my useState looked like this for the data part: const [myData, setMyData] = useState([]);

it would keep failing with this same error until I changed it to have a default value like this, to match my labels: const [myData, setMyData] = useState([0, 0, 0, 0, 0, 0]);

imoby avatar Dec 09 '20 15:12 imoby

The better and more elegant solution that I settled with which has been a common fix with these strange errors related to chart/SVG rendering in react native is to set a loading boolean key along with your data in the state. The main idea is that you don't render the chart/svg until that loading variable becomes false. Try this:

You would expect the default empty array would work in useState([ ]) by simply showing a blank chart briefly, but turns out that this causes most chart rendering libraries in the react native realm to break.

Try something like the code below, this is a generic answer and not strictly related to this package. That's why I have used a generic ChartOrSVG component instead but I hope you will understand my point. Forgive me if I am making some mistake. I am not the best developer when it comes to React concepts.

const [values,setValues] = useState({data:[],loading:true}) useEffect(()=>{ fetchData().then(data=>{ setData({data:data , loading:false}) }) },[]), return ( <View> {loading? null : <ChartOrSVG data={values.data} /> </View> )

sudo-vaibhav avatar Jan 18 '21 22:01 sudo-vaibhav

I was able to solve this issue by making sure that I had an initial value if my data was referencing a state value that had not yet been set.

For example, my useState looked like this for the data part: const [myData, setMyData] = useState([]);

it would keep failing with this same error until I changed it to have a default value like this, to match my labels: const [myData, setMyData] = useState([0, 0, 0, 0, 0, 0]);

Hi, that's did not work

vahidinline avatar Feb 11 '22 12:02 vahidinline

same problem happen in lineChart InvalidNumber: M-Infinity 140.2 64 140.2z

did the issue get solved ??

wilsonfurtado2000 avatar Mar 02 '22 09:03 wilsonfurtado2000

I just got this issue too in 2022 :)

jetonk avatar May 31 '22 22:05 jetonk

@jetonk @wilsonfurtado2000 try @sudo-vaibhav solution or apply it at your specific case, the core of this problem is that the engine try to make chart before all data has been set. The problem is useState it is async event queued on event loop executed after the chart render, use a loading state to mange render.

AndreaRettaroli avatar Oct 07 '22 08:10 AndreaRettaroli

This error occurs when I try to set the progress of the chart to the value 0 / 0 that is, zero divided by zero. I solved this by doing a 0 / Math.max(variable, 1).

homit-dalia avatar Mar 18 '24 10:03 homit-dalia