M
M
Michael2017-07-29 22:25:25
Django
Michael, 2017-07-29 22:25:25

How to properly build a python dictionary for google charts?

It is necessary to form a dictionary for google charts. But it has a strange design, and the library responds

Cannot read property '1' of undefined

coun = Country.objects.values('country')
link = Proxy.objects.filter(country__in=coun).order_by('country').values_list('country').annotate(Count('country'))
dicts =  ({'c':[{'v': x[0]}, {'v': x[1]}]} for x in link)
 return HttpResponse(dicts)

This is how it should be done on the client
jQuery.get("/cantry/",
                 function(data) {
                     dd = data;
                     //console.log(dd);
                     var data = new google.visualization.DataTable({
                     'cols': [
                              {'label':'Страна', 'type':'string'},
                              {'label':'Число', 'type':'number'}
                          ],
                     'rows': [
                     // 			{'c':[{'v':'24.05.2014'},{'v':3}]}, //<--Но в словаре который приходит нет запятых js.
                     // 			{'c':[{'v':'25.05.2014'},{'v':1}]},
                     // 			{'c':[{'v':'25.05.2014'},{'v':1}]}
                     dd
                          ]
                     });
                     var options = {
                         title: '',
                         chartArea:{left:0,top:0,width:'100%',height:'75%'},
                         legend: {position: 'bottom', maxLines: '2'}
                     };
                     var chart = new google.visualization.PieChart(document.getElementById('piechart'));

                     chart.draw(data, options);
                     alert(dd);
                 }
             );

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Michael, 2017-07-29
@DJWOMS1

The answer is

var google_data = {
         "cols": [{
                       label: 'Страна', "type": "string"
           }, {
                       "type": "string"
            }],
            rows: []
                       };
                       for(var i=0;i<data.length;i++){
                           google_data.rows.push({c:[{v:data[i]}, {v:parseInt(data[i])}]});
                       }

I
Ilya Oskin, 2017-07-30
@Oskin1

Instead: Use this:
for x in link.items()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question