简体   繁体   中英

How to get Jquery data to google pie chart?

I am trying to use my data for google Pie chart but its not working.

When I am using this code

 var data = google.visualization.arrayToDataTable([

        ['ProductCategory', 'ItemsSold'],
        [dit[0].CategoryName, dit[0].ItemsSold],
        [dit[1].CategoryName, dit[1].ItemsSold],            
        [dit[2].CategoryName, dit[2].ItemsSold]
    ]);

It works as I am putting things statically, but when I am using my data in the loop and bind this way then no data appears in the chart

   var data1=[];
     var Header= ['ProductCategory', 'ItemsSold'];
     data1.push(Header);
    for (var i = 0; i < dit.length; i++) {
        var temp = [];
         temp.push(dit[i].CategoryName);
         temp.push(parseInt(dit[i].ItemsSold));
         data1.push(temp);


    }

    var data = google.visualization.arrayToDataTable([data1]);

Here, dit is my dictionary with CategoryName and ItemsSold.

One thing which I noticed is that when I am trying to check "data" in console, the header for former one had 2 types string and int but for the latter, the header contained my whole pairs of data as well.

Like this for when I used loop

Like this for when I use the 1st way and it works

since you're starting with an array, here...

var data1=[];

you do not need to surround with another array, here...

var data = google.visualization.arrayToDataTable([data1]);

change to...

var data = google.visualization.arrayToDataTable(data1);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM