我正在构建一个简单的Web应用程序,它接受表单中的信息,使用该信息从API查询数据,然后从数据框中的API响应中返回相关数据。
数据帧按预期呈现。但是,除了数据框之外,我还想使用数据框的两个相关列返回散点图。这是我遇到问题的地方。我以为我正在遵循正确的步骤,但我显然遗漏了一些东西:
settings.py
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
.
.
'django.contrib.admin',
'chartit',
)
models.py
from django.db import models
class results2(models.Model):
item_1 = models.IntegerField()
item_2 = models.IntegerField()
item_3 = models.CharField(max_length=150)
item_4 = models.IntegerField()
item_5 = models.CharField(max_length=150)
.
.
item_19 = models.IntegerField()
item_20 = models.IntegerField()
views.py
from django.template import loader, Context
from django.shortcuts import render_to_response
from django.http import HttpResponse
from blog.models import SearchForm, results2
from numpy import *
from pandas import *
from chartit import DataPool, Chart
results = results2()
def search(request):
if request.method == 'POST':
form = SearchForm(request.POST)
if form.is_valid():
.
. #DO SOME STUFF
.
final_frame.reset_index(drop="True")
for i in range(1,len(final_frame),1):
results.item_1 = int32(output[0])
results.item_2 = int32(final_frame.loc[i,['item_2']])
results.item_3 = str(final_frame.loc[i,['item_3']])
results.item_4 = int32(final_frame.loc[i,['item_4']])
results.item_5 = str(final_frame.loc[i,['item_5']])
.
.
results.item_19 = int32(final_frame.loc[i,['item_19']])
results.item_20 = int32(final_frame.loc[i,['item_20']])
results.save()
final_frame = final_frame.loc[:,['item_2','item_3','item_4','item_5','item_6','item_7','item_8','item_9','item_10','item_11','item_12','item_13']]
return HttpResponse(final_frame.to_html(), 'results.html')
else:
form = SearchForm()
return render(request, 'search.html', {'form': form})
def scatter_cart_view(request):
scatterdata = DataPool(
series =
[{'options': {
'source': results2.objects.all()},
'terms': [
'item_4',
'item_5']}])
cht = Chart(
datasource = scatterdata,
series_options =
[{'options':{
'type': 'scatter'},
'terms':[
'item_4',
'item_5']
}],
chart_options =
{'title':{
'text': 'Scatter Title'},
'xAxis': {
'title': {
'text': 'Axis Title'}}})
return render_to_response('results.html', {'scatterchart':cht})
results.html
<html>
<head>
<script src ="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
<script type ="text/javascript" src ="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<script type="text/javascript" src="/static/js/highcharts.js"></script>
{% load chartit %}
{{scatterchart|load_charts:"container"}}
</head>
<body>
<div id ="container"></div>
<div id ="content">{{% final_frame %}}</div>
</body>
</html>
我没有收到任何错误消息;图表根本不显示。在某些时候,我需要过滤模型列,以便只绘制与查询相关的数据,但这不应该是一个因素。我没有正确保存数据吗?我需要多个视图吗?我没有正确引入JQuery和Highcharts吗?我在这里不知所措。
感谢您的帮助!