任何人都知道如何在view.py中添加高级图表?这意味着我的数据是基于python代码的过滤器和从dataframe转换为highchart。
答案 0 :(得分:1)
使用pip安装django-highcharts(我们建议在virtualenv中执行此操作)。
git clone https://github.com/novapost/django-highcharts.git
cd django-highcharts
pip install -e ./
要将其集成到Django项目中,只需将其添加到INSTALLED_APPS:
INSTALLED_APPS = [
# some interesting stuff...
'highcharts',
# some other stuff...
]
不要忘记设置STATIC_ROOT路径并运行以下命令来更新静态文件:
python manage.py collectstatic
查看强>
from highcharts.views import HighChartsBarView
class BarView(HighChartsBarView):
categories = ['Orange', 'Bananas', 'Apples']
@property
def series(self):
result = []
for name in ('Joe', 'Jack', 'William', 'Averell'):
data = []
for x in range(len(self.categories)):
data.append(random.randint(0, 10))
result.append({'name': name, "data": data})
return result
<强>模板强>
{% load staticfiles %}<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Hello</title>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript" src="{% static 'js/highcharts/highcharts.js' %}"></script>
<script type="text/javascript">
$(function () {
$.getJSON("{% url 'bar' %}", function(data) {
$('#container').highcharts(data);
});
});
</script>
</head>
<body>
<div id="container" style="height: 300px"></div>
</body>
</html>
请注意,应该在JQuery库之后调用highcharts.js文件。
查看Documentation PDF了解详情。
编辑1
使用pip
安装软件包pip install pandas-highcharts
在视图中导入
import pandas_highcharts
df = ... # create your dataframe here
chart = pandas_highcharts.serialize(df, render_to='my-chart', output_type='json')
n你的模板
<div id="my-chart"></div>
<script type="text/javascript">
new Highcharts.Chart({{chart|safe}});
</script>