首页 > 解决方案 > 如何在 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"
        }
      ]
    }""")

标签: pythondjangopython-3.xfusioncharts

解决方案


推荐阅读