2013-02-08 46 views
1
graphs.html 


{% extends "base.html" %} 
<script type = "text/javascript" src = "{{ STATIC_URL }}js/highcharts.js"></script> 
<script type = "text/javascript" src = "{{ STATIC_URL }}js/jquery-1.9.1.min.js">                                     </script> 
<body > 
{% block menu %} 
{{block.super}} 
{% endblock menu %} 

{% block content %} 
{{weatherchart}} 


<!-- 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        --> 
{% load chartit %} 
{{ weatherchart|load_charts:"container" } 
<div id='container'> </div> 
    {% endblock content %} 
</body> 



views.py 


def plot_graph(request): 
month_number=[] 
months=KEBReading.objects.filter().values("datetime_reading") 
print months 
for obj in months: 
    month_number=obj["datetime_reading"].day 
    print month_number 
#Step 1: Create a DataPool with the data we want to retrieve. 
weatherdata = \ 
    DataPool(
     series= 
     [{'options': { 
      'source': KEBReading.objects.filter().values("id","truepower_consumed","voltage_reading")}, 
      'terms': [ 
      'id', 
      'truepower_consumed','voltage_reading' 
      ]}]) 





#Step 2: Create the Chart object 
cht = Chart(
     datasource = weatherdata, 
     series_options = 
      [{'options':{ 
       'type': 'line', 
       'stacking': False}, 
      'terms':{ 
       'id': [ 
       'truepower_consumed','voltage_reading'] 


       }}], 

     chart_options = 
      {'title': { 
       'text': 'Graph For Power Management'}, 
      'xAxis': { 
       'title':{'text': 'month_number'}}}) 
print "weather chart" 
print cht 

#Step 3: Send the chart object to the template. 
return render_to_response('graph.html',{'weatherchart': cht},context_instance=RequestContext(request)) 

我在使用Django-Chartit.I時遇到了問題想使用我的數據庫中的數據創建圖表。 我已經像上面那樣做了。但圖表沒有被顯示。我是否以正確的方式做。 幫助表示讚賞。提前致謝。 (graphs.html)使用Django Chartit顯示圖表

+0

我們遇到同樣的問題,可惜我從來沒有用django代碼運行它。我切換到jscript來創建一個圖 – catherine 2013-02-08 12:02:50

回答

0

我有同樣的問題,偶然發現沒有記錄在任何地方的解決方案,爲了讓我得到它的工作,我不得不把load_charts調用內「容器」 DIV像這樣......

<div id="container">{{ chart|load_charts:"container" }}</div> 

還不錯的做法是使用雙引號在HTML的DIV ID,而不是單一的像你上面記載。

1

嘗試

{% extends "base.html" %} 
{% load chartit %} 
{% block scripts %} 
{{ weatherchart|load_charts:"container" }} 
{% endblock %} 

其中腳本塊是所有js文件的標籤。 確保jQuery在加載圖之前加載