【问题标题】:Want to draw data using chartjs in django想在 django 中使用 chartjs 绘制数据
【发布时间】:2017-04-25 07:40:49
【问题描述】:

我的应用程序将使用表单获取数据并将其保存到数据库中,然后使用 chartjs 进行绘制。 我已经尝试过这段代码,但给了我一个错误。我的代码如下:

def get_temperature_show(request):
all_temperatures = Temper.objects.all()

dates_label = []
sensor1_temp = []
sensor2_temp = []
sensor3_temp = []
sensor4_temp = []

for model in all_temperatures:
    dates_label.append(model.date)
    sensor1_temp.append(model.sensor1_temp)
    sensor2_temp.append(model.sensor2_temp)
    sensor3_temp.append(model.sensor3_temp)
    sensor4_temp.append(model.sensor4_temp)

data = {
    'dates': dates_label,
    'sensor1': sensor1_temp,
    'sensor2': sensor2_temp,
    'sensor3': sensor3_temp,
    'sensor4': sensor4_temp
}

return render(request, 'weather/showtemper.html', json.dumps({"data":data}))

但它给出了以下内容-

我的模板示例是这样的:

var ctx = document.getElementById('myChart').getContext('2d');
var myChart = new Chart(ctx, {
    type: 'line',
    data: {
        labels: ['M', 'T', 'W', 'T', 'F', 'S', 'S'],
        datasets: [{
            label: 'apples',
            data: [12, 19, 3, 17, 6, 3, 7],
            backgroundColor: "rgba(153,255,51,0.4)"
        }, {
            label: 'oranges',
            data: [2, 29, 5, 5, 2, 3, 10],
            backgroundColor: "rgba(255,153,0,0.4)"
        }]
    }
});

请帮我解决这个错误

提前致谢

【问题讨论】:

    标签: javascript django chart.js


    【解决方案1】:

    视图应该是这样的:

    def get_temperature_show(request):
    
    data = Temper.objects.all()
    return render(request,'weather/showtemper.html',{'data':data})
    

    在模板中只是迭代数据。代码如下:

    var ctx = document.getElementById('myChart').getContext('2d');
    var myChart = new Chart(ctx, {
    type: 'line',
    data: {
        {#        labels: ['M', 'T', 'W', 'T', 'F', 'S', 'S'],#}
    
        labels: [{% for d in data %} "{{ d.date }}", {% endfor %}],
        datasets: [{
            label: 'Sensor 1',
            {#            data: [12, 19, 3, 17, 6, 3, 7],#}
            data: [{% for temp in data %} "{{ temp.sensor1_temp }}", {% endfor %}],
            backgroundColor: "rgba(153,255,51,0.4)"
        }, {
            label: 'Sensor 2',
            data: [{% for temp in data %} "{{ temp.sensor2_temp }}", {% endfor %}],
            backgroundColor: "rgba(255,153,0,0.4)"
        },
            {
                label: 'Sensor 3',
                data: [{% for temp in data %} "{{ temp.sensor3_temp }}", {% endfor %}],
                backgroundColor: "rgba(0,0,255,0.4)"
            },
            {
                label: 'Sensor 4',
                data: [{% for temp in data %} "{{ temp.sensor4_temp }}", {% endfor %}],
                backgroundColor: "rgba(255,0,0,0.4)"
            }]
    }
    });
    

    【讨论】:

      【解决方案2】:
      from json import JSONEncoder
      class MyEncoder(JSONEncoder):
         def default(self, o):
             if isinstance( o, datetime.datetime):
                 return o.isoformat()
             return o 
      
      
      json.dumps(cls=MyEncoder)
      

      当你在你的 django 代码中转储时试试这个

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-06-08
        • 2018-04-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多