I have an actuals array of array coming from django queryset as a json. I am having trouble feeding it to the google charts.js Below is the code. views.py
def historicals(request):
actuals= serializers.serialize("json",Del.objects.all()[1:10])
json_object = json.loads(actuals)
actuals=[]
i=1
for element in json_object:
dt=element['fields']['Date']
dt=datetime.datetime.strptime(dt,'%Y-%m-%dT%H:%M:%S%z')
dt=dt.date()
dt=datetime.datetime.strftime(dt,'%Y-%m-%d')
Vol=str(element['fields']['Volume'])
print (dt)
cn=str(element['fields']['Customer'])
cn = str(cn)
actuals.append([pxn,Vol,dt])
print (actuals)
context['actuals']=actuals
return render(request,'customer.html',context)
var actuals = "{{ actuals | safe }}";
will shows the following in page source.
[['001', '5155', '2020-01-14'], ['001', '57.0', '2020-02-13'], ['001', '510', '2020-02-25'], ['001', '5760', '2020-03-23'], ['001', '46.625', '2020-03-30'], ['001', '910', '2020-04-01'], ['001', '435300.0', '2020-03-20']]
console.log(actuals); //Would show the above array of arrays
for (i = 0; i < actuals.length; i++) {
for (j = 0; j < actuals[i].length; j++) {
var dt = i;
actual = actuals[i][j]; //tried parseFloat too didnt work
//data.addRow([i,actual[i][1]);
console.log(actuals[i][j]); //--> this shows individual characters in the array [ , 0 0 0 0 1
}
}
I am trying to iteratively add a row using the data.addRow line. My problem is actuals[i][j] is showing an individual character (this shows individual characters in the array [ , 0 0 0 0 1....) in console.log instead of whole string or numbers or dates.
I was expecting the console.log(actuals[i][j]) to show
['001', '5155', '2020-01-14'] in the browser console. Can you help?
[['001', '5155', '2020-01-14'], ['001', '57.0', '2020-02-13'], ['001', '510', '2020-02-25'], ['001', '5760', '2020-03-23'], ['001', '46.625', '2020-03-30'], ['001', '910', '2020-04-01'], ['001', '435300.0', '2020-03-20']]actual = actuals([i][j]);you getTypeError: actuals is not a functiontry insteadactual = actuals[i][j];without parenthesesjson_scriptto pass context variables to your JS