我试图在一个Django项目中使用pandas-highcharts;我能够生成图表,但数据不会被绘制,所以我猜测我的熊猫数据框格式不正确,或者我没有使用正确的方法来呈现模板。如何格式化一个熊猫数据框以便在Django中与熊猫一起使用
我的数据框:
Entrée d'eau - Archimède - 0013A2004166CFCD
timestamp
2016-12-23 00:05:18+00:00 29.0
2016-12-23 00:05:27+00:00 29.0
2016-12-23 00:05:37+00:00 29.0
2016-12-23 00:05:47+00:00 29.0
2016-12-23 00:05:58+00:00 29.0
我的观点:
from django.shortcuts import render
from data.models import Value
import pandas as pd
from pandas_highcharts.core import serialize
# [...]
df = pd.DataFrame.from_records(
Value.objects.filter(device=a).values("timestamp", "leak_value"))
df.dropna(inplace=True) # not sure about this
df.set_index("timestamp", inplace=True)
df.sort_index(inplace=True)
df = df.truncate(
before=pd.to_datetime(request.POST.get("start")),
after=pd.to_datetime(request.POST.get("stop")))
df = df.rename(
index=str,
columns={"leak_value": "{} - {} - {}".format(
Room.objects.filter(unit=unit_sel).get(device=a),
Device.objects.get(address=a).devicetype,
a)})
print(df.head()) # DEBUG
chart = serialize(
df=df,
render_to='Leak Values',
title="Leak Values",
output_type='json')
return render(request, "leak_chart.html", context={"chart": chart})
我的模板(包括我在base.html文件jQuery和highcharts):
{% extends "base.html" %}
{% block body %}
{% load staticfiles %}
<div id="Leak Values"></div>
<script type="text/javascript">
new Highcharts.Chart({{chart|safe}});
</script>
{% endblock %}
页面的源代码: https://pastebin.com/EkJYQPLQ
通过我还没有发现大熊猫,highcharts一个标签,我不认为我有特权创建它的方式。我使用的是熊猫,highcharts 0.5.2
编辑:这似乎question有关,但我不能回答适用于我的具体情况。
你可以检查传递给图表leak_chart.html的格式是什么。??它甚至是一个JSON格式.. ?? – MoChen
该系列应该是这样的:'系列:[{ \t名: '简', \t数据:[1,0,4] },{ \t名: '约翰', \t数据:[5 ,[7,3] }]'这是我在生成图形之后从页面源中提取的内容:''series“:[{0} {0} [''''''''”'data“:[ \t \t [”2016-12-23 00:05 :18 + 00:00“,29.0], \t \t [”2016-12-23 00:05:27 + 00:00“,29.0], \t \t [”2016-12-23 19:11:34 +00:00“,29.0] \t], \t “Y轴”:0, \t “名”: “ENTR \ u00e9e D'淡 - Archim \ u00e8de - 0013A2004166CFCD” }]' –