0
template.html 
<script src="http://code.highcharts.com/highcharts.js"> 
</script> 
 <script >
$(document).ready(function() {
var chart = new Highcharts.Chart({

    chart: {
        renderTo: 'container',
        type: 'line'
    },
    title: {
        text: 'Power Consumption Vs Generator Consumption'
    },
    xAxis: {
        categories: [{{month_number|join:"','" }}]
    },
    yAxis: {
        title: {
            text: 'Fruit eaten'
        }
    },

    // series: [{
        // name: 'Truepower Consumed',
        // data: [1, 0, 4]},
    // {
        // name: 'Generator Consumed',
        // data: [5, 7, 3]}],
// });
 series: [{
        name: 'Truepower Consumed',
        data: [ '{{ p|join:"','" }}']},
    {
        name: 'Generator Consumed',
        data:   [ '{{ q|join:"','" }}']}],
});

});

 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
    q2=KEBReading.objects.filter().values("truepower_consumed")
    print q2
    q3=GeneratorReading.objects.filter().values("perday_dgunit")
    print q3
    for item in q2:
        power_values=item["truepower_consumed"]
        print power_values
    print "tee"
    for item in q3:
        gen_values=item["perday_dgunit"]
        print gen_values
    context={'m':month_number,'p':power_values,'q':gen_values}    
    return render_to_response('graph.html',context,
                               context_instance=RequestContext(request))

我试图绘制图表。我能知道如何将视图中的值传递给图形的 jQuery。context={'m':month_number,'p':power_values,'q':gen_values} p 和 q 是 y 轴值,m 有 x 轴值

 models.py 


 class KEBReading(models.Model):

   datetime_reading=models.DateTimeField()

   truepower_reading=models.DecimalField(verbose_name="True Power                          Reading(KWH)",max_digits=6,decimal_places=2,blank=False,null=False)
apparentpower_reading=models.DecimalField(verbose_name="Apparent Power Reading(KVAH)",max_digits=6,decimal_places=2,blank=False,null=False)
 truepower_consumed=models.DecimalField(max_digits=6,decimal_places=2,blank=False,null=False)
voltage_reading=models.IntegerField(blank=False,null=False)
powerfactor=models.DecimalField(max_digits=3,decimal_places=2)



 class GeneratorReading(models.Model):

  datetime_reading=models.DateTimeField()

 running_time=models.IntegerField(blank=False,null=False)
 running_time_consumed=models.SmallIntegerField(blank=False,null=False)
 dgunit_reading=models.DecimalField(max_digits=6, blank=True,decimal_places=2)
 perday_dgunit=models.DecimalField(max_digits=6, decimal_places=2)
4

1 回答 1

0

当我需要这种行为时,我经常编写一个过滤器,将我的 Object 或 Queryset 转换为 json 对象。

def jsonify(object):
    if isinstance(object, QuerySet):
         return serialize('json', object)
    return simplejson.dumps(object)

register.filter('jsonify', jsonify)

<script id="json_data" type="text/javascript">
    {{ my_data|jsonify|safe }}
</script>
于 2013-02-11T09:36:40.730 回答