hi all,
I am writing a python script that connects to MySQL database and gets data and I store that data as python dictionary. I want to plot a graph between keys and values using Google Visualization api.
Here's the code as well. How to parse the python dictionary into gviz data table?
#!/usr/bin/python
import sys, pkg_resources, imp
import commands
import pyodbc
import MySQLdb
import gviz_api
#Connect to ens-db, database circuits
conn_ensdb = MySQLdb.Connection(db='NHS_db', host='ens-db', user='asdgghh', passwd='ilthnmk')
cursor_ensdb = conn_ensdb.cursor()
cursor_ensdb.execute("SELECT ManagedType,TotalCount from TotalManaged_DC")
rows = cursor_ensdb.fetchall()
#converting tuple output to dictionary format
my_list = [data for data in rows]
dict_with_strs=dict(my_list) //Output of dictionary will be:- {'Center': 4, ' Office ': 14, ' Core Data ': 7, 'Managed ': 25}
dict_with_ints=dict((k,int(v)) for k,v in dict_with_strs.iteritems())
print ""
print '''
<html>
<head>
<script type="text/javascript" src="
https://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("visualization", "1.1", {packages:["bar"]});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable([ //I want to parse this dictionary into data table. Plot Google charts between dict keys vs dict values .How can I pass dict ?
]);
var options = {
chart: {
title: 'Company Performance',
},
bars: 'horizontal' // Required for Material Bar Charts.
};
var chart = new google.charts.Bar(document.getElementById('barchart_material'));
chart.draw(data, options);
}
</script>