Django - Fusion Charts在一个饼图中显示所有值

9jyewag0  于 2023-10-21  发布在  Go
关注(0)|答案(2)|浏览(105)

这是一个饼图的相关融合图表代码。我想显示所有的属性在我的数据库中的所有条目,但根据'名称'

dataSource2['data'] = []
    # Iterate through the data in `Revenue` model and insert in to the 
 `dataSource['data']` list.
    for key in all_books:
        data = {}
        data['label'] = key.name
        data['value'] = key.price
        dataSource2['data'].append(data)

详细说明:下面的概念,我想实现显示所有单一的参数,为每一个条目

data[value1]=key.printingcost
    data[value2]=key.papercost
    data[value3]=key.binding

是否有可能(在饼图中)起诉融合图表库??
附图供参考

lxkprmvk

lxkprmvk1#

是的,为了在FusionCharts中渲染饼图,您需要创建一个数据数组,其中包含标签和值作为每个对象,您可以在饼图中展示许多数据,但饼图不适合展示大数据,但FusionCharts将根据您的数据绘制饼图切片,了解更多关于饼图的信息请查看此链接-https://www.fusioncharts.com/dev/chart-guide/standard-charts/pie-and-doughnut-charts#pie-d-chart-2
FusionCharts还提供了一个专用的Django Package 器,使用它可以从数据库中获取数据并相应地呈现图表-https://www.fusioncharts.com/dev/getting-started/django/install-using-django

wgmfuz8q

wgmfuz8q2#

是的,图表的数据应该在一个数组中,数组的每个元素都是JSON对象,并将labelvalue作为键值对。
您可以参考文档从数据库-https://www.fusioncharts.com/dev/getting-started/django/create-charts-in-django-using-database中获取数据
饼图的示例代码-

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

相关问题