如何使用 Javascript 在 Django 中制作股票图表?
我想用Django建立一个股票网站,我找到了一个Javascript库(tickp)来制作图表,但我不懂 Javascript,也不知道如何读取 Django 模板中的 json 数据。我使用此代码从雅虎财经检索股票数据。
我将这些 .py 和 .js 放在我的文件夹中,我的看法是这样的:
from stock.stockretriever import StockRetriever
def stockretriever(request,number):
data = StockRetriever().get_historical_info('YHOO')
return HttpResponse(simplejson.dumps(data),mimetype='application/json')
但我不知道应该如何编写模板,有人可以告诉我吗?
谢谢。
I want to build a stock website with Django, and I found a Javascript library (tickp) to make charts, but I don't know Javascript, nor do I know how to read json data in a Django template. I use this code to retrieve the stock data from Yahoo Finance.
I put these .py and .js in my folder, and my views like this:
from stock.stockretriever import StockRetriever
def stockretriever(request,number):
data = StockRetriever().get_historical_info('YHOO')
return HttpResponse(simplejson.dumps(data),mimetype='application/json')
But I don't know how I should write the template, can somebody tell me?
thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您有两个选择:
如果您选择 1.,您可以将类似以下内容添加到您的模板中:
该模板还包括从数据生成绘图的 javascript 代码。视图函数将包括数据获取并返回一个上下文对象,如下所示:
如果您选择 2,您将需要使用 jQuery.ajax 之类的东西来使用 ajax 请求动态加载 json 数据。该请求将调用您的视图,在 jQuery.ajax 调用中您指定该请求返回 JSON,这会自动使数据作为 Javascript 的对象可用。在 jQuery.ajax 成功处理程序中,您可以将数据传递给绘图函数。
You have two options:
If you go for 1. you could add something like this to your template:
The template includes also the javascript code that generates the plots from the data. The view function would include the data fetching and return a context object like so:
If you go for 2. you would need to use something like jQuery.ajax to dynamically load json data using an ajax request. That request would invoke your view, in the jQuery.ajax call you specify that the request returns JSON which automatically makes the data available as an object to Javascript. In the jQuery.ajax success handler you would pass the data to your plot function.
我没有安装这些库,但基于自述文件 的
tickp
库,您将需要以下数据:[日期、开盘价、最高价、最低价、收盘价和可选交易量]
。get_historical_info
函数返回列[日期、开盘价、最高价、最低价、收盘价、成交量、AdjClose]
。这里不匹配的是AdjClose
,因此您需要从StockRetriever
获取的数据中删除它:按照自述文件,您需要以下内容模板中的行:
请注意,我已经在可能的 Ajax 调用或正确的格式和使用方面偷工减料,但它应该为您提供一些入门知识。如果您遗漏了某些内容,请根据您遇到的具体问题更新您的问题。
I don't have these libraries installed, but based on the readme of the
tickp
library, you'll need the following data:[date, open, high, low, close and optionally volume]
. Theget_historical_info
function returns the columns[Date, Open, High, Low, Close, Volume, AdjClose]
. The mismatch here is theAdjClose
, so you'd need to strip that from the data you get from theStockRetriever
:Following along with the readme, you need something along the following lines in your template:
Note that I've cut some corners with respect to possible Ajax calls or proper formatting and usage, but it should give you something to get started with. When you're missing something, please update your question with the specific issues you're having.