Javascript 具有可变数据的海图
我试图在Django中使用Highcharts——但我对Javascript一点也不熟悉 我正在为不同的大学绘制排放清单。用户将选择要显示的学院,因此返回的数据量会有所不同 我已将每个Django变量转换为Javascript变量,以便可以访问它们:Javascript 具有可变数据的海图,javascript,django,python-2.7,Javascript,Django,Python 2.7,我试图在Django中使用Highcharts——但我对Javascript一点也不熟悉 我正在为不同的大学绘制排放清单。用户将选择要显示的学院,因此返回的数据量会有所不同 我已将每个Django变量转换为Javascript变量,以便可以访问它们: {% for college in collegelist %} <script> function removeCommas(str) { return(str.replace(/,/g,''));
{% 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 %}
但是,由于要绘制图表的数据量会有所不同,因此我希望能够编写的基本内容相当于:
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
我在我的网站中使用Highcharts,在脚本中forloop对象并不困难。就像在模板中循环一样。你能发布你想在图表中显示的型号代码吗。我上面提到过,如果相关的话,我没有使用Django Chartit,所以我没有直接从模型中绘制图表。我尝试将Django Chartit与Highcharts一起使用,但我无法使其工作,所以我最终只使用HighChart,你想在图表中显示哪些字段?Catherine——你太棒了。谢谢你的帮助。实际上我刚刚解决了这个问题;我马上在这里发布一个答案。
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