从Django直接输入数据作为D3图形 [英] Direct data input from Django for a D3 graph

查看:220
本文介绍了从Django直接输入数据作为D3图形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎所有的D3示例图形都将外部.csv或.tsv文件用作输入数据。是否有任何方法来修改代码以从Django中的变量获取数据。假设{{data}}采用JSON格式,如何在 http:// bl。 ocks.org/3885304 http://bl.ocks.org/3887051 ?我试图避免总是写一个.csv文件。

It seems all the D3 example graphs take an external .csv or .tsv file as input data. Is there any way to modify the code to take data from a variable in Django. Suppose {{ data }} is in JSON format, how do you implement this in a graph such as http://bl.ocks.org/3885304 or http://bl.ocks.org/3887051 ? I'm trying to avoid always writing a .csv file.

推荐答案

你可以总是做一个视图,将服务动态csv文件这将由D3消耗。这种方式还允许用户在需要原始数据而不是图形时下载数据。

You can always make a view which will serve dynamic csv file which will be consumed by D3. This way will also allow users to download the data in case they need the raw data instead of a graph.

def foo(request, ...):
    model = get_object_or_404(Foo, ...)
    data = model.get_data() # should return csv formatted string
    return HttpResponse(data, content_type='text/csv')

这篇关于从Django直接输入数据作为D3图形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆