【问题标题】:Load Unknown Number of Charts with Django Chartit使用 Django Chartit 加载未知数量的图表
【发布时间】:2018-07-18 02:56:41
【问题描述】:

我有一个脚本可以生成多个图表,这些图表传递给我的chart.html 中的datachart 变量。我希望每个图表都有自己的<div>。遍历datachart 并创建所需的<div> 数量很容易,但我面临的问题是datachart 中的图表数量会有所不同。在下面的示例中,生成了 12 个图表,我必须手动传递 datachart|loadchart:"p1,p2,..."

有没有办法迭代图表或根据datachart 数组的长度动态构造datachart|loadchart:"p1,p2,..."

来自 chart.html 模板的片段

<head>
    <!-- code to include the highcharts and jQuery libraries goes here -->
    <!-- load_charts filter takes a comma-separated list of id's where -->
    <!-- the charts need to be rendered to                             -->
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
    <script src="http://code.highcharts.com/highcharts.js" type="text/javascript"></script>
    <script src="/highcharts.js" type="text/javascript"></script>

    {% load chartit %}
    {{ datachart|load_charts:"p1,p2,p3,p4,p5,p6,p7,p8,p9,p10,p11,p12" }}

</head>

<center>
    {% for c in datachart %}
      <div id='p{{forloop.counter}}' style="height: 40%; width: 80%">  </div>
    {% endfor %}
</center>

views.py

views.py 附加到 charts 变量。 charts 最终包含图表对象,[&lt;chartit.charts.Chart object at 0x000001B3ECA44F60&gt;, &lt;chartit.charts.Chart object at 0x000001B3ECA6F0F0&gt;,...]

def getSurfData(request,pk, project_id, house_id, stage_id):
    client = Client.objects.get(pk=pk)
    project = Project.objects.filter(clients=client, pk=project_id).get()
    house = house.objects.filter(project=project, pk=house_id).get()

    stages = Stage.objects.get(house=house, pk=stage_id)

    pprop_type = PerfProperty.objects.filter(perf__in = stages.perf_set.all()).values('prop_type').distinct()

    for p in pprop_type:
        seriesOpt = []
        chartOpt = []
        for pNum in stages.perf_set.all():
            exp = PerfProperty.objects.filter(perf=pNum, prop_type=p['prop_type']).values('perf__perf_number','prop_type','time','value')
            options = {'options': {
                        'source':  exp,
                            },
                        'terms': [{'pt':'prop_type','t{}'.format(pNum):'time','Number {}'.format(pNum):'value'}]

                        }

            seriesOpt.append(options)

            options = {'options': {
                          'type': 'line',
                          },
                        'terms': {'t{}'.format(pNum): ['Number {}'.format(pNum)]}}
            chartOpt.append(options)

            dp = DataPool (series = seriesOpt)

            cht = Chart(
                    datasource = dp,
                    series_options = chartOpt,

                    chart_options =
                      {'title': {
                           'text': "{}".format(p['prop_type'])
                               },
                       'xAxis': {
#                            'categories':{'t{}'.format(pNum)},
                            'crosshair':'true',
                            'labels': {
                                'format': '{value}'
                        },
                            'title': {
                               'text':'Time'},
                            },
                        'yAxis':{
                            'title': {
                                'text': "Value"}

                                },
                        'chart': {
                            'zoomType': 'x',
                                },

                            })
        charts.append(cht)

#   
    return render(request,'reporter/chart.html',{'datachart': charts, 'client': client, 'projects': project, 'house': house, 'stage':stages})

【问题讨论】:

    标签: javascript django charts highcharts


    【解决方案1】:

    这是我想出的解决方案。我尝试循环遍历datachart,但我相信 chartit 总是将 js 变量命名为相同的东西,唯一显示的图表是最后一个。为了克服这个问题,我制作了一个自定义标签,它生成一个包含所有 div id 的字符串,然后将其传递给load_charts

    自定义标签createDivId在:

    appname/templatetags/appname_extras.py

    from django import template
    
    register = template.Library()
    
    @register.filter
    def createDivId(prefix, length):
        for n in range(len(length)):
            if n ==0:
                tmp = str(prefix)+str(n+1)
            else:
                tmp = str(tmp)+","+str(prefix)+str(n+1)
        print(tmp)
        return str(tmp)
    

    charts.html

    <head>
        <!-- code to include the highcharts and jQuery libraries goes here -->
        <!-- load_charts filter takes a comma-separated list of id's where -->
        <!-- the charts need to be rendered to                             -->
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
        <script src="http://code.highcharts.com/highcharts.js" type="text/javascript"></script>
        <script src="/highcharts.js" type="text/javascript"></script>
    
        {% with divID="p"|createDivId:datachart %}
          {% load chartit %}
          {{ datachart|load_charts:divID}}
        {% endwith %}
    </head>
    
    <center>
        {% for c in datachart %}
            <div id='p{{forloop.counter}}' style="height: 40%; width: 80%"></div>
        {% endfor %}
    
    </center>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-24
      • 2018-08-18
      相关资源
      最近更新 更多