Django -融合饼图中的多个数据值

8iwquhpp  于 2023-10-21  发布在  Go
关注(0)|答案(1)|浏览(112)

我想添加多个数据值在我的饼图采取模型字段。目前我是这样做的:

def detail(request, pk):
if not request.user.is_authenticated():
    return render(request, 'registration/login.html')
else:
    bowler = Bowlers.objects.get(pk=pk)
    select_list = UserSelect.objects.filter(user=request.user, bowler=pk)
    all_select = UserSelect.objects.filter(user=request.user).count()
    team = Team.objects.filter(user=request.user)
    dataSource = {}
    dataSource['chart'] = {
        "caption": "last year",
        "subCaption": "Harry's SuperMart",
        "xAxisName": "Month",
        "yAxisName": "Revenues (In USD)",
        "numberPrefix": "$",
        "theme": "zune",
        "type": "doughnut2d"
    }
    dataSource['data'] = []
    # Iterate through the data in `Revenue` model and insert in to the `dataSource['data']` list.
    data = {}
    data['label'] = bowler.name
    data['value'] = bowler.ave
    dataSource['data'].append(data)
    column2D = FusionCharts("doughnut2d", "ex1", "600", "350", "chart-1", "json", dataSource)
    context = {
        'bowler': bowler,
        'select_list': select_list,
        'all_select': all_select,
        'team': team,
        'output': column2D.render()
    }
    return render(request, 'bowler_detail.html', context)

我想添加更多的值,像这样:(这是添加多个数据值的JSON格式)

"data": [
    {
        "label": "Venezuela",
        "value": "290"
    },
    {
        "label": "Saudi",
        "value": "260"
    },
 ]

有什么指南可以指导我如何在图表中添加更多从django模型获取的值吗?

qnakjoqk

qnakjoqk1#

饼图的示例代码-

from django.shortcuts import render
from django.http import HttpResponse

# Include the `fusioncharts.py` file which has required functions to embed the charts in html page
from ..fusioncharts import FusionCharts

# Loading Data from a Static JSON String
# It is a example to show a Pie 3D chart where data is passed as JSON string format.
# The `chart` method is defined to load chart data from an JSON string.

def chart(request):
    # Create an object for the pie3d chart using the FusionCharts class constructor
  pie3d = FusionCharts("pie3d", "ex2" , "100%", "400", "chart-1", "json", 
        # The data is passed as a string in the `dataSource` as parameter.
    """{ 
        "chart": {
            "caption": "Recommended Portfolio Split",
            "subCaption" : "For a net-worth of $1M",
            "showValues":"1",
            "showPercentInTooltip" : "0",
            "numberPrefix" : "$",
            "enableMultiSlicing":"1",
            "theme": "candy"
        },
        "data": [{
            "label": "Equity",
            "value": "300000"
        }, {
            "label": "Debt",
            "value": "230000"
        }, {
            "label": "Bullion",
            "value": "180000"
        }, {
            "label": "Real-estate",
            "value": "270000"
        }, {
            "label": "Insurance",
            "value": "20000"
        }]
    }""")
  
    # returning complete JavaScript and HTML code, which is used to generate chart in the browsers. 
  return  render(request, 'index.html', {'output' : pie3d.render(), 'chartTitle': 'Pie 3D Chart'})

另请参阅使用API的切片数据图-https://www.fusioncharts.com/dev/getting-started/django/slice-data-plot-using-custom-api-in-django

相关问题