【发布时间】:2017-10-06 20:47:25
【问题描述】:
我正在尝试在 Django 项目中使用 pandas-highcharts;我能够生成图表,但没有绘制数据,所以我猜测我的 pandas 数据框格式不正确,或者我没有使用正确的方法来呈现模板。
我的数据框:
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
顺便说一句,我还没有找到 pandas-highcharts 的标签,而且我认为我没有创建它的权限。我正在使用 pandas-highcharts 0.5.2
编辑:这个question 似乎相关,但我无法将答案应用于我的具体情况。
【问题讨论】:
-
你能检查一下传递给图表的leak_chart.html的格式是什么吗??它甚至是JSON格式..??
-
系列应该是这样的:
series: [{ name: 'Jane', data: [1, 0, 4] }, { name: 'John', data: [5, 7, 3] }]这是我生成图表后从页面源中提取的:"series": [{ "data": [ ["2016-12-23 00:05:18+00:00", 29.0], ["2016-12-23 00:05:27+00:00", 29.0], ["2016-12-23 19:11:34+00:00", 29.0] ], "yAxis": 0, "name": "Entr\u00e9e d'eau - Archim\u00e8de - 0013A2004166CFCD" }]
标签: python django highcharts