Jason Hawkins
Jason Hawkins

Reputation: 635

Google chart input data

I have a python script to build inputs for a Google chart. It correctly creates column headers and the correct number of rows, but repeats the data for the last row in every row. I tried explicitly setting the row indices rather than using a loop (which wouldn't work in practice, but should have worked in testing). It still gives me the same values for each entry. I also had it working when I had this code on the same page as the HTML user form.

end1 = number of rows in the data table

end2 = number of columns in the data table represented by a list of column headers

viewData = data stored in database

c = connections['default'].cursor()
c.execute("SELECT * FROM {0}.\"{1}\"".format(analysis_schema, viewName))
viewData=c.fetchall()
curDesc = c.description
end1 = len(viewData)
end2 = len(curDesc)

Creates column headers:

colOrder=[curDesc[2][0]]
if activityOrCommodity=="activity":
    tableDescription={curDesc[2][0] : ("string", "Activity")}
elif (activityOrCommodity == "commodity") or (activityOrCommodity == "aa_commodity"):   
    tableDescription={curDesc[2][0] : ("string", "Commodity")}
for i in range(3,end2 ):
    attValue =  curDesc[i][0]
    tableDescription[curDesc[i][0]]= ("number", attValue)
    colOrder.append(curDesc[i][0])

Creates row data:

data=[]
values = {}
for i in range(0,end1):
    for j in range(2, end2):
        if j == 2:
            values[curDesc[j][0]] = viewData[i][j].encode("utf-8")  
        else:
            values[curDesc[j][0]] = viewData[i][j]
    data.append(values)

dataTable = gviz_api.DataTable(tableDescription)
dataTable.LoadData(data)

return dataTable.ToJSon(columns_order=colOrder)

An example javascript output:

var dt = new google.visualization.DataTable({cols:[{id:'activity',label:'Activity',type:'string'},{id:'size',label:'size',type:'number'},{id:'compositeutility',label:'compositeutility',type:'number'}],rows:[{c:[{v:'AA26FedGovAccounts'},{v:49118957568.0},{v:1.94956132673}]},{c:[{v:'AA26FedGovAccounts'},{v:49118957568.0},{v:1.94956132673}]},{c:[{v:'AA26FedGovAccounts'},{v:49118957568.0},{v:1.94956132673}]},{c:[{v:'AA26FedGovAccounts'},{v:49118957568.0},{v:1.94956132673}]},{c:[{v:'AA26FedGovAccounts'},{v:49118957568.0},{v:1.94956132673}]}]}, 0.6);

Upvotes: 1

Views: 229

Answers (1)

user3012759
user3012759

Reputation: 2095

it seems you're appending values to the data but your values are not being reset after each iteration...

i assume this is not intended right? if so just move values inside the first for loop in your row setting code

Upvotes: 1

Related Questions