[英]Django Flot e javascript
我想在我的django應用程序中做一些圖形。 我正在使用Flot。 我是關於javascript的新手,我不知道如何在我的情況下傳遞參數。 這是我的代碼:
view.py
data = []
query = tab_data.objects.all()
for row in query:
data.append([str(row.time), str(row.rate)])
data = json.dumps(data, cls=DjangoJSONEncoder)
...
return render_to_response('index.html', {'data' : data}, context_instance=RequestContext(request))
的index.html
<div class="portlet-body">
<div id="site_statistics_loading">
<img src="/static/assets/img/loading.gif" alt="loading" />
</div>
<div id="site_statistics_content" class="hide">
<div id="site_statistics" class="chart"></div>
</div>
</div>
...
... # at the end of the page I have this
Index.initCharts(); // init index page's custom scripts
Index.js
initCharts: function () {
if (!jQuery.plot) {
return;
}
var data = []; # Here I would like my data.
如何將數據傳遞給文件index.js? 抱歉這個瑣碎的問題,但我不知道該怎么做...謝謝
系列可以是原始數據,也可以是具有屬性的對象。 原始數據格式是一個點數組:
[[x1,y1],[x2,y2],...]
例如(在你的情況下)
data= [[1, 3], [2, 14.01], [3.5, 3.14]]; //You can build this string in the html generated and/or add it with jquery/ajax callback function.
如果你想要一些更復雜的(例如,在線條和點中使用樣式,你必須構建基本上構建“選項”參數。
$.plot(placeholder, data, options);
占位符是包含圖形的div容器。 data是前面描述的格式的數據。 (照顧字符串構建) 選項是圖形的選項(線條,系列,樣式,點)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.