如何使用 Javascript 在 Django 中制作股票图表?

发布于 2024-12-18 20:51:37 字数 583 浏览 3 评论 0原文

我想用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

对不⑦ 2024-12-25 20:51:37

您有两个选择:

  1. 渲染包含数据的模板
  2. 从服务器动态获取数据

如果您选择 1.,您可以将类似以下内容添加到您的模板中:

<script type="text/javascript">
   var my_data = {{ json_plot_data }};
</script>

该模板还包括从数据生成绘图的 javascript 代码。视图函数将包括数据获取并返回一个上下文对象,如下所示:

def my_stock_plot_view(request, number):
    # get stock data
    json_data = simplejson.dumps(data)
    django.shortcuts.render(request, 'template.html', {'json_plot_data':json_data})

如果您选择 2,您将需要使用 jQuery.ajax 之类的东西来使用 ajax 请求动态加载 json 数据。该请求将调用您的视图,在 jQuery.ajax 调用中您指定该请求返回 JSON,这会自动使数据作为 Javascript 的对象可用。在 jQuery.ajax 成功处理程序中,您可以将数据传递给绘图函数。

You have two options:

  1. render a template with data included
  2. dynamically fetch data from the server

If you go for 1. you could add something like this to your template:

<script type="text/javascript">
   var my_data = {{ json_plot_data }};
</script>

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:

def my_stock_plot_view(request, number):
    # get stock data
    json_data = simplejson.dumps(data)
    django.shortcuts.render(request, 'template.html', {'json_plot_data':json_data})

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.

本宫微胖 2024-12-25 20:51:37

我没有安装这些库,但基于自述文件tickp 库,您将需要以下数据:[日期、开盘价、最高价、最低价、收盘价和可选交易量]get_historical_info 函数返回列[日期、开盘价、最高价、最低价、收盘价、成交量、AdjClose]。这里不匹配的是 AdjClose,因此您需要从 StockRetriever 获取的数据中删除它:

from django.shortcuts import render
from stock.stockretriever import StockRetriever

def stockretriever(request, number):
    data = StockRetriever().get_historical_info('YHOO')
    # Assuming data is returned as a list of lists
    new_data = [d[:-1] for d in data]
    return render(request, 'stock.html', { 'data': simplejson.dumps(new_data) })

按照自述文件,您需要以下内容模板中的行:

<html>
<head><script src="tickp.js"></script><script src="stats.js"></script></head>
<body onload='plot = window.tickp("#chart"); plot.read({{ data }}); plot.plot();'>
  <div id="chart"></div>
</body>
</html>

请注意,我已经在可能的 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]. The get_historical_info function returns the columns [Date, Open, High, Low, Close, Volume, AdjClose]. The mismatch here is the AdjClose, so you'd need to strip that from the data you get from the StockRetriever:

from django.shortcuts import render
from stock.stockretriever import StockRetriever

def stockretriever(request, number):
    data = StockRetriever().get_historical_info('YHOO')
    # Assuming data is returned as a list of lists
    new_data = [d[:-1] for d in data]
    return render(request, 'stock.html', { 'data': simplejson.dumps(new_data) })

Following along with the readme, you need something along the following lines in your template:

<html>
<head><script src="tickp.js"></script><script src="stats.js"></script></head>
<body onload='plot = window.tickp("#chart"); plot.read({{ data }}); plot.plot();'>
  <div id="chart"></div>
</body>
</html>

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文