Hi Shivan, Google charts is loading and calling your drawChart callback before your getJSON call returns, so that's why data2 is not defined when you use it. You'll need to combine those two paths into one so you can be sure that your data2 is defined before it is used.
On Thu, Apr 16, 2020 at 10:40 AM Shivan Singh <[email protected]> wrote: > Here is my code > > $(document).ready(function(){ > $.getJSON("https://api.covid19india.org/data.json", function(data){ > $.each(data.statewise, function(key, value){ > if (value.state === "Total") { > var data2 = [['Covid19', 'Stats'], > ['Active cases',value.active], > ['Deaths', value.deaths], > ['recovered', value.recovered],]; > } > }); > }); > }); > > google.charts.load("current", {packages:["corechart"]}); > google.charts.setOnLoadCallback(drawChart); > function drawChart() { > var data = google.visualization.arrayToDataTable(data2); > > var options = { > width:"350", > height:"350", > pieHole: 0.5, > backgroundColor: { fill:"transparent"}, > chartArea:{ > left:10, > right:10, // !!! works !!! > bottom:20, // !!! works !!! > top:20, > width:"100%", > height:"100%" > }, > pieSliceTextStyle: { > color: "white", > }, > legend: {position: "bottom", textStyle: {color: "gray"}}, > > pieSliceText: "none", > }; > > var chart = new > google.visualization.PieChart(document.getElementById("covid19")); > chart.draw(data, options); > } > > > I got error Uncaught (in promise) ReferenceError: data2 is not defined > at drawChart > > -- > You received this message because you are subscribed to the Google Groups > "Google Visualization API" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > To view this discussion on the web visit > https://groups.google.com/d/msgid/google-visualization-api/e06d3ad4-467f-4c54-92c4-e7e88f57e542%40googlegroups.com > <https://groups.google.com/d/msgid/google-visualization-api/e06d3ad4-467f-4c54-92c4-e7e88f57e542%40googlegroups.com?utm_medium=email&utm_source=footer> > . > -- Daniel LaLiberte <https://plus.google.com/100631381223468223275?prsrc=2> [email protected] <[email protected]> Cambridge MA -- You received this message because you are subscribed to the Google Groups "Google Visualization API" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/google-visualization-api/CAOtcSJMOn74_sT62jQDn7e85ezpoPFJS5_qpnUjQk0ks%3D%3DHO0Q%40mail.gmail.com.
