1

我正在尝试将 Highcharts 与 Django 一起使用——但我对 Javascript 一点也不熟悉。

我正在绘制不同大学的排放清单。用户将选择他们想要显示的大学,因此返回的数据量会有所不同。

我已将每个 Django 变量转换为 Javascript 变量,以便可以访问它们:

{% for college in collegelist %}
    <script>
    function removeCommas(str) {
        return(str.replace(/,/g,''));
    }
    collegearray[collegecounter] = [parseInt(removeCommas("{{ college.stationarycombustion }}")), parseInt(removeCommas("{{ college.mobilecombustion }}")), parseInt(removeCommas("{{ college.processemissions }}")), parseInt(removeCommas("{{ college.fugitiveemissions }}")), parseInt(removeCommas("{{ college.scope1 }}")) ];
    collegecounter++;
    </script>
{% endfor %}

如您所见,collegearray 包含包含大学所有数据(Stationary Combustion、Mobile Combustion 等)的数组。

然后 Highcharts Javascript 函数包含如下赋值:

series: [{
            name: 'College 1',
            data: [83.6, 78.8, 98.5, 93.4, 106.0]

        }, {
            name: 'College 2',
            data: [83.6, 78.8, 98.5, 93.4, 106.0]

        },

但是,由于要绘制的数据量会有所不同,基本上我希望能够编写的内容相当于:

counter = 0
for college in collegelist:
    name: collegename[counter],
    data: collegearray[counter]
    counter += 1

但是,Javascript 一直抱怨我不能使用 for 循环以这种方式处理分配。有没有办法打破 Javascript,使用 Django 的模板条件 {% for college in college list %},并将输出分配回 Javascript 函数?还是其他方式?我是否让这变得比需要的更困难?

谢谢你的建议,布雷特

根据请求,模型:我应该注意我没有使用 Django-Chartit,所以我没有直接从模型构建图表。

class Inventories(models.Model):
    institution = models.CharField(max_length=100)
    publicationdate = models.CharField(max_length=10)
    stationarycombustion = models.CharField(max_length=100)
    mobilecombustion = models.CharField(max_length=100)
    processemissions = models.CharField(max_length=100)
    fugitiveemissions = models.CharField(max_length=100)
    scope1 = models.CharField(max_length=100)
    purchasedelectricity = models.CharField(max_length=100)
    purchasedheating = models.CharField(max_length=100)
    purchasedcooling = models.CharField(max_length=100)
    purchasedsteam = models.CharField(max_length=100)
    scope2 = models.CharField(max_length=100)
    commuting = models.CharField(max_length=100)
    airtravel = models.CharField(max_length=100)
    solidwaste = models.CharField(max_length=100)
    wastewater = models.CharField(max_length=100)
    paperemissions = models.CharField(max_length=100)
    scope2td = models.CharField(max_length=100)
    scope3 = models.CharField(max_length=100)
    carbonoffsets = models.CharField(max_length=100)
    totalrecs = models.CharField(max_length=100)
    landsequestration = models.CharField(max_length=100)
    compostsequestration = models.CharField(max_length=100)
    gsf = models.CharField(max_length=100)
    residentialspace = models.CharField(max_length=100)
    studentfte = models.CharField(max_length=100)

    def __unicode__(self):
        return self.institution+", "+self.publicationdate
4

0 回答 0