我的数据存储在MySQL数据库中,我需要将d3.js中的数据可视化为气泡图。我有可能在Django框架中执行此操作吗?
是的,您可以使用Django执行此操作。您需要做的就是创建Django PyDev(python)应用程序。在settings.py文件中,将数据库指定为
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'myDB', # your mysql database name
'USER': '123', # your mysql user for the database
'PASSWORD': '123', # password for user
'HOST': '127.0.0.1',
'PORT': '3306',
}
在views.py的函数定义中,将mysql连接设置为
conn = MySQLdb.connect (host = "127.0.0.1",
user = "root", # mysql root
passwd = "root", # mysql root password
db = "myDB")
使用游标从表中检索数据,然后将其转换为json,
cursor.execute ("select <column> from <table>")
rows=dictfetchall(cursor)
object_list = []
for row in rows:
d = collections.defaultdict()
d['name'] = row['name']
object_list.append(d)
j = json.dumps(object_list)
objects_file = 'path of json file to be created'
m = open(objects_file,'w')
print >> m,j #to write to file
conn.close()
def dictfetchall(cursor):
"Returns all rows from a cursor as a dictionary"
desc = cursor.description
return [
dict(zip([col[0] for col in desc], row))
for row in cursor.fetchall()
]
现在,您可以使用此json文件创建任何类型的d3js。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句