python-3.x 如何在Django中使用mysql数据库数据到FusionCharts

ar7v8xwq  于 2023-10-21  发布在  Python
关注(0)|答案(1)|浏览(208)

我想使用的数据是通过运行查询存储在MySQL数据库中,并显示数据与FusionCharts的帮助。
现在fusionCharts使用json数据格式,但MySQL数据没有。
如何使用fusionCharts显示我的数据库数据。我需要代码帮助。
这是我的views.py(这不是所有的views.py只是相关的一个。

  1. from .fusioncharts import FusionCharts
  2. def chart(request):
  3. # Create an object for the column2d chart using the FusionCharts class constructor
  4. monthly = FusionCharts('column2d', 'ex1', '600', '400', 'chart-1', 'json', """{
  5. "chart": {
  6. "caption": "Monthly offense",
  7. "subcaption": "Month of July",
  8. "xaxisname": "Country",
  9. "yaxisname": "Offenses",
  10. "numbersuffix": "",
  11. "theme": "fusion"
  12. },
  13. "data": [
  14. {
  15. "label": "Jan",
  16. "value": "290"
  17. },
  18. {
  19. "label": "Feb",
  20. "value": "260"
  21. },
  22. {
  23. "label": "Mar",
  24. "value": "180"
  25. },
  26. {
  27. "label": "April",
  28. "value": "140"
  29. },
  30. {
  31. "label": "May",
  32. "value": "115"
  33. },
  34. {
  35. "label": "June",
  36. "value": "100"
  37. },
  38. {
  39. "label": "July",
  40. "value": "30"
  41. },
  42. {
  43. "label": "Aug",
  44. "value": "30"
  45. },
  46. {
  47. "label": "Sept",
  48. "value": "30"
  49. },
  50. {
  51. "label": "Oct",
  52. "value": "30"
  53. },
  54. {
  55. "label": "Nov",
  56. "value": "30"
  57. },
  58. {
  59. "label": "Dec",
  60. "value": "30"
  61. }
  62. ]
  63. }""")
  64. context = {
  65. 'all_count': all_count,
  66. 'total_high': total_high,
  67. 'total_medium': total_medium,
  68. 'total_low': total_low,
  69. 'monthly': monthly.render(),
  70. 'status': status.render(),
  71. 'category_chart': category_chart.render(),
  72. 'all_category': all_category.render()
  73. }
  74. # returning complete JavaScript and HTML code, which is used to generate chart in the browsers.
  75. return render(request, 'dashboard.html', {'context': context})

到目前为止,我已经尝试过了。我做了一个字典,然后把它转换成json,并把它传递给我的chart对象。它在html模板上给出“无效数据”。

  1. all_count = Offenses.objects.all().count()
  2. open_status = Offenses.objects.filter(status__iexact='OPEN').count()
  3. closed_status = Offenses.objects.filter(status__iexact='CLOSED').count()
  4. hidden_status = Offenses.objects.filter(status__iexact='HIDDEN').count()
  5. high1 = Offenses.objects.filter(magnitude=7).count()
  6. high2 = Offenses.objects.filter(magnitude=6).count()
  7. high3 = Offenses.objects.filter(magnitude=8).count()
  8. high4 = Offenses.objects.filter(magnitude=9).count()
  9. medium1 = Offenses.objects.filter(magnitude=5).count()
  10. medium2 = Offenses.objects.filter(magnitude=4).count()
  11. medium3 = Offenses.objects.filter(magnitude=3).count()
  12. low1 = Offenses.objects.filter(magnitude=1).count()
  13. low2 = Offenses.objects.filter(magnitude=2).count()
  14. total_high = high1+high2+high3+high4
  15. total_medium = medium1+medium2+medium3
  16. total_low = low1+low2
  17. status_data = {
  18. 'open_status': open_status,
  19. 'closed_status': closed_status,
  20. 'hidden_status': hidden_status,
  21. }
  22. status_data_json = json.dumps(status_data)
  23. status = FusionCharts('doughnut2d', 'ex3', '600', '400', 'chart-2', 'status_data_json', """{
  24. "chart": {
  25. "caption": "Offense Status",
  26. "showpercentvalues": "1",
  27. "aligncaptionwithcanvas": "0",
  28. "captionpadding": "0",
  29. "decimals": "1",
  30. "plottooltext": "<b>$percentValue</b> of offenses are <b>$label</b>",
  31. "centerlabel": "# Users: $value",
  32. "theme": "fusion"
  33. },
  34. "data": [
  35. {
  36. "label": "Opened offense",
  37. "value": "open_status"
  38. },
  39. {
  40. "label": "Closed offense",
  41. "value": "5300"
  42. }
  43. ]
  44. }""")
nwsw7zdq

nwsw7zdq1#

除了直接在JSON/XML代码中指定图表数据(或存储图表数据的文件的URL)之外,您还可以从数据库中获取图表数据。
Django模型Map(大致)到一个数据库表-

  1. from django.db import models
  2. class Revenue(models.Model):
  3. MonthlyRevenue = models.CharField(max_length=50)
  4. Month = models.CharField(max_length=50)
  5. def __unicode__(self):
  6. return u'%s %s' % (self.MonthlyRevenue, self.Month)

示例代码-

  1. from django.shortcuts import render
  2. from django.http import HttpResponse
  3. # Include the `fusioncharts.py` file that contains functions to embed the charts.
  4. from fusioncharts import FusionCharts
  5. from ..models import *
  6. # The `chart` function is defined to generate Column 2D chart from database.
  7. def chart(request):
  8. # Chart data is passed to the `dataSource` parameter, as dict, in the form of key-value pairs.
  9. dataSource = {}
  10. dataSource['chart'] = {
  11. "caption": "Monthly revenue for last year",
  12. "subCaption": "Harry's SuperMart",
  13. "xAxisName": "Month",
  14. "yAxisName": "Revenues (In USD)",
  15. "numberPrefix": "$",
  16. "theme": "zune"
  17. }
  18. # The data for the chart should be in an array where each element of the array is a JSON object
  19. # having the `label` and `value` as key value pair.
  20. dataSource['data'] = []
  21. # Iterate through the data in `Revenue` model and insert in to the `dataSource['data']` list.
  22. for key in Revenue.objects.all():
  23. data = {}
  24. data['label'] = key.Month
  25. data['value'] = key.MonthlyRevenue
  26. dataSource['data'].append(data)
  27. # Create an object for the Column 2D chart using the FusionCharts class constructor
  28. column2D = FusionCharts("column2D", "ex1" , "600", "350", "chart-1", "json", dataSource)
  29. return render(request, 'index.html', {'output': column2D.render()})

要了解更多有关此功能的信息,请参阅-https://www.fusioncharts.com/dev/getting-started/django/create-charts-in-django-using-database
希望这对你有帮助。

展开查看全部

相关问题