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 时遇到问题。我想使用数据库中的数据制作图表。我已经像上面那样做了。但图表没有显示出来。我做对了吗?帮助表示赞赏。提前致谢。模板中是否缺少其他内容?(graphs.html)

4

2 回答 2

1

尝试

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

其中 block scripts 是所有 js 文件的标签。确保在 highcharts 之前加载 jquery

于 2014-04-21T06:08:28.003 回答
0

我遇到了同样的问题,偶然发现了没有在任何地方记录的解决方案,为了让它工作,我必须像这样将 load_charts 调用放在“容器”DIV 中......

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

在您的 HTML 中为 DIV ID 使用双引号而不是像上面记录的那样使用单引号也是一种很好的做法。

于 2013-04-11T02:37:23.597 回答