python - 如何在 Django 中使用 mysql 数据库数据到 FusionCharts
问题描述
我想通过运行查询来使用存储在 MySQL 数据库中的数据,并在 FusionCharts 的帮助下显示数据。
现在 fusionCharts 使用 json 数据格式,但 MySQL 数据没有。
如何使用 fusionCharts 显示我的数据库数据。我需要代码方面的帮助。
这是我的views.py(这不是所有views.py,只是相关的一个。)
from .fusioncharts import FusionCharts
def chart(request):
# Create an object for the column2d chart using the FusionCharts class constructor
monthly = FusionCharts('column2d', 'ex1', '600', '400', 'chart-1', 'json', """{
"chart": {
"caption": "Monthly offense",
"subcaption": "Month of July",
"xaxisname": "Country",
"yaxisname": "Offenses",
"numbersuffix": "",
"theme": "fusion"
},
"data": [
{
"label": "Jan",
"value": "290"
},
{
"label": "Feb",
"value": "260"
},
{
"label": "Mar",
"value": "180"
},
{
"label": "April",
"value": "140"
},
{
"label": "May",
"value": "115"
},
{
"label": "June",
"value": "100"
},
{
"label": "July",
"value": "30"
},
{
"label": "Aug",
"value": "30"
},
{
"label": "Sept",
"value": "30"
},
{
"label": "Oct",
"value": "30"
},
{
"label": "Nov",
"value": "30"
},
{
"label": "Dec",
"value": "30"
}
]
}""")
context = {
'all_count': all_count,
'total_high': total_high,
'total_medium': total_medium,
'total_low': total_low,
'monthly': monthly.render(),
'status': status.render(),
'category_chart': category_chart.render(),
'all_category': all_category.render()
}
# returning complete JavaScript and HTML code, which is used to generate chart in the browsers.
return render(request, 'dashboard.html', {'context': context})
到目前为止,我已经尝试过了。我制作了一本字典,然后将其转换为 json 并将其传递给我的图表对象。它在 html 模板上给出“无效数据”。
all_count = Offenses.objects.all().count()
open_status = Offenses.objects.filter(status__iexact='OPEN').count()
closed_status = Offenses.objects.filter(status__iexact='CLOSED').count()
hidden_status = Offenses.objects.filter(status__iexact='HIDDEN').count()
high1 = Offenses.objects.filter(magnitude=7).count()
high2 = Offenses.objects.filter(magnitude=6).count()
high3 = Offenses.objects.filter(magnitude=8).count()
high4 = Offenses.objects.filter(magnitude=9).count()
medium1 = Offenses.objects.filter(magnitude=5).count()
medium2 = Offenses.objects.filter(magnitude=4).count()
medium3 = Offenses.objects.filter(magnitude=3).count()
low1 = Offenses.objects.filter(magnitude=1).count()
low2 = Offenses.objects.filter(magnitude=2).count()
total_high = high1+high2+high3+high4
total_medium = medium1+medium2+medium3
total_low = low1+low2
status_data = {
'open_status': open_status,
'closed_status': closed_status,
'hidden_status': hidden_status,
}
status_data_json = json.dumps(status_data)
status = FusionCharts('doughnut2d', 'ex3', '600', '400', 'chart-2', 'status_data_json', """{
"chart": {
"caption": "Offense Status",
"showpercentvalues": "1",
"aligncaptionwithcanvas": "0",
"captionpadding": "0",
"decimals": "1",
"plottooltext": "<b>$percentValue</b> of offenses are <b>$label</b>",
"centerlabel": "# Users: $value",
"theme": "fusion"
},
"data": [
{
"label": "Opened offense",
"value": "open_status"
},
{
"label": "Closed offense",
"value": "5300"
}
]
}""")
解决方案
推荐阅读
- javascript - 单击按钮时防止默认值不随机工作
- pandas - 使用特定行中的标题将文本文件分成多个 Pandas 数据框
- c# - 使用 NetOffice 创建 Excel 文件 (C#) 检索 COM 类时未找到间歇性文件异常
- excel - VBA:检查一个单元格是否包含某个值以及如何将该值复制/粘贴到另一个单元格
- reactjs - React.js 组件中的无限控制台日志记录
- android - 想要显示通过文本视图收到的电话号码和消息
- laravel - 一种模型,一张表,不同用户类型,不同控制器,如何处理策略?
- python-3.x - 使用python3实时绘制到TK窗口
- c# - 无法将文件从 MVC 控制器 POST 到 Web API 控制器
- arrays - R中的元素比较