Created
January 2, 2019 15:38
-
-
Save palistha01/811f8e3d76100f91655756c43f5e7d64 to your computer and use it in GitHub Desktop.
Displaying dynamic data in google barchart using django and jquery
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function load_monthwise_ward(year, title) { | |
var temp_title = title + ' ' + year + ''; | |
console.log(temp_title); | |
$.ajax({ | |
url: '{% url "ward" %}', | |
type: "GET", | |
data: { | |
"_token": "{% csrf_token %}" | |
}, | |
dataType: "json", | |
success: function (data) { | |
if (data) { | |
var results = data; | |
var count = results.results; | |
drawMonthwiseChartWard(count, temp_title); | |
} | |
} | |
}); | |
} | |
function drawMonthwiseChartWard(chart_data, chart_main_title) { | |
var date = $("select#ward").val(); | |
var jsonData = chart_data; | |
console.log(jsonData); | |
var data = new google.visualization.DataTable(); | |
data.addColumn('string', 'Ward'); | |
data.addColumn('number', 'Total'); | |
$.each(jsonData, function (i, jsonData) { | |
var month = date; | |
var profit = jsonData; | |
data.addRows([[month, profit]]); | |
var options = { | |
title: chart_main_title, | |
hAxis: { | |
title: "Ward" | |
}, | |
vAxis: { | |
title: 'Total' | |
} | |
}; | |
var chart = new google.visualization.ColumnChart(document.getElementById('chart_area1')); | |
chart.draw(data, options); | |
}); | |
} | |
$(document).ready(function () { | |
var ward = $('#ward').prop('id') | |
if (ward != '') { | |
load_monthwise_ward(ward, 'Ward'); | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def ward(request): | |
results = NewRegistrationModel.objects.all().values('ward_no') | |
return JsonResponse({'results': list(results)}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment